-
Notifications
You must be signed in to change notification settings - Fork 645
/
resource_ibm_appid_application_roles.go
185 lines (146 loc) · 4.82 KB
/
resource_ibm_appid_application_roles.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
package appid
import (
"context"
"fmt"
"log"
"strings"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
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 ResourceIBMAppIDApplicationRoles() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMAppIDApplicationRolesCreate,
ReadContext: resourceIBMAppIDApplicationRolesRead,
DeleteContext: resourceIBMAppIDApplicationRolesDelete,
UpdateContext: resourceIBMAppIDApplicationRolesUpdate,
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,
Required: true,
ForceNew: true,
},
"roles": {
Description: "A list of role IDs for roles that you want to be assigned to the application (this is different from AppID role access)",
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
},
},
}
}
func resourceIBMAppIDApplicationRolesCreate(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)
roles := flex.ExpandStringList(d.Get("roles").([]interface{}))
roleOpts := &appid.PutApplicationsRolesOptions{
TenantID: &tenantID,
ClientID: &clientID,
Roles: &appid.UpdateUserRolesParamsRoles{
Ids: roles,
},
}
_, resp, err := appIDClient.PutApplicationsRolesWithContext(ctx, roleOpts)
if err != nil {
return diag.Errorf("Error setting application roles: %s\n%s", err, resp)
}
d.SetId(fmt.Sprintf("%s/%s", tenantID, clientID))
return resourceIBMAppIDApplicationRolesRead(ctx, d, meta)
}
func resourceIBMAppIDApplicationRolesRead(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]
roles, resp, err := appIDClient.GetApplicationRolesWithContext(ctx, &appid.GetApplicationRolesOptions{
TenantID: &tenantID,
ClientID: &clientID,
})
if err != nil {
if resp != nil && resp.StatusCode == 404 {
log.Printf("[WARN] AppID application '%s' is not found, removing roles from state", clientID)
d.SetId("")
return nil
}
return diag.Errorf("Error getting AppID application roles: %s\n%s", err, resp)
}
var appRoles []interface{}
if roles.Roles != nil {
for _, v := range roles.Roles {
appRoles = append(appRoles, *v.ID)
}
}
if err := d.Set("roles", appRoles); err != nil {
return diag.Errorf("Error setting application roles: %s", err)
}
d.Set("tenant_id", tenantID)
d.Set("client_id", clientID)
return nil
}
func resourceIBMAppIDApplicationRolesUpdate(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)
roles := flex.ExpandStringList(d.Get("roles").([]interface{}))
roleOpts := &appid.PutApplicationsRolesOptions{
TenantID: &tenantID,
ClientID: &clientID,
Roles: &appid.UpdateUserRolesParamsRoles{
Ids: roles,
},
}
_, resp, err := appIDClient.PutApplicationsRolesWithContext(ctx, roleOpts)
if err != nil {
return diag.Errorf("Error updating application roles: %s\n%s", err, resp)
}
return resourceIBMAppIDApplicationRolesRead(ctx, d, meta)
}
func resourceIBMAppIDApplicationRolesDelete(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)
roleOpts := &appid.PutApplicationsRolesOptions{
TenantID: &tenantID,
ClientID: &clientID,
Roles: &appid.UpdateUserRolesParamsRoles{
Ids: []string{},
},
}
_, resp, err := appIDClient.PutApplicationsRolesWithContext(ctx, roleOpts)
if err != nil {
return diag.Errorf("Error clearing application roles: %s\n%s", err, resp)
}
d.SetId("")
return nil
}