/
org_configuration_resource.go
370 lines (333 loc) · 13.5 KB
/
org_configuration_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
package provider
import (
"context"
"fmt"
cmTypes "github.com/control-monkey/controlmonkey-sdk-go/services/commons"
"github.com/control-monkey/terraform-provider-cm/internal/helpers"
"github.com/control-monkey/terraform-provider-cm/internal/provider/commons"
tfOrgConfiguration "github.com/control-monkey/terraform-provider-cm/internal/provider/entities/org_configuration"
cmStringValidators "github.com/control-monkey/terraform-provider-cm/internal/provider/validators/string"
"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/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 = &OrgConfigurationResource{}
func NewOrgConfigurationResource() resource.Resource {
return &OrgConfigurationResource{}
}
type OrgConfigurationResource struct {
client *ControlMonkeyAPIClient
}
func (r *OrgConfigurationResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_org_configuration"
}
func (r *OrgConfigurationResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Creates, updates and destroys org configuration.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The unique ID of this resource.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"iac_config": schema.SingleNestedAttribute{
MarkdownDescription: "IaC configuration that defines default versions. If not explicitly overridden, these defaults apply to all namespaces/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,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
"terragrunt_version": schema.StringAttribute{
MarkdownDescription: "the Terragrunt version that will be used for terragrunt operations.",
Optional: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
"opentofu_version": schema.StringAttribute{
MarkdownDescription: "the OpenTofu version that will be used for tofu operations.",
Optional: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
},
},
"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)),
Optional: 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.ValidateUniqueListWithNoBlankValues(),
},
"is_overridable": schema.BoolAttribute{
MarkdownDescription: "By setting this option, you allow this configuration to be overridden in specific namespaces/stacks.",
Optional: true,
},
},
},
"s3_state_files_locations": schema.ListNestedAttribute{
MarkdownDescription: "The S3 buckets of your current terraform state files. This will be used by ControlMonkey to scan for existing managed resources.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"bucket_name": schema.StringAttribute{
MarkdownDescription: "The name of the bucket.",
Required: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
"bucket_region": schema.StringAttribute{
MarkdownDescription: "The region of the bucket.",
Required: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
"aws_account_id": schema.StringAttribute{
MarkdownDescription: "The AWS account ID in which the bucket is situated.",
Required: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
},
},
},
"suppressed_resources": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"managed_by_tags": schema.ListNestedAttribute{
MarkdownDescription: "List of tags by which any AWS resource with one of the configured tags will be considered as managed. The tag key/value definition is case sensitive.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"key": schema.StringAttribute{
MarkdownDescription: "The key of the tag.",
Required: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
"value": schema.StringAttribute{
MarkdownDescription: "The value of the tag.",
Optional: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
},
},
},
},
},
"report_configurations": schema.ListNestedAttribute{
MarkdownDescription: "The S3 buckets of your current terraform state files. This will be used by ControlMonkey to scan for existing managed resources.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"enabled": schema.BoolAttribute{
MarkdownDescription: "Indicates whether the report distribution is enabled or disabled.",
Required: true,
},
"type": schema.StringAttribute{
MarkdownDescription: fmt.Sprintf("The type of the report. Supported types: %s.", helpers.EnumForDocs(cmTypes.ReportTypes)),
Required: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
"recipients": schema.SingleNestedAttribute{
MarkdownDescription: "Specifies who will receive the report.",
Optional: true,
Attributes: map[string]schema.Attribute{
"all_admins": schema.BoolAttribute{
MarkdownDescription: "If enabled, the report will be sent to every administrator within your organization.",
Optional: true,
},
"email_addresses": schema.ListAttribute{
MarkdownDescription: "List of email addresses to which the report will be sent.",
ElementType: types.StringType,
Optional: true,
Validators: commons.ValidateUniqueListWithNoBlankValues(),
},
"email_addresses_to_exclude": schema.ListAttribute{
MarkdownDescription: "List of email addresses to which the report will not be sent.",
ElementType: types.StringType,
Optional: true,
Validators: commons.ValidateUniqueListWithNoBlankValues(),
},
},
},
},
},
},
},
}
}
// Configure adds the provider configured client to the data source.
func (r *OrgConfigurationResource) 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
}
// Read refreshes the Terraform state with the latest data.
func (r *OrgConfigurationResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
//Get current state
var state tfOrgConfiguration.ResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
res, err := r.client.Client.organization.ReadOrgConfiguration(ctx)
if err != nil {
if commons.IsNotFoundResponseError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(fmt.Sprintf("Failed to read org configuration"), err.Error())
return
}
tfOrgConfiguration.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 *OrgConfigurationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
//only a single org config must exist. So, before creating a new one, we check if one is already exists
diags := r.checkIfExistsBeforeCreate(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
//Retrieve values from plan
var plan tfOrgConfiguration.ResourceModel
diags = req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
body, _ := tfOrgConfiguration.Converter(&plan, nil, commons.CreateConverter)
if _, err := r.client.Client.organization.UpsertOrgConfiguration(ctx, body); err != nil {
resp.Diagnostics.AddError(
resourceCreationFailedError,
fmt.Sprintf("Failed to create org configuration, error: %s", err),
)
return
}
plan.ID = types.StringValue(tfOrgConfiguration.ImportID)
// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *OrgConfigurationResource) checkIfExistsBeforeCreate(ctx context.Context) diag.Diagnostics {
retVal := diag.Diagnostics{}
res, err := r.client.Client.organization.ReadOrgConfiguration(ctx)
if err != nil {
retVal.AddError(resourceCreationFailedError, fmt.Sprintf("Failed to create org configuration. Error: %s", err))
} else if helpers.IsAllNilFields(res) == false {
retVal.AddError("Org Configuration already exists, there is only one configuration allowed per organization",
fmt.Sprintf("Import operation is required to manage this resourcce. Use import command e.g 'terraform import cm_org_configuration.<resource_name> %s'", tfOrgConfiguration.ImportID),
)
}
return retVal
}
func (r *OrgConfigurationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan tfOrgConfiguration.ResourceModel
var state tfOrgConfiguration.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
}
body, _ := tfOrgConfiguration.Converter(&plan, &state, commons.UpdateConverter)
if _, err := r.client.Client.organization.UpsertOrgConfiguration(ctx, body); err != nil {
if commons.IsNotFoundResponseError(err) {
resp.Diagnostics.AddError(resourceNotFoundError, fmt.Sprintf("Org Configuration not found"))
return
}
resp.Diagnostics.AddError(
resourceUpdateFailedError,
fmt.Sprintf("failed to update org configuration, error: %s", 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 *OrgConfigurationResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state tfOrgConfiguration.ResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if _, err := r.client.Client.organization.DeleteOrgConfiguration(ctx); err != nil {
if commons.IsNotFoundResponseError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
resourceDeletionFailedError,
fmt.Sprintf("Failed to delete org configuration, error: %s", err),
)
return
}
}
func (r *OrgConfigurationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
if req.ID != tfOrgConfiguration.ImportID {
resp.Diagnostics.AddError(validationError, fmt.Sprintf("ID must be '%s'", tfOrgConfiguration.ImportID))
return
}
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}