Skip to content

Commit dd8131d

Browse files
committed
review
Signed-off-by: Mauritz Uphoff <mauritz.uphoff@stackit.cloud>
1 parent b2546ae commit dd8131d

4 files changed

Lines changed: 41 additions & 13 deletions

File tree

docs/data-sources/machine_type.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ page_title: "stackit_machine_type Data Source - stackit"
44
subcategory: ""
55
description: |-
66
Machine type data source.
7+
~> This datasource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources.
78
---
89

910
# stackit_machine_type (Data Source)
1011

1112
Machine type data source.
1213

14+
~> This datasource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources.
15+
1316
## Example Usage
1417

1518
```terraform

stackit/internal/services/iaas/iaas_acc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4061,14 +4061,14 @@ func TestAccProject(t *testing.T) {
40614061
})
40624062
}
40634063

4064-
func TestAccMachineTyp(t *testing.T) {
4064+
func TestAccMachineType(t *testing.T) {
40654065
t.Logf("TestAccMachineTyp projectid: %s", testutil.ConvertConfigVariable(testConfigMachineTypeVars["project_id"]))
40664066
resource.ParallelTest(t, resource.TestCase{
40674067
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
40684068
Steps: []resource.TestStep{
40694069
{
40704070
ConfigVariables: testConfigMachineTypeVars,
4071-
Config: fmt.Sprintf("%s\n%s", dataSourceMachineTypeConfig, testutil.IaaSProviderConfig()),
4071+
Config: fmt.Sprintf("%s\n%s", dataSourceMachineTypeConfig, testutil.IaaSProviderConfigWithBetaResourcesEnabled()),
40724072
Check: resource.ComposeTestCheckFunc(
40734073
resource.TestCheckResourceAttr("data.stackit_machine_type.two_vcpus_filter", "project_id", testutil.ConvertConfigVariable(testConfigMachineTypeVars["project_id"])),
40744074
resource.TestCheckResourceAttrSet("data.stackit_machine_type.two_vcpus_filter", "id"),

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1717
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1818
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
19+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
1920
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
2021
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
2122
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
@@ -46,28 +47,33 @@ type machineTypeDataSource struct {
4647
client *iaas.APIClient
4748
}
4849

49-
func (m *machineTypeDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
50+
func (d *machineTypeDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
5051
resp.TypeName = req.ProviderTypeName + "_machine_type"
5152
}
5253

53-
func (m *machineTypeDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
54+
func (d *machineTypeDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
5455
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
5556
if !ok {
5657
return
5758
}
5859

60+
features.CheckBetaResourcesEnabled(ctx, &providerData, &resp.Diagnostics, "stackit_machine_type", "datasource")
61+
if resp.Diagnostics.HasError() {
62+
return
63+
}
64+
5965
client := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
6066
if resp.Diagnostics.HasError() {
6167
return
6268
}
63-
m.client = client
69+
d.client = client
6470

6571
tflog.Info(ctx, "IAAS client configured")
6672
}
6773

68-
func (m *machineTypeDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
74+
func (d *machineTypeDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
6975
resp.Schema = schema.Schema{
70-
MarkdownDescription: "Machine type data source.",
76+
MarkdownDescription: features.AddBetaDescription("Machine type data source.", core.Datasource),
7177
Attributes: map[string]schema.Attribute{
7278
"id": schema.StringAttribute{
7379
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`image_id`\".",
@@ -126,7 +132,7 @@ See https://expr-lang.org/docs/language-definition for syntax.`,
126132
}
127133
}
128134

129-
func (m *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
135+
func (d *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
130136
var model DataSourceModel
131137
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
132138
if resp.Diagnostics.HasError() {
@@ -140,7 +146,7 @@ func (m *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadReq
140146
ctx = tflog.SetField(ctx, "filter_is_null", model.Filter.IsNull())
141147
ctx = tflog.SetField(ctx, "filter_is_unknown", model.Filter.IsUnknown())
142148

143-
listMachineTypeReq := m.client.ListMachineTypes(ctx, projectId)
149+
listMachineTypeReq := d.client.ListMachineTypes(ctx, projectId)
144150

145151
if !model.Filter.IsNull() && !model.Filter.IsUnknown() && strings.TrimSpace(model.Filter.ValueString()) != "" {
146152
listMachineTypeReq = listMachineTypeReq.Filter(strings.TrimSpace(model.Filter.ValueString()))
@@ -175,13 +181,15 @@ func (m *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadReq
175181
return
176182
}
177183

178-
first := sorted[0]
179-
if err := mapDataSourceFields(ctx, first, &model); err != nil {
180-
core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping error", fmt.Sprintf("Failed to translate API response: %v", err))
184+
if err := mapDataSourceFields(ctx, sorted[0], &model); err != nil {
185+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading machine type", fmt.Sprintf("Failed to translate API response: %v", err))
181186
return
182187
}
183188

184189
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
190+
if resp.Diagnostics.HasError() {
191+
return
192+
}
185193
tflog.Info(ctx, "Successfully read machine type")
186194
}
187195

@@ -219,7 +227,7 @@ func sortMachineTypeByName(input []*iaas.MachineType, ascending bool) ([]*iaas.M
219227
}
220228

221229
// Filter out nil or missing name
222-
filtered := make([]*iaas.MachineType, 0)
230+
var filtered []*iaas.MachineType
223231
for _, m := range input {
224232
if m != nil && m.Name != nil {
225233
filtered = append(filtered, m)

stackit/internal/testutil/testutil.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ func IaaSProviderConfig() string {
136136
)
137137
}
138138

139+
func IaaSProviderConfigWithBetaResourcesEnabled() string {
140+
if IaaSCustomEndpoint == "" {
141+
return `
142+
provider "stackit" {
143+
enable_beta_resources = true
144+
default_region = "eu01"
145+
}`
146+
}
147+
return fmt.Sprintf(`
148+
provider "stackit" {
149+
enable_beta_resources = true
150+
iaas_custom_endpoint = "%s"
151+
}`,
152+
IaaSCustomEndpoint,
153+
)
154+
}
155+
139156
func IaaSProviderConfigWithExperiments() string {
140157
if IaaSCustomEndpoint == "" {
141158
return `

0 commit comments

Comments
 (0)