diff --git a/internal/provider/instance_client.go b/internal/provider/instance_client.go index 3748a24..2d39df4 100644 --- a/internal/provider/instance_client.go +++ b/internal/provider/instance_client.go @@ -92,6 +92,30 @@ func (ic *InstanceClient) launch() error { return nil } +// TODO consider using "delete --kill" +func (ic *InstanceClient) terminate() error { + tflog.Info(ic.ctx, "Terminating AEM instance(s)") + + // TODO use systemd service instead and stop it + textOut, err := ic.cl.RunShellWithEnv(fmt.Sprintf("cd %s && sh aemw instance terminate", ic.DataDir())) + if err != nil { + return fmt.Errorf("unable to terminate AEM instance: %w", err) + } + + textStr := string(textOut) // TODO how about streaming it line by line to tflog ;) + tflog.Info(ic.ctx, "Terminated AEM instance(s)") + tflog.Info(ic.ctx, textStr) // TODO consider checking 'changed' flag here if needed + + return nil +} + +func (ic *InstanceClient) deleteDataDir() error { + if _, err := ic.cl.RunShell(fmt.Sprintf("rm -fr %s", ic.DataDir())); err != nil { + return fmt.Errorf("cannot delete AEM data directory: %w", err) + } + return nil +} + type InstanceStatus struct { Data struct { Instances []struct { diff --git a/internal/provider/instance_resource.go b/internal/provider/instance_resource.go index 839cbda..8c2d260 100644 --- a/internal/provider/instance_resource.go +++ b/internal/provider/instance_resource.go @@ -299,7 +299,7 @@ func (r *InstanceResource) Read(ctx context.Context, req resource.ReadRequest, r ic, err := r.client(ctx, model, time.Second*15) if err != nil { - resp.Diagnostics.AddWarning("Unable to connect to AEM instance", fmt.Sprintf("%s", err)) + tflog.Info(ctx, "Cannot read AEM instance state as it is not possible to connect at the moment. Possible reasons: machine IP change is in progress, machine is not yet created or booting up, etc.") } else { defer func(ic *InstanceClient) { err := ic.Close() @@ -322,15 +322,39 @@ func (r *InstanceResource) Read(ctx context.Context, req resource.ReadRequest, r } func (r *InstanceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data InstanceResourceModel + model := r.newModel() // Read Terraform prior state data into the model - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &model)...) if resp.Diagnostics.HasError() { return } - // TODO ... delete the resource + tflog.Info(ctx, "Started deleting AEM instance resource") + + ic, err := r.client(ctx, model, time.Minute*5) + if err != nil { + resp.Diagnostics.AddError("Unable to connect to AEM instance", fmt.Sprintf("%s", err)) + return + } + defer func(ic *InstanceClient) { + err := ic.Close() + if err != nil { + resp.Diagnostics.AddWarning("Unable to disconnect from AEM instance", fmt.Sprintf("%s", err)) + } + }(ic) + + if err := ic.terminate(); err != nil { + resp.Diagnostics.AddError("Unable to terminate AEM instance", fmt.Sprintf("%s", err)) + return + } + + if err := ic.deleteDataDir(); err != nil { + resp.Diagnostics.AddError("Unable to delete AEM data directory", fmt.Sprintf("%s", err)) + return + } + + tflog.Info(ctx, "Finished deleting AEM instance resource") } func (r *InstanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {