-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_global_role.go
340 lines (289 loc) · 9.88 KB
/
resource_global_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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package platform
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/jfrog/terraform-provider-shared/util"
utilfw "github.com/jfrog/terraform-provider-shared/util/fw"
)
const (
globalRolePostEndpoint = "/access/api/v1/roles"
globalRoleGetEndpoint = "/access/api/v1/roles/{name}"
)
var globalRoleTypes []string = []string{"ADMIN", "CUSTOM_GLOBAL", "PREDEFINED"}
var globalRoleActions []string = []string{
"READ_REPOSITORY",
"ANNOTATE_REPOSITORY",
"DEPLOY_CACHE_REPOSITORY",
"DELETE_OVERWRITE_REPOSITORY",
"MANAGE_XRAY_MD_REPOSITORY",
"READ_RELEASE_BUNDLE",
"ANNOTATE_RELEASE_BUNDLE",
"CREATE_RELEASE_BUNDLE",
"DISTRIBUTE_RELEASE_BUNDLE",
"DELETE_RELEASE_BUNDLE",
"MANAGE_XRAY_MD_RELEASE_BUNDLE",
"READ_BUILD",
"ANNOTATE_BUILD",
"DEPLOY_BUILD",
"DELETE_BUILD",
"MANAGE_XRAY_MD_BUILD",
"READ_SOURCES_PIPELINE",
"TRIGGER_PIPELINE",
"READ_INTEGRATIONS_PIPELINE",
"READ_POOLS_PIPELINE",
"REPORTS_SECURITY",
"WATCHES_SECURITY",
"POLICIES_SECURITY",
"RULES_SECURITY",
"READ_POLICIES_SECURITY",
}
var _ resource.Resource = (*globalRoleResource)(nil)
type globalRoleResource struct {
ProviderData util.ProviderMetadata
TypeName string
}
func NewGlobalRoleResource() resource.Resource {
return &globalRoleResource{
TypeName: "platform_global_role",
}
}
func (r *globalRoleResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = r.TypeName
}
func (r *globalRoleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Description: "Name of the role",
},
"description": schema.StringAttribute{
Optional: true,
Description: "Description of the role",
},
"type": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(globalRoleTypes...),
},
Description: fmt.Sprintf("Type of the role. Allowed values: %s", strings.Join(globalRoleTypes, ", ")),
},
"environments": schema.SetAttribute{
ElementType: types.StringType,
Required: true,
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
},
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
Description: "List of global or custom environments. A repository can be available in different environments. Members with roles defined in the set environment will have access to the repository.",
},
"actions": schema.SetAttribute{
ElementType: types.StringType,
Required: true,
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
setvalidator.ValueStringsAre(stringvalidator.OneOf(globalRoleActions...)),
},
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
Description: fmt.Sprintf("List of actions. Allowed values: %s", strings.Join(globalRoleActions, ", ")),
},
},
MarkdownDescription: "Provides a JFrog [global role](https://jfrog.com/help/r/jfrog-platform-administration-documentation/global-and-project-role-types) resource to manage custom global roles.",
}
}
type globalRoleResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Type types.String `tfsdk:"type"`
Environments types.Set `tfsdk:"environments"`
Actions types.Set `tfsdk:"actions"`
}
func (r *globalRoleResourceModel) toAPIModel(ctx context.Context, apiModel *globalRoleAPIModel) (ds diag.Diagnostics) {
var environments []string
ds.Append(r.Environments.ElementsAs(ctx, &environments, false)...)
if ds.HasError() {
return
}
var actions []string
ds.Append(r.Actions.ElementsAs(ctx, &actions, false)...)
if ds.HasError() {
return
}
*apiModel = globalRoleAPIModel{
Name: r.Name.ValueString(),
Description: r.Description.ValueString(),
Type: r.Type.ValueString(),
Environments: environments,
Actions: actions,
}
return nil
}
func (r *globalRoleResourceModel) fromAPIModel(ctx context.Context, apiModel *globalRoleAPIModel) (ds diag.Diagnostics) {
r.Name = types.StringValue(apiModel.Name)
r.Description = types.StringValue(apiModel.Description)
r.Type = types.StringValue(apiModel.Type)
environments, d := types.SetValueFrom(ctx, types.StringType, apiModel.Environments)
if d != nil {
ds = append(ds, d...)
}
if ds.HasError() {
return
}
r.Environments = environments
actions, d := types.SetValueFrom(ctx, types.StringType, apiModel.Actions)
if d != nil {
ds = append(ds, d...)
}
if ds.HasError() {
return
}
r.Actions = actions
return
}
type globalRoleAPIModel struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Environments []string `json:"environments"`
Actions []string `json:"actions"`
}
func (r *globalRoleResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
r.ProviderData = req.ProviderData.(util.ProviderMetadata)
}
func (r *globalRoleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
go util.SendUsageResourceCreate(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var plan globalRoleResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
var role globalRoleAPIModel
resp.Diagnostics.Append(plan.toAPIModel(ctx, &role)...)
if resp.Diagnostics.HasError() {
return
}
response, err := r.ProviderData.Client.R().
SetBody(&role).
Post(globalRolePostEndpoint)
if err != nil {
utilfw.UnableToCreateResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToCreateResourceError(resp, response.String())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *globalRoleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
go util.SendUsageResourceRead(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var state globalRoleResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
var role globalRoleAPIModel
response, err := r.ProviderData.Client.R().
SetPathParam("name", state.Name.ValueString()).
SetResult(&role).
Get(globalRoleGetEndpoint)
if err != nil {
utilfw.UnableToRefreshResourceError(resp, err.Error())
return
}
// Treat HTTP 404 Not Found status as a signal to recreate resource
// and return early
if response.StatusCode() == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
if response.IsError() {
utilfw.UnableToRefreshResourceError(resp, response.String())
return
}
// Convert from the API data model to the Terraform data model
// and refresh any attribute values.
resp.Diagnostics.Append(state.fromAPIModel(ctx, &role)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *globalRoleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
go util.SendUsageResourceUpdate(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var plan globalRoleResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
var role globalRoleAPIModel
resp.Diagnostics.Append(plan.toAPIModel(ctx, &role)...)
if resp.Diagnostics.HasError() {
return
}
response, err := r.ProviderData.Client.R().
SetPathParam("name", plan.Name.ValueString()).
SetBody(&role).
Put(globalRoleGetEndpoint)
if err != nil {
utilfw.UnableToUpdateResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToUpdateResourceError(resp, response.String())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *globalRoleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
go util.SendUsageResourceDelete(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var state globalRoleResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
response, err := r.ProviderData.Client.R().
SetPathParam("name", state.Name.ValueString()).
Delete(globalRoleGetEndpoint)
if err != nil {
utilfw.UnableToDeleteResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToDeleteResourceError(resp, response.String())
return
}
// If the logic reaches here, it implicitly succeeded and will remove
// the resource from state if there are no other errors.
}
func (r *globalRoleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}