/
namespace_resource.go
414 lines (369 loc) · 14.4 KB
/
namespace_resource.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package provider
import (
"context"
"fmt"
"github.com/control-monkey/terraform-provider-cm/internal/provider/commons"
"github.com/control-monkey/terraform-provider-cm/internal/provider/entities/namespace"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/control-monkey/controlmonkey-sdk-go/controlmonkey"
cmTypes "github.com/control-monkey/controlmonkey-sdk-go/services/commons"
"github.com/control-monkey/terraform-provider-cm/internal/helpers"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"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/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces
var _ resource.Resource = &NamespaceResource{}
func NewNamespaceResource() resource.Resource {
return &NamespaceResource{}
}
type NamespaceResource struct {
client *ControlMonkeyAPIClient
}
func (r *NamespaceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_namespace"
}
func (r *NamespaceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Creates, updates and destroys namespaces.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The unique ID of the namespace.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the namespace.",
Required: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The description of the namespace.",
Optional: true,
Validators: []validator.String{
stringvalidator.NoneOf(""),
},
},
"external_credentials": schema.ListNestedAttribute{
MarkdownDescription: "List of cloud credentials attached to the namespace.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
MarkdownDescription: fmt.Sprintf("The type of the credentials. Allowed values: %s.", helpers.EnumForDocs(cmTypes.ExternalCredentialTypes)),
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(cmTypes.ExternalCredentialTypes...),
},
},
"external_credentials_id": schema.StringAttribute{
MarkdownDescription: "The ControlMonkey unique ID of the credentials.",
Required: true,
},
"aws_profile_name": schema.StringAttribute{
MarkdownDescription: "Profile name for AWS credentials.",
Optional: true,
},
},
},
Validators: []validator.List{listvalidator.SizeAtLeast(1)},
},
"policy": schema.SingleNestedAttribute{
MarkdownDescription: "The policy of the namespace.",
Optional: true,
Attributes: map[string]schema.Attribute{
"ttl_config": schema.SingleNestedAttribute{
MarkdownDescription: "The time to live config of the namespace policy regarding to its stacks.",
Optional: true,
Attributes: map[string]schema.Attribute{
"max_ttl": schema.SingleNestedAttribute{
MarkdownDescription: "The max time to live for new stacks in the namespace.",
Required: true,
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
MarkdownDescription: fmt.Sprintf("The type of the ttl. Allowed values: %s.", helpers.EnumForDocs(cmTypes.TtlTypes)),
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(cmTypes.TtlTypes...),
},
},
"value": schema.Int64Attribute{
MarkdownDescription: "The value that corresponds the type",
Required: true,
},
},
},
"default_ttl": schema.SingleNestedAttribute{
MarkdownDescription: "The default time to live for new stacks in the namespace.",
Required: true,
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
MarkdownDescription: fmt.Sprintf("The type of the ttl. Allowed values: %s.", helpers.EnumForDocs(cmTypes.TtlTypes)),
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(cmTypes.TtlTypes...),
},
},
"value": schema.Int64Attribute{
MarkdownDescription: "The value that corresponds the type",
Required: true,
},
},
},
},
},
},
},
"iac_config": schema.SingleNestedAttribute{
MarkdownDescription: "IaC configuration of the namespace. If not overridden, this becomes the default for its stacks.",
Optional: true,
Attributes: map[string]schema.Attribute{
"terraform_version": schema.StringAttribute{
MarkdownDescription: "the Terraform version that will be used for terraform operations.",
Optional: true,
},
"terragrunt_version": schema.StringAttribute{
MarkdownDescription: "the Terragrunt version that will be used for terragrunt operations.",
Optional: true,
},
"opentofu_version": schema.StringAttribute{
MarkdownDescription: "the OpenTofu version that will be used for tofu operations.",
Optional: true,
},
},
},
"runner_config": schema.SingleNestedAttribute{
MarkdownDescription: "Configure the runner settings to specify whether ControlMonkey manages the runner or it is self-hosted.",
Optional: true,
Attributes: map[string]schema.Attribute{
"mode": schema.StringAttribute{
MarkdownDescription: fmt.Sprintf("The runner mode. Allowed values: %s.", helpers.EnumForDocs(cmTypes.RunnerConfigModeTypes)),
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(cmTypes.RunnerConfigModeTypes...),
},
},
"groups": schema.ListAttribute{
MarkdownDescription: fmt.Sprintf("In case that `mode` is `%s`, groups must contain at least one runners group. If `mode` is `%s`, this field must not be configures.", cmTypes.SelfHosted, cmTypes.Managed),
ElementType: types.StringType,
Optional: true,
Validators: commons.ValidateUniqueNotEmptyListWithNoBlankValues(),
},
"is_overridable": schema.BoolAttribute{
MarkdownDescription: "Determine if stacks within the namespace can override the runner_config.",
Required: true,
},
},
},
"deployment_approval_policy": schema.SingleNestedAttribute{
MarkdownDescription: "Set up requirements to approve a deployment",
Optional: true,
Attributes: map[string]schema.Attribute{
"rules": schema.ListNestedAttribute{
MarkdownDescription: "Set up rules for approving deployment processes. At least one rule should be configured",
Required: true,
Validators: []validator.List{
listvalidator.SizeAtLeast(1),
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
MarkdownDescription: fmt.Sprintf("The type of the rule. Allowed values: %s.", helpers.EnumForDocs(cmTypes.DeploymentApprovalPolicyRuleTypes)),
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(cmTypes.DeploymentApprovalPolicyRuleTypes...),
},
},
},
},
},
"override_behavior": schema.StringAttribute{
MarkdownDescription: fmt.Sprintf("Decide whether stacks can override this configuration. Allowed values: %s.", helpers.EnumForDocs(cmTypes.OverrideBehaviorTypes)),
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(cmTypes.OverrideBehaviorTypes...),
},
},
},
},
},
}
}
// Configure adds the provider configured client to the data source.
func (r *NamespaceResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*ControlMonkeyAPIClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *ControlMonkeyAPIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *NamespaceResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
var data namespace.ResourceModel
if diags := req.Config.Get(ctx, &data); diags.HasError() {
return
}
externalCredentials := data.ExternalCredentials
if externalCredentials != nil {
for _, credentials := range externalCredentials {
credentialsType := credentials.Type
profileName := credentials.AwsProfileName
if helpers.IsKnown(credentialsType) && credentialsType.ValueString() != cmTypes.AwsAssumeRole && profileName.IsNull() == false {
resp.Diagnostics.AddError(
validationError, fmt.Sprintf("external_credentials cannot have aws_profile_name configured for non AWS provider."),
)
}
}
}
runnerConfig := data.RunnerConfig
if runnerConfig != nil {
mode := runnerConfig.Mode
if helpers.IsKnown(mode) {
modeValue := mode.ValueString()
if modeValue == cmTypes.Managed && runnerConfig.Groups.IsNull() == false {
resp.Diagnostics.AddError(
validationError, fmt.Sprintf("runner_config.mode with type '%s' cannot have runner_config.groups", cmTypes.Managed),
)
} else if modeValue == cmTypes.SelfHosted && helpers.IsKnown(runnerConfig.Groups) {
if len(runnerConfig.Groups.Elements()) == 0 {
resp.Diagnostics.AddError(
validationError, fmt.Sprintf("runner_config.mode with type '%s' requires runner_config.groups to be not empty", cmTypes.SelfHosted),
)
} else if helpers.DoesTfListContainsEmptyValue(runnerConfig.Groups) {
resp.Diagnostics.AddError(
validationError, "Found empty string in runner_config.groups",
)
} else if !helpers.IsTfStringSliceUnique(runnerConfig.Groups) {
resp.Diagnostics.AddError(
validationError, "Found duplicate in runner_config.groups",
)
}
}
}
}
}
// Read refreshes the Terraform state with the latest data.
func (r *NamespaceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
//Get current state
var state namespace.ResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
res, err := r.client.Client.namespace.ReadNamespace(ctx, id)
if err != nil {
if commons.IsNotFoundResponseError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(fmt.Sprintf("Failed to read namespace %s", id), fmt.Sprintf("%s", err))
return
}
namespace.UpdateStateAfterRead(res, &state)
// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Create creates the resource and sets the initial Terraform state.
func (r *NamespaceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
//Retrieve values from plan
var plan namespace.ResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
body, _ := namespace.Converter(&plan, nil, commons.CreateConverter)
res, err := r.client.Client.namespace.CreateNamespace(ctx, body)
if err != nil {
resp.Diagnostics.AddError(
"Namespace creation failed",
fmt.Sprintf("failed to create namespace, error: %s", err.Error()),
)
return
}
plan.ID = types.StringValue(controlmonkey.StringValue(res.Namespace.ID))
// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *NamespaceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan namespace.ResourceModel
var state namespace.ResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id := plan.ID.ValueString()
body, _ := namespace.Converter(&plan, &state, commons.UpdateConverter)
_, err := r.client.Client.namespace.UpdateNamespace(ctx, id, body)
if err != nil {
if commons.IsNotFoundResponseError(err) {
resp.Diagnostics.AddError(resourceNotFoundError, fmt.Sprintf("Namespace '%s' not found", id))
return
}
resp.Diagnostics.AddError(
resourceUpdateFailedError,
fmt.Sprintf("failed to update namespace %s, error: %s", id, err),
)
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *NamespaceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state namespace.ResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
_, err := r.client.Client.namespace.DeleteNamespace(ctx, id)
if err != nil {
if commons.IsNotFoundResponseError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Namespace deletion failed",
fmt.Sprintf("Failed to delete namespace %s, error: %s", id, err),
)
return
}
}
func (r *NamespaceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
//region Private
//endregion