-
Notifications
You must be signed in to change notification settings - Fork 25
/
resource_api_key.go
273 lines (227 loc) · 7.61 KB
/
resource_api_key.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
package iam
import (
"context"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"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/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
exoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/terraform-provider-exoscale/pkg/config"
providerConfig "github.com/exoscale/terraform-provider-exoscale/pkg/provider/config"
)
const ResourceAPIKeyDescription = `Manage Exoscale [IAM](https://community.exoscale.com/documentation/iam/) API Key.
`
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &ResourceAPIKey{}
var _ resource.ResourceWithImportState = &ResourceAPIKey{}
func NewResourceAPIKey() resource.Resource {
return &ResourceAPIKey{}
}
// ResourceAPIKey defines the IAM Organization Policy resource implementation.
type ResourceAPIKey struct {
client *exoscale.Client
env string
}
// ResourceAPIKeyModel describes the IAM Organization Policy resource data model.
type ResourceAPIKeyModel struct {
ID types.String `tfsdk:"id"`
Key types.String `tfsdk:"key"`
Name types.String `tfsdk:"name"`
Secret types.String `tfsdk:"secret"`
RoleID types.String `tfsdk:"role_id"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}
func (r *ResourceAPIKey) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_iam_api_key"
}
func (r *ResourceAPIKey) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: ResourceAPIKeyDescription,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of this resource.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "❗ IAM API Key name.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"role_id": schema.StringAttribute{
MarkdownDescription: "❗ IAM API role ID.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"key": schema.StringAttribute{
MarkdownDescription: "The IAM API Key to match.",
Computed: true,
},
"secret": schema.StringAttribute{
MarkdownDescription: "Secret for the IAM API Key.",
Computed: true,
Sensitive: true,
},
},
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx, timeouts.Opts{
Read: true,
}),
},
}
}
func (r *ResourceAPIKey) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
r.client = req.ProviderData.(*providerConfig.ExoscaleProviderConfig).ClientV2
r.env = req.ProviderData.(*providerConfig.ExoscaleProviderConfig).Environment
}
func (r *ResourceAPIKey) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data ResourceAPIKeyModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Set timeout
t, diags := data.Timeouts.Create(ctx, config.DefaultTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, t)
defer cancel()
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(r.env, config.DefaultZone))
key := exoscale.APIKey{
Name: data.Name.ValueStringPointer(),
RoleID: data.RoleID.ValueStringPointer(),
}
newKey, secret, err := r.client.CreateAPIKey(ctx, config.DefaultZone, &key)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create IAM API Key",
err.Error(),
)
return
}
data.ID = types.StringPointerValue(newKey.Key)
data.Secret = types.StringValue(secret)
// Read created policy
r.read(ctx, &resp.Diagnostics, &data)
if resp.Diagnostics.HasError() {
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Trace(ctx, "resource created", map[string]interface{}{
"id": data.ID,
})
}
func (r *ResourceAPIKey) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data ResourceAPIKeyModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Set timeout
t, diags := data.Timeouts.Read(ctx, config.DefaultTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, t)
defer cancel()
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(r.env, config.DefaultZone))
r.read(ctx, &resp.Diagnostics, &data)
if resp.Diagnostics.HasError() {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Trace(ctx, "resource read done", map[string]interface{}{
"id": data.ID,
})
}
// Update is NOOP becauses all arguments require restart..
func (r *ResourceAPIKey) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
}
func (r *ResourceAPIKey) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data ResourceAPIKeyModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Set timeout
t, diags := data.Timeouts.Read(ctx, config.DefaultTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, t)
defer cancel()
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(r.env, config.DefaultZone))
err := r.client.DeleteAPIKey(
ctx,
config.DefaultZone,
&exoscale.APIKey{Key: data.ID.ValueStringPointer()},
)
if err != nil {
resp.Diagnostics.AddError(
"Unable to delete API Key",
err.Error(),
)
return
}
tflog.Trace(ctx, "resource deleted", map[string]interface{}{
"id": data.ID,
})
}
func (r *ResourceAPIKey) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
var data ResourceAPIKeyModel
// Set timeouts (quirk https://github.com/hashicorp/terraform-plugin-framework-timeouts/issues/46)
var timeouts timeouts.Value
resp.Diagnostics.Append(resp.State.GetAttribute(ctx, path.Root("timeouts"), &timeouts)...)
if resp.Diagnostics.HasError() {
return
}
data.Timeouts = timeouts
data.ID = types.StringValue(req.ID)
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Trace(ctx, "resource imported", map[string]interface{}{
"id": data.ID,
})
}
func (r *ResourceAPIKey) read(
ctx context.Context,
d *diag.Diagnostics,
data *ResourceAPIKeyModel,
) {
apiKey, err := r.client.GetAPIKey(
ctx,
config.DefaultZone,
data.ID.ValueString(),
)
if err != nil {
d.AddError(
"Unable to get IAM Role",
err.Error(),
)
return
}
data.Key = types.StringPointerValue(apiKey.Key)
data.Name = types.StringPointerValue(apiKey.Name)
data.RoleID = types.StringPointerValue(apiKey.RoleID)
}