@@ -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}
@@ -201,9 +193,8 @@ func mapDataSourceFields(ctx context.Context, machineType *iaas.MachineType, mod
201193 if machineType .Name == nil || * machineType .Name == "" {
202194 return fmt .Errorf ("machine type name is missing" )
203195 }
204- name := * machineType .Name
205196
206- model .Id = utils .BuildInternalTerraformId (model .ProjectId .ValueString (), name )
197+ model .Id = utils .BuildInternalTerraformId (model .ProjectId .ValueString (), * machineType . Name )
207198 model .Name = types .StringPointerValue (machineType .Name )
208199 model .Description = types .StringPointerValue (machineType .Description )
209200 model .Disk = types .Int64PointerValue (machineType .Disk )
0 commit comments