generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute_group_resource.go
265 lines (215 loc) · 7.58 KB
/
attribute_group_resource.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package provider
import (
"context"
"fmt"
"github.com/0xfrej/terraform-provider-akeneo/internal/akeneox"
"github.com/0xfrej/terraform-provider-akeneo/internal/validator/stringvalidatorx"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &AttributeGroupResource{}
var _ resource.ResourceWithImportState = &AttributeGroupResource{}
var _ resource.ResourceWithConfigure = &AttributeGroupResource{}
func NewAttributeGroupResource() resource.Resource {
return &AttributeGroupResource{}
}
// AttributeGroupResource defines the resource implementation.
type AttributeGroupResource struct {
client *akeneox.AttributeService
}
// AttributeGroupResourceModel describes the resource data model.
type AttributeGroupResourceModel struct {
Code types.String `tfsdk:"code"`
SortOrder types.Int64 `tfsdk:"sort_order"`
Labels types.Map `tfsdk:"labels"`
}
func (r *AttributeGroupResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_attribute_group"
}
func (r *AttributeGroupResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Akeneo attribute group resource",
Attributes: map[string]schema.Attribute{
"code": schema.StringAttribute{
Description: "Attribute group code",
Required: true,
},
"sort_order": schema.Int64Attribute{
Description: "Order of the attribute group",
Optional: true,
Validators: []validator.Int64{
int64validator.Between(0, 2147483647),
},
},
"labels": schema.MapAttribute{
Description: "Label definition per locale",
Optional: true,
ElementType: types.StringType,
Validators: []validator.Map{
mapvalidator.KeysAre(stringvalidatorx.IsLocaleCode()),
},
},
},
}
}
func (r *AttributeGroupResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
data, ok := req.ProviderData.(*ResourceData)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *ResourceData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
if data.Client == nil {
resp.Diagnostics.AddError(
"Missing client instance",
"Client instance pointer passed to Configure is required, got nil",
)
return
}
r.client = akeneox.NewAttributeClient(data.Client)
}
func (r *AttributeGroupResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data AttributeGroupResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
apiData := r.mapToApiObject(ctx, &resp.Diagnostics, &data)
if resp.Diagnostics.HasError() {
return
}
err := r.client.CreateAttributeGroup(*apiData)
if err != nil {
resp.Diagnostics.AddError(
"Error while creating an attribute group",
"An unexpected error occurred when creating attribute group. \n\n"+
"Akeneo API Error: "+err.Error(),
)
return
}
if resp.Diagnostics.HasError() {
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *AttributeGroupResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data AttributeGroupResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
attrData, err := r.client.GetAttributeGroup(data.Code.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error while reading an attribute group",
"An unexpected error occurred when reading attribute group. \n\n"+
"Akeneo API Error: "+err.Error(),
)
return
}
if resp.Diagnostics.HasError() {
return
}
r.mapToTfObject(&resp.Diagnostics, &data, attrData)
if resp.Diagnostics.HasError() {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *AttributeGroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data AttributeGroupResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
apiData := r.mapToApiObject(ctx, &resp.Diagnostics, &data)
if resp.Diagnostics.HasError() {
return
}
_, err := r.client.UpdateAttributeGroup(*apiData)
if err != nil {
resp.Diagnostics.AddError(
"Error while updating an attribute group",
"An unexpected error occurred when updating attribute group. \n\n"+
"Akeneo API Error: "+err.Error(),
)
return
}
if resp.Diagnostics.HasError() {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *AttributeGroupResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
//var data AttributeGroupResourceModel
//
//// Read Terraform prior state data into the model
//resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
//
//if resp.Diagnostics.HasError() {
// return
//}
resp.Diagnostics.AddError(
"This resource does not support deletes",
"This resource does not support deletes. The Akeneo API does not support deletes for attribute group.",
)
}
func (r *AttributeGroupResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("code"), req, resp)
}
func (r *AttributeGroupResource) mapToApiObject(ctx context.Context, diags *diag.Diagnostics, data *AttributeGroupResourceModel) *akeneox.AttributeGroup {
a := akeneox.AttributeGroup{
Code: data.Code.ValueString(),
}
if !(data.SortOrder.IsNull() || data.SortOrder.IsUnknown()) {
a.SortOrder = int(data.SortOrder.ValueInt64())
}
if !(data.Labels.IsNull() || data.Labels.IsUnknown()) {
elements := make(map[string]types.String, len(data.Labels.Elements()))
diags.Append(data.Labels.ElementsAs(ctx, &elements, false)...)
labels := make(map[string]string)
for locale, label := range elements {
labels[locale] = label.ValueString()
}
a.Labels = labels
}
if diags.HasError() {
return nil
}
return &a
}
func (r *AttributeGroupResource) mapToTfObject(respDiags *diag.Diagnostics, data *AttributeGroupResourceModel, attrData *akeneox.AttributeGroup) {
data.Code = types.StringValue(attrData.Code)
if attrData.Labels != nil {
elements := make(map[string]attr.Value, len(attrData.Labels))
for k, v := range attrData.Labels {
elements[k] = types.StringValue(v)
}
mapVal, diags := types.MapValue(types.StringType, elements)
if diags.HasError() {
respDiags.Append(diags...)
}
data.Labels = mapVal
}
data.SortOrder = types.Int64Value(int64(attrData.SortOrder))
}