Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/alb v0.12.1
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1
github.com/stackitcloud/stackit-sdk-go/services/edge v0.7.0
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5
github.com/stackitcloud/stackit-sdk-go/services/kms v1.6.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
github.com/stackitcloud/stackit-sdk-go/core v0.23.0 h1:zPrOhf3Xe47rKRs1fg/AqKYUiJJRYjdcv+3qsS50mEs=
github.com/stackitcloud/stackit-sdk-go/core v0.23.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
github.com/stackitcloud/stackit-sdk-go/core v0.24.0 h1:kHCcezCJ5OGSP7RRuGOxD5rF2wejpkEiRr/OdvNcuPQ=
github.com/stackitcloud/stackit-sdk-go/core v0.24.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.12.1 h1:RKaxAymxlyxxE0Gta3yRuQWf07LnlcX+mfGnVB96NHA=
Expand All @@ -165,8 +163,8 @@ github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0 h1:iRJK2d3I2QqWp8hqh
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0/go.mod h1:URWWMIbvq4YgWdGYCbccr3eat4Y+0qRpufZsEAsvoLM=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1 h1:VfszhFq/Snsd0LnflS8PbM0d9cG98hOFpamfjlcTnDQ=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1/go.mod h1:gBv6YkB3Xf3c0ZXg2GwtWY8zExwGPF/Ag114XiiERxg=
github.com/stackitcloud/stackit-sdk-go/services/edge v0.7.0 h1:DNBiHWQEWXHSbaZBmnXb+CaPXX1uVsSfp4FTHoH4wrM=
github.com/stackitcloud/stackit-sdk-go/services/edge v0.7.0/go.mod h1:CfqSEGCW0b5JlijCwtUT1kfjThmQ5jXX47TWrdD5rTU=
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0 h1:lsOTEvItzrUerCzUodyyOy6pZ4QiSzwID7U20ZvZvHw=
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0/go.mod h1:CfqSEGCW0b5JlijCwtUT1kfjThmQ5jXX47TWrdD5rTU=
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0 h1:siKBCYl7BwR5S25Y4SwUAX6eFTXSCkQ+8JIQEqyKIXo=
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0/go.mod h1:QKy74hhLVZKXItw3y+elF8s9QezFqpEsAUmaqvNDzYs=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5 h1:W57+XRa8wTLsi5CV9Tqa7mGgt/PvlRM//RurXSmvII8=
Expand Down
10 changes: 5 additions & 5 deletions stackit/internal/services/edgecloud/edge_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/hashicorp/terraform-plugin-testing/terraform"
coreConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/edge"
"github.com/stackitcloud/stackit-sdk-go/services/edge/wait"
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
"github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
)
Expand Down Expand Up @@ -357,7 +357,7 @@ func testAccCheckEdgeCloudInstanceDestroy(s *terraform.State) error {
}
projectId, region, instanceId := idParts[0], idParts[1], idParts[2]

_, err := client.GetInstance(ctx, projectId, region, instanceId).Execute()
_, err := client.DefaultAPI.GetInstance(ctx, projectId, region, instanceId).Execute()
if err == nil {
return fmt.Errorf("edge instance %q still exists", instanceId)
}
Expand All @@ -366,11 +366,11 @@ func testAccCheckEdgeCloudInstanceDestroy(s *terraform.State) error {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
if !ok || oapiErr.StatusCode != http.StatusNotFound {
err := client.DeleteInstance(ctx, projectId, region, instanceId).Execute()
err := client.DefaultAPI.DeleteInstance(ctx, projectId, region, instanceId).Execute()
if err != nil {
return fmt.Errorf("deleting instance %s during CheckDestroy: %w", instanceId, err)
}
_, err = wait.DeleteInstanceWaitHandler(ctx, client, projectId, region, instanceId).WaitWithContext(ctx)
_, err = wait.DeleteInstanceWaitHandler(ctx, client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("waiting for instance deletion %s during CheckDestroy: %w", instanceId, err)
}
Expand Down
41 changes: 18 additions & 23 deletions stackit/internal/services/edgecloud/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/edge"
edgewait "github.com/stackitcloud/stackit-sdk-go/services/edge/wait"
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
edgewait "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api/wait"
"github.com/stackitcloud/stackit-sdk-go/services/serviceenablement"
enablementWait "github.com/stackitcloud/stackit-sdk-go/services/serviceenablement/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
Expand Down Expand Up @@ -264,7 +264,7 @@ func (i *instanceResource) Create(ctx context.Context, req resource.CreateReques

tflog.Info(ctx, "Creating new Edge Cloud instance")
payload := toCreatePayload(&model)
createResp, err := i.client.CreateInstance(ctx, projectId, region).CreateInstancePayload(payload).Execute()
createResp, err := i.client.DefaultAPI.CreateInstance(ctx, projectId, region).CreateInstancePayload(payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
return
Expand All @@ -276,23 +276,18 @@ func (i *instanceResource) Create(ctx context.Context, req resource.CreateReques
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "API returned nil response")
return
}
if createResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", "API returned nil Instance ID")
return
}

edgeCloudInstanceId := *createResp.Id
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
"project_id": projectId,
"instance_id": edgeCloudInstanceId,
"instance_id": createResp.Id,
"region": region,
})
if resp.Diagnostics.HasError() {
return
}

waitResp, err := edgewait.CreateOrUpdateInstanceWaitHandler(ctx, i.client, projectId, region, edgeCloudInstanceId).WaitWithContext(ctx)
waitResp, err := edgewait.CreateOrUpdateInstanceWaitHandler(ctx, i.client.DefaultAPI, projectId, region, createResp.Id).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance waiting: %v", err))
return
Expand Down Expand Up @@ -329,7 +324,7 @@ func (i *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)

edgeCloudInstanceResp, err := i.client.GetInstance(ctx, projectId, region, instanceId).Execute()
edgeCloudInstanceResp, err := i.client.DefaultAPI.GetInstance(ctx, projectId, region, instanceId).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
Expand Down Expand Up @@ -372,15 +367,15 @@ func (i *instanceResource) Update(ctx context.Context, req resource.UpdateReques

tflog.Info(ctx, "Updating Edge Cloud instance", map[string]any{"instance_id": instanceId})
payload := toUpdatePayload(&model)
err := i.client.UpdateInstance(ctx, projectId, region, instanceId).UpdateInstancePayload(payload).Execute()
err := i.client.DefaultAPI.UpdateInstance(ctx, projectId, region, instanceId).UpdateInstancePayload(payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Calling API: %v", err))
return
}

ctx = core.LogResponse(ctx)

waitResp, err := edgewait.CreateOrUpdateInstanceWaitHandler(ctx, i.client, projectId, region, instanceId).WaitWithContext(ctx)
waitResp, err := edgewait.CreateOrUpdateInstanceWaitHandler(ctx, i.client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance waiting: %v", err))
return
Expand Down Expand Up @@ -414,15 +409,15 @@ func (i *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)

err := i.client.DeleteInstance(ctx, projectId, region, instanceId).Execute()
err := i.client.DefaultAPI.DeleteInstance(ctx, projectId, region, instanceId).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err))
return
}

ctx = core.LogResponse(ctx)

_, err = edgewait.DeleteInstanceWaitHandler(ctx, i.client, projectId, region, instanceId).WaitWithContext(ctx)
_, err = edgewait.DeleteInstanceWaitHandler(ctx, i.client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err))
return
Expand Down Expand Up @@ -461,8 +456,8 @@ func mapFields(resp *edge.Instance, model *Model) error {
var instanceId string
if model.InstanceId.ValueString() != "" {
instanceId = model.InstanceId.ValueString()
} else if resp.Id != nil {
instanceId = *resp.Id
} else if resp.Id != "" {
instanceId = resp.Id
}
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.Region.ValueString(), instanceId)
model.InstanceId = types.StringValue(instanceId)
Expand All @@ -471,10 +466,10 @@ func mapFields(resp *edge.Instance, model *Model) error {
} else {
model.Created = types.StringNull()
}
model.FrontendUrl = types.StringPointerValue(resp.FrontendUrl)
model.DisplayName = types.StringPointerValue(resp.DisplayName)
model.PlanID = types.StringPointerValue(resp.PlanId)
model.Status = types.StringValue(string(*resp.Status))
model.FrontendUrl = types.StringValue(resp.FrontendUrl)
model.DisplayName = types.StringValue(resp.DisplayName)
model.PlanID = types.StringValue(resp.PlanId)
model.Status = types.StringValue(resp.Status)

if resp.Description != nil {
model.Description = types.StringValue(*resp.Description)
Expand All @@ -488,9 +483,9 @@ func mapFields(resp *edge.Instance, model *Model) error {
// toCreatePayload creates the payload for creating an Edge Cloud instance.
func toCreatePayload(model *Model) edge.CreateInstancePayload {
return edge.CreateInstancePayload{
DisplayName: model.DisplayName.ValueStringPointer(),
DisplayName: model.DisplayName.ValueString(),
Description: model.Description.ValueStringPointer(),
PlanId: model.PlanID.ValueStringPointer(),
PlanId: model.PlanID.ValueString(),
}
}

Expand Down
35 changes: 17 additions & 18 deletions stackit/internal/services/edgecloud/instance/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/edge"
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
)

func TestMapFields(t *testing.T) {
Expand All @@ -25,13 +24,13 @@ func TestMapFields(t *testing.T) {
{
"all_parameter_set",
&edge.Instance{
Id: new("iid-123"),
Created: &testTime,
DisplayName: new("test-instance"),
Id: "iid-123",
Created: testTime,
DisplayName: "test-instance",
Description: new("Test description"),
PlanId: new(uuidString),
Status: utils.Ptr(edge.InstanceStatus("CREATING")),
FrontendUrl: new("https://iid-123.example.com"),
PlanId: uuidString,
Status: "CREATING",
FrontendUrl: "https://iid-123.example.com",
},
&Model{
ProjectId: types.StringValue(uuidString),
Expand All @@ -54,13 +53,13 @@ func TestMapFields(t *testing.T) {
{
"empty_description",
&edge.Instance{
Id: new("iid-123"),
Created: &testTime,
DisplayName: new("test-instance"),
Id: "iid-123",
Created: testTime,
DisplayName: "test-instance",
Description: new(""),
PlanId: new(uuidString),
Status: utils.Ptr(edge.InstanceStatus("ACTIVE")),
FrontendUrl: new("https://iid-123.example.com"),
PlanId: uuidString,
Status: "ACTIVE",
FrontendUrl: "https://iid-123.example.com",
},
&Model{
ProjectId: types.StringValue(uuidString),
Expand Down Expand Up @@ -132,9 +131,9 @@ func TestToCreatePayload(t *testing.T) {
PlanID: types.StringValue(uuidString),
},
edge.CreateInstancePayload{
DisplayName: new("new-instance"),
DisplayName: "new-instance",
Description: new("A new test instance"),
PlanId: new(uuidString),
PlanId: uuidString,
},
true,
},
Expand All @@ -146,9 +145,9 @@ func TestToCreatePayload(t *testing.T) {
PlanID: types.StringValue(uuidString),
},
edge.CreateInstancePayload{
DisplayName: new("new-instance"),
DisplayName: "new-instance",
Description: nil,
PlanId: new(uuidString),
PlanId: uuidString,
},
true,
},
Expand Down
57 changes: 13 additions & 44 deletions stackit/internal/services/edgecloud/instances/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/edge"
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
Expand Down Expand Up @@ -166,7 +166,7 @@ func (d *instancesDataSource) Read(ctx context.Context, req datasource.ReadReque
ctx = tflog.SetField(ctx, "region", region)

// Fetch all instances for the project and region
instancesResp, err := d.client.ListInstances(ctx, projectId, region).Execute()
instancesResp, err := d.client.DefaultAPI.ListInstances(ctx, projectId, region).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down Expand Up @@ -216,20 +216,11 @@ func (d *instancesDataSource) Read(ctx context.Context, req datasource.ReadReque
}

// buildInstancesList constructs a list of instance attributes
func buildInstancesList(ctx context.Context, instances edge.InstanceListGetInstancesAttributeType, region string, diags *diag.Diagnostics) []attr.Value {
func buildInstancesList(_ context.Context, instances []edge.Instance, region string, diags *diag.Diagnostics) []attr.Value {
var instancesList []attr.Value

for _, instance := range *instances {
instanceAttrs, err := mapInstanceToAttrs(instance, region)
if err != nil {
// Keep going in case there are more errors
instanceId := "without id"
if instance.Id != nil {
instanceId = *instance.Id
}
core.LogAndAddError(ctx, diags, "Error reading instances", fmt.Sprintf("Could not process instance %q: %v", instanceId, err))
continue
}
for _, instance := range instances {
instanceAttrs := mapInstanceToAttrs(&instance, region)

instanceObjectValue, objDiags := types.ObjectValue(instanceTypes, instanceAttrs)
diags.Append(objDiags...)
Expand All @@ -242,38 +233,16 @@ func buildInstancesList(ctx context.Context, instances edge.InstanceListGetInsta
return instancesList
}

func mapInstanceToAttrs(instance edge.Instance, region string) (map[string]attr.Value, error) {
if instance.Id == nil {
return nil, fmt.Errorf("instance is missing an 'id'")
}
if instance.DisplayName == nil || *instance.DisplayName == "" {
return nil, fmt.Errorf("instance %q is missing a 'displayName'", *instance.Id)
}
if instance.PlanId == nil {
return nil, fmt.Errorf("instance %q is missing a 'planId'", *instance.Id)
}
if instance.FrontendUrl == nil {
return nil, fmt.Errorf("instance %q is missing a 'frontendUrl'", *instance.Id)
}
if instance.Status == nil {
return nil, fmt.Errorf("instance %q is missing a 'status'", *instance.Id)
}
if instance.Created == nil {
return nil, fmt.Errorf("instance %q is missing a 'created' timestamp", *instance.Id)
}
if instance.Description == nil {
return nil, fmt.Errorf("instance %q is missing a 'description'", *instance.Id)
}
Comment on lines -246 to -266
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the nil-validation checks in mapInstanceToAttrs, rather then turning them into zero-value checks to simplify the code.


func mapInstanceToAttrs(instance *edge.Instance, region string) map[string]attr.Value {
attrs := map[string]attr.Value{
"instance_id": types.StringValue(*instance.Id),
"display_name": types.StringValue(*instance.DisplayName),
"instance_id": types.StringValue(instance.Id),
"display_name": types.StringValue(instance.DisplayName),
"region": types.StringValue(region),
"plan_id": types.StringValue(*instance.PlanId),
"frontend_url": types.StringValue(*instance.FrontendUrl),
"status": types.StringValue(string(instance.GetStatus())),
"plan_id": types.StringValue(instance.PlanId),
"frontend_url": types.StringValue(instance.FrontendUrl),
"status": types.StringValue(instance.Status),
"created": types.StringValue(instance.Created.String()),
"description": types.StringValue(*instance.Description),
"description": types.StringPointerValue(instance.Description),
}
return attrs, nil
return attrs
}
Loading
Loading