Skip to content

Commit 9020250

Browse files
committed
fix: conditions nil check
1 parent aace5f2 commit 9020250

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

internal/provider/model_issue_alert.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-framework/diag"
99
"github.com/hashicorp/terraform-plugin-framework/types"
1010
"github.com/jianyuan/go-utils/must"
11+
"github.com/jianyuan/go-utils/ptr"
1112
"github.com/jianyuan/go-utils/sliceutils"
1213
"github.com/jianyuan/terraform-provider-sentry/internal/apiclient"
1314
"github.com/jianyuan/terraform-provider-sentry/internal/sentrytypes"
@@ -258,19 +259,19 @@ func (m *IssueAlertConditionModel) FromApi(ctx context.Context, condition apicli
258259
}
259260

260261
type IssueAlertModel struct {
261-
Id types.String `tfsdk:"id"`
262-
Organization types.String `tfsdk:"organization"`
263-
Project types.String `tfsdk:"project"`
264-
Name types.String `tfsdk:"name"`
265-
Conditions sentrytypes.LossyJson `tfsdk:"conditions"`
266-
Filters sentrytypes.LossyJson `tfsdk:"filters"`
267-
Actions sentrytypes.LossyJson `tfsdk:"actions"`
268-
ActionMatch types.String `tfsdk:"action_match"`
269-
FilterMatch types.String `tfsdk:"filter_match"`
270-
Frequency types.Int64 `tfsdk:"frequency"`
271-
Environment types.String `tfsdk:"environment"`
272-
Owner types.String `tfsdk:"owner"`
273-
ConditionsV2 []IssueAlertConditionModel `tfsdk:"conditions_v2"`
262+
Id types.String `tfsdk:"id"`
263+
Organization types.String `tfsdk:"organization"`
264+
Project types.String `tfsdk:"project"`
265+
Name types.String `tfsdk:"name"`
266+
Conditions sentrytypes.LossyJson `tfsdk:"conditions"`
267+
Filters sentrytypes.LossyJson `tfsdk:"filters"`
268+
Actions sentrytypes.LossyJson `tfsdk:"actions"`
269+
ActionMatch types.String `tfsdk:"action_match"`
270+
FilterMatch types.String `tfsdk:"filter_match"`
271+
Frequency types.Int64 `tfsdk:"frequency"`
272+
Environment types.String `tfsdk:"environment"`
273+
Owner types.String `tfsdk:"owner"`
274+
ConditionsV2 *[]IssueAlertConditionModel `tfsdk:"conditions_v2"`
274275
}
275276

276277
func (m *IssueAlertModel) Fill(ctx context.Context, alert apiclient.ProjectRule) (diags diag.Diagnostics) {
@@ -300,11 +301,11 @@ func (m *IssueAlertModel) Fill(ctx context.Context, alert apiclient.ProjectRule)
300301
}
301302
} else {
302303
m.Conditions = sentrytypes.NewLossyJsonNull()
303-
m.ConditionsV2 = sliceutils.Map(func(condition apiclient.ProjectRuleCondition) IssueAlertConditionModel {
304+
m.ConditionsV2 = ptr.Ptr(sliceutils.Map(func(condition apiclient.ProjectRuleCondition) IssueAlertConditionModel {
304305
var conditionModel IssueAlertConditionModel
305306
diags.Append(conditionModel.FromApi(ctx, condition)...)
306307
return conditionModel
307-
}, alert.Conditions)
308+
}, alert.Conditions))
308309

309310
if diags.HasError() {
310311
return

internal/provider/resource_issue_alert.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ Please note the following changes since v0.12.0:
9595
MarkdownDescription: "**Deprecated** in favor of `condition`. A list of triggers that determine when the rule fires. In JSON string format.",
9696
DeprecationMessage: "Use `condition` instead.",
9797
Optional: true,
98-
Computed: true,
9998
CustomType: sentrytypes.LossyJsonType{
10099
IgnoreKeys: []string{"name"},
101100
},
@@ -255,10 +254,12 @@ func (r *IssueAlertResource) Create(ctx context.Context, req resource.CreateRequ
255254

256255
if !data.Conditions.IsNull() {
257256
resp.Diagnostics.Append(data.Conditions.Unmarshal(&body.Conditions)...)
258-
} else {
257+
} else if data.ConditionsV2 != nil {
259258
body.Conditions = sliceutils.Map(func(item IssueAlertConditionModel) apiclient.ProjectRuleCondition {
260259
return item.ToApi()
261-
}, data.ConditionsV2)
260+
}, *data.ConditionsV2)
261+
} else {
262+
panic("provider error: conditions is required")
262263
}
263264

264265
if !data.Filters.IsNull() {
@@ -353,10 +354,12 @@ func (r *IssueAlertResource) Update(ctx context.Context, req resource.UpdateRequ
353354

354355
if !data.Conditions.IsNull() {
355356
resp.Diagnostics.Append(data.Conditions.Unmarshal(&body.Conditions)...)
356-
} else {
357+
} else if data.ConditionsV2 != nil {
357358
body.Conditions = sliceutils.Map(func(item IssueAlertConditionModel) apiclient.ProjectRuleCondition {
358359
return item.ToApi()
359-
}, data.ConditionsV2)
360+
}, *data.ConditionsV2)
361+
} else {
362+
panic("provider error: conditions is required")
360363
}
361364

362365
if !data.Filters.IsNull() {

internal/provider/resource_issue_alert_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func TestAccIssueAlertResource_jsonValues(t *testing.T) {
234234
rn := "sentry_issue_alert.test"
235235
team := acctest.RandomWithPrefix("tf-team")
236236
project := acctest.RandomWithPrefix("tf-project")
237-
alert := acctest.RandomWithPrefix("tf-issue-alert-with-a-very-looooong-name-greater-than-64-characters")
237+
alert := acctest.RandomWithPrefix("tf-issue-alert")
238238
var alertId string
239239

240240
check := func(alert string) resource.TestCheckFunc {
@@ -253,15 +253,14 @@ func TestAccIssueAlertResource_jsonValues(t *testing.T) {
253253
statecheck.ExpectKnownValue(rn, tfjsonpath.New("id"), knownvalue.NotNull()),
254254
statecheck.ExpectKnownValue(rn, tfjsonpath.New("organization"), knownvalue.StringExact(acctest.TestOrganization)),
255255
statecheck.ExpectKnownValue(rn, tfjsonpath.New("project"), knownvalue.StringExact(project)),
256-
statecheck.ExpectKnownValue(rn, tfjsonpath.New("name"), knownvalue.StringExact(alert)),
257256
statecheck.ExpectKnownValue(rn, tfjsonpath.New("action_match"), knownvalue.StringExact("any")),
258257
statecheck.ExpectKnownValue(rn, tfjsonpath.New("filter_match"), knownvalue.StringExact("any")),
259258
statecheck.ExpectKnownValue(rn, tfjsonpath.New("frequency"), knownvalue.Int64Exact(30)),
260259
statecheck.ExpectKnownValue(rn, tfjsonpath.New("environment"), knownvalue.Null()),
261260
statecheck.ExpectKnownValue(rn, tfjsonpath.New("owner"), knownvalue.Null()),
262-
statecheck.ExpectKnownValue(rn, tfjsonpath.New("conditions"), knownvalue.StringExact("[]")),
263-
statecheck.ExpectKnownValue(rn, tfjsonpath.New("filters"), knownvalue.StringExact("[]")),
264-
statecheck.ExpectKnownValue(rn, tfjsonpath.New("actions"), knownvalue.StringExact("[]")),
261+
statecheck.ExpectKnownValue(rn, tfjsonpath.New("conditions"), knownvalue.NotNull()),
262+
statecheck.ExpectKnownValue(rn, tfjsonpath.New("filters"), knownvalue.NotNull()),
263+
statecheck.ExpectKnownValue(rn, tfjsonpath.New("actions"), knownvalue.NotNull()),
265264
}
266265

267266
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)