Skip to content

Commit

Permalink
Create and read
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Oct 29, 2023
1 parent 7fbda5c commit 2fcb09a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
3 changes: 1 addition & 2 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func (c Client) Connect() error {
return c.connection.Connect()
}

func (c Client) ConnectWithRetry(callback func()) error {
timeout := time.Minute * 5
func (c Client) ConnectWithRetry(timeout time.Duration, callback func()) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
Expand Down
43 changes: 23 additions & 20 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/wttech/terraform-provider-aem/internal/client"
"time"
)

// Ensure provider defined types fully satisfy framework interfaces.
Expand Down Expand Up @@ -179,7 +180,7 @@ func (r *InstanceResource) Create(ctx context.Context, req resource.CreateReques

tflog.Info(ctx, "Creating AEM instance resource")

ic, err := r.Client(ctx, model)
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
Expand Down Expand Up @@ -231,7 +232,9 @@ func (r *InstanceResource) Create(ctx context.Context, req resource.CreateReques
}

func (r *InstanceResource) defaultModel() InstanceResourceModel {
return InstanceResourceModel{}
model := InstanceResourceModel{}
model.Instances = types.ListValueMust(types.ObjectType{AttrTypes: InstanceStatusItemModel{}.attrTypes()}, []attr.Value{})
return model
}

func (r *InstanceResource) fillModelWithStatus(ctx context.Context, model *InstanceResourceModel, status InstanceStatus) diag.Diagnostics {
Expand Down Expand Up @@ -269,26 +272,26 @@ func (r *InstanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}

ic, err := r.Client(ctx, model)
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, err := r.Client(ctx, model, time.Second*15)
if err == nil {
resp.Diagnostics.AddWarning("Unable to connect to AEM instance", fmt.Sprintf("%s", err))
} else {
defer func(ic *InstanceClient) {
err := ic.Close()
if err != nil {
resp.Diagnostics.AddWarning("Unable to disconnect from AEM instance", fmt.Sprintf("%s", err))
}
}(ic)

status, err := ic.ReadStatus()
if err != nil { //
resp.Diagnostics.AddError("Unable to read AEM instance data", fmt.Sprintf("%s", err))
return
}
}(ic)

status, err := ic.ReadStatus()
if err != nil { //
resp.Diagnostics.AddError("Unable to read AEM instance data", fmt.Sprintf("%s", err))
return
resp.Diagnostics.Append(r.fillModelWithStatus(ctx, &model, status)...)
}

resp.Diagnostics.Append(r.fillModelWithStatus(ctx, &model, status)...)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}
Expand Down Expand Up @@ -324,7 +327,7 @@ func (r *InstanceResource) ImportState(ctx context.Context, req resource.ImportS
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *InstanceResource) Client(ctx context.Context, data InstanceResourceModel) (*InstanceClient, error) {
func (r *InstanceResource) Client(ctx context.Context, data InstanceResourceModel, timeout time.Duration) (*InstanceClient, error) {
tflog.Info(ctx, "Connecting to AEM instance machine")

typeName := data.Client.Type.ValueString()
Expand All @@ -336,7 +339,7 @@ func (r *InstanceResource) Client(ctx context.Context, data InstanceResourceMode
return nil, err
}

if err := cl.ConnectWithRetry(func() { tflog.Info(ctx, "Awaiting connection to AEM instance machine") }); err != nil {
if err := cl.ConnectWithRetry(timeout, func() { tflog.Info(ctx, "Awaiting connection to AEM instance machine") }); err != nil {
return nil, err
}

Expand Down

0 comments on commit 2fcb09a

Please sign in to comment.