-
Notifications
You must be signed in to change notification settings - Fork 670
/
resource_ibm_appid_application.go
213 lines (173 loc) · 5.63 KB
/
resource_ibm_appid_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
package appid
import (
"context"
"fmt"
"log"
"strings"
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceIBMAppIDApplication() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMAppIDApplicationCreate,
ReadContext: resourceIBMAppIDApplicationRead,
DeleteContext: resourceIBMAppIDApplicationDelete,
UpdateContext: resourceIBMAppIDApplicationUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"tenant_id": {
Description: "The service `tenantId`",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"client_id": {
Description: "The `client_id` is a public identifier for applications",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "The application name to be registered. Application name cannot exceed 50 characters.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},
"type": {
Description: "The type of application to be registered. Allowed types are `regularwebapp` and `singlepageapp`, default is `regularwebapp`.",
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: "regularwebapp",
ValidateFunc: validation.StringInSlice([]string{"regularwebapp", "singlepageapp"}, false),
},
"secret": {
Description: "The `secret` is a secret known only to the application and the authorization server",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"oauth_server_url": {
Description: "Base URL for common OAuth endpoints, like `/authorization`, `/token` and `/publickeys`",
Type: schema.TypeString,
Computed: true,
},
"profiles_url": {
Type: schema.TypeString,
Computed: true,
},
"discovery_endpoint": {
Description: "This URL returns OAuth Authorization Server Metadata",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceIBMAppIDApplicationCreate(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)
appName := d.Get("name").(string)
appType := d.Get("type").(string)
input := &appid.RegisterApplicationOptions{
TenantID: &tenantID,
Name: &appName,
Type: &appType,
}
app, resp, err := appIDClient.RegisterApplicationWithContext(ctx, input)
if err != nil {
return diag.Errorf("Error creating AppID application: %s\n%s", err, resp)
}
d.SetId(fmt.Sprintf("%s/%s", tenantID, *app.ClientID))
return resourceIBMAppIDApplicationRead(ctx, d, meta)
}
func resourceIBMAppIDApplicationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
id := d.Id()
idParts := strings.Split(id, "/")
if len(idParts) < 2 {
return diag.Errorf("Incorrect ID %s: ID should be a combination of tenantID/clientID", d.Id())
}
tenantID := idParts[0]
clientID := idParts[1]
app, resp, err := appIDClient.GetApplicationWithContext(ctx, &appid.GetApplicationOptions{
TenantID: &tenantID,
ClientID: &clientID,
})
if err != nil {
if resp != nil && resp.StatusCode == 404 {
log.Printf("[WARN] AppID application '%s' is not found, removing from state", clientID)
d.SetId("")
return nil
}
return diag.Errorf("Error getting AppID application: %s\n%s", err, resp)
}
if app.Name != nil {
d.Set("name", *app.Name)
}
if app.Secret != nil {
d.Set("secret", *app.Secret)
}
if app.OAuthServerURL != nil {
d.Set("oauth_server_url", *app.OAuthServerURL)
}
if app.ProfilesURL != nil {
d.Set("profiles_url", *app.ProfilesURL)
}
if app.DiscoveryEndpoint != nil {
d.Set("discovery_endpoint", *app.DiscoveryEndpoint)
}
if app.Type != nil {
d.Set("type", *app.Type)
}
d.Set("tenant_id", tenantID)
d.Set("client_id", clientID)
return nil
}
func resourceIBMAppIDApplicationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChange("name") {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
tenantID := d.Get("tenant_id").(string)
appName := d.Get("name").(string)
clientID := d.Get("client_id").(string)
_, resp, err := appIDClient.UpdateApplicationWithContext(ctx, &appid.UpdateApplicationOptions{
TenantID: &tenantID,
Name: &appName,
ClientID: &clientID,
})
if err != nil {
return diag.Errorf("Error updating AppID application: %s\n%s", err, resp)
}
}
return resourceIBMAppIDApplicationRead(ctx, d, meta)
}
func resourceIBMAppIDApplicationDelete(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)
clientID := d.Get("client_id").(string)
resp, err := appIDClient.DeleteApplicationWithContext(ctx, &appid.DeleteApplicationOptions{
TenantID: &tenantID,
ClientID: &clientID,
})
if err != nil {
return diag.Errorf("Error deleting AppID application: %s\n%s", err, resp)
}
d.SetId("")
return nil
}