Skip to content

Commit 6397ee1

Browse files
committed
edit upgrade
1 parent f343977 commit 6397ee1

File tree

1 file changed

+77
-40
lines changed

1 file changed

+77
-40
lines changed

internal/fleet/integration_policy/upgrade.go

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55

66
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
7-
"github.com/hashicorp/terraform-plugin-framework/attr"
7+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
88
"github.com/hashicorp/terraform-plugin-framework/path"
99
"github.com/hashicorp/terraform-plugin-framework/resource"
1010
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -14,6 +14,29 @@ import (
1414
"github.com/hashicorp/terraform-plugin-framework/types"
1515
)
1616

17+
// V0 model structures - used regular string types for JSON fields
18+
type integrationPolicyModelV0 struct {
19+
ID types.String `tfsdk:"id"`
20+
PolicyID types.String `tfsdk:"policy_id"`
21+
Name types.String `tfsdk:"name"`
22+
Namespace types.String `tfsdk:"namespace"`
23+
AgentPolicyID types.String `tfsdk:"agent_policy_id"`
24+
Description types.String `tfsdk:"description"`
25+
Enabled types.Bool `tfsdk:"enabled"`
26+
Force types.Bool `tfsdk:"force"`
27+
IntegrationName types.String `tfsdk:"integration_name"`
28+
IntegrationVersion types.String `tfsdk:"integration_version"`
29+
Input types.List `tfsdk:"input"` //> integrationPolicyInputModelV0
30+
VarsJson types.String `tfsdk:"vars_json"`
31+
}
32+
33+
type integrationPolicyInputModelV0 struct {
34+
InputID types.String `tfsdk:"input_id"`
35+
Enabled types.Bool `tfsdk:"enabled"`
36+
StreamsJson types.String `tfsdk:"streams_json"`
37+
VarsJson types.String `tfsdk:"vars_json"`
38+
}
39+
1740
func getSchemaV0() *schema.Schema {
1841
return &schema.Schema{
1942
Version: 0,
@@ -45,70 +68,84 @@ func getSchemaV0() *schema.Schema {
4568
}
4669
}
4770

48-
func getInputTypeV0() attr.Type {
49-
return getSchemaV0().Blocks["input"].Type().(attr.TypeWithElementType).ElementType()
50-
}
51-
52-
type integrationPolicyModelV0 struct {
53-
ID types.String `tfsdk:"id"`
54-
PolicyID types.String `tfsdk:"policy_id"`
55-
Name types.String `tfsdk:"name"`
56-
Namespace types.String `tfsdk:"namespace"`
57-
AgentPolicyID types.String `tfsdk:"agent_policy_id"`
58-
Description types.String `tfsdk:"description"`
59-
Enabled types.Bool `tfsdk:"enabled"`
60-
Force types.Bool `tfsdk:"force"`
61-
IntegrationName types.String `tfsdk:"integration_name"`
62-
IntegrationVersion types.String `tfsdk:"integration_version"`
63-
Input types.List `tfsdk:"input"` //> integrationPolicyInputModelV0
64-
VarsJson types.String `tfsdk:"vars_json"`
65-
}
66-
67-
type integrationPolicyInputModelV0 struct {
68-
InputID types.String `tfsdk:"input_id"`
69-
Enabled types.Bool `tfsdk:"enabled"`
70-
StreamsJson types.String `tfsdk:"streams_json"`
71-
VarsJson types.String `tfsdk:"vars_json"`
72-
}
73-
7471
// The schema between V0 and V1 is mostly the same, however vars_json and
7572
// streams_json saved "" values to the state when null values were in the
7673
// config. jsontypes.Normalized correctly states this is invalid JSON.
7774
func upgradeV0(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
78-
var stateModel integrationPolicyModelV0
75+
var stateModelV0 integrationPolicyModelV0
7976

80-
diags := req.State.Get(ctx, &stateModel)
77+
diags := req.State.Get(ctx, &stateModelV0)
8178
resp.Diagnostics.Append(diags...)
8279
if resp.Diagnostics.HasError() {
8380
return
8481
}
8582

86-
if varsJSON := stateModel.VarsJson.ValueStringPointer(); varsJSON != nil {
83+
// Convert V0 model to V1 model
84+
stateModelV1 := integrationPolicyModel{
85+
ID: stateModelV0.ID,
86+
PolicyID: stateModelV0.PolicyID,
87+
Name: stateModelV0.Name,
88+
Namespace: stateModelV0.Namespace,
89+
AgentPolicyID: stateModelV0.AgentPolicyID,
90+
AgentPolicyIDs: types.ListNull(types.StringType), // V0 didn't have agent_policy_ids
91+
Description: stateModelV0.Description,
92+
Enabled: stateModelV0.Enabled,
93+
Force: stateModelV0.Force,
94+
IntegrationName: stateModelV0.IntegrationName,
95+
IntegrationVersion: stateModelV0.IntegrationVersion,
96+
}
97+
98+
// Convert vars_json from string to normalized JSON type
99+
if varsJSON := stateModelV0.VarsJson.ValueStringPointer(); varsJSON != nil {
87100
if *varsJSON == "" {
88-
stateModel.VarsJson = types.StringNull()
101+
stateModelV1.VarsJson = jsontypes.NewNormalizedNull()
102+
} else {
103+
stateModelV1.VarsJson = jsontypes.NewNormalizedValue(*varsJSON)
89104
}
105+
} else {
106+
stateModelV1.VarsJson = jsontypes.NewNormalizedNull()
90107
}
91108

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 {
109+
// Convert inputs from V0 to V1
110+
inputsV0 := utils.ListTypeAs[integrationPolicyInputModelV0](ctx, stateModelV0.Input, path.Root("input"), &resp.Diagnostics)
111+
var inputsV1 []integrationPolicyInputModel
112+
113+
for _, inputV0 := range inputsV0 {
114+
inputV1 := integrationPolicyInputModel{
115+
InputID: inputV0.InputID,
116+
Enabled: inputV0.Enabled,
117+
}
118+
119+
// Convert vars_json
120+
if varsJSON := inputV0.VarsJson.ValueStringPointer(); varsJSON != nil {
95121
if *varsJSON == "" {
96-
input.VarsJson = types.StringNull()
122+
inputV1.VarsJson = jsontypes.NewNormalizedNull()
123+
} else {
124+
inputV1.VarsJson = jsontypes.NewNormalizedValue(*varsJSON)
97125
}
126+
} else {
127+
inputV1.VarsJson = jsontypes.NewNormalizedNull()
98128
}
99-
if streamsJSON := input.StreamsJson.ValueStringPointer(); streamsJSON != nil {
129+
130+
// Convert streams_json
131+
if streamsJSON := inputV0.StreamsJson.ValueStringPointer(); streamsJSON != nil {
100132
if *streamsJSON == "" {
101-
input.StreamsJson = types.StringNull()
133+
inputV1.StreamsJson = jsontypes.NewNormalizedNull()
134+
} else {
135+
inputV1.StreamsJson = jsontypes.NewNormalizedValue(*streamsJSON)
102136
}
137+
} else {
138+
inputV1.StreamsJson = jsontypes.NewNormalizedNull()
103139
}
104-
inputs[index] = input
140+
141+
inputsV1 = append(inputsV1, inputV1)
105142
}
106143

107-
stateModel.Input = utils.ListValueFrom(ctx, inputs, getInputTypeV0(), path.Root("input"), &resp.Diagnostics)
144+
stateModelV1.Input = utils.ListValueFrom(ctx, inputsV1, getInputTypeV1(), path.Root("input"), &resp.Diagnostics)
108145
if resp.Diagnostics.HasError() {
109146
return
110147
}
111148

112-
diags = resp.State.Set(ctx, stateModel)
149+
diags = resp.State.Set(ctx, stateModelV1)
113150
resp.Diagnostics.Append(diags...)
114151
}

0 commit comments

Comments
 (0)