Skip to content

Commit 20dcaac

Browse files
authored
chore(edge): switch to new SDK structure (#1346)
relates to STACKITTPR-555
1 parent 37d1ed0 commit 20dcaac

File tree

12 files changed

+117
-288
lines changed

12 files changed

+117
-288
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/stackitcloud/stackit-sdk-go/services/alb v0.12.1
1717
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0
1818
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1
19-
github.com/stackitcloud/stackit-sdk-go/services/edge v0.7.0
19+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0
2020
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0
2121
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5
2222
github.com/stackitcloud/stackit-sdk-go/services/kms v1.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0 h1:iRJK2d3I2QqWp8hqh
163163
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0/go.mod h1:URWWMIbvq4YgWdGYCbccr3eat4Y+0qRpufZsEAsvoLM=
164164
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1 h1:VfszhFq/Snsd0LnflS8PbM0d9cG98hOFpamfjlcTnDQ=
165165
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1/go.mod h1:gBv6YkB3Xf3c0ZXg2GwtWY8zExwGPF/Ag114XiiERxg=
166-
github.com/stackitcloud/stackit-sdk-go/services/edge v0.7.0 h1:DNBiHWQEWXHSbaZBmnXb+CaPXX1uVsSfp4FTHoH4wrM=
167-
github.com/stackitcloud/stackit-sdk-go/services/edge v0.7.0/go.mod h1:CfqSEGCW0b5JlijCwtUT1kfjThmQ5jXX47TWrdD5rTU=
166+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0 h1:lsOTEvItzrUerCzUodyyOy6pZ4QiSzwID7U20ZvZvHw=
167+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0/go.mod h1:CfqSEGCW0b5JlijCwtUT1kfjThmQ5jXX47TWrdD5rTU=
168168
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0 h1:siKBCYl7BwR5S25Y4SwUAX6eFTXSCkQ+8JIQEqyKIXo=
169169
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0/go.mod h1:QKy74hhLVZKXItw3y+elF8s9QezFqpEsAUmaqvNDzYs=
170170
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5 h1:W57+XRa8wTLsi5CV9Tqa7mGgt/PvlRM//RurXSmvII8=

stackit/internal/services/edgecloud/edge_acc_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1717
"github.com/hashicorp/terraform-plugin-testing/terraform"
1818
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
19-
"github.com/stackitcloud/stackit-sdk-go/services/edge"
20-
"github.com/stackitcloud/stackit-sdk-go/services/edge/wait"
19+
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
20+
"github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api/wait"
2121
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2222
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
2323
)
@@ -349,7 +349,7 @@ func testAccCheckEdgeCloudInstanceDestroy(s *terraform.State) error {
349349
}
350350
projectId, region, instanceId := idParts[0], idParts[1], idParts[2]
351351

352-
_, err := client.GetInstance(ctx, projectId, region, instanceId).Execute()
352+
_, err := client.DefaultAPI.GetInstance(ctx, projectId, region, instanceId).Execute()
353353
if err == nil {
354354
return fmt.Errorf("edge instance %q still exists", instanceId)
355355
}
@@ -358,11 +358,11 @@ func testAccCheckEdgeCloudInstanceDestroy(s *terraform.State) error {
358358
var oapiErr *oapierror.GenericOpenAPIError
359359
ok := errors.As(err, &oapiErr)
360360
if !ok || oapiErr.StatusCode != http.StatusNotFound {
361-
err := client.DeleteInstance(ctx, projectId, region, instanceId).Execute()
361+
err := client.DefaultAPI.DeleteInstance(ctx, projectId, region, instanceId).Execute()
362362
if err != nil {
363363
return fmt.Errorf("deleting instance %s during CheckDestroy: %w", instanceId, err)
364364
}
365-
_, err = wait.DeleteInstanceWaitHandler(ctx, client, projectId, region, instanceId).WaitWithContext(ctx)
365+
_, err = wait.DeleteInstanceWaitHandler(ctx, client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
366366
if err != nil {
367367
return fmt.Errorf("waiting for instance deletion %s during CheckDestroy: %w", instanceId, err)
368368
}

stackit/internal/services/edgecloud/instance/resource.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"github.com/hashicorp/terraform-plugin-framework/types"
1919
"github.com/hashicorp/terraform-plugin-log/tflog"
2020
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
21-
"github.com/stackitcloud/stackit-sdk-go/services/edge"
22-
edgewait "github.com/stackitcloud/stackit-sdk-go/services/edge/wait"
21+
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
22+
edgewait "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api/wait"
2323
"github.com/stackitcloud/stackit-sdk-go/services/serviceenablement"
2424
enablementWait "github.com/stackitcloud/stackit-sdk-go/services/serviceenablement/wait"
2525
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
@@ -264,7 +264,7 @@ func (i *instanceResource) Create(ctx context.Context, req resource.CreateReques
264264

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

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

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

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

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

381376
ctx = core.LogResponse(ctx)
382377

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

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

423418
ctx = core.LogResponse(ctx)
424419

425-
_, err = edgewait.DeleteInstanceWaitHandler(ctx, i.client, projectId, region, instanceId).WaitWithContext(ctx)
420+
_, err = edgewait.DeleteInstanceWaitHandler(ctx, i.client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
426421
if err != nil {
427422
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err))
428423
return
@@ -461,8 +456,8 @@ func mapFields(resp *edge.Instance, model *Model) error {
461456
var instanceId string
462457
if model.InstanceId.ValueString() != "" {
463458
instanceId = model.InstanceId.ValueString()
464-
} else if resp.Id != nil {
465-
instanceId = *resp.Id
459+
} else if resp.Id != "" {
460+
instanceId = resp.Id
466461
}
467462
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.Region.ValueString(), instanceId)
468463
model.InstanceId = types.StringValue(instanceId)
@@ -471,10 +466,10 @@ func mapFields(resp *edge.Instance, model *Model) error {
471466
} else {
472467
model.Created = types.StringNull()
473468
}
474-
model.FrontendUrl = types.StringPointerValue(resp.FrontendUrl)
475-
model.DisplayName = types.StringPointerValue(resp.DisplayName)
476-
model.PlanID = types.StringPointerValue(resp.PlanId)
477-
model.Status = types.StringValue(string(*resp.Status))
469+
model.FrontendUrl = types.StringValue(resp.FrontendUrl)
470+
model.DisplayName = types.StringValue(resp.DisplayName)
471+
model.PlanID = types.StringValue(resp.PlanId)
472+
model.Status = types.StringValue(resp.Status)
478473

479474
if resp.Description != nil {
480475
model.Description = types.StringValue(*resp.Description)
@@ -488,9 +483,9 @@ func mapFields(resp *edge.Instance, model *Model) error {
488483
// toCreatePayload creates the payload for creating an Edge Cloud instance.
489484
func toCreatePayload(model *Model) edge.CreateInstancePayload {
490485
return edge.CreateInstancePayload{
491-
DisplayName: model.DisplayName.ValueStringPointer(),
486+
DisplayName: model.DisplayName.ValueString(),
492487
Description: model.Description.ValueStringPointer(),
493-
PlanId: model.PlanID.ValueStringPointer(),
488+
PlanId: model.PlanID.ValueString(),
494489
}
495490
}
496491

stackit/internal/services/edgecloud/instance/resource_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88
"github.com/google/go-cmp/cmp"
99
"github.com/google/uuid"
1010
"github.com/hashicorp/terraform-plugin-framework/types"
11-
"github.com/stackitcloud/stackit-sdk-go/core/utils"
12-
"github.com/stackitcloud/stackit-sdk-go/services/edge"
11+
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
1312
)
1413

1514
func TestMapFields(t *testing.T) {
@@ -25,13 +24,13 @@ func TestMapFields(t *testing.T) {
2524
{
2625
"all_parameter_set",
2726
&edge.Instance{
28-
Id: new("iid-123"),
29-
Created: &testTime,
30-
DisplayName: new("test-instance"),
27+
Id: "iid-123",
28+
Created: testTime,
29+
DisplayName: "test-instance",
3130
Description: new("Test description"),
32-
PlanId: new(uuidString),
33-
Status: utils.Ptr(edge.InstanceStatus("CREATING")),
34-
FrontendUrl: new("https://iid-123.example.com"),
31+
PlanId: uuidString,
32+
Status: "CREATING",
33+
FrontendUrl: "https://iid-123.example.com",
3534
},
3635
&Model{
3736
ProjectId: types.StringValue(uuidString),
@@ -54,13 +53,13 @@ func TestMapFields(t *testing.T) {
5453
{
5554
"empty_description",
5655
&edge.Instance{
57-
Id: new("iid-123"),
58-
Created: &testTime,
59-
DisplayName: new("test-instance"),
56+
Id: "iid-123",
57+
Created: testTime,
58+
DisplayName: "test-instance",
6059
Description: new(""),
61-
PlanId: new(uuidString),
62-
Status: utils.Ptr(edge.InstanceStatus("ACTIVE")),
63-
FrontendUrl: new("https://iid-123.example.com"),
60+
PlanId: uuidString,
61+
Status: "ACTIVE",
62+
FrontendUrl: "https://iid-123.example.com",
6463
},
6564
&Model{
6665
ProjectId: types.StringValue(uuidString),
@@ -132,9 +131,9 @@ func TestToCreatePayload(t *testing.T) {
132131
PlanID: types.StringValue(uuidString),
133132
},
134133
edge.CreateInstancePayload{
135-
DisplayName: new("new-instance"),
134+
DisplayName: "new-instance",
136135
Description: new("A new test instance"),
137-
PlanId: new(uuidString),
136+
PlanId: uuidString,
138137
},
139138
true,
140139
},
@@ -146,9 +145,9 @@ func TestToCreatePayload(t *testing.T) {
146145
PlanID: types.StringValue(uuidString),
147146
},
148147
edge.CreateInstancePayload{
149-
DisplayName: new("new-instance"),
148+
DisplayName: "new-instance",
150149
Description: nil,
151-
PlanId: new(uuidString),
150+
PlanId: uuidString,
152151
},
153152
true,
154153
},

stackit/internal/services/edgecloud/instances/datasource.go

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1313
"github.com/hashicorp/terraform-plugin-framework/types"
1414
"github.com/hashicorp/terraform-plugin-log/tflog"
15-
"github.com/stackitcloud/stackit-sdk-go/services/edge"
15+
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
1616
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1717
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1818
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
@@ -166,7 +166,7 @@ func (d *instancesDataSource) Read(ctx context.Context, req datasource.ReadReque
166166
ctx = tflog.SetField(ctx, "region", region)
167167

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

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

222-
for _, instance := range *instances {
223-
instanceAttrs, err := mapInstanceToAttrs(instance, region)
224-
if err != nil {
225-
// Keep going in case there are more errors
226-
instanceId := "without id"
227-
if instance.Id != nil {
228-
instanceId = *instance.Id
229-
}
230-
core.LogAndAddError(ctx, diags, "Error reading instances", fmt.Sprintf("Could not process instance %q: %v", instanceId, err))
231-
continue
232-
}
222+
for _, instance := range instances {
223+
instanceAttrs := mapInstanceToAttrs(&instance, region)
233224

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

245-
func mapInstanceToAttrs(instance edge.Instance, region string) (map[string]attr.Value, error) {
246-
if instance.Id == nil {
247-
return nil, fmt.Errorf("instance is missing an 'id'")
248-
}
249-
if instance.DisplayName == nil || *instance.DisplayName == "" {
250-
return nil, fmt.Errorf("instance %q is missing a 'displayName'", *instance.Id)
251-
}
252-
if instance.PlanId == nil {
253-
return nil, fmt.Errorf("instance %q is missing a 'planId'", *instance.Id)
254-
}
255-
if instance.FrontendUrl == nil {
256-
return nil, fmt.Errorf("instance %q is missing a 'frontendUrl'", *instance.Id)
257-
}
258-
if instance.Status == nil {
259-
return nil, fmt.Errorf("instance %q is missing a 'status'", *instance.Id)
260-
}
261-
if instance.Created == nil {
262-
return nil, fmt.Errorf("instance %q is missing a 'created' timestamp", *instance.Id)
263-
}
264-
if instance.Description == nil {
265-
return nil, fmt.Errorf("instance %q is missing a 'description'", *instance.Id)
266-
}
267-
236+
func mapInstanceToAttrs(instance *edge.Instance, region string) map[string]attr.Value {
268237
attrs := map[string]attr.Value{
269-
"instance_id": types.StringValue(*instance.Id),
270-
"display_name": types.StringValue(*instance.DisplayName),
238+
"instance_id": types.StringValue(instance.Id),
239+
"display_name": types.StringValue(instance.DisplayName),
271240
"region": types.StringValue(region),
272-
"plan_id": types.StringValue(*instance.PlanId),
273-
"frontend_url": types.StringValue(*instance.FrontendUrl),
274-
"status": types.StringValue(string(instance.GetStatus())),
241+
"plan_id": types.StringValue(instance.PlanId),
242+
"frontend_url": types.StringValue(instance.FrontendUrl),
243+
"status": types.StringValue(instance.Status),
275244
"created": types.StringValue(instance.Created.String()),
276-
"description": types.StringValue(*instance.Description),
245+
"description": types.StringPointerValue(instance.Description),
277246
}
278-
return attrs, nil
247+
return attrs
279248
}

0 commit comments

Comments
 (0)