Skip to content

Commit

Permalink
fix: handle deletion properly (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
miton18 authored Jan 5, 2024
1 parent 164bb21 commit 467411f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
4 changes: 4 additions & 0 deletions pkg/resources/addon/resource_addon_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions pkg/resources/nodejs/resource_nodejs_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...)
Expand All @@ -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...)
Expand Down
44 changes: 21 additions & 23 deletions pkg/resources/php/resource_php_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/resources/postgresql/resource_postgresql_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 467411f

Please sign in to comment.