Skip to content

Commit

Permalink
Merge pull request #13 from wttech/instance-update
Browse files Browse the repository at this point in the history
Instance create/update/destroy works
  • Loading branch information
krystian-panek-vmltech authored Nov 3, 2023
2 parents 426ec5b + 238f280 commit 1ba3abc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
24 changes: 24 additions & 0 deletions internal/provider/instance_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ func (ic *InstanceClient) launch() error {
return nil
}

// TODO consider using "delete --kill"

Check failure on line 95 in internal/provider/instance_client.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
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 {
Expand Down
32 changes: 28 additions & 4 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit 1ba3abc

Please sign in to comment.