66 "path/filepath"
77 "time"
88
9- "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
109 "github.com/hashicorp/terraform-plugin-framework/path"
1110 "github.com/hashicorp/terraform-plugin-framework/resource"
1211 "github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
@@ -56,18 +55,11 @@ func (r *ResourceSecret) Configure(ctx context.Context, request resource.Configu
5655 "Cannot get meta from provider" ,
5756 "cannot get meta from provider" ,
5857 )
59- }
60-
61- client := m .ScwClient ()
62- if ! ok {
63- response .Diagnostics .AddError (
64- "Unexpected Action Configure Type" ,
65- fmt .Sprintf ("Expected *scw.Client, got: %T. Please report this issue to the provider developers." , request .ProviderData ),
66- )
6758
6859 return
6960 }
7061
62+ client := m .ScwClient ()
7163 r .secretAPI = secret .NewAPI (client )
7264}
7365
@@ -271,22 +263,29 @@ func (r *ResourceSecret) Schema(ctx context.Context, request resource.SchemaRequ
271263 // },
272264 //},
273265 },
274- Blocks : map [string ]schema.Block {
275- "timeouts" : timeouts .Block (ctx ,
276- timeouts.Opts {
277- Create : true ,
278- },
279- ),
280- },
266+ //Blocks: map[string]schema.Block{
267+ // "timeouts": timeouts.Block(ctx,
268+ // timeouts.Opts{
269+ // Create: true,
270+ // CreateDescription: "Timeout to apply on Create",
271+ // Read: true,
272+ // ReadDescription: "Timeout to apply on Read",
273+ // Update: true,
274+ // UpdateDescription: "Timeout to apply on Update",
275+ // Delete: true,
276+ // DeleteDescription: "Timeout to apply on Delete",
277+ // },
278+ // ),
279+ //},
281280 }
282281}
283282
284283type ResourceSecretModel struct {
285- ID types.String `tfsdk:"id"`
286- Name types.String `tfsdk:"name"`
287- Protected types.Bool `tfsdk:"protected"`
288- Type types.String `tfsdk:"type"`
289- Tags types.List `tfsdk:"tags"`
284+ ID types.String `tfsdk:"id"`
285+ Name types.String `tfsdk:"name"`
286+ Protected types.Bool `tfsdk:"protected"`
287+ Type types.String `tfsdk:"type"`
288+ // Tags types.List `tfsdk:"tags"`
290289 Description types.String `tfsdk:"description"`
291290 Path types.String `tfsdk:"path"`
292291 // EphemeralPolicy types.String `tfsdk:"ephemeral_policy"`
@@ -297,7 +296,24 @@ type ResourceSecretModel struct {
297296 CreatedAt types.String `tfsdk:"created_at"`
298297 UpdatedAt types.String `tfsdk:"updated_at"`
299298 // Versions types.List `tfsdk:"versions"`
300- Timeouts timeouts.Value `tfsdk:"timeouts"`
299+ // Timeouts timeouts.Value `tfsdk:"timeouts"`
300+ }
301+
302+ func NewModelFromSecret (s secret.Secret ) * ResourceSecretModel {
303+ return & ResourceSecretModel {
304+ Name : types .StringValue (s .Name ),
305+ Status : types .StringValue (s .Status .String ()),
306+ ProjectID : types .StringValue (s .ProjectID ),
307+ Protected : types .BoolValue (s .Protected ),
308+ Description : types .StringPointerValue (s .Description ),
309+ CreatedAt : types .StringValue (s .CreatedAt .Format (time .RFC3339 )),
310+ UpdatedAt : types .StringValue (s .UpdatedAt .Format (time .RFC3339 )),
311+ Type : types .StringValue (s .Type .String ()),
312+ VersionCount : types .Int32Value (int32 (s .VersionCount )),
313+ ID : types .StringValue (regional .NewIDString (s .Region , s .ID )),
314+ Region : types .StringValue (s .Region .String ()),
315+ Path : types .StringValue (s .Path ),
316+ }
301317}
302318
303319func (r * ResourceSecret ) Create (ctx context.Context , request resource.CreateRequest , response * resource.CreateResponse ) {
@@ -348,6 +364,8 @@ func (r *ResourceSecret) Create(ctx context.Context, request resource.CreateRequ
348364 "error while creating secret" ,
349365 err .Error (),
350366 )
367+
368+ return
351369 }
352370 if apiResponse == nil {
353371 response .Diagnostics .AddError (
@@ -366,19 +384,9 @@ func (r *ResourceSecret) Create(ctx context.Context, request resource.CreateRequ
366384 response .Diagnostics .Append (response .Identity .Set (ctx , identity )... )
367385
368386 // Save data into Terraform state
369- data .ID = types .StringValue (regional .NewIDString (apiResponse .Region , apiResponse .ID ))
370- data .Name = types .StringValue (apiResponse .Name )
371- data .Description = types .StringPointerValue (apiResponse .Description )
372- data .Region = types .StringValue (apiResponse .Region .String ())
373- data .Path = types .StringValue (apiResponse .Path )
374- data .VersionCount = types .Int32Value (int32 (apiResponse .VersionCount ))
375- data .ProjectID = types .StringValue (apiResponse .ProjectID )
376- data .Type = types .StringValue (apiResponse .Type .String ())
377- data .Status = types .StringValue (apiResponse .Status .String ())
378- data .CreatedAt = types .StringValue (apiResponse .CreatedAt .Format (time .RFC3339 ))
379- data .UpdatedAt = types .StringValue (apiResponse .UpdatedAt .Format (time .RFC3339 ))
380- data .Protected = types .BoolValue (apiResponse .Protected )
381- response .Diagnostics .Append (response .State .Set (ctx , & data )... )
387+ dataToSave := NewModelFromSecret (* apiResponse )
388+
389+ response .Diagnostics .Append (response .State .Set (ctx , & dataToSave )... )
382390}
383391
384392func (r * ResourceSecret ) Read (ctx context.Context , request resource.ReadRequest , response * resource.ReadResponse ) {
@@ -394,6 +402,7 @@ func (r *ResourceSecret) Read(ctx context.Context, request resource.ReadRequest,
394402 if err != nil {
395403 if httperrors .Is404 (err ) {
396404 response .State .RemoveResource (ctx )
405+
397406 return
398407 }
399408
@@ -422,17 +431,8 @@ func (r *ResourceSecret) Read(ctx context.Context, request resource.ReadRequest,
422431 // return diag.FromErr(err)
423432 //}
424433
425- data .Name = types .StringValue (secretResponse .Name )
426- data .Description = types .StringPointerValue (secretResponse .Description )
427- data .Region = types .StringValue (secretResponse .Region .String ())
428- data .Path = types .StringValue (secretResponse .Path )
429434 // data.VersionCount = types.Int32Value(int32(versions.TotalCount))
430- data .ProjectID = types .StringValue (secretResponse .ProjectID )
431- data .Type = types .StringValue (secretResponse .Type .String ())
432- data .Status = types .StringValue (secretResponse .Status .String ())
433- data .CreatedAt = types .StringValue (secretResponse .CreatedAt .Format (time .RFC3339 ))
434- data .UpdatedAt = types .StringValue (secretResponse .UpdatedAt .Format (time .RFC3339 ))
435- data .Protected = types .BoolValue (secretResponse .Protected )
435+
436436 //_ = d.Set("ephemeral_policy", flattenEphemeralPolicy(secretResponse.EphemeralPolicy))
437437
438438 //versionsList := make([]map[string]any, 0, len(versions.Versions))
@@ -451,7 +451,8 @@ func (r *ResourceSecret) Read(ctx context.Context, request resource.ReadRequest,
451451 //_ = d.Set("versions", versionsList)
452452
453453 // Save updated data into Terraform state
454- response .Diagnostics .Append (response .State .Set (ctx , & data )... )
454+ dataToSave := NewModelFromSecret (* secretResponse )
455+ response .Diagnostics .Append (response .State .Set (ctx , & dataToSave )... )
455456}
456457
457458func (r * ResourceSecret ) Update (ctx context.Context , request resource.UpdateRequest , response * resource.UpdateResponse ) {
@@ -505,22 +506,14 @@ func (r *ResourceSecret) Update(ctx context.Context, request resource.UpdateRequ
505506 "unable to update secret" ,
506507 err .Error (),
507508 )
509+
510+ return
508511 }
509512
510- plan .Name = types .StringValue (secretResponse .Name )
511- plan .Description = types .StringPointerValue (secretResponse .Description )
512- plan .Region = types .StringValue (secretResponse .Region .String ())
513- plan .Path = types .StringValue (secretResponse .Path )
514- // data.VersionCount = types.Int32Value(int32(versions.TotalCount))
515- plan .ProjectID = types .StringValue (secretResponse .ProjectID )
516- plan .Type = types .StringValue (secretResponse .Type .String ())
517- plan .Status = types .StringValue (secretResponse .Status .String ())
518- plan .CreatedAt = types .StringValue (secretResponse .CreatedAt .Format (time .RFC3339 ))
519- plan .UpdatedAt = types .StringValue (secretResponse .UpdatedAt .Format (time .RFC3339 ))
520- plan .Protected = types .BoolValue (secretResponse .Protected )
513+ dataToSave := NewModelFromSecret (* secretResponse )
521514
522515 // Save updated data into Terraform state
523- response .Diagnostics .Append (response .State .Set (ctx , & plan )... )
516+ response .Diagnostics .Append (response .State .Set (ctx , & dataToSave )... )
524517 }
525518
526519 //if !plan.Protected.Equal(state.Protected) {
0 commit comments