/
resource_scalr_access_policy.go
298 lines (259 loc) · 8.3 KB
/
resource_scalr_access_policy.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
package scalr
import (
"context"
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scalr/go-scalr"
)
type Scope string
type Subject string
const (
User Subject = "user"
Team Subject = "team"
ServiceAccount Subject = "service_account"
)
const (
Workspace Scope = "workspace"
Environment Scope = "environment"
Account Scope = "account"
)
func (s Scope) IsValid() error {
switch s {
case Workspace, Environment, Account:
return nil
}
return errors.New("Invalid scope type")
}
func (s Subject) IsValid() error {
switch s {
case User, Team, ServiceAccount:
return nil
}
return errors.New("Invalid subject type")
}
func resourceScalrAccessPolicy() *schema.Resource {
return &schema.Resource{
Description: "Manages the Scalr IAM access policies. Create, update and destroy.",
CreateContext: resourceScalrAccessPolicyCreate,
ReadContext: resourceScalrAccessPolicyRead,
UpdateContext: resourceScalrAccessPolicyUpdate,
DeleteContext: resourceScalrAccessPolicyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"is_system": {
Description: "The access policy is a built-in read-only policy that cannot be updated or deleted.",
Type: schema.TypeBool,
Computed: true,
},
"subject": {
Description: "Defines the subject of the access policy.",
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "The subject ID, `user-<RANDOM STRING>` for user, `team-<RANDOM STRING>` for team, `sa-<RANDOM STRING>` for service account.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Description: "The subject type, is one of `user`, `team`, or `service_account`.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if err := Subject(v).IsValid(); err != nil {
errs = append(errs, fmt.Errorf("%s must be one of [user, team, service_account], got: %s", key, v))
}
return
},
},
},
},
},
"scope": {
Description: "Defines the scope where access policy is applied.",
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "The scope ID, `acc-<RANDOM STRING>` for account, `env-<RANDOM STRING>` for environment, `ws-<RANDOM STRING>` for workspace.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Description: "The scope identity type, is one of `account`, `environment`, or `workspace`.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if err := Scope(v).IsValid(); err != nil {
errs = append(errs, fmt.Errorf("%s must be one of [workspace, environment, account], got: %s", key, v))
}
return
}},
},
},
},
"role_ids": {
Description: "The list of the role IDs.",
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 128,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func parseRoleIdDefinitions(d *schema.ResourceData) ([]*scalr.Role, error) {
roles := make([]*scalr.Role, 0)
roleIds := d.Get("role_ids").([]interface{})
err := ValidateIDsDefinitions(roleIds)
if err != nil {
return nil, fmt.Errorf("Got error during parsing role ids: %s", err.Error())
}
for _, roleId := range roleIds {
roles = append(roles, &scalr.Role{ID: roleId.(string)})
}
return roles, nil
}
func resourceScalrAccessPolicyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
subject := d.Get("subject").([]interface{})[0].(map[string]interface{})
subjectType := subject["type"].(string)
subjectId := subject["id"].(string)
scope := d.Get("scope").([]interface{})[0].(map[string]interface{})
scopeType := scope["type"].(string)
scopeId := scope["id"].(string)
roles, err := parseRoleIdDefinitions(d)
if err != nil {
return diag.FromErr(err)
}
// Create a new options struct.
options := scalr.AccessPolicyCreateOptions{Roles: roles}
switch Subject(subjectType) {
case User:
options.User = &scalr.User{ID: subjectId}
case Team:
options.Team = &scalr.Team{ID: subjectId}
case ServiceAccount:
options.ServiceAccount = &scalr.ServiceAccount{ID: subjectId}
}
switch Scope(scopeType) {
case Workspace:
options.Workspace = &scalr.Workspace{ID: scopeId}
case Environment:
options.Environment = &scalr.Environment{ID: scopeId}
case Account:
options.Account = &scalr.Account{ID: scopeId}
}
log.Printf("[DEBUG] Create access policy for %s %s on %s %s", subjectType, subjectId, scopeType, scopeId)
ap, err := scalrClient.AccessPolicies.Create(ctx, options)
if err != nil {
return diag.Errorf(
"Error creating access policy for %s %s on %s %s: %v", subjectType, subjectId, scopeType, scopeId, err)
}
d.SetId(ap.ID)
return resourceScalrAccessPolicyRead(ctx, d, meta)
}
func resourceScalrAccessPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
id := d.Id()
log.Printf("[DEBUG] Read configuration of access policy: %s", id)
ap, err := scalrClient.AccessPolicies.Read(ctx, id)
if err != nil {
if errors.Is(err, scalr.ErrResourceNotFound) {
log.Printf("[DEBUG] AccessPolicy %s not found", id)
d.SetId("")
return nil
}
return diag.Errorf("Error reading configuration of access policy %s: %v", id, err)
}
var subject [1]interface{}
subjectEl := make(map[string]interface{})
if ap.User != nil {
subjectEl["type"] = User
subjectEl["id"] = ap.User.ID
} else if ap.Team != nil {
subjectEl["type"] = Team
subjectEl["id"] = ap.Team.ID
} else if ap.ServiceAccount != nil {
subjectEl["type"] = ServiceAccount
subjectEl["id"] = ap.ServiceAccount.ID
} else {
return diag.Errorf("Unable to extract subject from access policy %s", ap.ID)
}
subject[0] = subjectEl
_ = d.Set("subject", subject)
var scope [1]interface{}
scopeEl := make(map[string]interface{})
if ap.Workspace != nil {
scopeEl["type"] = Workspace
scopeEl["id"] = ap.Workspace.ID
} else if ap.Environment != nil {
scopeEl["type"] = Environment
scopeEl["id"] = ap.Environment.ID
} else if ap.Account != nil {
scopeEl["type"] = Account
scopeEl["id"] = ap.Account.ID
} else {
return diag.Errorf("Unable to extract scope from access policy %s", ap.ID)
}
scope[0] = scopeEl
_ = d.Set("scope", scope)
roleIds := make([]interface{}, 0)
for _, role := range ap.Roles {
roleIds = append(roleIds, role.ID)
}
_ = d.Set("role_ids", roleIds)
_ = d.Set("is_system", ap.IsSystem)
d.SetId(ap.ID)
return nil
}
func resourceScalrAccessPolicyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
id := d.Id()
if d.HasChange("role_ids") {
roles, err := parseRoleIdDefinitions(d)
if err != nil {
return diag.FromErr(err)
}
// Create a new options struct.
options := scalr.AccessPolicyUpdateOptions{Roles: roles}
log.Printf("[DEBUG] Update access policy %s", id)
_, err = scalrClient.AccessPolicies.Update(ctx, id, options)
if err != nil {
return diag.Errorf(
"Error updating access policy %s: %v", id, err)
}
}
return resourceScalrAccessPolicyRead(ctx, d, meta)
}
func resourceScalrAccessPolicyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
id := d.Id()
log.Printf("[DEBUG] Delete access policy %s", id)
err := scalrClient.AccessPolicies.Delete(ctx, id)
if err != nil {
if errors.Is(err, scalr.ErrResourceNotFound) {
return nil
}
return diag.Errorf(
"Error deleting access policy %s: %v", id, err)
}
return nil
}