Skip to content

Commit 8203ec9

Browse files
committed
review
Signed-off-by: Mauritz Uphoff <[email protected]>
1 parent 4fb45e8 commit 8203ec9

File tree

2 files changed

+10
-41
lines changed

2 files changed

+10
-41
lines changed

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@ func (m *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadReq
142142

143143
listMachineTypeReq := m.client.ListMachineTypes(ctx, projectId)
144144

145-
if !model.Filter.IsNull() && !model.Filter.IsUnknown() {
146-
if filter := strings.TrimSpace(model.Filter.ValueString()); filter != "" {
147-
listMachineTypeReq = listMachineTypeReq.Filter(filter)
148-
}
145+
if !model.Filter.IsNull() && !model.Filter.IsUnknown() && strings.TrimSpace(model.Filter.ValueString()) != "" {
146+
listMachineTypeReq = listMachineTypeReq.Filter(strings.TrimSpace(model.Filter.ValueString()))
149147
}
150148

151149
apiResp, err := listMachineTypeReq.Execute()
@@ -160,16 +158,15 @@ func (m *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadReq
160158
return
161159
}
162160

163-
items := apiResp.Items
164-
if items == nil || len(*items) == 0 {
161+
if apiResp.Items == nil || len(*apiResp.Items) == 0 {
165162
core.LogAndAddWarning(ctx, &resp.Diagnostics, "No machine types found", "No matching machine types.")
166163
return
167164
}
168165

169166
// Convert items to []*iaas.MachineType
170-
machineTypes := make([]*iaas.MachineType, len(*items))
171-
for i := range *items {
172-
machineTypes[i] = &(*items)[i]
167+
machineTypes := make([]*iaas.MachineType, len(*apiResp.Items))
168+
for i := range *apiResp.Items {
169+
machineTypes[i] = &(*apiResp.Items)[i]
173170
}
174171

175172
sorted, err := sortMachineTypeByName(machineTypes, sortAscending)
@@ -184,11 +181,6 @@ func (m *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadReq
184181
return
185182
}
186183

187-
if err := mapDataSourceFields(ctx, first, &model); err != nil {
188-
core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping error", fmt.Sprintf("Failed to translate API response: %v", err))
189-
return
190-
}
191-
192184
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
193185
tflog.Info(ctx, "Successfully read machine type")
194186
}

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ func TestMapDataSourceFields(t *testing.T) {
4545
Disk: types.Int64Value(20),
4646
Ram: types.Int64Value(2048),
4747
Vcpus: types.Int64Value(2),
48-
ExtraSpecs: mustMapString(map[string]string{
49-
"cpu": "amd-epycrome-7702",
50-
"overcommit": "1",
51-
"environment": "general",
48+
ExtraSpecs: types.MapValueMust(types.StringType, map[string]attr.Value{
49+
"cpu": types.StringValue("amd-epycrome-7702"),
50+
"overcommit": types.StringValue("1"),
51+
"environment": types.StringValue("general"),
5252
}),
5353
},
5454
expectError: false,
@@ -209,13 +209,6 @@ func TestSortMachineTypeByName(t *testing.T) {
209209
ascending: true,
210210
expectError: true,
211211
},
212-
{
213-
name: "equal names are stable",
214-
input: []*iaas.MachineType{{Name: utils.Ptr("same")}, {Name: utils.Ptr("same")}, {Name: utils.Ptr("same")}},
215-
ascending: true,
216-
expected: []string{"same", "same", "same"},
217-
expectError: false,
218-
},
219212
}
220213

221214
for _, tt := range tests {
@@ -246,19 +239,3 @@ func TestSortMachineTypeByName(t *testing.T) {
246239
})
247240
}
248241
}
249-
250-
func mustMapString(val map[string]string) types.Map {
251-
m, diags := types.MapValue(types.StringType, convertToAttr(val))
252-
if diags.HasError() {
253-
panic("invalid test: " + diags.Errors()[0].Detail())
254-
}
255-
return m
256-
}
257-
258-
func convertToAttr(m map[string]string) map[string]attr.Value {
259-
res := make(map[string]attr.Value, len(m))
260-
for k, v := range m {
261-
res[k] = types.StringValue(v)
262-
}
263-
return res
264-
}

0 commit comments

Comments
 (0)