55
66 "github.com/elastic/terraform-provider-elasticstack/internal/utils"
77 "github.com/hashicorp/terraform-plugin-framework/attr"
8+ "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
89 "github.com/hashicorp/terraform-plugin-framework/path"
910 "github.com/hashicorp/terraform-plugin-framework/resource"
1011 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -55,6 +56,7 @@ type integrationPolicyModelV0 struct {
5556 Name types.String `tfsdk:"name"`
5657 Namespace types.String `tfsdk:"namespace"`
5758 AgentPolicyID types.String `tfsdk:"agent_policy_id"`
59+ AgentPolicyIDs types.List `tfsdk:"agent_policy_ids"` // Added to handle V1 state
5860 Description types.String `tfsdk:"description"`
5961 Enabled types.Bool `tfsdk:"enabled"`
6062 Force types.Bool `tfsdk:"force"`
@@ -75,40 +77,80 @@ type integrationPolicyInputModelV0 struct {
7577// streams_json saved "" values to the state when null values were in the
7678// config. jsontypes.Normalized correctly states this is invalid JSON.
7779func upgradeV0 (ctx context.Context , req resource.UpgradeStateRequest , resp * resource.UpgradeStateResponse ) {
78- var stateModel integrationPolicyModelV0
80+ var stateModelV0 integrationPolicyModelV0
7981
80- diags := req .State .Get (ctx , & stateModel )
82+ diags := req .State .Get (ctx , & stateModelV0 )
8183 resp .Diagnostics .Append (diags ... )
8284 if resp .Diagnostics .HasError () {
8385 return
8486 }
8587
86- if varsJSON := stateModel .VarsJson .ValueStringPointer (); varsJSON != nil {
88+ // Convert V0 model to V1 model
89+ stateModelV1 := integrationPolicyModel {
90+ ID : stateModelV0 .ID ,
91+ PolicyID : stateModelV0 .PolicyID ,
92+ Name : stateModelV0 .Name ,
93+ Namespace : stateModelV0 .Namespace ,
94+ AgentPolicyID : stateModelV0 .AgentPolicyID ,
95+ AgentPolicyIDs : stateModelV0 .AgentPolicyIDs , // Copy existing value (may be null)
96+ Description : stateModelV0 .Description ,
97+ Enabled : stateModelV0 .Enabled ,
98+ Force : stateModelV0 .Force ,
99+ IntegrationName : stateModelV0 .IntegrationName ,
100+ IntegrationVersion : stateModelV0 .IntegrationVersion ,
101+ }
102+
103+ // Convert vars_json from string to normalized JSON type
104+ if varsJSON := stateModelV0 .VarsJson .ValueStringPointer (); varsJSON != nil {
87105 if * varsJSON == "" {
88- stateModel .VarsJson = types .StringNull ()
106+ stateModelV1 .VarsJson = jsontypes .NewNormalizedNull ()
107+ } else {
108+ stateModelV1 .VarsJson = jsontypes .NewNormalizedValue (* varsJSON )
89109 }
110+ } else {
111+ stateModelV1 .VarsJson = jsontypes .NewNormalizedNull ()
90112 }
91113
92- inputs := utils .ListTypeAs [integrationPolicyInputModelV0 ](ctx , stateModel .Input , path .Root ("input" ), & resp .Diagnostics )
93- for index , input := range inputs {
94- if varsJSON := input .VarsJson .ValueStringPointer (); varsJSON != nil {
114+ // Convert inputs from V0 to V1
115+ inputsV0 := utils .ListTypeAs [integrationPolicyInputModelV0 ](ctx , stateModelV0 .Input , path .Root ("input" ), & resp .Diagnostics )
116+ var inputsV1 []integrationPolicyInputModel
117+
118+ for _ , inputV0 := range inputsV0 {
119+ inputV1 := integrationPolicyInputModel {
120+ InputID : inputV0 .InputID ,
121+ Enabled : inputV0 .Enabled ,
122+ }
123+
124+ // Convert vars_json
125+ if varsJSON := inputV0 .VarsJson .ValueStringPointer (); varsJSON != nil {
95126 if * varsJSON == "" {
96- input .VarsJson = types .StringNull ()
127+ inputV1 .VarsJson = jsontypes .NewNormalizedNull ()
128+ } else {
129+ inputV1 .VarsJson = jsontypes .NewNormalizedValue (* varsJSON )
97130 }
131+ } else {
132+ inputV1 .VarsJson = jsontypes .NewNormalizedNull ()
98133 }
99- if streamsJSON := input .StreamsJson .ValueStringPointer (); streamsJSON != nil {
134+
135+ // Convert streams_json
136+ if streamsJSON := inputV0 .StreamsJson .ValueStringPointer (); streamsJSON != nil {
100137 if * streamsJSON == "" {
101- input .StreamsJson = types .StringNull ()
138+ inputV1 .StreamsJson = jsontypes .NewNormalizedNull ()
139+ } else {
140+ inputV1 .StreamsJson = jsontypes .NewNormalizedValue (* streamsJSON )
102141 }
142+ } else {
143+ inputV1 .StreamsJson = jsontypes .NewNormalizedNull ()
103144 }
104- inputs [index ] = input
145+
146+ inputsV1 = append (inputsV1 , inputV1 )
105147 }
106148
107- stateModel .Input = utils .ListValueFrom (ctx , inputs , getInputTypeV0 (), path .Root ("input" ), & resp .Diagnostics )
149+ stateModelV1 .Input = utils .ListValueFrom (ctx , inputsV1 , getInputTypeV1 (), path .Root ("input" ), & resp .Diagnostics )
108150 if resp .Diagnostics .HasError () {
109151 return
110152 }
111153
112- diags = resp .State .Set (ctx , stateModel )
154+ diags = resp .State .Set (ctx , stateModelV1 )
113155 resp .Diagnostics .Append (diags ... )
114156}
0 commit comments