Skip to content

Commit 81edeff

Browse files
committed
add prior schema
1 parent 175e66a commit 81edeff

File tree

2 files changed

+135
-2
lines changed

2 files changed

+135
-2
lines changed

example/cluster/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
plural = {
44
source = "pluralsh/plural"
5-
version = "0.2.29"
5+
version = "0.2.30"
66
}
77
}
88
}

internal/resource/cluster.go

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import (
1111

1212
"github.com/hashicorp/terraform-plugin-framework/path"
1313
"github.com/hashicorp/terraform-plugin-framework/resource"
14+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
15+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
16+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
17+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
18+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
19+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1420
"github.com/hashicorp/terraform-plugin-framework/types"
21+
"github.com/pluralsh/plural-cli/pkg/console"
1522
"github.com/samber/lo"
1623
"k8s.io/apimachinery/pkg/util/wait"
1724
)
@@ -218,8 +225,134 @@ func (r *clusterResource) UpgradeState(_ context.Context) map[int64]resource.Sta
218225
return map[int64]resource.StateUpgrader{
219226
// State upgrade from 0 to 1
220227
0: {
228+
PriorSchema: &schema.Schema{
229+
Version: 0,
230+
Attributes: map[string]schema.Attribute{
231+
"id": schema.StringAttribute{
232+
Computed: true,
233+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
234+
},
235+
"inserted_at": schema.StringAttribute{
236+
Computed: true,
237+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
238+
},
239+
"name": schema.StringAttribute{
240+
Required: true,
241+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
242+
},
243+
"handle": schema.StringAttribute{
244+
Optional: true,
245+
Computed: true,
246+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
247+
},
248+
"project_id": schema.StringAttribute{
249+
Optional: true,
250+
},
251+
"detach": schema.BoolAttribute{
252+
Optional: true,
253+
Computed: true,
254+
Default: booldefault.StaticBool(false),
255+
},
256+
"metadata": schema.StringAttribute{
257+
Optional: true,
258+
Computed: true,
259+
Default: stringdefault.StaticString("{}"),
260+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
261+
},
262+
"helm_repo_url": schema.StringAttribute{
263+
Optional: true,
264+
Computed: true,
265+
Default: stringdefault.StaticString(console.RepoUrl),
266+
},
267+
"helm_values": schema.StringAttribute{
268+
Optional: true,
269+
},
270+
"kubeconfig": common.KubeconfigResourceSchema(),
271+
"protect": schema.BoolAttribute{
272+
Optional: true,
273+
Computed: true,
274+
Default: booldefault.StaticBool(false),
275+
},
276+
"tags": schema.MapAttribute{
277+
Optional: true,
278+
ElementType: types.StringType,
279+
},
280+
"bindings": schema.SingleNestedAttribute{
281+
Optional: true,
282+
Attributes: map[string]schema.Attribute{
283+
"read": schema.SetNestedAttribute{
284+
Optional: true,
285+
NestedObject: schema.NestedAttributeObject{
286+
Attributes: map[string]schema.Attribute{
287+
"group_id": schema.StringAttribute{Optional: true},
288+
"id": schema.StringAttribute{Optional: true},
289+
"user_id": schema.StringAttribute{Optional: true},
290+
},
291+
},
292+
},
293+
"write": schema.SetNestedAttribute{
294+
Optional: true,
295+
Description: "Write policies of this cluster.",
296+
MarkdownDescription: "Write policies of this cluster.",
297+
NestedObject: schema.NestedAttributeObject{
298+
Attributes: map[string]schema.Attribute{
299+
"group_id": schema.StringAttribute{
300+
Optional: true,
301+
},
302+
"id": schema.StringAttribute{
303+
Optional: true,
304+
},
305+
"user_id": schema.StringAttribute{
306+
Optional: true,
307+
},
308+
},
309+
},
310+
},
311+
},
312+
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
313+
},
314+
},
315+
},
221316
StateUpgrader: func(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
222-
resp.Diagnostics.Append(resp.State.Set(ctx, cluster{AgentDeployed: types.BoolValue(true)})...)
317+
var priorStateData struct {
318+
Id types.String `tfsdk:"id"`
319+
InsertedAt types.String `tfsdk:"inserted_at"`
320+
Name types.String `tfsdk:"name"`
321+
Handle types.String `tfsdk:"handle"`
322+
ProjectId types.String `tfsdk:"project_id"`
323+
Detach types.Bool `tfsdk:"detach"`
324+
Protect types.Bool `tfsdk:"protect"`
325+
Tags types.Map `tfsdk:"tags"`
326+
Metadata types.String `tfsdk:"metadata"`
327+
Bindings *common.Bindings `tfsdk:"bindings"`
328+
HelmRepoUrl types.String `tfsdk:"helm_repo_url"`
329+
HelmValues types.String `tfsdk:"helm_values"`
330+
Kubeconfig *common.Kubeconfig `tfsdk:"kubeconfig"`
331+
}
332+
333+
resp.Diagnostics.Append(req.State.Get(ctx, &priorStateData)...)
334+
if resp.Diagnostics.HasError() {
335+
return
336+
}
337+
338+
upgradedStateData := cluster{
339+
Id: priorStateData.Id,
340+
InsertedAt: priorStateData.InsertedAt,
341+
Name: priorStateData.Name,
342+
Handle: priorStateData.Handle,
343+
ProjectId: priorStateData.ProjectId,
344+
Detach: priorStateData.Detach,
345+
Protect: priorStateData.Protect,
346+
Tags: priorStateData.Tags,
347+
Metadata: priorStateData.Metadata,
348+
Bindings: priorStateData.Bindings,
349+
HelmRepoUrl: priorStateData.HelmRepoUrl,
350+
HelmValues: priorStateData.HelmValues,
351+
Kubeconfig: priorStateData.Kubeconfig,
352+
AgentDeployed: types.BoolValue(true),
353+
}
354+
355+
resp.Diagnostics.Append(resp.State.Set(ctx, upgradedStateData)...)
223356
},
224357
},
225358
}

0 commit comments

Comments
 (0)