-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathresource_permission.go
269 lines (232 loc) · 6.86 KB
/
resource_permission.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
package codefresh
import (
"context"
"fmt"
"log"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
funk "github.com/thoas/go-funk"
)
func resourcePermission() *schema.Resource {
return &schema.Resource{
Description: "Permissions are used to set up access control and define which teams have access to which clusters and pipelines based on tags.",
Create: resourcePermissionCreate,
Read: resourcePermissionRead,
Update: resourcePermissionUpdate,
Delete: resourcePermissionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"_id": {
Description: "The permission ID.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"team": {
Description: "The Id of the team the permissions apply to.",
Type: schema.TypeString,
Required: true,
},
"resource": {
Description: `
The type of resources the permission applies to. Possible values:
* pipeline
* cluster
* project
`,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"pipeline",
"cluster",
"project",
}, false),
},
"related_resource": {
Description: `
Specifies the resource to use when evaluating the tags. Possible values:
* project
`,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"project",
}, false),
},
"action": {
Description: `
Action to be allowed. Possible values:
* create
* read
* update
* delete
* run (Only valid for pipeline resource)
* approve (Only valid for pipeline resource)
* debug (Only valid for pipeline resource)
`,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"create",
"read",
"update",
"delete",
"run",
"approve",
"debug",
}, false),
},
"rule_type": {
Description: "Rule type - can be either `all` or `any`. If all is specified the rule will apply on resources that have all the tags. If any is specified the rule will apply on resources that have any of the tags. If not specified, deafult behavior is `any`.",
Type: schema.TypeString,
Optional: true,
//Default: "any",
ValidateFunc: validation.StringInSlice([]string{"all", "any"}, false),
},
"tags": {
Description: `
The tags for which to apply the permission. Supports two custom tags:
* untagged: Apply to all resources without tags
* (asterisk): Apply to all resources with any tag
`,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
CustomizeDiff: customdiff.All(
resourcePermissionCustomDiff,
),
}
}
func resourcePermissionCustomDiff(ctx context.Context, diff *schema.ResourceDiff, v interface{}) error {
if diff.HasChanges("resource", "related_resource") {
if diff.Get("related_resource").(string) != "" && diff.Get("resource").(string) != "pipeline" {
return fmt.Errorf("related_resource is only valid when resource is 'pipeline'")
}
}
if diff.HasChanges("resource", "action") {
if funk.Contains([]string{"run", "approve", "debug"}, diff.Get("action").(string)) && diff.Get("resource").(string) != "pipeline" {
return fmt.Errorf("action %v is only valid when resource is 'pipeline'", diff.Get("action").(string))
}
}
return nil
}
func resourcePermissionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
permission := *mapResourceToPermission(d)
newPermission, err := client.CreatePermission(&permission)
if err != nil {
return err
}
if newPermission == nil {
return fmt.Errorf("resourcePermissionCreate - failed to create permission, empty response")
}
d.SetId(newPermission.ID)
return resourcePermissionRead(d, meta)
}
func resourcePermissionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
permissionID := d.Id()
if permissionID == "" {
d.SetId("")
return nil
}
permission, err := client.GetPermissionByID(permissionID)
if err != nil {
return err
}
err = mapPermissionToResource(permission, d)
if err != nil {
return err
}
return nil
}
func resourcePermissionUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
permission := *mapResourceToPermission(d)
// In case team, action or relatedResource or resource have changed - a new permission needs to be created (but without recreating the terraform resource as destruction of resources is alarming for end users)
if d.HasChanges("team", "action", "related_resource", "resource", "rule_type") {
deleteErr := resourcePermissionDelete(d, meta)
if deleteErr != nil {
log.Printf("[WARN] failed to delete permission %v: %v", permission, deleteErr)
}
resp, err := client.CreatePermission(&permission)
if err != nil {
return err
}
d.SetId(resp.ID)
// Only tags can be updated
} else if d.HasChange("tags") {
err := client.UpdatePermissionTags(&permission)
if err != nil {
return err
}
}
return resourcePermissionRead(d, meta)
}
func resourcePermissionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
err := client.DeletePermission(d.Id())
if err != nil {
return err
}
return nil
}
func mapPermissionToResource(permission *cfclient.Permission, d *schema.ResourceData) error {
err := d.Set("_id", permission.ID)
if err != nil {
return err
}
err = d.Set("team", permission.Team)
if err != nil {
return err
}
err = d.Set("action", permission.Action)
if err != nil {
return err
}
err = d.Set("resource", permission.Resource)
if err != nil {
return err
}
err = d.Set("related_resource", permission.RelatedResource)
if err != nil {
return err
}
err = d.Set("tags", permission.Tags)
if err != nil {
return err
}
err = d.Set("rule_type", permission.RuleType)
if err != nil {
return err
}
return nil
}
func mapResourceToPermission(d *schema.ResourceData) *cfclient.Permission {
tagsI := d.Get("tags").(*schema.Set).List()
var tags []string
if len(tagsI) > 0 {
tags = datautil.ConvertStringArr(tagsI)
} else {
tags = []string{"*", "untagged"}
}
permission := &cfclient.Permission{
ID: d.Id(),
Team: d.Get("team").(string),
Action: d.Get("action").(string),
Resource: d.Get("resource").(string),
RelatedResource: d.Get("related_resource").(string),
RuleType: d.Get("rule_type").(string),
Tags: tags,
}
return permission
}