/
gen_obj_settings_auth_saml_resource.go
442 lines (411 loc) · 15.4 KB
/
gen_obj_settings_auth_saml_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
package awx
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
p "path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"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/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 = &settingsAuthSamlResource{}
_ resource.ResourceWithConfigure = &settingsAuthSamlResource{}
)
// NewSettingsAuthSAMLResource is a helper function to simplify the provider implementation.
func NewSettingsAuthSAMLResource() resource.Resource {
return &settingsAuthSamlResource{}
}
type settingsAuthSamlResource struct {
client c.Client
endpoint string
}
func (o *settingsAuthSamlResource) 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/saml/"
}
func (o *settingsAuthSamlResource) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = request.ProviderTypeName + "_settings_auth_saml"
}
func (o *settingsAuthSamlResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Request elements
"saml_auto_create_objects": schema.BoolAttribute{
Description: "When enabled (the default), mapped Organizations and Teams will be created automatically on successful SAML login.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Validators: []validator.Bool{},
},
"social_auth_saml_enabled_idps": schema.StringAttribute{
Description: "Configure the Entity ID, SSO URL and certificate for each identity provider (IdP) in use. Multiple SAML IdPs are supported. Some IdPs may provide user data using attribute names that differ from the default OIDs. Attribute names may be overridden for each IdP. Refer to the Ansible documentation for additional details and syntax.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_extra_data": schema.ListAttribute{
ElementType: types.StringType,
Description: "A list of tuples that maps IDP attributes to extra_attributes. Each attribute will be a list of values, even if only 1 value.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
Validators: []validator.List{},
},
"social_auth_saml_organization_attr": schema.StringAttribute{
Description: "Used to translate user organization membership.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_organization_map": schema.StringAttribute{
Description: "Mapping to organization admins/users from social auth accounts. This setting\ncontrols which users are placed into which organizations based on their\nusername and email address. Configuration details are available in the\ndocumentation.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_org_info": schema.StringAttribute{
Description: "Provide the URL, display name, and the name of your app. 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{},
},
"social_auth_saml_security_config": schema.StringAttribute{
Description: "A dict of key value pairs that are passed to the underlying python-saml security setting https://github.com/onelogin/python-saml#settings",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{"requestedAuthnContext":false}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_sp_entity_id": schema.StringAttribute{
Description: "The application-defined unique identifier used as the audience of the SAML service provider (SP) configuration. This is usually the URL for the service.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(``),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_sp_extra": schema.StringAttribute{
Description: "A dict of key value pairs to be passed to the underlying python-saml Service Provider configuration setting.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_sp_private_key": schema.StringAttribute{
Description: "Create a keypair to use as a service provider (SP) and include the private key content here.",
Sensitive: true,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(``),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_sp_public_cert": schema.StringAttribute{
Description: "Create a keypair to use as a service provider (SP) and include the certificate content here.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(``),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_support_contact": schema.StringAttribute{
Description: "Provide the name and email address of the support contact for your service provider. 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{},
},
"social_auth_saml_team_attr": schema.StringAttribute{
Description: "Used to translate user team membership.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_team_map": schema.StringAttribute{
Description: "Mapping of team members (users) from social auth accounts. Configuration\ndetails are available in the documentation.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
"social_auth_saml_technical_contact": schema.StringAttribute{
Description: "Provide the name and email address of the technical contact for your service provider. 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{},
},
"social_auth_saml_user_flags_by_attr": schema.StringAttribute{
Description: "Used to map super users and system auditors from SAML.",
Sensitive: false,
Required: false,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(`{}`),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{},
},
// Write only elements
// Data only elements
"social_auth_saml_callback_url": schema.StringAttribute{
Description: "Register the service as a service provider (SP) with each identity provider (IdP) you have configured. Provide your SP Entity ID and this ACS URL for your application.",
Required: false,
Optional: false,
Computed: true,
Sensitive: false,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"social_auth_saml_metadata_url": schema.StringAttribute{
Description: "If your identity provider (IdP) allows uploading an XML metadata file, you can download one from this URL.",
Required: false,
Optional: false,
Computed: true,
Sensitive: false,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}
func (o *settingsAuthSamlResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var err error
var plan, state settingsAuthSamlTerraformModel
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for SettingsAuthSAML
var r *http.Request
var endpoint = p.Clean(o.endpoint) + "/"
var buf bytes.Buffer
var bodyRequest = plan.BodyRequest()
tflog.Debug(ctx, "[SettingsAuthSAML/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 SettingsAuthSAML on %s for create", endpoint),
err.Error(),
)
return
}
// Create a new SettingsAuthSAML 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 SettingsAuthSAML 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 = hookSettingsSaml(ctx, ApiVersion, hooks.SourceResource, hooks.CalleeCreate, &plan, &state); err != nil {
response.Diagnostics.AddError(
"Unable to process custom hook for the state on SettingsAuthSAML",
err.Error(),
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *settingsAuthSamlResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var err error
// Get current state
var state settingsAuthSamlTerraformModel
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
var orig = state.Clone()
// Creates a new request for SettingsAuthSAML
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 SettingsAuthSAML on %s for read", endpoint),
err.Error(),
)
return
}
// Get refreshed values for SettingsAuthSAML 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 SettingsAuthSAML 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 = hookSettingsSaml(ctx, ApiVersion, hooks.SourceResource, hooks.CalleeRead, &orig, &state); err != nil {
response.Diagnostics.AddError(
"Unable to process custom hook for the state on SettingsAuthSAML",
err.Error(),
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *settingsAuthSamlResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
var err error
var plan, state settingsAuthSamlTerraformModel
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for SettingsAuthSAML
var r *http.Request
var endpoint = p.Clean(o.endpoint) + "/"
var buf bytes.Buffer
var bodyRequest = plan.BodyRequest()
tflog.Debug(ctx, "[SettingsAuthSAML/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 SettingsAuthSAML on %s for update", endpoint),
err.Error(),
)
return
}
// Create a new SettingsAuthSAML 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 SettingsAuthSAML 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 = hookSettingsSaml(ctx, ApiVersion, hooks.SourceResource, hooks.CalleeUpdate, &plan, &state); err != nil {
response.Diagnostics.AddError(
"Unable to process custom hook for the state on SettingsAuthSAML",
err.Error(),
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *settingsAuthSamlResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
}