Skip to content

Commit

Permalink
Merge pull request #16 from browningluke/handle_upstream_deletion
Browse files Browse the repository at this point in the history
Handle upstream resource deletion
  • Loading branch information
browningluke authored Jul 22, 2023
2 parents 4e59e51 + 98592f6 commit e7e7f4a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/service/interfaces_vlan_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (r *InterfacesVlanResource) Read(ctx context.Context, req resource.ReadRequ
// Get VLAN from OPNsense core API
vlan, err := r.client.Interfaces.GetVlan(ctx, data.Id.ValueString())
if err != nil {
if err.Error() == "unable to find resource. it may have been deleted upstream" {
tflog.Warn(ctx, fmt.Sprintf("vlan not present in remote, removing from state"))
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to read vlan, got error: %s", err))
return
Expand Down
6 changes: 6 additions & 0 deletions internal/service/route_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (r *RouteResource) Read(ctx context.Context, req resource.ReadRequest, resp
// Get route from OPNsense core API
route, err := r.client.Routes.GetRoute(ctx, data.Id.ValueString())
if err != nil {
if err.Error() == "unable to find resource. it may have been deleted upstream" {
tflog.Warn(ctx, fmt.Sprintf("route not present in remote, removing from state"))
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to read route, got error: %s", err))
return
Expand Down
6 changes: 6 additions & 0 deletions internal/service/unbound_domain_override_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (r *UnboundDomainOverrideResource) Read(ctx context.Context, req resource.R
// Get domain override from OPNsense unbound API
override, err := r.client.Unbound.GetDomainOverride(ctx, data.Id.ValueString())
if err != nil {
if err.Error() == "unable to find resource. it may have been deleted upstream" {
tflog.Warn(ctx, fmt.Sprintf("domain override not present in remote, removing from state"))
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to read domain override, got error: %s", err))
return
Expand Down
6 changes: 6 additions & 0 deletions internal/service/unbound_forward_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (r *UnboundForwardResource) Read(ctx context.Context, req resource.ReadRequ
// Get forward from OPNsense unbound API
forward, err := r.client.Unbound.GetForward(ctx, data.Id.ValueString())
if err != nil {
if err.Error() == "unable to find resource. it may have been deleted upstream" {
tflog.Warn(ctx, fmt.Sprintf("forward not present in remote, removing from state"))
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to read forward, got error: %s", err))
return
Expand Down
6 changes: 6 additions & 0 deletions internal/service/unbound_host_alias_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ func (r *UnboundHostAliasResource) Read(ctx context.Context, req resource.ReadRe
// Get host alias from OPNsense unbound API
alias, err := r.client.Unbound.GetHostAlias(ctx, data.Id.ValueString())
if err != nil {
if err.Error() == "unable to find resource. it may have been deleted upstream" {
tflog.Warn(ctx, fmt.Sprintf("host alias not present in remote, removing from state"))
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to read parse alias, got error: %s", err))
return
Expand Down
6 changes: 6 additions & 0 deletions internal/service/unbound_host_override_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (r *UnboundHostOverrideResource) Read(ctx context.Context, req resource.Rea
// Get host override from OPNsense unbound API
override, err := r.client.Unbound.GetHostOverride(ctx, data.Id.ValueString())
if err != nil {
if err.Error() == "unable to find resource. it may have been deleted upstream" {
tflog.Warn(ctx, fmt.Sprintf("host override not present in remote, removing from state"))
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to read host override, got error: %s", err))
return
Expand Down

0 comments on commit e7e7f4a

Please sign in to comment.