/
provider.go
754 lines (720 loc) · 28.7 KB
/
provider.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
package provider
import (
"context"
"fmt"
"net/url"
"sync"
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/magodo/terraform-provider-restful/internal/client"
myvalidator "github.com/magodo/terraform-provider-restful/internal/validator"
)
var _ provider.Provider = &Provider{}
type Provider struct {
client *client.Client
apiOpt apiOption
once sync.Once
}
type providerData struct {
config providerConfig
provider *Provider
}
type providerConfig struct {
BaseURL types.String `tfsdk:"base_url"`
Security types.Object `tfsdk:"security"`
CreateMethod types.String `tfsdk:"create_method"`
UpdateMethod types.String `tfsdk:"update_method"`
DeleteMethod types.String `tfsdk:"delete_method"`
MergePatchDisabled types.Bool `tfsdk:"merge_patch_disabled"`
Query types.Map `tfsdk:"query"`
Header types.Map `tfsdk:"header"`
CookieEnabled types.Bool `tfsdk:"cookie_enabled"`
TlsInsecureSkipVerify types.Bool `tfsdk:"tls_insecure_skip_verify"`
}
type securityData struct {
HTTP types.Object `tfsdk:"http"`
OAuth2 types.Object `tfsdk:"oauth2"`
APIKey types.Set `tfsdk:"apikey"`
}
type httpData struct {
Basic types.Object `tfsdk:"basic"`
Token types.Object `tfsdk:"token"`
}
type httpBasicData struct {
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
}
type httpTokenData struct {
Token types.String `tfsdk:"token"`
Scheme types.String `tfsdk:"scheme"`
}
type apikeyData struct {
Name types.String `tfsdk:"name"`
In types.String `tfsdk:"in"`
Value types.String `tfsdk:"value"`
}
type oauth2Data struct {
Password types.Object `tfsdk:"password"`
ClientCredentials types.Object `tfsdk:"client_credentials"`
RefreshToken types.Object `tfsdk:"refresh_token"`
}
type oauth2PasswordData struct {
TokenUrl types.String `tfsdk:"token_url"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
ClientID types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
Scopes types.List `tfsdk:"scopes"`
In types.String `tfsdk:"in"`
}
type oauth2ClientCredentialsData struct {
TokenUrl types.String `tfsdk:"token_url"`
ClientID types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
EndpointParams types.Map `tfsdk:"endpoint_params"`
Scopes types.List `tfsdk:"scopes"`
In types.String `tfsdk:"in"`
}
type oauth2RefreshTokenData struct {
TokenUrl types.String `tfsdk:"token_url"`
RefreshToken types.String `tfsdk:"refresh_token"`
ClientID types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
Scopes types.List `tfsdk:"scopes"`
In types.String `tfsdk:"in"`
TokenType types.String `tfsdk:"token_type"`
}
func New() provider.Provider {
return &Provider{}
}
func (*Provider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "restful"
}
func (*Provider) DataSources(context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
func() datasource.DataSource {
return &DataSource{}
},
}
}
func (*Provider) Resources(context.Context) []func() resource.Resource {
return []func() resource.Resource{
func() resource.Resource {
return &Resource{}
},
func() resource.Resource {
return &OperationResource{}
},
}
}
func (*Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "The restful provider provides resource and data source to interact with a platform that exposes a restful API.",
MarkdownDescription: "The restful provider provides resource and data source to interact with a platform that exposes a restful API.",
Attributes: map[string]schema.Attribute{
"base_url": schema.StringAttribute{
Description: "The base URL of the API provider.",
MarkdownDescription: "The base URL of the API provider.",
Required: true,
Validators: []validator.String{
myvalidator.StringIsParsable("HTTP url", func(s string) error {
_, err := url.Parse(s)
return err
}),
},
},
"security": schema.SingleNestedAttribute{
Description: "The OpenAPI security scheme that is be used for auth. Only one of `http`, `apikey` and `oauth2` can be specified.",
MarkdownDescription: "The OpenAPI security scheme that is be used for auth. Only one of `http`, `apikey` and `oauth2` can be specified.",
Optional: true,
Attributes: map[string]schema.Attribute{
"http": schema.SingleNestedAttribute{
Description: "Configuration for the HTTP authentication scheme. Exactly one of `basic` and `token` must be specified.",
MarkdownDescription: "Configuration for the HTTP authentication scheme. Exactly one of `basic` and `token` must be specified.",
Optional: true,
Attributes: map[string]schema.Attribute{
"basic": schema.SingleNestedAttribute{
Description: "Basic authentication",
MarkdownDescription: "Basic authentication",
Optional: true,
Attributes: map[string]schema.Attribute{
"username": schema.StringAttribute{
Description: "The username",
MarkdownDescription: "The username",
Required: true,
},
"password": schema.StringAttribute{
Description: "The password",
MarkdownDescription: "The password",
Required: true,
Sensitive: true,
},
},
Validators: []validator.Object{
objectvalidator.ExactlyOneOf(
path.MatchRoot("security").AtName("http").AtName("basic"),
path.MatchRoot("security").AtName("http").AtName("token"),
),
},
},
"token": schema.SingleNestedAttribute{
Description: "Auth token (e.g. Bearer).",
MarkdownDescription: "Auth token (e.g. Bearer).",
Optional: true,
Attributes: map[string]schema.Attribute{
"token": schema.StringAttribute{
Description: "The value of the token.",
MarkdownDescription: "The value of the token.",
Required: true,
Sensitive: true,
},
"scheme": schema.StringAttribute{
Description: "The auth scheme. Defaults to `Bearer`.",
MarkdownDescription: "The auth scheme. Defaults to `Bearer`.",
Optional: true,
},
},
Validators: []validator.Object{
objectvalidator.ExactlyOneOf(
path.MatchRoot("security").AtName("http").AtName("basic"),
path.MatchRoot("security").AtName("http").AtName("token"),
),
},
},
},
Validators: []validator.Object{
objectvalidator.ConflictsWith(
path.MatchRoot("security").AtName("apikey"),
path.MatchRoot("security").AtName("oauth2"),
),
},
},
"apikey": schema.SetNestedAttribute{
Description: "Configuration for the API Key authentication scheme.",
MarkdownDescription: "Configuration for the API Key authentication scheme.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The API Key name",
MarkdownDescription: "The API Key name",
Required: true,
},
"value": schema.StringAttribute{
Description: "The API Key value",
MarkdownDescription: "The API Key value",
Required: true,
},
"in": schema.StringAttribute{
Description: fmt.Sprintf("Specifies how is the API Key is sent. Possible values are `%s`, `%s` and `%s`.",
client.APIKeyAuthInQuery, client.APIKeyAuthInHeader, client.APIKeyAuthInCookie),
MarkdownDescription: fmt.Sprintf("Specifies how is the API Key is sent. Possible values are `%s`, `%s` and `%s`.",
client.APIKeyAuthInQuery, client.APIKeyAuthInHeader, client.APIKeyAuthInCookie),
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(
string(client.APIKeyAuthInHeader),
string(client.APIKeyAuthInQuery),
string(client.APIKeyAuthInCookie),
),
},
},
},
},
Validators: []validator.Set{
setvalidator.ConflictsWith(
path.MatchRoot("security").AtName("http"),
path.MatchRoot("security").AtName("oauth2"),
),
},
},
"oauth2": schema.SingleNestedAttribute{
Description: "Configuration for the OAuth2 authentication scheme. Exactly one of `password`, `client_credentials` and `refresh_token` must be specified.",
MarkdownDescription: "Configuration for the OAuth2 authentication scheme. Exactly one of `password`, `client_credentials` and `refresh_token` must be specified.",
Optional: true,
Attributes: map[string]schema.Attribute{
"password": schema.SingleNestedAttribute{
Description: "Resource owner password credential.",
MarkdownDescription: "[Resource owner password credential](https://www.rfc-editor.org/rfc/rfc6749#section-4.3).",
Optional: true,
Attributes: map[string]schema.Attribute{
"token_url": schema.StringAttribute{
Description: "The token URL to be used for this flow.",
MarkdownDescription: "The token URL to be used for this flow.",
Required: true,
},
"username": schema.StringAttribute{
Description: "The username.",
MarkdownDescription: "The username.",
Required: true,
},
"password": schema.StringAttribute{
Sensitive: true,
Description: "The password.",
MarkdownDescription: "The password.",
Required: true,
},
"client_id": schema.StringAttribute{
Description: "The application's ID.",
MarkdownDescription: "The application's ID.",
Optional: true,
},
"client_secret": schema.StringAttribute{
Sensitive: true,
Description: "The application's secret.",
MarkdownDescription: "The application's secret.",
Optional: true,
},
"in": schema.StringAttribute{
Description: fmt.Sprintf("Specifies how is the client ID & secret sent. Possible values are `%s` and `%s`. If absent, the style used will be auto detected.",
client.OAuth2AuthStyleInParams, client.OAuth2AuthStyleInHeader),
MarkdownDescription: fmt.Sprintf("Specifies how is th client ID & secret sent. Possible values are `%s` and `%s`. If absent, the style used will be auto detected.",
client.OAuth2AuthStyleInParams, client.OAuth2AuthStyleInHeader),
Optional: true,
Validators: []validator.String{stringvalidator.OneOf(string(client.OAuth2AuthStyleInParams), string(client.OAuth2AuthStyleInHeader))},
},
"scopes": schema.ListAttribute{
ElementType: types.StringType,
Description: "The optional requested permissions.",
MarkdownDescription: "The optional requested permissions.",
Optional: true,
},
},
Validators: []validator.Object{
objectvalidator.ExactlyOneOf(
path.MatchRoot("security").AtName("oauth2").AtName("password"),
path.MatchRoot("security").AtName("oauth2").AtName("client_credentials"),
path.MatchRoot("security").AtName("oauth2").AtName("refresh_token"),
),
},
},
"client_credentials": schema.SingleNestedAttribute{
Description: "Client credentials.",
MarkdownDescription: "[Client credentials](https://www.rfc-editor.org/rfc/rfc6749#section-4.4).",
Optional: true,
Attributes: map[string]schema.Attribute{
"token_url": schema.StringAttribute{
Description: "The token URL to be used for this flow.",
MarkdownDescription: "The token URL to be used for this flow.",
Required: true,
},
"client_id": schema.StringAttribute{
Description: "The application's ID.",
MarkdownDescription: "The application's ID.",
Required: true,
},
"client_secret": schema.StringAttribute{
Sensitive: true,
Description: "The application's secret.",
MarkdownDescription: "The application's secret.",
Required: true,
},
"in": schema.StringAttribute{
Description: fmt.Sprintf("Specifies how is the client ID & secret sent. Possible values are `%s` and `%s`. If absent, the style used will be auto detected.",
client.OAuth2AuthStyleInParams, client.OAuth2AuthStyleInHeader),
MarkdownDescription: fmt.Sprintf("Specifies how is th client ID & secret sent. Possible values are `%s` and `%s`. If absent, the style used will be auto detected.",
client.OAuth2AuthStyleInParams, client.OAuth2AuthStyleInHeader),
Optional: true,
Validators: []validator.String{stringvalidator.OneOf(string(client.OAuth2AuthStyleInParams), string(client.OAuth2AuthStyleInHeader))},
},
"scopes": schema.ListAttribute{
ElementType: types.StringType,
Description: "The optional requested permissions.",
MarkdownDescription: "The optional requested permissions.",
Optional: true,
},
"endpoint_params": schema.MapAttribute{
ElementType: types.ListType{ElemType: types.StringType},
Description: "The additional parameters for requests to the token endpoint.",
MarkdownDescription: "The additional parameters for requests to the token endpoint.",
Optional: true,
},
},
Validators: []validator.Object{
objectvalidator.ExactlyOneOf(
path.MatchRoot("security").AtName("oauth2").AtName("password"),
path.MatchRoot("security").AtName("oauth2").AtName("client_credentials"),
path.MatchRoot("security").AtName("oauth2").AtName("refresh_token"),
),
},
},
"refresh_token": schema.SingleNestedAttribute{
Description: "Refresh token.",
MarkdownDescription: "[Refresh token](https://www.rfc-editor.org/rfc/rfc6749#section-6).",
Optional: true,
Attributes: map[string]schema.Attribute{
"token_url": schema.StringAttribute{
Description: "The token URL to be used for this flow.",
MarkdownDescription: "The token URL to be used for this flow.",
Required: true,
},
"refresh_token": schema.StringAttribute{
Description: "The refresh token.",
MarkdownDescription: "The refresh token.",
Sensitive: true,
Required: true,
},
"client_id": schema.StringAttribute{
Description: "The application's ID.",
MarkdownDescription: "The application's ID.",
Optional: true,
},
"client_secret": schema.StringAttribute{
Sensitive: true,
Description: "The application's secret.",
MarkdownDescription: "The application's secret.",
Optional: true,
},
"scopes": schema.ListAttribute{
ElementType: types.StringType,
Description: "The optional requested permissions.",
MarkdownDescription: "The optional requested permissions.",
Optional: true,
},
"in": schema.StringAttribute{
Description: fmt.Sprintf("Specifies how is the client ID & secret sent. Possible values are `%s` and `%s`. If absent, the style used will be auto detected.",
client.OAuth2AuthStyleInParams, client.OAuth2AuthStyleInHeader),
MarkdownDescription: fmt.Sprintf("Specifies how is th client ID & secret sent. Possible values are `%s` and `%s`. If absent, the style used will be auto detected.",
client.OAuth2AuthStyleInParams, client.OAuth2AuthStyleInHeader),
Optional: true,
Validators: []validator.String{stringvalidator.OneOf(string(client.OAuth2AuthStyleInParams), string(client.OAuth2AuthStyleInHeader))},
},
"token_type": schema.StringAttribute{
Description: `The type of the access token. Defaults to "Bearer".`,
MarkdownDescription: `The type of the access token. Defaults to "Bearer".`,
Optional: true,
},
},
Validators: []validator.Object{
objectvalidator.ExactlyOneOf(
path.MatchRoot("security").AtName("oauth2").AtName("password"),
path.MatchRoot("security").AtName("oauth2").AtName("client_credentials"),
path.MatchRoot("security").AtName("oauth2").AtName("refresh_token"),
),
},
},
},
Validators: []validator.Object{
objectvalidator.ConflictsWith(
path.MatchRoot("security").AtName("http"),
path.MatchRoot("security").AtName("apikey"),
),
},
},
},
},
"create_method": schema.StringAttribute{
Description: "The method used to create the resource. Possible values are `PUT` and `POST`. Defaults to `POST`.",
MarkdownDescription: "The method used to create the resource. Possible values are `PUT` and `POST`. Defaults to `POST`.",
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("PUT", "POST"),
},
},
"update_method": schema.StringAttribute{
Description: "The method used to update the resource. Possible values are `PUT` and `PATCH`. Defaults to `PUT`.",
MarkdownDescription: "The method used to update the resource. Possible values are `PUT` and `PATCH`. Defaults to `PUT`.",
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("PUT", "PATCH"),
},
},
"delete_method": schema.StringAttribute{
Description: "The method used to delete the resource. Possible values are `DELETE` and `POST`. Defaults to `DELETE`.",
MarkdownDescription: "The method used to delete the resource. Possible values are `DELETE` and `POST`. Defaults to `DELETE`.",
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("DELETE", "POST"),
},
},
"merge_patch_disabled": schema.BoolAttribute{
Description: "Whether to use a JSON Merge Patch as the request body in the PATCH update? Defaults to `false`. This is only effective when `update_method` is set to `PATCH`.",
MarkdownDescription: "Whether to use a JSON Merge Patch as the request body in the PATCH update? Defaults to `false`. This is only effective when `update_method` is set to `PATCH`.",
Optional: true,
},
"query": schema.MapAttribute{
Description: "The query parameters that are applied to each request.",
MarkdownDescription: "The query parameters that are applied to each request.",
ElementType: types.ListType{ElemType: types.StringType},
Optional: true,
},
"header": schema.MapAttribute{
Description: "The header parameters that are applied to each request.",
MarkdownDescription: "The header parameters that are applied to each request.",
ElementType: types.StringType,
Optional: true,
},
"cookie_enabled": schema.BoolAttribute{
Description: "Save cookies during API contracting. Defaults to `false`.",
MarkdownDescription: "Save cookies during API contracting. Defaults to `false`.",
Optional: true,
},
"tls_insecure_skip_verify": schema.BoolAttribute{
Description: "Whether a client verifies the server's certificate chain and host name. Defaults to `false`.",
MarkdownDescription: "Whether a client verifies the server's certificate chain and host name. Defaults to `false`.",
Optional: true,
},
},
}
}
func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
data := providerData{
provider: &Provider{},
}
diags := req.Config.Get(ctx, &data.config)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
resp.ResourceData = data
resp.DataSourceData = data
return
}
func (p *Provider) Init(ctx context.Context, config providerConfig) diag.Diagnostics {
var odiags diag.Diagnostics
p.once.Do(func() {
clientOpt := client.BuildOption{
CookieEnabled: config.CookieEnabled.ValueBool(),
}
clientOpt.TLSConfig.InsecureSkipVerify = config.TlsInsecureSkipVerify.ValueBool()
if secRaw := config.Security; !secRaw.IsNull() {
var sec securityData
if diags := secRaw.As(ctx, &sec, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
switch {
case !sec.HTTP.IsNull():
var http httpData
if diags := sec.HTTP.As(ctx, &http, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
switch {
case !http.Basic.IsNull():
var basic httpBasicData
if diags := http.Basic.As(ctx, &basic, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
opt := client.HTTPBasicOption{
Username: basic.Username.ValueString(),
Password: basic.Password.ValueString(),
}
clientOpt.Security = opt
case !http.Token.IsNull():
var token httpTokenData
if diags := http.Token.As(ctx, &token, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
opt := client.HTTPTokenOption{
Token: token.Token.ValueString(),
Scheme: token.Scheme.ValueString(),
}
clientOpt.Security = opt
}
case !sec.APIKey.IsNull():
opt := client.APIKeyAuthOption{}
for _, apikeyRaw := range sec.APIKey.Elements() {
apikeyObj := apikeyRaw.(types.Object)
if apikeyObj.IsNull() {
continue
}
var apikey apikeyData
if diags := apikeyObj.As(ctx, &apikey, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
opt = append(opt, client.APIKeyAuthOpt{
Name: apikey.Name.ValueString(),
In: client.APIKeyAuthIn(apikey.In.ValueString()),
Value: apikey.Value.ValueString(),
})
}
clientOpt.Security = opt
case !sec.OAuth2.IsNull():
var oauth2 oauth2Data
if diags := sec.OAuth2.As(ctx, &oauth2, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
switch {
case !oauth2.Password.IsNull():
var password oauth2PasswordData
if diags := oauth2.Password.As(ctx, &password, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
opt := client.OAuth2PasswordOption{
TokenURL: password.TokenUrl.ValueString(),
Username: password.Username.ValueString(),
Password: password.Password.ValueString(),
ClientId: password.ClientID.ValueString(),
ClientSecret: password.ClientSecret.ValueString(),
AuthStyle: client.OAuth2AuthStyle(password.In.ValueString()),
}
if !password.Scopes.IsNull() {
var scopes []string
for _, scope := range password.Scopes.Elements() {
scope := scope.(types.String)
if scope.IsNull() {
continue
}
scopes = append(scopes, scope.ValueString())
}
opt.Scopes = scopes
}
clientOpt.Security = opt
case !oauth2.ClientCredentials.IsNull():
var cc oauth2ClientCredentialsData
if diags := oauth2.ClientCredentials.As(ctx, &cc, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
opt := client.OAuth2ClientCredentialOption{
TokenURL: cc.TokenUrl.ValueString(),
ClientId: cc.ClientID.ValueString(),
ClientSecret: cc.ClientSecret.ValueString(),
AuthStyle: client.OAuth2AuthStyle(cc.In.ValueString()),
}
if !cc.Scopes.IsNull() {
var scopes []string
for _, scope := range cc.Scopes.Elements() {
scope := scope.(types.String)
if scope.IsNull() {
continue
}
scopes = append(scopes, scope.ValueString())
}
opt.Scopes = scopes
}
if !cc.EndpointParams.IsNull() {
endpointParams := map[string][]string{}
for k, values := range cc.EndpointParams.Elements() {
var vs []string
values := values.(types.List)
for _, value := range values.Elements() {
value := value.(types.String)
if value.IsNull() {
continue
}
vs = append(vs, value.ValueString())
}
endpointParams[k] = vs
}
opt.EndpointParams = endpointParams
}
clientOpt.Security = opt
case !oauth2.RefreshToken.IsNull():
var refreshToken oauth2RefreshTokenData
if diags := oauth2.RefreshToken.As(ctx, &refreshToken, basetypes.ObjectAsOptions{}); diags.HasError() {
odiags = diags
return
}
opt := client.OAuth2RefreshTokenOption{
TokenURL: refreshToken.TokenUrl.ValueString(),
RefreshToken: refreshToken.RefreshToken.ValueString(),
ClientId: refreshToken.ClientID.ValueString(),
ClientSecret: refreshToken.ClientSecret.ValueString(),
AuthStyle: client.OAuth2AuthStyle(refreshToken.In.ValueString()),
TokenType: refreshToken.TokenType.ValueString(),
}
if !refreshToken.Scopes.IsNull() {
var scopes []string
for _, scope := range refreshToken.Scopes.Elements() {
scope := scope.(types.String)
if scope.IsNull() {
continue
}
scopes = append(scopes, scope.ValueString())
}
opt.Scopes = scopes
}
clientOpt.Security = opt
}
}
}
var (
diags diag.Diagnostics
err error
)
p.client, err = client.New(ctx, config.BaseURL.ValueString(), &clientOpt)
if err != nil {
diags.AddError(
"Failed to configure provider",
fmt.Sprintf("Failed to new client: %v", err),
)
odiags = diags
return
}
uRL, err := url.Parse(config.BaseURL.ValueString())
if err != nil {
diags.AddError(
"Failed to configure provider",
fmt.Sprintf("Parsing the base url %q: %v", config.BaseURL, err),
)
odiags = diags
return
}
p.apiOpt = apiOption{
BaseURL: *uRL,
CreateMethod: "POST",
UpdateMethod: "PUT",
DeleteMethod: "DELETE",
MergePatchDisabled: false,
Query: map[string][]string{},
Header: map[string]string{},
}
if !config.CreateMethod.IsNull() {
p.apiOpt.CreateMethod = config.CreateMethod.ValueString()
}
if !config.UpdateMethod.IsNull() {
p.apiOpt.UpdateMethod = config.UpdateMethod.ValueString()
}
if !config.DeleteMethod.IsNull() {
p.apiOpt.DeleteMethod = config.DeleteMethod.ValueString()
}
if !config.MergePatchDisabled.IsNull() {
p.apiOpt.MergePatchDisabled = config.MergePatchDisabled.ValueBool()
}
if !config.Query.IsNull() {
queries := map[string][]string{}
for k, values := range config.Query.Elements() {
var vs []string
values := values.(types.List)
for _, value := range values.Elements() {
value := value.(types.String)
if value.IsNull() {
continue
}
vs = append(vs, value.ValueString())
}
queries[k] = vs
}
p.apiOpt.Query = queries
}
if !config.Header.IsNull() {
headers := map[string]string{}
for k, value := range config.Header.Elements() {
value := value.(types.String)
if value.IsNull() {
continue
}
headers[k] = value.ValueString()
}
p.apiOpt.Header = headers
}
})
return odiags
}