Skip to content

Commit 2076f8d

Browse files
authored
ref: Make project filters computed (#541)
* ref: use diagnostics when filling models * fix: project filters are now computed
1 parent e00330e commit 2076f8d

5 files changed

+167
-130
lines changed

internal/provider/data_source_organization.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-framework/datasource"
88
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
910
"github.com/hashicorp/terraform-plugin-framework/types"
1011
"github.com/jianyuan/terraform-provider-sentry/internal/apiclient"
1112
"github.com/jianyuan/terraform-provider-sentry/internal/diagutils"
@@ -21,13 +22,13 @@ type OrganizationDataSourceModel struct {
2122
InternalId types.String `tfsdk:"internal_id"`
2223
}
2324

24-
func (m *OrganizationDataSourceModel) Fill(org apiclient.Organization) error {
25+
func (m *OrganizationDataSourceModel) Fill(ctx context.Context, org apiclient.Organization) (diags diag.Diagnostics) {
2526
m.Id = types.StringValue(org.Slug)
2627
m.Slug = types.StringValue(org.Slug)
2728
m.Name = types.StringValue(org.Name)
2829
m.InternalId = types.StringValue(org.Id)
2930

30-
return nil
31+
return
3132
}
3233

3334
func NewOrganizationDataSource() datasource.DataSource {
@@ -85,16 +86,13 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
8586
resp.Diagnostics.Append(diagutils.NewNotFoundError("organization"))
8687
resp.State.RemoveResource(ctx)
8788
return
88-
} else if httpResp.StatusCode() != http.StatusOK {
89+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
8990
resp.Diagnostics.Append(diagutils.NewClientStatusError("read", httpResp.StatusCode(), httpResp.Body))
9091
return
91-
} else if httpResp.JSON200 == nil {
92-
resp.Diagnostics.Append(diagutils.NewClientError("read", diagutils.ErrEmptyResponse))
93-
return
9492
}
9593

96-
if err := data.Fill(*httpResp.JSON200); err != nil {
97-
resp.Diagnostics.Append(diagutils.NewFillError(err))
94+
resp.Diagnostics.Append(data.Fill(ctx, *httpResp.JSON200)...)
95+
if resp.Diagnostics.HasError() {
9896
return
9997
}
10098

internal/provider/resource_client_key.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ func (m ClientKeyJavascriptLoaderScriptResourceModel) AttributeTypes() map[strin
3939
}
4040
}
4141

42-
func (m *ClientKeyJavascriptLoaderScriptResourceModel) Fill(ctx context.Context, key apiclient.ProjectKey) diag.Diagnostics {
43-
var diags diag.Diagnostics
44-
42+
func (m *ClientKeyJavascriptLoaderScriptResourceModel) Fill(ctx context.Context, key apiclient.ProjectKey) (diags diag.Diagnostics) {
4543
m.BrowserSdkVersion = types.StringValue(key.BrowserSdkVersion)
4644
m.PerformanceMonitoringEnabled = types.BoolValue(key.DynamicSdkLoaderOptions.HasPerformance)
4745
m.SessionReplayEnabled = types.BoolValue(key.DynamicSdkLoaderOptions.HasReplay)
4846
m.DebugEnabled = types.BoolValue(key.DynamicSdkLoaderOptions.HasDebug)
4947

50-
return diags
48+
return
5149
}
5250

5351
type ClientKeyResourceModel struct {
@@ -67,9 +65,7 @@ type ClientKeyResourceModel struct {
6765
DsnCsp types.String `tfsdk:"dsn_csp"`
6866
}
6967

70-
func (m *ClientKeyResourceModel) Fill(ctx context.Context, key apiclient.ProjectKey) diag.Diagnostics {
71-
var diags diag.Diagnostics
72-
68+
func (m *ClientKeyResourceModel) Fill(ctx context.Context, key apiclient.ProjectKey) (diags diag.Diagnostics) {
7369
m.Id = types.StringValue(key.Id)
7470
m.ProjectId = types.StringValue(key.ProjectId.String())
7571
m.Name = types.StringValue(key.Name)
@@ -114,7 +110,7 @@ func (m *ClientKeyResourceModel) Fill(ctx context.Context, key apiclient.Project
114110
m.DsnCsp = types.StringNull()
115111
}
116112

117-
return diags
113+
return
118114
}
119115

120116
var _ resource.Resource = &ClientKeyResource{}
@@ -382,28 +378,22 @@ func (r *ClientKeyResource) Update(ctx context.Context, req resource.UpdateReque
382378
}
383379

384380
if !plan.JavascriptLoaderScript.Equal(state.JavascriptLoaderScript) {
385-
var javascriptLoaderScriptPlan, javascriptLoaderScriptState ClientKeyJavascriptLoaderScriptResourceModel
386-
resp.Diagnostics.Append(plan.JavascriptLoaderScript.As(ctx, &javascriptLoaderScriptPlan, basetypes.ObjectAsOptions{})...)
381+
var javascriptLoaderScript ClientKeyJavascriptLoaderScriptResourceModel
382+
resp.Diagnostics.Append(plan.JavascriptLoaderScript.As(ctx, &javascriptLoaderScript, basetypes.ObjectAsOptions{})...)
387383
if resp.Diagnostics.HasError() {
388384
return
389385
}
390386

391-
if !javascriptLoaderScriptPlan.BrowserSdkVersion.Equal(javascriptLoaderScriptState.BrowserSdkVersion) {
392-
body.BrowserSdkVersion = javascriptLoaderScriptPlan.BrowserSdkVersion.ValueStringPointer()
393-
}
394-
395-
if !javascriptLoaderScriptPlan.SessionReplayEnabled.Equal(javascriptLoaderScriptState.SessionReplayEnabled) ||
396-
!javascriptLoaderScriptPlan.PerformanceMonitoringEnabled.Equal(javascriptLoaderScriptState.PerformanceMonitoringEnabled) ||
397-
!javascriptLoaderScriptPlan.DebugEnabled.Equal(javascriptLoaderScriptState.DebugEnabled) {
398-
body.DynamicSdkLoaderOptions = &struct {
399-
HasDebug *bool `json:"hasDebug,omitempty"`
400-
HasPerformance *bool `json:"hasPerformance,omitempty"`
401-
HasReplay *bool `json:"hasReplay,omitempty"`
402-
}{
403-
HasReplay: javascriptLoaderScriptPlan.SessionReplayEnabled.ValueBoolPointer(),
404-
HasDebug: javascriptLoaderScriptPlan.DebugEnabled.ValueBoolPointer(),
405-
HasPerformance: javascriptLoaderScriptPlan.PerformanceMonitoringEnabled.ValueBoolPointer(),
406-
}
387+
// NOTE: Both `BrowserSdkVersion` and `DynamicSdkLoaderOptions` must be set together.
388+
body.BrowserSdkVersion = javascriptLoaderScript.BrowserSdkVersion.ValueStringPointer()
389+
body.DynamicSdkLoaderOptions = &struct {
390+
HasDebug *bool `json:"hasDebug,omitempty"`
391+
HasPerformance *bool `json:"hasPerformance,omitempty"`
392+
HasReplay *bool `json:"hasReplay,omitempty"`
393+
}{
394+
HasReplay: javascriptLoaderScript.SessionReplayEnabled.ValueBoolPointer(),
395+
HasDebug: javascriptLoaderScript.DebugEnabled.ValueBoolPointer(),
396+
HasPerformance: javascriptLoaderScript.PerformanceMonitoringEnabled.ValueBoolPointer(),
407397
}
408398
}
409399

internal/provider/resource_client_key_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func TestAccClientKeyResource(t *testing.T) {
248248
RateLimitCount: ptr.Ptr(2),
249249
Extras: `
250250
javascript_loader_script = {
251-
browser_sdk_version = "7.x"
251+
browser_sdk_version = "7.x"
252252
}
253253
`,
254254
}),

0 commit comments

Comments
 (0)