/
gen_obj_credential_resource.go
385 lines (350 loc) · 11.5 KB
/
gen_obj_credential_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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package awx
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
p "path"
"strconv"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-log/tflog"
c "github.com/ilijamt/terraform-provider-awx/internal/client"
"github.com/ilijamt/terraform-provider-awx/internal/hooks"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &credentialResource{}
_ resource.ResourceWithConfigure = &credentialResource{}
_ resource.ResourceWithImportState = &credentialResource{}
)
// NewCredentialResource is a helper function to simplify the provider implementation.
func NewCredentialResource() resource.Resource {
return &credentialResource{}
}
type credentialResource struct {
client c.Client
endpoint string
}
func (o *credentialResource) Configure(ctx context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) {
if request.ProviderData == nil {
return
}
o.client = request.ProviderData.(c.Client)
o.endpoint = "/api/v2/credentials/"
}
func (o *credentialResource) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = request.ProviderTypeName + "_credential"
}
func (o *credentialResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Request elements
"credential_type": schema.Int64Attribute{
Description: "Specify the type of credential you want to create. Refer to the documentation for details on each type.",
Sensitive: false,
Required: true,
Optional: false,
Computed: false,
PlanModifiers: []planmodifier.Int64{},
Validators: []validator.Int64{},
},
"description": schema.StringAttribute{
Description: "Optional description of this credential.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(``),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"inputs": schema.StringAttribute{
Description: "Enter inputs using either JSON or YAML syntax. Refer to the documentation for example syntax.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"name": schema.StringAttribute{
Description: "Name of this credential.",
Sensitive: false,
Required: true,
Optional: false,
Computed: false,
PlanModifiers: []planmodifier.String{},
Validators: []validator.String{
stringvalidator.LengthAtMost(512),
},
},
"organization": schema.Int64Attribute{
Description: "Inherit permissions from organization roles. If provided on creation, do not give either user or team.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
// Write only elements
// Data only elements
"cloud": schema.BoolAttribute{
Description: "",
Required: false,
Optional: false,
Computed: true,
Sensitive: false,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
},
"id": schema.Int64Attribute{
Description: "Database ID for this credential.",
Required: false,
Optional: false,
Computed: true,
Sensitive: false,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
"kind": schema.StringAttribute{
Description: "",
Required: false,
Optional: false,
Computed: true,
Sensitive: false,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"kubernetes": schema.BoolAttribute{
Description: "",
Required: false,
Optional: false,
Computed: true,
Sensitive: false,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
},
"managed": schema.BoolAttribute{
Description: "",
Required: false,
Optional: false,
Computed: true,
Sensitive: false,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
},
},
}
}
func (o *credentialResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
var id, err = strconv.ParseInt(request.ID, 10, 64)
if err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to parse '%v' as an int64 number, please provide the ID for the Credential.", request.ID),
err.Error(),
)
return
}
response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root("id"), id)...)
}
func (o *credentialResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var err error
var plan, state credentialTerraformModel
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for Credential
var r *http.Request
var endpoint = p.Clean(o.endpoint) + "/"
var buf bytes.Buffer
var bodyRequest = plan.BodyRequest()
tflog.Debug(ctx, "[Credential/Create] Making a request", map[string]interface{}{
"payload": bodyRequest,
"method": http.MethodPost,
"endpoint": endpoint,
})
_ = json.NewEncoder(&buf).Encode(bodyRequest)
if r, err = o.client.NewRequest(ctx, http.MethodPost, endpoint, &buf); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for Credential on %s for create", endpoint),
err.Error(),
)
return
}
// Create a new Credential resource in AWX
var data map[string]any
if data, err = o.client.Do(ctx, r); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create resource for Credential on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if d, err = state.updateFromApiData(data); err != nil {
response.Diagnostics.Append(d...)
return
}
if err = hookCredential(ctx, ApiVersion, hooks.SourceResource, hooks.CalleeCreate, &plan, &state); err != nil {
response.Diagnostics.AddError(
"Unable to process custom hook for the state on Credential",
err.Error(),
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *credentialResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var err error
// Get current state
var state credentialTerraformModel
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
var orig = state.Clone()
// Creates a new request for Credential
var r *http.Request
var id = state.ID.ValueInt64()
var endpoint = p.Clean(fmt.Sprintf("%s/%v", o.endpoint, id)) + "/"
if r, err = o.client.NewRequest(ctx, http.MethodGet, endpoint, nil); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for Credential on %s for read", endpoint),
err.Error(),
)
return
}
// Get refreshed values for Credential from AWX
var data map[string]any
if data, err = o.client.Do(ctx, r); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to read resource for Credential on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if d, err = state.updateFromApiData(data); err != nil {
response.Diagnostics.Append(d...)
return
}
if err = hookCredential(ctx, ApiVersion, hooks.SourceResource, hooks.CalleeRead, &orig, &state); err != nil {
response.Diagnostics.AddError(
"Unable to process custom hook for the state on Credential",
err.Error(),
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *credentialResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
var err error
var plan, state credentialTerraformModel
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for Credential
var r *http.Request
var id = plan.ID.ValueInt64()
var endpoint = p.Clean(fmt.Sprintf("%s/%v", o.endpoint, id)) + "/"
var buf bytes.Buffer
var bodyRequest = plan.BodyRequest()
tflog.Debug(ctx, "[Credential/Update] Making a request", map[string]interface{}{
"payload": bodyRequest,
"method": http.MethodPost,
"endpoint": endpoint,
})
_ = json.NewEncoder(&buf).Encode(bodyRequest)
if r, err = o.client.NewRequest(ctx, http.MethodPatch, endpoint, &buf); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for Credential on %s for update", endpoint),
err.Error(),
)
return
}
// Create a new Credential resource in AWX
var data map[string]any
if data, err = o.client.Do(ctx, r); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to update resource for Credential on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if d, err = state.updateFromApiData(data); err != nil {
response.Diagnostics.Append(d...)
return
}
if err = hookCredential(ctx, ApiVersion, hooks.SourceResource, hooks.CalleeUpdate, &plan, &state); err != nil {
response.Diagnostics.AddError(
"Unable to process custom hook for the state on Credential",
err.Error(),
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *credentialResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
var err error
// Retrieve values from state
var state credentialTerraformModel
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for Credential
var r *http.Request
var id = state.ID
var endpoint = p.Clean(fmt.Sprintf("%s/%v", o.endpoint, id.ValueInt64())) + "/"
if r, err = o.client.NewRequest(ctx, http.MethodDelete, endpoint, nil); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for Credential on %s for delete", endpoint),
err.Error(),
)
return
}
// Delete existing Credential
if _, err = o.client.Do(ctx, r); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to delete resource for Credential on %s", endpoint),
err.Error(),
)
return
}
}