From 467411facf87973d8854588f2c14bd3aba4dc9a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Collignon-Ducret=20R=C3=A9mi?= Date: Fri, 5 Jan 2024 10:38:11 +0100 Subject: [PATCH] fix: handle deletion properly (#52) --- pkg/resources/addon/resource_addon_crud.go | 4 ++ pkg/resources/nodejs/resource_nodejs_crud.go | 18 ++++++-- pkg/resources/php/resource_php_crud.go | 44 +++++++++---------- .../postgresql/resource_postgresql_crud.go | 4 ++ 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/pkg/resources/addon/resource_addon_crud.go b/pkg/resources/addon/resource_addon_crud.go index bf743aa..56bf55d 100644 --- a/pkg/resources/addon/resource_addon_crud.go +++ b/pkg/resources/addon/resource_addon_crud.go @@ -107,6 +107,10 @@ func (r *ResourceAddon) Read(ctx context.Context, req resource.ReadRequest, resp } addonRes := tmp.GetAddon(ctx, r.cc, r.org, ad.ID.ValueString()) + if addonRes.IsNotFoundError() { + req.State.RemoveResource(ctx) + return + } if addonRes.HasError() { resp.Diagnostics.AddError("failed to get addon", addonRes.Error().Error()) return diff --git a/pkg/resources/nodejs/resource_nodejs_crud.go b/pkg/resources/nodejs/resource_nodejs_crud.go index c2c9978..df2e3bb 100644 --- a/pkg/resources/nodejs/resource_nodejs_crud.go +++ b/pkg/resources/nodejs/resource_nodejs_crud.go @@ -5,7 +5,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" - "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" "go.clever-cloud.com/terraform-provider/pkg" "go.clever-cloud.com/terraform-provider/pkg/application" @@ -116,7 +115,7 @@ func (r *ResourceNodeJS) Read(ctx context.Context, req resource.ReadRequest, res return } - appRes := tmp.GetApp(ctx, r.cc, r.org, app.ID.ValueString()) + /*appRes := tmp.GetApp(ctx, r.cc, r.org, app.ID.ValueString()) if appRes.IsNotFoundError() { diags = resp.State.SetAttribute(ctx, path.Root("id"), types.StringUnknown) resp.Diagnostics.Append(diags...) @@ -129,8 +128,19 @@ func (r *ResourceNodeJS) Read(ctx context.Context, req resource.ReadRequest, res } appNode := appRes.Payload() - app.DeployURL = pkg.FromStr(appNode.DeployURL) - app.VHost = pkg.FromStr(appNode.Vhosts[0].Fqdn) + */ + appRes, diags := application.ReadApp(ctx, r.cc, r.org, app.ID.ValueString()) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + if appRes.AppIsDeleted { + resp.State.RemoveResource(ctx) + return + } + + app.DeployURL = pkg.FromStr(appRes.App.DeployURL) + app.VHost = pkg.FromStr(appRes.App.Vhosts[0].Fqdn) diags = resp.State.Set(ctx, app) resp.Diagnostics.Append(diags...) diff --git a/pkg/resources/php/resource_php_crud.go b/pkg/resources/php/resource_php_crud.go index 80d9e6f..4c6f7c0 100644 --- a/pkg/resources/php/resource_php_crud.go +++ b/pkg/resources/php/resource_php_crud.go @@ -108,34 +108,32 @@ func (r *ResourcePHP) Read(ctx context.Context, req resource.ReadRequest, resp * return } - appRes := tmp.GetApp(ctx, r.cc, r.org, state.ID.ValueString()) - if appRes.IsNotFoundError() { + appPHP, diags := application.ReadApp(ctx, r.cc, r.org, state.ID.ValueString()) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + if appPHP.AppIsDeleted { resp.State.RemoveResource(ctx) - if resp.Diagnostics.HasError() { - return - } - } - if appRes.HasError() { - resp.Diagnostics.AddError("failed to get app", appRes.Error().Error()) - } - - appPHP := appRes.Payload() - state.Name = pkg.FromStr(appPHP.Name) - state.Description = pkg.FromStr(appPHP.Description) - state.MinInstanceCount = pkg.FromI(int64(appPHP.Instance.MinInstances)) - state.MaxInstanceCount = pkg.FromI(int64(appPHP.Instance.MaxInstances)) - state.SmallestFlavor = pkg.FromStr(appPHP.Instance.MinFlavor.Name) - state.BiggestFlavor = pkg.FromStr(appPHP.Instance.MaxFlavor.Name) - state.Region = pkg.FromStr(appPHP.Zone) - state.DeployURL = pkg.FromStr(appPHP.DeployURL) - - if appPHP.SeparateBuild { - state.BuildFlavor = pkg.FromStr(appPHP.BuildFlavor.Name) + return + } + + state.Name = pkg.FromStr(appPHP.App.Name) + state.Description = pkg.FromStr(appPHP.App.Description) + state.MinInstanceCount = pkg.FromI(int64(appPHP.App.Instance.MinInstances)) + state.MaxInstanceCount = pkg.FromI(int64(appPHP.App.Instance.MaxInstances)) + state.SmallestFlavor = pkg.FromStr(appPHP.App.Instance.MinFlavor.Name) + state.BiggestFlavor = pkg.FromStr(appPHP.App.Instance.MaxFlavor.Name) + state.Region = pkg.FromStr(appPHP.App.Zone) + state.DeployURL = pkg.FromStr(appPHP.App.DeployURL) + + if appPHP.App.SeparateBuild { + state.BuildFlavor = pkg.FromStr(appPHP.App.BuildFlavor.Name) } else { state.BuildFlavor = types.StringNull() } - vhosts := pkg.Map(appPHP.Vhosts, func(vhost tmp.Vhost) string { + vhosts := pkg.Map(appPHP.App.Vhosts, func(vhost tmp.Vhost) string { return vhost.Fqdn }) hasDefaultVHost := pkg.HasSome(vhosts, func(vhost string) bool { diff --git a/pkg/resources/postgresql/resource_postgresql_crud.go b/pkg/resources/postgresql/resource_postgresql_crud.go index c7dbfd1..881c124 100644 --- a/pkg/resources/postgresql/resource_postgresql_crud.go +++ b/pkg/resources/postgresql/resource_postgresql_crud.go @@ -116,6 +116,10 @@ func (r *ResourcePostgreSQL) Read(ctx context.Context, req resource.ReadRequest, return } } + if addonPGRes.IsNotFoundError() { + resp.State.RemoveResource(ctx) + return + } if addonPGRes.HasError() { resp.Diagnostics.AddError("failed to get Postgres resource", addonPGRes.Error().Error()) }