-
Notifications
You must be signed in to change notification settings - Fork 662
/
resource_ibm_appid_idp_custom.go
144 lines (112 loc) · 3.81 KB
/
resource_ibm_appid_idp_custom.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
package appid
import (
"context"
"log"
"github.com/IBM-Cloud/bluemix-go/helpers"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
appid "github.com/IBM/appid-management-go-sdk/appidmanagementv4"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceIBMAppIDIDPCustom() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMAppIDIDPCustomCreate,
ReadContext: resourceIBMAppIDIDPCustomRead,
DeleteContext: resourceIBMAppIDIDPCustomDelete,
UpdateContext: resourceIBMAppIDIDPCustomUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"tenant_id": {
Description: "The service `tenantId`",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"is_active": {
Type: schema.TypeBool,
Required: true,
},
"public_key": {
Description: "This is the public key used to validate your signed JWT. It is required to be a PEM in the RS256 or greater format.",
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceIBMAppIDIDPCustomRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
tenantID := d.Id()
config, resp, err := appIDClient.GetCustomIDPWithContext(ctx, &appid.GetCustomIDPOptions{
TenantID: &tenantID,
})
if err != nil {
if resp != nil && resp.StatusCode == 404 {
log.Printf("[WARN] AppID instance '%s' is not found, removing AppID custom IDP configuration from state", tenantID)
d.SetId("")
return nil
}
return diag.Errorf("Error loading AppID custom IDP: %s\n%s", err, resp)
}
d.Set("is_active", *config.IsActive)
if config.Config != nil && config.Config.PublicKey != nil {
if err := d.Set("public_key", *config.Config.PublicKey); err != nil {
return diag.Errorf("Failed setting AppID custom IDP public_key: %s", err)
}
}
d.Set("tenant_id", tenantID)
return nil
}
func resourceIBMAppIDIDPCustomCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
tenantID := d.Get("tenant_id").(string)
isActive := d.Get("is_active").(bool)
config := &appid.SetCustomIDPOptions{
TenantID: &tenantID,
IsActive: &isActive,
}
if isActive {
config.Config = &appid.CustomIDPConfigParamsConfig{}
if pKey, ok := d.GetOk("public_key"); ok {
config.Config.PublicKey = helpers.String(pKey.(string))
}
}
_, resp, err := appIDClient.SetCustomIDPWithContext(ctx, config)
if err != nil {
return diag.Errorf("Error applying AppID custom IDP configuration: %s\n%s", err, resp)
}
d.SetId(tenantID)
return resourceIBMAppIDIDPCustomRead(ctx, d, meta)
}
func appIDCustomIDPDefaults(tenantID string) *appid.SetCustomIDPOptions {
return &appid.SetCustomIDPOptions{
TenantID: &tenantID,
IsActive: helpers.Bool(false),
}
}
func resourceIBMAppIDIDPCustomDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
tenantID := d.Get("tenant_id").(string)
config := appIDCustomIDPDefaults(tenantID)
_, resp, err := appIDClient.SetCustomIDPWithContext(ctx, config)
if err != nil {
return diag.Errorf("Error resetting AppID custom IDP configuration: %s\n%s", err, resp)
}
d.SetId("")
return nil
}
func resourceIBMAppIDIDPCustomUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// since this is configuration we can reuse create method
return resourceIBMAppIDIDPCustomCreate(ctx, d, m)
}