generated from hashicorp/terraform-provider-scaffolding-framework
/
resource_tenant.go
394 lines (306 loc) · 11.5 KB
/
resource_tenant.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
package provider
import (
"context"
"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/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/permitio/permit-golang/pkg/models"
"github.com/permitio/permit-golang/pkg/permit"
"strings"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &tenantResource{}
var _ resource.ResourceWithImportState = &tenantResource{}
func NewTenantResource() resource.Resource {
return &tenantResource{}
}
// tenantResource defines the resource implementation.
type tenantResource struct {
client *permit.Client
}
// tenantResourceModel describes the resource data model.
type tenantResourceModel struct {
Id types.String `tfsdk:"id"`
OrganizationId types.String `tfsdk:"organization_id"`
ProjectId types.String `tfsdk:"project_id"`
EnvironmentId types.String `tfsdk:"environment_id"`
Key types.String `tfsdk:"key"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
// Configure adds the provider configured client to the data source.
func (r *tenantResource) Configure(ctx context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*permit.Client)
if !ok {
tflog.Error(ctx, "Unable to prepare client")
return
}
r.client = client
}
func (r *tenantResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_tenant"
}
func (r *tenantResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Tenant resource",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Tenant identifier",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"organization_id": schema.StringAttribute{
MarkdownDescription: "Organization identifier",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"project_id": schema.StringAttribute{
MarkdownDescription: "Project identifier",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"environment_id": schema.StringAttribute{
MarkdownDescription: "Environment identifier",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"key": schema.StringAttribute{
MarkdownDescription: "Tenant key",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Tenant name",
Required: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "Tenant description",
Optional: true,
},
},
}
}
func (r *tenantResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
tflog.Debug(ctx, "Preparing to create tenant resource")
var plan *tenantResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Building new tenant request")
projectId := plan.ProjectId.ValueString()
environmentId := plan.EnvironmentId.ValueString()
tenantKey := plan.Key.ValueString()
tenantName := plan.Name.ValueString()
tenantDescription := plan.Description.ValueString()
newTenant := *models.NewTenantCreate(tenantKey, tenantName)
if tenantDescription != "" {
newTenant.SetDescription(tenantDescription)
}
ctx = tflog.SetField(ctx, "permit_project_id", projectId)
ctx = tflog.SetField(ctx, "permit_environment_id", environmentId)
ctx = tflog.SetField(ctx, "permit_tenant_key", tenantKey)
tflog.Debug(ctx, "Setting context for tenant")
r.client.Api.SetContext(ctx, projectId, environmentId)
tflog.Debug(ctx, "Creating tenant resource")
tenant, err := r.client.Api.Tenants.Create(ctx, newTenant)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create tenant",
err.Error(),
)
return
}
tflog.Debug(ctx, "Completed new tenant request")
plan.Id = types.StringValue(tenant.Id)
plan.OrganizationId = types.StringValue(tenant.OrganizationId)
plan.ProjectId = types.StringValue(tenant.ProjectId)
plan.EnvironmentId = types.StringValue(tenant.EnvironmentId)
plan.Key = types.StringValue(tenant.Key)
plan.Name = types.StringValue(tenant.Name)
plan.Description = types.StringValue(*tenant.Description)
tflog.Debug(ctx, "Updating tenant state")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Finished creating tenant resource", map[string]any{"success": true})
}
func (r *tenantResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
tflog.Debug(ctx, "Preparing to read tenant resource")
var state tenantResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
projectId := state.ProjectId.ValueString()
environmentId := state.EnvironmentId.ValueString()
tenantKey := state.Key.ValueString()
ctx = tflog.SetField(ctx, "permit_project_id", projectId)
ctx = tflog.SetField(ctx, "permit_environment_id", environmentId)
ctx = tflog.SetField(ctx, "permit_tenant_key", tenantKey)
tflog.Debug(ctx, "Reading resource resource")
r.client.Api.SetContext(ctx, projectId, environmentId)
tenant, err := r.client.Api.Tenants.Get(ctx, tenantKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read tenant",
err.Error(),
)
return
}
tflog.Debug(ctx, "Completed read tenant request")
// Map response body to model
state = tenantResourceModel{
Id: types.StringValue(tenant.Id),
OrganizationId: types.StringValue(tenant.OrganizationId),
ProjectId: types.StringValue(tenant.ProjectId),
EnvironmentId: types.StringValue(tenant.EnvironmentId),
Key: types.StringValue(tenant.Key),
Name: types.StringValue(tenant.Name),
Description: types.StringValue(*tenant.Description),
}
tflog.Debug(ctx, "Updating tenant state")
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Finished reading tenant resource", map[string]any{"success": true})
}
func (r *tenantResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
tflog.Debug(ctx, "Preparing to update tenant resource")
var plan tenantResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Building update tenant request")
projectId := plan.ProjectId.ValueString()
environmentId := plan.EnvironmentId.ValueString()
tenantKey := plan.Key.ValueString()
tenantName := plan.Name.ValueString()
tenantDescription := plan.Description.ValueString()
updateTenant := *models.NewTenantUpdate()
updateTenant.SetName(tenantName)
if tenantDescription != "" {
updateTenant.SetDescription(tenantDescription)
}
ctx = tflog.SetField(ctx, "permit_project_id", projectId)
ctx = tflog.SetField(ctx, "permit_environment_id", environmentId)
ctx = tflog.SetField(ctx, "permit_tenant_key", tenantKey)
tflog.Debug(ctx, "Updating tenant resource")
r.client.Api.SetContext(ctx, projectId, environmentId)
tenant, err := r.client.Api.Tenants.Update(ctx, tenantKey, updateTenant)
if err != nil {
resp.Diagnostics.AddError(
"Unable to update tenant",
err.Error(),
)
return
}
tflog.Debug(ctx, "Completed update tenant request")
// Overwrite items with refreshed state
plan = tenantResourceModel{
Id: types.StringValue(tenant.Id),
OrganizationId: types.StringValue(tenant.OrganizationId),
ProjectId: types.StringValue(tenant.ProjectId),
EnvironmentId: types.StringValue(tenant.EnvironmentId),
Key: types.StringValue(tenant.Key),
Name: types.StringValue(tenant.Name),
Description: types.StringValue(*tenant.Description),
}
tflog.Debug(ctx, "Updating tenant state")
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Finished updating tenant resource", map[string]any{"success": true})
}
func (r *tenantResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
tflog.Debug(ctx, "Preparing to delete tenant resource")
var state *tenantResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
projectId := state.ProjectId.ValueString()
environmentId := state.EnvironmentId.ValueString()
tenantKey := state.Key.ValueString()
ctx = tflog.SetField(ctx, "permit_project_id", projectId)
ctx = tflog.SetField(ctx, "permit_environment_id", environmentId)
ctx = tflog.SetField(ctx, "permit_tenant_key", tenantKey)
tflog.Debug(ctx, "Deleting tenant resource")
r.client.Api.SetContext(ctx, projectId, environmentId)
err := r.client.Api.Tenants.Delete(ctx, tenantKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to delete tenant",
err.Error(),
)
return
}
tflog.Debug(ctx, "Finished deleting tenant resource", map[string]any{"success": true})
}
func (r *tenantResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
tflog.Debug(ctx, "Preparing to import tenant resource")
split := strings.Split(req.ID, "/")
if len(split) != 3 {
resp.Diagnostics.AddError(
"Error importing tenant",
"Could not import tenant, ID should be an {project-key}/{environment-key}/{tenant-key}",
)
return
}
projectKey := split[0]
environmentKey := split[1]
tenantKey := split[2]
project, err := r.client.Api.Projects.Get(ctx, projectKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read project",
err.Error(),
)
return
}
r.client.Api.SetContext(ctx, project.Id, "")
environment, err := r.client.Api.Environments.Get(ctx, environmentKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read environment",
err.Error(),
)
return
}
ctx = tflog.SetField(ctx, "permit_project_id", project.Id)
ctx = tflog.SetField(ctx, "permit_environment_id", environment.Id)
ctx = tflog.SetField(ctx, "permit_tenant_key", tenantKey)
tflog.Debug(ctx, "Importing tenant resource")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), project.Id)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("environment_id"), environment.Id)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("key"), tenantKey)...)
}