forked from launchdarkly/terraform-provider-launchdarkly
/
resource_launchdarkly_custom_role.go
207 lines (175 loc) · 6.71 KB
/
resource_launchdarkly_custom_role.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
package launchdarkly
import (
"context"
"fmt"
"log"
"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"
ldapi "github.com/launchdarkly/api-client-go/v7"
)
func resourceCustomRole() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCustomRoleCreate,
ReadContext: resourceCustomRoleRead,
UpdateContext: resourceCustomRoleUpdate,
DeleteContext: resourceCustomRoleDelete,
Exists: resourceCustomRoleExists,
Importer: &schema.ResourceImporter{
State: resourceCustomRoleImport,
},
Schema: map[string]*schema.Schema{
KEY: {
Type: schema.TypeString,
Required: true,
Description: "A unique key that will be used to reference the custom role in your code",
ForceNew: true,
ValidateDiagFunc: validateKey(),
},
NAME: {
Type: schema.TypeString,
Required: true,
Description: "A name for the custom role",
},
DESCRIPTION: {
Type: schema.TypeString,
Optional: true,
Description: "Description of the custom role",
},
BASE_PERMISSIONS: {
Type: schema.TypeString,
Optional: true,
Description: "The base permission level - either reader or no_access. Defaults to reader",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"reader", "no_access"}, false)),
Default: "reader",
},
POLICY: policyArraySchema(),
POLICY_STATEMENTS: policyStatementsSchema(policyStatementSchemaOptions{}),
},
}
}
func resourceCustomRoleCreate(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
client := metaRaw.(*Client)
customRoleKey := d.Get(KEY).(string)
customRoleName := d.Get(NAME).(string)
customRoleDescription := d.Get(DESCRIPTION).(string)
customRoleBasePermissions := d.Get(BASE_PERMISSIONS).(string)
customRolePolicies := policiesFromResourceData(d)
policyStatements, err := policyStatementsFromResourceData(d.Get(POLICY_STATEMENTS).([]interface{}))
if err != nil {
return diag.FromErr(err)
}
if len(policyStatements) > 0 {
customRolePolicies = policyStatements
}
customRoleBody := ldapi.CustomRolePost{
Key: customRoleKey,
Name: customRoleName,
Description: ldapi.PtrString(customRoleDescription),
Policy: customRolePolicies,
}
if customRoleBasePermissions != "" {
customRoleBody.BasePermissions = ldapi.PtrString(customRoleBasePermissions)
}
_, _, err = client.ld.CustomRolesApi.PostCustomRole(client.ctx).CustomRolePost(customRoleBody).Execute()
if err != nil {
return diag.Errorf("failed to create custom role with name %q: %s", customRoleName, handleLdapiErr(err))
}
d.SetId(customRoleKey)
return resourceCustomRoleRead(ctx, d, metaRaw)
}
func resourceCustomRoleRead(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := metaRaw.(*Client)
customRoleID := d.Id()
customRole, res, err := client.ld.CustomRolesApi.GetCustomRole(client.ctx, customRoleID).Execute()
if isStatusNotFound(res) {
log.Printf("[WARN] failed to find custom role with id %q, removing from state", customRoleID)
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("[WARN] failed to find custom role with id %q, removing from state", customRoleID),
})
d.SetId("")
return diags
}
if err != nil {
return diag.Errorf("failed to get custom role with id %q: %s", customRoleID, handleLdapiErr(err))
}
_ = d.Set(KEY, customRole.Key)
_ = d.Set(NAME, customRole.Name)
_ = d.Set(DESCRIPTION, customRole.Description)
_ = d.Set(BASE_PERMISSIONS, customRole.BasePermissions)
// Because "policy" is now deprecated in favor of "policy_statements", only set "policy" if it has
// already been set by the user.
// TODO: Somehow this seems to also add an empty policystatement of
// policy {
// + actions = []
// + resources = []
// }
if _, ok := d.GetOk(POLICY); ok {
policies := policiesToResourceData(customRole.Policy)
err = d.Set(POLICY, policies)
} else {
err = d.Set(POLICY_STATEMENTS, policyStatementsToResourceData(statementsToStatementReps(customRole.Policy)))
}
if err != nil {
return diag.Errorf("could not set policy on custom role with id %q: %v", customRoleID, err)
}
return diags
}
func resourceCustomRoleUpdate(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
client := metaRaw.(*Client)
customRoleKey := d.Get(KEY).(string)
customRoleName := d.Get(NAME).(string)
customRoleDescription := d.Get(DESCRIPTION).(string)
customRoleBasePermissions := d.Get(BASE_PERMISSIONS).(string)
customRolePolicies := policiesFromResourceData(d)
policyStatements, err := policyStatementsFromResourceData(d.Get(POLICY_STATEMENTS).([]interface{}))
if err != nil {
return diag.FromErr(err)
}
if len(policyStatements) > 0 {
customRolePolicies = policyStatements
}
patch := ldapi.PatchWithComment{
Patch: []ldapi.PatchOperation{
patchReplace("/name", &customRoleName),
patchReplace("/description", &customRoleDescription),
patchReplace("/policy", &customRolePolicies),
}}
if customRoleBasePermissions != "" {
patch.Patch = append(patch.Patch, patchReplace("/basePermissions", &customRoleBasePermissions))
}
_, _, err = client.ld.CustomRolesApi.PatchCustomRole(client.ctx, customRoleKey).PatchWithComment(patch).Execute()
if err != nil {
return diag.Errorf("failed to update custom role with key %q: %s", customRoleKey, handleLdapiErr(err))
}
return resourceCustomRoleRead(ctx, d, metaRaw)
}
func resourceCustomRoleDelete(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := metaRaw.(*Client)
customRoleKey := d.Id()
_, err := client.ld.CustomRolesApi.DeleteCustomRole(client.ctx, customRoleKey).Execute()
if err != nil {
return diag.Errorf("failed to delete custom role with key %q: %s", customRoleKey, handleLdapiErr(err))
}
return diags
}
func resourceCustomRoleExists(d *schema.ResourceData, metaRaw interface{}) (bool, error) {
return customRoleExists(d.Id(), metaRaw.(*Client))
}
func customRoleExists(customRoleKey string, meta *Client) (bool, error) {
_, res, err := meta.ld.CustomRolesApi.GetCustomRole(meta.ctx, customRoleKey).Execute()
if isStatusNotFound(res) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to get custom role with key %q: %s", customRoleKey, handleLdapiErr(err))
}
return true, nil
}
func resourceCustomRoleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
_ = d.Set(KEY, d.Id())
return []*schema.ResourceData{d}, nil
}