Skip to content

Commit

Permalink
Merge pull request #2 from dalet-oss/timeouts
Browse files Browse the repository at this point in the history
  Timeouts
  • Loading branch information
gxben authored Dec 14, 2023
2 parents 3b45171 + a4c8f19 commit dda97cc
Show file tree
Hide file tree
Showing 18 changed files with 685 additions and 414 deletions.
67 changes: 43 additions & 24 deletions internal/provider/kowabunga_adapter_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/dalet-oss/kowabunga-api/sdk/go/client/subnet"
"github.com/dalet-oss/kowabunga-api/sdk/go/models"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -40,20 +41,19 @@ type AdapterResource struct {
}

type AdapterResourceModel struct {
//anonymous field
ResourceBaseModel

Name types.String `tfsdk:"name"`
Desc types.String `tfsdk:"desc"`
Subnet types.String `tfsdk:"subnet"`
MAC types.String `tfsdk:"hwaddress"`
Addresses types.List `tfsdk:"addresses"`
Assign types.Bool `tfsdk:"assign"`
Reserved types.Bool `tfsdk:"reserved"`
CIDR types.String `tfsdk:"cidr"`
Netmask types.String `tfsdk:"netmask"`
NetmaskBitSize types.Int64 `tfsdk:"netmask_bitsize"`
Gateway types.String `tfsdk:"gateway"`
ID types.String `tfsdk:"id"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
Name types.String `tfsdk:"name"`
Desc types.String `tfsdk:"desc"`
Subnet types.String `tfsdk:"subnet"`
MAC types.String `tfsdk:"hwaddress"`
Addresses types.List `tfsdk:"addresses"`
Assign types.Bool `tfsdk:"assign"`
Reserved types.Bool `tfsdk:"reserved"`
CIDR types.String `tfsdk:"cidr"`
Netmask types.String `tfsdk:"netmask"`
NetmaskBitSize types.Int64 `tfsdk:"netmask_bitsize"`
Gateway types.String `tfsdk:"gateway"`
}

func (r *AdapterResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -214,7 +214,12 @@ func (r *AdapterResource) Create(ctx context.Context, req resource.CreateRequest
return
}

ctx, createTimeout, cancel := data.SetCreateTimeout(ctx, resp, DefaultCreateTimeout)
timeout, diags := data.Timeouts.Create(ctx, DefaultCreateTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
Expand All @@ -229,7 +234,7 @@ func (r *AdapterResource) Create(ctx context.Context, req resource.CreateRequest

// create a new adapter
cfg := adapterResourceToModel(data)
params := subnet.NewCreateAdapterParams().WithSubnetID(subnetId).WithBody(&cfg).WithTimeout(createTimeout)
params := subnet.NewCreateAdapterParams().WithSubnetID(subnetId).WithBody(&cfg).WithTimeout(timeout)
if data.Assign.ValueBool() && len(cfg.Addresses) == 0 {
params = params.WithAssignIP(data.Assign.ValueBoolPointer())
}
Expand Down Expand Up @@ -259,27 +264,30 @@ func (r *AdapterResource) Read(ctx context.Context, req resource.ReadRequest, re
return
}

ctx, readTimeout, cancel := data.SetReadTimeout(ctx, resp, DefaultReadTimeout)
timeout, diags := data.Timeouts.Read(ctx, DefaultReadTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
defer r.Data.Mutex.Unlock()

params := adapter.NewGetAdapterParams().WithAdapterID(data.ID.ValueString()).WithTimeout(readTimeout)
params := adapter.NewGetAdapterParams().WithAdapterID(data.ID.ValueString()).WithTimeout(timeout)
obj, err := r.Data.K.Adapter.GetAdapter(params, nil)
if err != nil {
errorReadGeneric(resp, err)
return
}

adapterModelToResource(obj.Payload, data)

err = r.GetSubnetData(data)
if err != nil {
errorReadGeneric(resp, err)
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

Expand All @@ -290,14 +298,19 @@ func (r *AdapterResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

ctx, updateTimeout, cancel := data.SetUpdateTimeout(ctx, resp, DefaultUpdateTimeout)
timeout, diags := data.Timeouts.Update(ctx, DefaultUpdateTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
defer r.Data.Mutex.Unlock()

cfg := adapterResourceToModel(data)
params := adapter.NewUpdateAdapterParams().WithAdapterID(data.ID.ValueString()).WithBody(&cfg).WithTimeout(updateTimeout)
params := adapter.NewUpdateAdapterParams().WithAdapterID(data.ID.ValueString()).WithBody(&cfg).WithTimeout(timeout)
_, err := r.Data.K.Adapter.UpdateAdapter(params, nil)
if err != nil {
errorUpdateGeneric(resp, err)
Expand All @@ -313,16 +326,22 @@ func (r *AdapterResource) Delete(ctx context.Context, req resource.DeleteRequest
if resp.Diagnostics.HasError() {
return
}
_, deleteTimeout, cancel := data.SetDeleteTimeout(ctx, resp, DefaultDeleteTimeout)
timeout, diags := data.Timeouts.Delete(ctx, DefaultDeleteTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
defer r.Data.Mutex.Unlock()

params := adapter.NewDeleteAdapterParams().WithAdapterID(data.ID.ValueString()).WithTimeout(deleteTimeout)
params := adapter.NewDeleteAdapterParams().WithAdapterID(data.ID.ValueString()).WithTimeout(timeout)
_, err := r.Data.K.Adapter.DeleteAdapter(params, nil)
if err != nil {
errorDeleteGeneric(resp, err)
return
}
tflog.Trace(ctx, "Deleted "+params.AdapterID)
}
54 changes: 36 additions & 18 deletions internal/provider/kowabunga_dns_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dalet-oss/kowabunga-api/sdk/go/client/record"
"github.com/dalet-oss/kowabunga-api/sdk/go/models"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -32,13 +33,12 @@ type DnsRecordResource struct {
}

type DnsRecordResourceModel struct {
//anonymous field
ResourceBaseModel

Name types.String `tfsdk:"name"`
Desc types.String `tfsdk:"desc"`
Project types.String `tfsdk:"project"`
Addresses types.List `tfsdk:"addresses"`
ID types.String `tfsdk:"id"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
Name types.String `tfsdk:"name"`
Desc types.String `tfsdk:"desc"`
Project types.String `tfsdk:"project"`
Addresses types.List `tfsdk:"addresses"`
}

func (r *DnsRecordResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -100,7 +100,12 @@ func (r *DnsRecordResource) Create(ctx context.Context, req resource.CreateReque
return
}

ctx, createTimeout, cancel := data.SetCreateTimeout(ctx, resp, DefaultCreateTimeout)
timeout, diags := data.Timeouts.Create(ctx, DefaultCreateTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
Expand All @@ -112,16 +117,14 @@ func (r *DnsRecordResource) Create(ctx context.Context, req resource.CreateReque
errorCreateGeneric(resp, err)
return
}

// create a new record
cfg := recordResourceToModel(data)
params := project.NewCreateProjectDNSRecordParams().WithProjectID(projectId).WithBody(&cfg).WithTimeout(createTimeout)
params := project.NewCreateProjectDNSRecordParams().WithProjectID(projectId).WithBody(&cfg).WithTimeout(timeout)
obj, err := r.Data.K.Project.CreateProjectDNSRecord(params, nil)
if err != nil {
errorCreateGeneric(resp, err)
return
}

data.ID = types.StringValue(obj.Payload.ID)
tflog.Trace(ctx, "created DNS record resource")
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand All @@ -134,13 +137,18 @@ func (r *DnsRecordResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

ctx, readTimeout, cancel := data.SetReadTimeout(ctx, resp, DefaultReadTimeout)
timeout, diags := data.Timeouts.Read(ctx, DefaultReadTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
defer r.Data.Mutex.Unlock()

params := record.NewGetDNSRecordParams().WithRecordID(data.ID.ValueString()).WithTimeout(readTimeout)
params := record.NewGetDNSRecordParams().WithRecordID(data.ID.ValueString()).WithTimeout(timeout)
obj, err := r.Data.K.Record.GetDNSRecord(params, nil)
if err != nil {
tflog.Trace(ctx, err.Error())
Expand All @@ -159,20 +167,24 @@ func (r *DnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
return
}

ctx, updateTimeout, cancel := data.SetUpdateTimeout(ctx, resp, DefaultUpdateTimeout)
timeout, diags := data.Timeouts.Update(ctx, DefaultUpdateTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
defer r.Data.Mutex.Unlock()

cfg := recordResourceToModel(data)
params := record.NewUpdateDNSRecordParams().WithRecordID(data.ID.ValueString()).WithBody(&cfg).WithTimeout(updateTimeout)
params := record.NewUpdateDNSRecordParams().WithRecordID(data.ID.ValueString()).WithBody(&cfg).WithTimeout(timeout)
_, err := r.Data.K.Record.UpdateDNSRecord(params, nil)
if err != nil {
errorUpdateGeneric(resp, err)
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

Expand All @@ -182,16 +194,22 @@ func (r *DnsRecordResource) Delete(ctx context.Context, req resource.DeleteReque
if resp.Diagnostics.HasError() {
return
}
_, deleteTimeout, cancel := data.SetDeleteTimeout(ctx, resp, DefaultDeleteTimeout)
timeout, diags := data.Timeouts.Delete(ctx, DefaultDeleteTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
_, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

r.Data.Mutex.Lock()
defer r.Data.Mutex.Unlock()

params := record.NewDeleteDNSRecordParams().WithRecordID(data.ID.ValueString()).WithTimeout(deleteTimeout)
params := record.NewDeleteDNSRecordParams().WithRecordID(data.ID.ValueString()).WithTimeout(timeout)
_, err := r.Data.K.Record.DeleteDNSRecord(params, nil)
if err != nil {
errorDeleteGeneric(resp, err)
return
}
tflog.Trace(ctx, "Deleted "+params.RecordID)
}
Loading

0 comments on commit dda97cc

Please sign in to comment.