/
gen_obj_settings_jobs_resource.go
483 lines (455 loc) · 17.5 KB
/
gen_obj_settings_jobs_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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package awx
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
p "path"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"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-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
c "github.com/ilijamt/terraform-provider-awx/internal/client"
"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 = &settingsJobsResource{}
_ resource.ResourceWithConfigure = &settingsJobsResource{}
)
// NewSettingsJobsResource is a helper function to simplify the provider implementation.
func NewSettingsJobsResource() resource.Resource {
return &settingsJobsResource{}
}
type settingsJobsResource struct {
client c.Client
endpoint string
}
func (o *settingsJobsResource) 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/settings/jobs/"
}
func (o *settingsJobsResource) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = request.ProviderTypeName + "_settings_jobs"
}
func (o *settingsJobsResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Request elements
"ad_hoc_commands": schema.ListAttribute{
ElementType: types.StringType,
Description: "List of modules allowed to be used by ad-hoc jobs.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
Validators: []validator.List{},
},
"allow_jinja_in_extra_vars": schema.StringAttribute{
Description: "Ansible allows variable substitution via the Jinja2 templating language for --extra-vars. This poses a potential security risk where users with the ability to specify extra vars at job launch time can use Jinja2 templates to run arbitrary Python. It is recommended that this value be set to \"template\" or \"never\".",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`template`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
stringvalidator.OneOf([]string{"always", "never", "template"}...),
},
},
"ansible_fact_cache_timeout": schema.Int64Attribute{
Description: "Maximum time, in seconds, that stored Ansible facts are considered valid since the last time they were modified. Only valid, non-stale, facts will be accessible by a playbook. Note, this does not influence the deletion of ansible_facts from the database. Use a value of 0 to indicate that no timeout should be imposed.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"awx_ansible_callback_plugins": schema.ListAttribute{
ElementType: types.StringType,
Description: "List of paths to search for extra callback plugins to be used when running jobs. Enter one path per line.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
Validators: []validator.List{},
},
"awx_collections_enabled": schema.BoolAttribute{
Description: "Allows collections to be dynamically downloaded from a requirements.yml file for SCM projects.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Validators: []validator.Bool{},
},
"awx_isolation_base_path": schema.StringAttribute{
Description: "The directory in which the service will create new temporary directories for job execution and isolation (such as credential files).",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`/tmp`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"awx_isolation_show_paths": schema.ListAttribute{
ElementType: types.StringType,
Description: "List of paths that would otherwise be hidden to expose to isolated jobs. Enter one path per line. Volumes will be mounted from the execution node to the container. The supported format is HOST-DIR[:CONTAINER-DIR[:OPTIONS]]. ",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
Validators: []validator.List{},
},
"awx_mount_isolated_paths_on_k8s": schema.BoolAttribute{
Description: "Expose paths via hostPath for the Pods created by a Container Group. HostPath volumes present many security risks, and it is a best practice to avoid the use of HostPaths when possible. ",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Validators: []validator.Bool{},
},
"awx_roles_enabled": schema.BoolAttribute{
Description: "Allows roles to be dynamically downloaded from a requirements.yml file for SCM projects.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Validators: []validator.Bool{},
},
"awx_show_playbook_links": schema.BoolAttribute{
Description: "Follow symbolic links when scanning for playbooks. Be aware that setting this to True can lead to infinite recursion if a link points to a parent directory of itself.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Validators: []validator.Bool{},
},
"awx_task_env": schema.StringAttribute{
Description: "Additional environment variables set for playbook runs, inventory updates, project updates, and notification sending.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"default_inventory_update_timeout": schema.Int64Attribute{
Description: "Maximum time in seconds to allow inventory updates to run. Use value of 0 to indicate that no timeout should be imposed. A timeout set on an individual inventory source will override this.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"default_job_idle_timeout": schema.Int64Attribute{
Description: "If no output is detected from ansible in this number of seconds the execution will be terminated. Use value of 0 to indicate that no idle timeout should be imposed.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"default_job_timeout": schema.Int64Attribute{
Description: "Maximum time in seconds to allow jobs to run. Use value of 0 to indicate that no timeout should be imposed. A timeout set on an individual job template will override this.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"default_project_update_timeout": schema.Int64Attribute{
Description: "Maximum time in seconds to allow project updates to run. Use value of 0 to indicate that no timeout should be imposed. A timeout set on an individual project will override this.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"event_stdout_max_bytes_display": schema.Int64Attribute{
Description: "Maximum Size of Standard Output in bytes to display for a single job or ad hoc command event. `stdout` will end with `…` when truncated.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(1024),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"galaxy_ignore_certs": schema.BoolAttribute{
Description: "If set to true, certificate validation will not be done when installing content from any Galaxy server.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Validators: []validator.Bool{},
},
"galaxy_task_env": schema.StringAttribute{
Description: "Additional environment variables set for invocations of ansible-galaxy within project updates. Useful if you must use a proxy server for ansible-galaxy but not git.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{"ANSIBLE_FORCE_COLOR":"false","GIT_SSH_COMMAND":"ssh -o StrictHostKeyChecking=no"}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"max_forks": schema.Int64Attribute{
Description: "Saving a Job Template with more than this number of forks will result in an error. When set to 0, no limit is applied.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(200),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"max_websocket_event_rate": schema.Int64Attribute{
Description: "Maximum number of messages to update the UI live job output with per second. Value of 0 means no limit.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(30),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"project_update_vvv": schema.BoolAttribute{
Description: "Adds the CLI -vvv flag to ansible-playbook runs of project_update.yml used for project updates.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Validators: []validator.Bool{},
},
"schedule_max_jobs": schema.Int64Attribute{
Description: "Maximum number of the same job template that can be waiting to run when launching from a schedule before no more are created.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(10),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
"stdout_max_bytes_display": schema.Int64Attribute{
Description: "Maximum Size of Standard Output in bytes to display before requiring the output be downloaded.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(1.048576e+06),
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{},
},
// Write only elements
// Data only elements
},
}
}
func (o *settingsJobsResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var err error
var plan, state settingsJobsTerraformModel
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for SettingsJobs
var r *http.Request
var endpoint = p.Clean(o.endpoint) + "/"
var buf bytes.Buffer
var bodyRequest = plan.BodyRequest()
tflog.Debug(ctx, "[SettingsJobs/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.MethodPatch, endpoint, &buf); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for SettingsJobs on %s for create", endpoint),
err.Error(),
)
return
}
// Create a new SettingsJobs 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 SettingsJobs on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if d, err = state.updateFromApiData(data); err != nil {
response.Diagnostics.Append(d...)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *settingsJobsResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var err error
// Get current state
var state settingsJobsTerraformModel
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for SettingsJobs
var r *http.Request
var endpoint = p.Clean(o.endpoint) + "/"
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 SettingsJobs on %s for read", endpoint),
err.Error(),
)
return
}
// Get refreshed values for SettingsJobs 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 SettingsJobs on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if d, err = state.updateFromApiData(data); err != nil {
response.Diagnostics.Append(d...)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *settingsJobsResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
var err error
var plan, state settingsJobsTerraformModel
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for SettingsJobs
var r *http.Request
var endpoint = p.Clean(o.endpoint) + "/"
var buf bytes.Buffer
var bodyRequest = plan.BodyRequest()
tflog.Debug(ctx, "[SettingsJobs/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 SettingsJobs on %s for update", endpoint),
err.Error(),
)
return
}
// Create a new SettingsJobs 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 SettingsJobs on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if d, err = state.updateFromApiData(data); err != nil {
response.Diagnostics.Append(d...)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *settingsJobsResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
}