/
gen_obj_job_template_data_source.go
457 lines (434 loc) · 14.2 KB
/
gen_obj_job_template_data_source.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
package awx
import (
"context"
"fmt"
"net/http"
"net/url"
p "path"
c "github.com/ilijamt/terraform-provider-awx/internal/client"
"github.com/ilijamt/terraform-provider-awx/internal/helpers"
"github.com/ilijamt/terraform-provider-awx/internal/hooks"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = &jobTemplateDataSource{}
_ datasource.DataSourceWithConfigure = &jobTemplateDataSource{}
)
// NewJobTemplateDataSource is a helper function to instantiate the JobTemplate data source.
func NewJobTemplateDataSource() datasource.DataSource {
return &jobTemplateDataSource{}
}
// jobTemplateDataSource is the data source implementation.
type jobTemplateDataSource struct {
client c.Client
endpoint string
}
// Configure adds the provider configured client to the data source.
func (o *jobTemplateDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
o.client = req.ProviderData.(c.Client)
o.endpoint = "/api/v2/job_templates/"
}
// Metadata returns the data source type name.
func (o *jobTemplateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_job_template"
}
// Schema defines the schema for the data source.
func (o *jobTemplateDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Data only elements
"allow_simultaneous": schema.BoolAttribute{
Description: "Allow simultaneous",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_credential_on_launch": schema.BoolAttribute{
Description: "Ask credential on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_diff_mode_on_launch": schema.BoolAttribute{
Description: "Ask diff mode on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_execution_environment_on_launch": schema.BoolAttribute{
Description: "Ask execution environment on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_forks_on_launch": schema.BoolAttribute{
Description: "Ask forks on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_instance_groups_on_launch": schema.BoolAttribute{
Description: "Ask instance groups on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_inventory_on_launch": schema.BoolAttribute{
Description: "Ask inventory on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_job_slice_count_on_launch": schema.BoolAttribute{
Description: "Ask job slice count on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_job_type_on_launch": schema.BoolAttribute{
Description: "Ask job type on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_labels_on_launch": schema.BoolAttribute{
Description: "Ask labels on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_limit_on_launch": schema.BoolAttribute{
Description: "Ask limit on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_scm_branch_on_launch": schema.BoolAttribute{
Description: "Ask scm branch on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_skip_tags_on_launch": schema.BoolAttribute{
Description: "Ask skip tags on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_tags_on_launch": schema.BoolAttribute{
Description: "Ask tags on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_timeout_on_launch": schema.BoolAttribute{
Description: "Ask timeout on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_variables_on_launch": schema.BoolAttribute{
Description: "Ask variables on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"ask_verbosity_on_launch": schema.BoolAttribute{
Description: "Ask verbosity on launch",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"become_enabled": schema.BoolAttribute{
Description: "Become enabled",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"description": schema.StringAttribute{
Description: "Optional description of this job template.",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"diff_mode": schema.BoolAttribute{
Description: "If enabled, textual changes made to any templated files on the host are shown in the standard output",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"execution_environment": schema.Int64Attribute{
Description: "The container image to be used for execution.",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"extra_vars": schema.StringAttribute{
Description: "Extra vars",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"force_handlers": schema.BoolAttribute{
Description: "Force handlers",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"forks": schema.Int64Attribute{
Description: "Forks",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{
int64validator.Between(0, 2147483647),
},
},
"host_config_key": schema.StringAttribute{
Description: "Host config key",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"id": schema.Int64Attribute{
Description: "Database ID for this job template.",
Sensitive: false,
Optional: true,
Computed: true,
Validators: []validator.Int64{
int64validator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
),
},
},
"inventory": schema.Int64Attribute{
Description: "Inventory",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"job_slice_count": schema.Int64Attribute{
Description: "The number of jobs to slice into at runtime. Will cause the Job Template to launch a workflow if value is greater than 1.",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{
int64validator.Between(0, 2147483647),
},
},
"job_tags": schema.StringAttribute{
Description: "Job tags",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"job_type": schema.StringAttribute{
Description: "Job type",
Sensitive: false,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf([]string{"run", "check"}...),
},
},
"limit": schema.StringAttribute{
Description: "Limit",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"name": schema.StringAttribute{
Description: "Name of this job template.",
Sensitive: false,
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
),
},
},
"organization": schema.Int64Attribute{
Description: "The organization used to determine access to this template.",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"playbook": schema.StringAttribute{
Description: "Playbook",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"prevent_instance_group_fallback": schema.BoolAttribute{
Description: "If enabled, the job template will prevent adding any inventory or organization instance groups to the list of preferred instances groups to run on.If this setting is enabled and you provided an empty list, the global instance groups will be applied.",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"project": schema.Int64Attribute{
Description: "Project",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"scm_branch": schema.StringAttribute{
Description: "Branch to use in job run. Project default used if blank. Only allowed if project allow_override field is set to true.",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"skip_tags": schema.StringAttribute{
Description: "Skip tags",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"start_at_task": schema.StringAttribute{
Description: "Start at task",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"survey_enabled": schema.BoolAttribute{
Description: "Survey enabled",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"timeout": schema.Int64Attribute{
Description: "The amount of time (in seconds) to run before the task is canceled.",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{
int64validator.Between(-2147483648, 2147483647),
},
},
"use_fact_cache": schema.BoolAttribute{
Description: "If enabled, the service will act as an Ansible Fact Cache Plugin; persisting facts at the end of a playbook run to the database and caching facts for use by Ansible.",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"verbosity": schema.StringAttribute{
Description: "Verbosity",
Sensitive: false,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf([]string{"0", "1", "2", "3", "4", "5"}...),
},
},
"webhook_credential": schema.Int64Attribute{
Description: "Personal Access Token for posting back the status to the service API",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"webhook_service": schema.StringAttribute{
Description: "Service that webhook requests will be accepted from",
Sensitive: false,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf([]string{"", "github", "gitlab"}...),
},
},
// Write only elements
},
}
}
func (o *jobTemplateDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{}
}
// Read refreshes the Terraform state with the latest data.
func (o *jobTemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state jobTemplateTerraformModel
var err error
var endpoint string
var searchDefined bool
// Only one group should evaluate to True, terraform should prevent from being able to set
// the conflicting groups
// Evaluate group 'by_id' based on the schema definition
var groupByIdExists = func() bool {
var groupByIdExists = true
var paramsById = []any{o.endpoint}
var attrID types.Int64
req.Config.GetAttribute(ctx, path.Root("id"), &attrID)
groupByIdExists = groupByIdExists && (!attrID.IsNull() && !attrID.IsUnknown())
paramsById = append(paramsById, attrID.ValueInt64())
if groupByIdExists {
endpoint = p.Clean(fmt.Sprintf("%s/%d/", paramsById...))
}
return groupByIdExists
}()
searchDefined = searchDefined || groupByIdExists
// Evaluate group 'by_name' based on the schema definition
var groupByNameExists = func() bool {
var groupByNameExists = true
var paramsByName = []any{o.endpoint}
var attrName types.String
req.Config.GetAttribute(ctx, path.Root("name"), &attrName)
groupByNameExists = groupByNameExists && (!attrName.IsNull() && !attrName.IsUnknown())
paramsByName = append(paramsByName, url.PathEscape(attrName.ValueString()))
if groupByNameExists {
endpoint = p.Clean(fmt.Sprintf("%s/?name__exact=%s", paramsByName...))
}
return groupByNameExists
}()
searchDefined = searchDefined || groupByNameExists
if !searchDefined {
var detailMessage string
resp.Diagnostics.AddError(
"missing configuration for one of the predefined search groups",
detailMessage,
)
return
}
// Creates a new request for JobTemplate
var r *http.Request
if r, err = o.client.NewRequest(ctx, http.MethodGet, endpoint, nil); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for JobTemplate on %s for read", o.endpoint),
err.Error(),
)
return
}
// Try and actually fetch the data for JobTemplate
var data map[string]any
if data, err = o.client.Do(ctx, r); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to read resource for JobTemplate on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if data, d, err = helpers.ExtractDataIfSearchResult(data); err != nil {
resp.Diagnostics.Append(d...)
return
}
if d, err = state.updateFromApiData(data); err != nil {
resp.Diagnostics.Append(d...)
return
}
// Set state
if err = hooks.RequireResourceStateOrOrig(ctx, ApiVersion, hooks.SourceData, hooks.CalleeRead, nil, &state); err != nil {
resp.Diagnostics.AddError(
"Unable to process custom hook for the state on JobTemplate",
err.Error(),
)
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}