/
resource_application.go
863 lines (797 loc) · 29.2 KB
/
resource_application.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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
package davinci
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/pingidentity/terraform-provider-davinci/internal/framework"
"github.com/pingidentity/terraform-provider-davinci/internal/sdk"
"github.com/pingidentity/terraform-provider-davinci/internal/verify"
dv "github.com/samir-gandhi/davinci-client-go/davinci"
)
func ResourceApplication() *schema.Resource {
return &schema.Resource{
CreateContext: resourceApplicationCreate,
ReadContext: resourceApplicationRead,
UpdateContext: resourceApplicationUpdate,
DeleteContext: resourceApplicationDelete,
Schema: map[string]*schema.Schema{
"environment_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the PingOne environment to create the DaVinci application. Must be a valid PingOne resource ID. This field is immutable and will trigger a replace plan if changed.",
ValidateDiagFunc: validation.ToDiagFunc(verify.ValidP1ResourceID),
ForceNew: true,
},
"customer_id": {
Type: schema.TypeString,
Computed: true,
Description: "An ID that represents the customer tenant.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The application name.",
},
"created_date": {
Type: schema.TypeInt,
Computed: true,
Description: "Resource creation date as epoch.",
},
"api_key_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "A boolean that specifies whether the API key is enabled for the application.",
},
"api_keys": {
Type: schema.TypeMap,
Computed: true,
Description: "A map of strings that represents the application's API Key.",
Elem: &schema.Schema{
Type: schema.TypeString,
Sensitive: true,
},
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Description: "Application Metadata.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"user_pools": {
Type: schema.TypeMap,
Computed: true,
Description: "Application User Pools.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"user_portal": {
Type: schema.TypeList,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release. A single object that describes user portal settings.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"up_title": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"add_auth_method_title": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"remove_auth_method_title": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"cred_page_title": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"cred_page_subtitle": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"name_auth_method_title": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"name_confirm_btn_text": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"update_message": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"update_body_message": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"remove_message": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"remove_body_message": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"remove_confirm_btn_text": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"remove_cancel_btn_text": {
Type: schema.TypeString,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"flow_timeout_seconds": {
Type: schema.TypeInt,
Optional: true,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"show_user_info": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"show_mfa_button": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"show_variables": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"show_logout_button": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "**Deprecation notice** This is now deprecated in the service and will be removed from the provider in the next major release.",
Deprecated: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
},
},
},
"oauth": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "A single list item specifying OIDC/OAuth 2.0 configuration.",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "A boolean that specifies whether OIDC/OAuth 2.0 settings are enabled for the application.",
},
"values": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "A single list item specifying OIDC/OAuth 2.0 configuration values.",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "A boolean that enables/disables the OAuth 2.0 configuration for the application.",
},
"client_secret": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "The client secret for the OIDC application.",
},
"enforce_signed_request_openid": {
Type: schema.TypeBool,
Optional: true,
Description: "A boolean that specifies whether to enforce receiving signed requests.",
},
"sp_jwks_url": {
Type: schema.TypeString,
Optional: true,
Description: "A string that specifies a service provider (SP) JWKS URL.",
},
"sp_jwks_openid": {
Type: schema.TypeString,
Optional: true,
Description: "A string that specifies service provider (SP) JWKS keys to verify the authorization request signature.",
},
"redirect_uris": {
Type: schema.TypeSet,
Optional: true,
Description: "Redirect URLs for the application.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"logout_uris": {
Type: schema.TypeSet,
Optional: true,
Description: "Logout URLs for the application.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"allowed_scopes": {
Type: schema.TypeSet,
Optional: true,
Description: "Allowed scopes for the application. Available scopes are `openid`, `profile`, `flow_analytics`.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"openid", "profile", "flow_analytics"}, false)),
},
},
"allowed_grants": {
Type: schema.TypeSet,
Optional: true,
Description: "Allowed grants for the application. Available grants are `authorizationCode`, `clientCredentials`, `implicit`.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"authorizationCode", "clientCredentials", "implicit"}, false)),
},
},
},
},
},
},
},
},
"saml": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "**Deprecation notice**: SAML configuration is now deprecated in the service and will be removed in the next major release. A single list item that specifies SAML configuration.",
Deprecated: "SAML configuration is now deprecated in the service and will be removed in the next major release.",
MaxItems: 1,
// DefaultFunc: func() (interface{}, error) {
// smap := []map[string]interface{}{{
// "values": []map[string]interface{}{{
// "enabled": true,
// "enforce_signed_request": false,
// }},
// }}
// return smap, nil
// },
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"values": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "SAML configuration",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Set to true if using saml block. This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"redirect_uri": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "The redirect URI for the SAML application. This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"enforce_signed_request": {
Type: schema.TypeBool,
Optional: true,
Description: "Field: 'Enforce Receiving Signed Requests' in UI. This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"audience": {
Type: schema.TypeString,
Optional: true,
Description: "Field: 'Audience' in UI. This is now deprecated in the service and will be removed from the provider in the next major release.",
},
"sp_cert": {
Type: schema.TypeString,
Optional: true,
Description: "This is now deprecated in the service and will be removed from the provider in the next major release.",
},
},
},
},
},
},
},
"policy": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "**Deprecation Notice** The `policy` block should be removed from application and managed as a `davinci_application_flow_policy` resource. Review the migration guidance here: https://registry.terraform.io/providers/pingidentity/davinci/latest/docs/guides/migrate-application-flow-policy. Flow Policy Configuration",
Deprecated: "The `policy` block should be removed from application and managed as a `davinci_application_flow_policy` resource. Review the migration guidance here: https://registry.terraform.io/providers/pingidentity/davinci/latest/docs/guides/migrate-application-flow-policy. This attribute will be removed in the next major release.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"policy_flow": {
Type: schema.TypeSet,
Optional: true,
Description: "Set of weighted flows that this application will use",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flow_id": {
Type: schema.TypeString,
Optional: true,
Description: "Identifier of the flow that this policy will use.",
},
"version_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Version of the flow that this policy will use. Use -1 for latest",
},
"weight": {
Type: schema.TypeInt,
Optional: true,
Description: "If multiple flows are specified, the weight determines the probability of the flow being used. This must add up to 100",
},
"success_nodes": {
Type: schema.TypeList,
Optional: true,
Description: "List of node ids used by analytics for tracking user interaction.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Policy friendly name",
},
"status": {
Type: schema.TypeString,
Optional: true,
Default: "enabled",
Description: "Policy status. Valid values are: enabled, disabled",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"enabled", "disabled"}, false)),
},
"policy_id": {
Type: schema.TypeString,
Computed: true,
Description: "Generated identifier of a created policy.",
},
"created_date": {
Type: schema.TypeInt,
Computed: true,
Description: "Creation epoch of policy.",
},
},
},
},
},
Importer: &schema.ResourceImporter{
StateContext: resourceApplicationImport,
},
}
}
func resourceApplicationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*dv.APIClient)
var diags diag.Diagnostics
app, err := expandApp(d)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
environmentID := d.Get("environment_id").(string)
sdkRes, err := sdk.DoRetryable(
ctx,
c,
environmentID,
func() (interface{}, *http.Response, error) {
return c.CreateInitializedApplicationWithResponse(environmentID, app)
},
)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("Unable to parse created app response from Davinci API")
diags = append(diags, diag.FromErr(err)...)
return diags
}
d.SetId(*res.AppID)
resourceApplicationRead(ctx, d, meta)
return diags
}
func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*dv.APIClient)
var diags diag.Diagnostics
appId := d.Id()
environmentID := d.Get("environment_id").(string)
skdRes, err := sdk.DoRetryable(
ctx,
c,
environmentID,
func() (interface{}, *http.Response, error) {
return c.ReadApplicationWithResponse(environmentID, appId)
},
)
if err != nil {
if dvError, ok := err.(dv.ErrorResponse); ok {
if dvError.HttpResponseCode == http.StatusNotFound || dvError.Code == dv.DV_ERROR_CODE_APPLICATION_NOT_FOUND {
d.SetId("")
return diags
}
}
diags = append(diags, diag.FromErr(err)...)
return diags
}
resp, ok := skdRes.(*dv.App)
if !ok {
err = fmt.Errorf("failed to cast App type to response on Application with id: %s", appId)
diags = append(diags, diag.FromErr(err)...)
return diags
}
flatResp, err := flattenApp(resp)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
for i, v := range flatResp {
if err = d.Set(i, v); err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
}
d.SetId(*resp.AppID)
return diags
}
func resourceApplicationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*dv.APIClient)
appId := d.Id()
environmentID := d.Get("environment_id").(string)
// Policy CRUD
// if policies have changes, compare changes. if new policy is added, create it. if old policy is removed, delete it. if policy is updated, update it.
if d.HasChange("policy") {
oldPols, newPols := d.GetChange("policy")
oldPolsList := expandFlowPolicies(oldPols)
newPolsList := expandFlowPolicies(newPols)
oldPolicyMap := make(map[string]dv.Policy)
for _, v := range oldPolsList {
oldPolicyMap[*v.PolicyID] = v
}
for _, newPol := range newPolsList {
oldPol, exists := oldPolicyMap[*newPol.PolicyID]
if exists {
//update policy
sdkRes, err := sdk.DoRetryable(
ctx,
c,
environmentID,
func() (interface{}, *http.Response, error) {
return c.UpdateFlowPolicyWithResponse(environmentID, appId, newPol)
},
)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("failed to cast update policy response to Application on id: %s", appId)
diags = append(diags, diag.FromErr(err)...)
return diags
}
delete(oldPolicyMap, *oldPol.PolicyID)
} else {
// create policy
sdkRes, err := sdk.DoRetryable(
ctx,
c,
environmentID,
func() (interface{}, *http.Response, error) {
return c.CreateFlowPolicyWithResponse(environmentID, appId, newPol)
},
)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("failed to cast create policy response to Application on id: %s", appId)
diags = append(diags, diag.FromErr(err)...)
return diags
}
delete(oldPolicyMap, *oldPol.PolicyID)
}
}
//delete old policies that are not in new policies
for _, oldPol := range oldPolicyMap {
_, err := sdk.DoRetryable(
ctx,
c,
environmentID,
func() (interface{}, *http.Response, error) {
return c.DeleteFlowPolicyWithResponse(environmentID, appId, *oldPol.PolicyID)
},
)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
}
}
if d.HasChangesExcept("policy") {
app, err := expandApp(d)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
appId := d.Id()
app.AppID = &appId
sdkRes, err := sdk.DoRetryable(
ctx,
c,
environmentID,
func() (interface{}, *http.Response, error) {
return c.UpdateApplicationWithResponse(environmentID, app)
},
)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
return diags
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("failed to cast update application response to Application on id: %v", *app.AppID)
diags = append(diags, diag.FromErr(err)...)
return diags
}
}
return resourceApplicationRead(ctx, d, meta)
}
func resourceApplicationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*dv.APIClient)
var diags diag.Diagnostics
appId := d.Id()
environmentID := d.Get("environment_id").(string)
sdkRes, err := sdk.DoRetryable(
ctx,
c,
environmentID,
func() (interface{}, *http.Response, error) {
return c.DeleteApplicationWithResponse(environmentID, appId)
},
)
if err != nil {
if dvError, ok := err.(dv.ErrorResponse); ok {
if dvError.HttpResponseCode == http.StatusNotFound || dvError.Code == dv.DV_ERROR_CODE_APPLICATION_NOT_FOUND {
return diags
}
}
diags = append(diags, diag.FromErr(err)...)
return diags
}
res, ok := sdkRes.(*dv.Message)
if !ok || res.Message == nil || *res.Message == "" {
err = fmt.Errorf("Error when deleting application with id %s, no response message found.", appId)
diags = append(diags, diag.FromErr(err)...)
return diags
}
d.SetId("")
return diags
}
func resourceApplicationImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idComponents := []framework.ImportComponent{
{
Label: "environment_id",
Regexp: verify.P1ResourceIDRegexp,
},
{
Label: "davinci_application_id",
Regexp: verify.P1DVResourceIDRegexp,
PrimaryID: true,
},
}
attributes, err := framework.ParseImportID(d.Id(), idComponents...)
if err != nil {
return nil, err
}
if err = d.Set("environment_id", attributes["environment_id"]); err != nil {
return nil, err
}
d.SetId(attributes["davinci_application_id"])
resourceApplicationRead(ctx, d, meta)
return []*schema.ResourceData{d}, nil
}
func expandApp(d *schema.ResourceData) (*dv.AppUpdate, error) {
// Set Top layer.
a := dv.AppUpdate{
Name: d.Get("name").(string),
APIKeyEnabled: d.Get("api_key_enabled").(bool),
}
// Set OAuth
oa, ok := d.GetOk("oauth")
if ok {
oal := oa.([]interface{})
oaUpdate := &dv.Oauth{}
//Set OAuthEnabled
oam := oal[0].(map[string]interface{})
if oalmEnabled, ok := oam["enabled"].(bool); ok {
oaUpdate.Enabled = oalmEnabled
}
// Set OAuth Values
oamValues := oam["values"].([]interface{})
if len(oamValues) == 1 {
oamValuesMap := oamValues[0].(map[string]interface{})
oaUpdate.Values = &dv.OauthValues{
Enabled: oamValuesMap["enabled"].(bool),
}
if v, ok := oamValuesMap["client_secret"].(string); ok {
oaUpdate.Values.ClientSecret = &v
}
if v, ok := oamValuesMap["enforce_signed_request_openid"].(bool); ok {
oaUpdate.Values.EnforceSignedRequestOpenid = &v
}
if v, ok := oamValuesMap["sp_jwks_url"].(string); ok {
oaUpdate.Values.SpjwksUrl = &v
}
if v, ok := oamValuesMap["sp_jwks_openid"].(string); ok {
oaUpdate.Values.SpJwksOpenid = &v
}
slist := expandStringList(oamValuesMap["redirect_uris"].(*schema.Set).List())
oaUpdate.Values.RedirectUris = slist
slist = expandStringList(oamValuesMap["logout_uris"].(*schema.Set).List())
oaUpdate.Values.LogoutUris = slist
slist = expandStringList(oamValuesMap["allowed_scopes"].(*schema.Set).List())
oaUpdate.Values.AllowedScopes = slist
slist = expandStringList(oamValuesMap["allowed_grants"].(*schema.Set).List())
oaUpdate.Values.AllowedGrants = slist
}
if len(oamValues) > 1 {
return nil, fmt.Errorf("Only one set for OAuth Values allowed")
}
a.Oauth = oaUpdate
}
//Set Saml
saml, ok := d.GetOk("saml")
if ok {
sl := saml.([]interface{})
if len(sl) == 1 {
svUpdate := &dv.Saml{}
sm := sl[0].(map[string]interface{})
samlValues := sm["values"].([]interface{})
if len(samlValues) == 1 {
svMap := samlValues[0].(map[string]interface{})
svUpdate.Values = &dv.SamlValues{
Enabled: svMap["enabled"].(bool),
}
if v, ok := svMap["enforce_signed_request"].(bool); ok {
svUpdate.Values.EnforceSignedRequest = &v
}
if v, ok := svMap["redirect_uri"].(string); ok {
svUpdate.Values.RedirectURI = &v
}
if v, ok := svMap["audience"].(string); ok {
svUpdate.Values.Audience = &v
}
if v, ok := svMap["sp_cert"].(string); ok {
svUpdate.Values.SpCert = &v
}
a.Saml = svUpdate
}
if len(samlValues) > 1 {
return nil, fmt.Errorf("Only one set for Saml Values allowed")
}
}
if len(sl) > 1 {
return nil, fmt.Errorf("Only one set for Saml Values allowed")
}
}
//Set User Portal
uv, ok := d.GetOk("user_portal")
if ok {
upValues := uv.([]interface{})
if len(upValues) == 1 {
up := &dv.UserPortal{}
up.Values = upValues[0].(*dv.UserPortalValues)
a.UserPortal = up
}
if len(upValues) > 1 {
return nil, fmt.Errorf("Only one set for User Portal Values allowed")
}
}
//Set Flow Policies
fp, ok := d.GetOk("policy")
if ok {
fvUpdate := expandFlowPolicies(fp)
if len(fvUpdate) > 0 {
a.Policies = fvUpdate
}
}
return &a, nil
}
func expandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, v.(string))
}
}
return vs
}
func expandFlowPolicies(fp interface{}) []dv.Policy {
policyList := fp.(*schema.Set).List()
fvUpdate := []dv.Policy{}
if len(policyList) > 0 {
for _, policy := range policyList {
flMap := policy.(map[string]interface{})
thisFvUpdate := dv.Policy{}
if v, ok := flMap["name"].(string); ok {
thisFvUpdate.Name = &v
}
if v, ok := flMap["status"].(string); ok {
thisFvUpdate.Status = &v
}
if v, ok := flMap["policy_id"].(string); ok {
thisFvUpdate.PolicyID = &v
}
thisPolicyFlows := flMap["policy_flow"].(*schema.Set).List()
for _, w := range thisPolicyFlows {
flPMap := w.(map[string]interface{})
thisFvPUpdate := dv.PolicyFlow{
FlowID: flPMap["flow_id"].(string),
VersionID: flPMap["version_id"].(int),
}
if v, ok := flPMap["weight"].(int); ok {
thisFvPUpdate.Weight = &v
}
thisFvUpdate.PolicyFlows = append(thisFvUpdate.PolicyFlows, thisFvPUpdate)
}
fvUpdate = append(fvUpdate, thisFvUpdate)
}
}
return fvUpdate
}