Skip to content

Commit 2e35b7f

Browse files
committed
fix(server): Handle boot bolume correctly
- Display id and delete_on_termination in datasource - Handle id and delete_on_termination in resource Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent a870b71 commit 2e35b7f

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

docs/data-sources/server.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,4 @@ Server datasource schema. Must have a `region` specified in the provider configu
4343
Read-Only:
4444

4545
- `delete_on_termination` (Boolean) Delete the volume during the termination of the server.
46-
- `id` (String) The ID of the source, either image ID or volume ID
47-
- `performance_class` (String) The performance class of the server.
48-
- `size` (Number) The size of the boot volume in GB.
49-
- `type` (String) The type of the source. Supported values are: `volume`, `image`.
46+
- `id` (String) The ID of the boot volume

docs/data-sources/server_backup_schedule.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,3 @@ Read-Only:
5252
- `name` (String)
5353
- `retention_period` (Number)
5454
- `volume_ids` (List of String)
55-
56-

docs/data-sources/server_backup_schedules.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,3 @@ Read-Only:
5858
- `name` (String)
5959
- `retention_period` (Number)
6060
- `volume_ids` (List of String)
61-
62-

docs/resources/server.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,7 @@ Optional:
410410
- `delete_on_termination` (Boolean) Delete the volume during the termination of the server. Only allowed when `source_type` is `image`.
411411
- `performance_class` (String) The performance class of the server.
412412
- `size` (Number) The size of the boot volume in GB. Must be provided when `source_type` is `image`.
413+
414+
Read-Only:
415+
416+
- `id` (String) The ID of the boot volume

docs/resources/server_backup_schedule.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,3 @@ Required:
6262
Optional:
6363

6464
- `volume_ids` (List of String)
65-
66-

stackit/internal/services/iaas/server/datasource.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/hashicorp/terraform-plugin-framework/attr"
1011
"github.com/hashicorp/terraform-plugin-framework/datasource"
1112
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1213
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -18,7 +19,6 @@ import (
1819
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1920
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
2021
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
21-
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
2222
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2323
)
2424

@@ -46,6 +46,11 @@ type DataSourceModel struct {
4646
UpdatedAt types.String `tfsdk:"updated_at"`
4747
}
4848

49+
var bootVolumeDataTypes = map[string]attr.Type{
50+
"id": basetypes.StringType{},
51+
"delete_on_termination": basetypes.BoolType{},
52+
}
53+
4954
// NewServerDataSource is a helper function to simplify the provider implementation.
5055
func NewServerDataSource() datasource.DataSource {
5156
return &serverDataSource{}
@@ -139,20 +144,8 @@ func (r *serverDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
139144
Description: "The boot volume for the server",
140145
Computed: true,
141146
Attributes: map[string]schema.Attribute{
142-
"performance_class": schema.StringAttribute{
143-
Description: "The performance class of the server.",
144-
Computed: true,
145-
},
146-
"size": schema.Int64Attribute{
147-
Description: "The size of the boot volume in GB.",
148-
Computed: true,
149-
},
150-
"type": schema.StringAttribute{
151-
Description: "The type of the source. " + utils.SupportedValuesDocumentation(supportedSourceTypes),
152-
Computed: true,
153-
},
154147
"id": schema.StringAttribute{
155-
Description: "The ID of the source, either image ID or volume ID",
148+
Description: "The ID of the boot volume",
156149
Computed: true,
157150
},
158151
"delete_on_termination": schema.BoolAttribute{
@@ -312,6 +305,19 @@ func mapDataSourceFields(ctx context.Context, serverResp *iaas.Server, model *Da
312305
model.NetworkInterfaces = types.ListNull(types.StringType)
313306
}
314307

308+
if serverResp.BootVolume != nil {
309+
bootVolume, diags := types.ObjectValue(bootVolumeDataTypes, map[string]attr.Value{
310+
"id": types.StringPointerValue(serverResp.BootVolume.Id),
311+
"delete_on_termination": types.BoolPointerValue(serverResp.BootVolume.DeleteOnTermination),
312+
})
313+
if diags.HasError() {
314+
return fmt.Errorf("failed to map bootVolume: %w", core.DiagsToError(diags))
315+
}
316+
model.BootVolume = bootVolume
317+
} else {
318+
model.BootVolume = types.ObjectNull(bootVolumeDataTypes)
319+
}
320+
315321
model.AvailabilityZone = types.StringPointerValue(serverResp.AvailabilityZone)
316322
model.ServerId = types.StringValue(serverId)
317323
model.MachineType = types.StringPointerValue(serverResp.MachineType)

stackit/internal/services/iaas/server/resource.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type Model struct {
7575

7676
// Struct corresponding to Model.BootVolume
7777
type bootVolumeModel struct {
78+
Id types.String `tfsdk:"id"`
7879
PerformanceClass types.String `tfsdk:"performance_class"`
7980
Size types.Int64 `tfsdk:"size"`
8081
SourceType types.String `tfsdk:"source_type"`
@@ -89,6 +90,7 @@ var bootVolumeTypes = map[string]attr.Type{
8990
"source_type": basetypes.StringType{},
9091
"source_id": basetypes.StringType{},
9192
"delete_on_termination": basetypes.BoolType{},
93+
"id": basetypes.StringType{},
9294
}
9395

9496
// NewServerResource is a helper function to simplify the provider implementation.
@@ -253,6 +255,13 @@ func (r *serverResource) Schema(_ context.Context, _ resource.SchemaRequest, res
253255
objectplanmodifier.RequiresReplace(),
254256
},
255257
Attributes: map[string]schema.Attribute{
258+
"id": schema.StringAttribute{
259+
Description: "The ID of the boot volume",
260+
Computed: true,
261+
PlanModifiers: []planmodifier.String{
262+
stringplanmodifier.UseStateForUnknown(),
263+
},
264+
},
256265
"performance_class": schema.StringAttribute{
257266
Description: "The performance class of the server.",
258267
Optional: true,
@@ -911,6 +920,34 @@ func mapFields(ctx context.Context, serverResp *iaas.Server, model *Model) error
911920
model.NetworkInterfaces = types.ListNull(types.StringType)
912921
}
913922

923+
if serverResp.BootVolume != nil {
924+
// convert boot volume model
925+
var bootVolumeModel = &bootVolumeModel{}
926+
if !(model.BootVolume.IsNull() || model.BootVolume.IsUnknown()) {
927+
diags := model.BootVolume.As(ctx, bootVolumeModel, basetypes.ObjectAsOptions{})
928+
if diags.HasError() {
929+
return fmt.Errorf("failed to map bootVolume: %w", core.DiagsToError(diags))
930+
}
931+
}
932+
933+
// Only the id and delete_on_termination is returned via response.
934+
// Take the other values from the model.
935+
bootVolume, diags := types.ObjectValue(bootVolumeTypes, map[string]attr.Value{
936+
"id": types.StringPointerValue(serverResp.BootVolume.Id),
937+
"delete_on_termination": types.BoolPointerValue(serverResp.BootVolume.DeleteOnTermination),
938+
"source_id": bootVolumeModel.SourceId,
939+
"size": bootVolumeModel.Size,
940+
"source_type": bootVolumeModel.SourceType,
941+
"performance_class": bootVolumeModel.PerformanceClass,
942+
})
943+
if diags.HasError() {
944+
return fmt.Errorf("failed to map bootVolume: %w", core.DiagsToError(diags))
945+
}
946+
model.BootVolume = bootVolume
947+
} else {
948+
model.BootVolume = types.ObjectNull(bootVolumeTypes)
949+
}
950+
914951
model.ServerId = types.StringValue(serverId)
915952
model.MachineType = types.StringPointerValue(serverResp.MachineType)
916953

0 commit comments

Comments
 (0)