@@ -29,6 +29,22 @@ var _ resource.ResourceWithConfigure = &IssueAlertResource{}
2929var _ resource.ResourceWithImportState = & IssueAlertResource {}
3030var _ resource.ResourceWithUpgradeState = & IssueAlertResource {}
3131
32+ var (
33+ issueAlertConditionV2ElemType types.ObjectType
34+ issueAlertFilterV2ElemType types.ObjectType
35+ issueAlertActionV2ElemType types.ObjectType
36+ )
37+
38+ func init () {
39+ r := & IssueAlertResource {}
40+ var resp resource.SchemaResponse
41+ r .Schema (context .Background (), resource.SchemaRequest {}, & resp )
42+
43+ issueAlertConditionV2ElemType = resp .Schema .Attributes ["conditions_v2" ].(schema.ListNestedAttribute ).NestedObject .Type ().(types.ObjectType )
44+ issueAlertFilterV2ElemType = resp .Schema .Attributes ["filters_v2" ].(schema.ListNestedAttribute ).NestedObject .Type ().(types.ObjectType )
45+ issueAlertActionV2ElemType = resp .Schema .Attributes ["actions_v2" ].(schema.ListNestedAttribute ).NestedObject .Type ().(types.ObjectType )
46+ }
47+
3248func NewIssueAlertResource () resource.Resource {
3349 return & IssueAlertResource {}
3450}
@@ -639,8 +655,14 @@ func (r *IssueAlertResource) ValidateConfig(ctx context.Context, req resource.Va
639655 return
640656 }
641657
642- if data .ConditionsV2 != nil {
643- for i , item := range * data .ConditionsV2 {
658+ if ! data .ConditionsV2 .IsNull () && ! data .ConditionsV2 .IsUnknown () {
659+ var conditions []IssueAlertConditionModel
660+ resp .Diagnostics .Append (data .ConditionsV2 .ElementsAs (ctx , & conditions , false )... )
661+ if resp .Diagnostics .HasError () {
662+ return
663+ }
664+
665+ for i , item := range conditions {
644666 if _ , diags := item .ToApi (ctx ); diags .HasError () {
645667 resp .Diagnostics .AddAttributeError (
646668 path .Root ("conditions_v2" ).AtListIndex (i ),
@@ -651,8 +673,14 @@ func (r *IssueAlertResource) ValidateConfig(ctx context.Context, req resource.Va
651673 }
652674 }
653675
654- if data .FiltersV2 != nil {
655- for i , item := range * data .FiltersV2 {
676+ if ! data .FiltersV2 .IsNull () && ! data .FiltersV2 .IsUnknown () {
677+ var filters []IssueAlertFilterModel
678+ resp .Diagnostics .Append (data .FiltersV2 .ElementsAs (ctx , & filters , false )... )
679+ if resp .Diagnostics .HasError () {
680+ return
681+ }
682+
683+ for i , item := range filters {
656684 if _ , diags := item .ToApi (ctx ); diags .HasError () {
657685 resp .Diagnostics .AddAttributeError (
658686 path .Root ("filters_v2" ).AtListIndex (i ),
@@ -671,16 +699,22 @@ func (r *IssueAlertResource) ValidateConfig(ctx context.Context, req resource.Va
671699 "You must add an action for this alert to fire" ,
672700 )
673701 }
674- } else if data .ActionsV2 != nil {
675- if len (* data .ActionsV2 ) == 0 {
702+ } else if ! data .ActionsV2 .IsNull () && ! data .ActionsV2 .IsUnknown () {
703+ var actions []IssueAlertActionModel
704+ resp .Diagnostics .Append (data .ActionsV2 .ElementsAs (ctx , & actions , false )... )
705+ if resp .Diagnostics .HasError () {
706+ return
707+ }
708+
709+ if len (actions ) == 0 {
676710 resp .Diagnostics .AddAttributeError (
677711 path .Root ("actions_v2" ),
678712 "Missing attribute configuration" ,
679713 "You must add an action for this alert to fire" ,
680714 )
681715 }
682716
683- for i , item := range * data . ActionsV2 {
717+ for i , item := range actions {
684718 if _ , diags := item .ToApi (ctx ); diags .HasError () {
685719 resp .Diagnostics .AddAttributeError (
686720 path .Root ("actions_v2" ).AtListIndex (i ),
@@ -712,9 +746,15 @@ func (r *IssueAlertResource) Create(ctx context.Context, req resource.CreateRequ
712746
713747 if ! data .Conditions .IsNull () {
714748 resp .Diagnostics .Append (data .Conditions .Unmarshal (& body .Conditions )... )
715- } else if data .ConditionsV2 != nil {
749+ } else if ! data .ConditionsV2 .IsNull () {
750+ var conditions []IssueAlertConditionModel
751+ resp .Diagnostics .Append (data .ConditionsV2 .ElementsAs (ctx , & conditions , false )... )
752+ if resp .Diagnostics .HasError () {
753+ return
754+ }
755+
716756 body .Conditions = []apiclient.ProjectRuleCondition {}
717- for i , item := range * data . ConditionsV2 {
757+ for i , item := range conditions {
718758 condition , diags := item .ToApi (ctx )
719759 if diags .HasError () {
720760 resp .Diagnostics .AddAttributeError (
@@ -732,9 +772,15 @@ func (r *IssueAlertResource) Create(ctx context.Context, req resource.CreateRequ
732772
733773 if ! data .Filters .IsNull () {
734774 resp .Diagnostics .Append (data .Filters .Unmarshal (& body .Filters )... )
735- } else if data .FiltersV2 != nil {
775+ } else if ! data .FiltersV2 .IsNull () {
776+ var filters []IssueAlertFilterModel
777+ resp .Diagnostics .Append (data .FiltersV2 .ElementsAs (ctx , & filters , false )... )
778+ if resp .Diagnostics .HasError () {
779+ return
780+ }
781+
736782 body .Filters = []apiclient.ProjectRuleFilter {}
737- for i , item := range * data . FiltersV2 {
783+ for i , item := range filters {
738784 filter , diags := item .ToApi (ctx )
739785 if diags .HasError () {
740786 resp .Diagnostics .AddAttributeError (
@@ -752,9 +798,15 @@ func (r *IssueAlertResource) Create(ctx context.Context, req resource.CreateRequ
752798
753799 if ! data .Actions .IsNull () {
754800 resp .Diagnostics .Append (data .Actions .Unmarshal (& body .Actions )... )
755- } else if data .ActionsV2 != nil {
801+ } else if ! data .ActionsV2 .IsNull () {
802+ var actions []IssueAlertActionModel
803+ resp .Diagnostics .Append (data .ActionsV2 .ElementsAs (ctx , & actions , false )... )
804+ if resp .Diagnostics .HasError () {
805+ return
806+ }
807+
756808 body .Actions = []apiclient.ProjectRuleAction {}
757- for i , item := range * data . ActionsV2 {
809+ for i , item := range actions {
758810 action , diags := item .ToApi (ctx )
759811 if diags .HasError () {
760812 resp .Diagnostics .AddAttributeError (
@@ -849,9 +901,15 @@ func (r *IssueAlertResource) Update(ctx context.Context, req resource.UpdateRequ
849901
850902 if ! data .Conditions .IsNull () {
851903 resp .Diagnostics .Append (data .Conditions .Unmarshal (& body .Conditions )... )
852- } else if data .ConditionsV2 != nil {
904+ } else if ! data .ConditionsV2 .IsNull () {
905+ var conditions []IssueAlertConditionModel
906+ resp .Diagnostics .Append (data .ConditionsV2 .ElementsAs (ctx , & conditions , false )... )
907+ if resp .Diagnostics .HasError () {
908+ return
909+ }
910+
853911 body .Conditions = []apiclient.ProjectRuleCondition {}
854- for i , item := range * data . ConditionsV2 {
912+ for i , item := range conditions {
855913 condition , diags := item .ToApi (ctx )
856914 if diags .HasError () {
857915 resp .Diagnostics .AddAttributeError (
@@ -869,9 +927,15 @@ func (r *IssueAlertResource) Update(ctx context.Context, req resource.UpdateRequ
869927
870928 if ! data .Filters .IsNull () {
871929 resp .Diagnostics .Append (data .Filters .Unmarshal (& body .Filters )... )
872- } else if data .FiltersV2 != nil {
930+ } else if ! data .FiltersV2 .IsNull () {
931+ var filters []IssueAlertFilterModel
932+ resp .Diagnostics .Append (data .FiltersV2 .ElementsAs (ctx , & filters , false )... )
933+ if resp .Diagnostics .HasError () {
934+ return
935+ }
936+
873937 body .Filters = []apiclient.ProjectRuleFilter {}
874- for i , item := range * data . FiltersV2 {
938+ for i , item := range filters {
875939 filter , diags := item .ToApi (ctx )
876940 if diags .HasError () {
877941 resp .Diagnostics .AddAttributeError (
@@ -889,9 +953,15 @@ func (r *IssueAlertResource) Update(ctx context.Context, req resource.UpdateRequ
889953
890954 if ! data .Actions .IsNull () {
891955 resp .Diagnostics .Append (data .Actions .Unmarshal (& body .Actions )... )
892- } else if data .ActionsV2 != nil {
956+ } else if ! data .ActionsV2 .IsNull () {
957+ var actions []IssueAlertActionModel
958+ resp .Diagnostics .Append (data .ActionsV2 .ElementsAs (ctx , & actions , false )... )
959+ if resp .Diagnostics .HasError () {
960+ return
961+ }
962+
893963 body .Actions = []apiclient.ProjectRuleAction {}
894- for i , item := range * data . ActionsV2 {
964+ for i , item := range actions {
895965 action , diags := item .ToApi (ctx )
896966 if diags .HasError () {
897967 resp .Diagnostics .AddAttributeError (
@@ -1060,6 +1130,10 @@ func (r *IssueAlertResource) UpgradeState(ctx context.Context) map[int64]resourc
10601130 Environment : priorStateData .Environment ,
10611131 }
10621132
1133+ upgradedStateData .ConditionsV2 = types .ListNull (issueAlertConditionV2ElemType )
1134+ upgradedStateData .FiltersV2 = types .ListNull (issueAlertFilterV2ElemType )
1135+ upgradedStateData .ActionsV2 = types .ListNull (issueAlertActionV2ElemType )
1136+
10631137 upgradedStateData .Conditions = sentrytypes .NewLossyJsonNull ()
10641138 if ! priorStateData .Conditions .IsNull () {
10651139 conditions := []map [string ]string {}
0 commit comments