/
credential_resource.go
298 lines (242 loc) · 8.34 KB
/
credential_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
package s3
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
v1 "github.com/orange-cloudavenue/cloudavenue-sdk-go/v1"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &CredentialResource{}
_ resource.ResourceWithConfigure = &CredentialResource{}
_ resource.ResourceWithModifyPlan = &CredentialResource{}
)
// NewCredentialResource is a helper function to simplify the provider implementation.
func NewCredentialResource() resource.Resource {
return &CredentialResource{}
}
// CredentialResource is the resource implementation.
type CredentialResource struct {
client *client.CloudAvenue
s3Client v1.S3Client
}
// Init Initializes the resource.
func (r *CredentialResource) Init(ctx context.Context, rm *CredentialModel) (diags diag.Diagnostics) {
r.s3Client = r.client.CAVSDK.V1.S3()
return
}
// Metadata returns the resource type name.
func (r *CredentialResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + categoryName + "_credential"
}
// Schema defines the schema for the resource.
func (r *CredentialResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = credentialSchema(ctx).GetResource(ctx)
}
func (r *CredentialResource) Configure(ctx 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.(*client.CloudAvenue)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *client.CloudAvenue, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *CredentialResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
plan := &CredentialModel{}
state := &CredentialModel{}
// Retrieve values from plan
d := req.Plan.Get(ctx, plan)
if d.HasError() {
// If there is an error in the plan, we don't need to continue
return
}
d = req.State.Get(ctx, state)
// If error in state will be is in create mode
if !d.HasError() {
return
}
// If save_in_tfstate is true print warning security risk
if plan.SaveInTFState.Get() {
resp.Diagnostics.AddWarning(
"save_in_tfstate is true",
"SaveInTFState is true. This is a security risk and should only be used for testing purposes.",
)
}
// if print_token is true print warning security risk
if plan.PrintToken.Get() {
resp.Diagnostics.AddWarning(
"print_token is true",
"PrintToken is true. This is a security risk and should only be used for testing purposes.",
)
}
}
// Create creates the resource and sets the initial Terraform state.
func (r *CredentialResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
defer metrics.New("cloudavenue_s3_credential", r.client.GetOrgName(), metrics.Create)()
plan := &CredentialModel{}
// Retrieve values from plan
resp.Diagnostics.Append(req.Plan.Get(ctx, plan)...)
if resp.Diagnostics.HasError() {
return
}
// Init the resource
resp.Diagnostics.Append(r.Init(ctx, plan)...)
if resp.Diagnostics.HasError() {
return
}
/*
Implement the resource creation logic here.
*/
user, oseErr := r.s3Client.GetUser(r.client.GetUserName())
if oseErr != nil {
if oseErr.IsNotFountError() {
resp.Diagnostics.AddError("User not found", fmt.Sprintf("The user %s is not found", r.client.GetUserName()))
return
}
resp.Diagnostics.AddError("Error getting user", oseErr.Error())
return
}
cred, err := user.NewCredential()
if err != nil {
resp.Diagnostics.AddError("Error creating credential", err.Error())
return
}
plan.AccessKey.Set(cred.GetAccessKey())
plan.Username.Set(user.GetName())
if !plan.SaveInTFState.Get() {
// Token is computed attribute so we need to set it to empty string if SaveInTfstate is false
plan.SecretKey.Set("")
}
if plan.PrintToken.Get() {
resp.Diagnostics.AddWarning(
fmt.Sprintf("New credential created for the user %s ", plan.Username.Get()),
fmt.Sprintf("Access key: %s\nSecret Key: %s", cred.GetAccessKey(), cred.GetSecretKey()),
)
}
if plan.SaveInTFState.Get() {
plan.SecretKey.Set(cred.GetSecretKey())
}
if plan.SaveInFile.Get() {
type credentialFile struct {
AK string `json:"accesskey"`
SK string `json:"secretkey"`
}
credFile := credentialFile{
AK: cred.GetAccessKey(),
SK: cred.GetSecretKey(),
}
b, err := json.Marshal(credFile)
if err != nil {
resp.Diagnostics.AddError("Error marshalling credential", err.Error())
return
}
if err := os.WriteFile(plan.FileName.Get(), b, 0o600); err != nil {
resp.Diagnostics.AddError("Error saving credential", err.Error())
return
}
}
// Use generic read function to refresh the state
state, _, d := r.read(ctx, plan)
if d.HasError() {
resp.Diagnostics.Append(d...)
return
}
// Set state to fully populated data
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
// Read refreshes the Terraform state with the latest data.
func (r *CredentialResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
defer metrics.New("cloudavenue_s3_credential", r.client.GetOrgName(), metrics.Read)()
state := &CredentialModel{}
// Get current state
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
// Init the resource
resp.Diagnostics.Append(r.Init(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
// Refresh the state
stateRefreshed, found, d := r.read(ctx, state)
if !found {
resp.State.RemoveResource(ctx)
return
}
if d.HasError() {
resp.Diagnostics.Append(d...)
return
}
// Set refreshed state
resp.Diagnostics.Append(resp.State.Set(ctx, stateRefreshed)...)
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *CredentialResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
defer metrics.New("cloudavenue_s3_credential", r.client.GetOrgName(), metrics.Update)()
// No update for this resource
}
// Delete deletes the resource and removes the Terraform state on success.
func (r *CredentialResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
defer metrics.New("cloudavenue_s3_credential", r.client.GetOrgName(), metrics.Delete)()
state := &CredentialModel{}
// Get current state
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
// Init the resource
resp.Diagnostics.Append(r.Init(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
/*
Implement the resource deletion here
*/
user, oseErr := r.s3Client.GetUser(state.Username.Get())
if oseErr != nil {
resp.Diagnostics.AddError("Error getting user", oseErr.Error())
return
}
cred, err := user.GetCredential(state.AccessKey.Get())
if err != nil {
resp.Diagnostics.AddError("Error getting credential", err.Error())
return
}
if err := cred.Delete(); err != nil {
resp.Diagnostics.AddError("Error deleting credential", err.Error())
return
}
}
// * CustomFuncs
// read is a generic read function that can be used by the resource Create, Read and Update functions.
func (r *CredentialResource) read(_ context.Context, planOrState *CredentialModel) (stateRefreshed *CredentialModel, found bool, diags diag.Diagnostics) {
stateRefreshed = planOrState.Copy()
/*
Implement the resource read here
*/
user, err := r.s3Client.GetUser(stateRefreshed.Username.Get())
if err != nil {
diags.AddError("Error getting user", err.Error())
return
}
if _, err := user.GetCredential(stateRefreshed.AccessKey.Get()); err != nil {
diags.AddError("Error getting credential", err.Error())
return
}
// ID is a username and 4 first characters of the access key. (e.g. `username-1234`)
stateRefreshed.ID.Set(fmt.Sprintf("%s-%s", stateRefreshed.Username.Get(), stateRefreshed.AccessKey.Get()[:4]))
return stateRefreshed, true, nil
}