-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_resource.go
More file actions
187 lines (151 loc) · 5.76 KB
/
example_resource.go
File metadata and controls
187 lines (151 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package config
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &exampleResource{}
)
// ExampleResource is a helper function to simplify the provider implementation.
func ExampleResource() resource.Resource {
return &exampleResource{}
}
// exampleResource is the resource implementation.
type exampleResource struct {
}
type exampleResourceModel struct {
Id types.String `tfsdk:"id"`
StringVal types.String `tfsdk:"string_val"`
SingleNestedVal types.Object `tfsdk:"single_nested_val"`
}
type exampleSingleNestedValResourceModel struct {
StringVal types.String `tfsdk:"string_val"`
StringValStatic types.String `tfsdk:"string_val_static"`
StringValDynamic types.String `tfsdk:"string_val_dynamic"`
}
// GetSchema defines the schema for the resource.
func (r *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Example resource.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Computed id",
Computed: true,
Optional: false,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"string_val": schema.StringAttribute{
Description: "Optional string attribute",
Optional: true,
},
"single_nested_val": schema.SingleNestedAttribute{
Description: "Optional single nested object",
Optional: true,
Attributes: map[string]schema.Attribute{
"string_val": schema.StringAttribute{
Description: "Required string attribute",
Required: true,
},
"string_val_static": schema.StringAttribute{
Description: "Computed single nested attribute object, using \"UseStateForUnknown()\" plan modifier",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"string_val_dynamic": schema.StringAttribute{
Description: "Computed single nested attribute object, without using \"UseStateForUnknown()\" plan modifier",
Computed: true,
},
},
},
},
}
}
// Metadata returns the resource type name.
func (r *exampleResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_example"
}
func (r *exampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan, state exampleResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
// Stub API
data := plan
data.SetId()
resp.Diagnostics.Append(state.toState(ctx, data)...)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *exampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data, state exampleResourceModel
// Stub API
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
data = state
resp.Diagnostics.Append(state.toState(ctx, data)...)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *exampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan, state exampleResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
// Stub API
data := plan
data.SetId()
resp.Diagnostics.Append(state.toState(ctx, data)...)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
// No backend so no logic needed
func (r *exampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
}
func (r *exampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
func (m *exampleResourceModel) SetId() {
m.Id = types.StringValue("id")
}
func (m *exampleResourceModel) toState(ctx context.Context, data exampleResourceModel) (diags diag.Diagnostics) {
m.Id = data.Id
m.StringVal = data.StringVal
singleNestedObjectTyp := map[string]attr.Type{
"string_val": types.StringType,
"string_val_static": types.StringType,
"string_val_dynamic": types.StringType,
}
var d diag.Diagnostics
singleNestedPlan := exampleSingleNestedValResourceModel{
StringVal: types.StringValue("required string"),
StringValStatic: types.StringValue("service static string"),
StringValDynamic: types.StringValue("service dynamic string"),
}
if !data.SingleNestedVal.IsNull() {
d = data.SingleNestedVal.As(context.Background(), &singleNestedPlan, basetypes.ObjectAsOptions{})
diags.Append(d...)
if diags.HasError() {
return
}
singleNestedPlan.StringValStatic = types.StringValue("service static string")
singleNestedPlan.StringValDynamic = types.StringValue("service dynamic string, updated because this block is defined")
}
m.SingleNestedVal, d = types.ObjectValueFrom(ctx, singleNestedObjectTyp, singleNestedPlan)
diags = append(diags, d...)
return
}