diff --git a/internal/provider/network_data_source.go b/internal/provider/network_data_source.go index 65a1bc7..cfdf109 100644 --- a/internal/provider/network_data_source.go +++ b/internal/provider/network_data_source.go @@ -106,12 +106,13 @@ func (d *NetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest } // Save data into Terraform state - resp.Diagnostics.Append(resp.State.Set(ctx, NetworkDataSourceModel{ - Id: types.StringValue(res.Network.Id), - DataCenterId: types.StringValue(res.Network.DataCenterId), - IPRange: types.StringValue(res.Network.IpRange), - Gateway: types.StringValue(res.Network.Gateway), - ExternalIPAddress: types.StringValue(res.Network.ExternalIpAddress), - InternalIPAddress: types.StringValue(res.Network.InternalIpAddress), - })...) + + state.Id = types.StringValue(res.Network.Id) + state.DataCenterId = types.StringValue(res.Network.DataCenterId) + state.IPRange = types.StringValue(res.Network.IpRange) + state.Gateway = types.StringValue(res.Network.Gateway) + state.ExternalIPAddress = types.StringValue(res.Network.ExternalIpAddress) + state.InternalIPAddress = types.StringValue(res.Network.InternalIpAddress) + + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } diff --git a/internal/provider/network_resource.go b/internal/provider/network_resource.go index eb70fbc..ac47f92 100644 --- a/internal/provider/network_resource.go +++ b/internal/provider/network_resource.go @@ -145,14 +145,11 @@ func (r *NetworkResource) Create(ctx context.Context, req resource.CreateRequest return } - resp.Diagnostics.Append(resp.State.Set(ctx, NetworkResourceModel{ - ID: types.StringValue(network.Id), - DataCenterId: types.StringValue(network.DataCenterId), - IPRange: types.StringValue(network.IpRange), - Gateway: types.StringValue(network.Gateway), - ExternalIPAddress: types.StringValue(network.ExternalIpAddress), - InternalIPAddress: types.StringValue(network.InternalIpAddress), - })...) + state.Gateway = types.StringValue(network.Gateway) + state.ExternalIPAddress = types.StringValue(network.ExternalIpAddress) + state.InternalIPAddress = types.StringValue(network.InternalIpAddress) + + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *NetworkResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { @@ -180,15 +177,15 @@ func (r *NetworkResource) Read(ctx context.Context, req resource.ReadRequest, re return } + state.ID = types.StringValue(res.Network.Id) + state.DataCenterId = types.StringValue(res.Network.DataCenterId) + state.ExternalIPAddress = types.StringValue(res.Network.ExternalIpAddress) + state.InternalIPAddress = types.StringValue(res.Network.InternalIpAddress) + state.IPRange = types.StringValue(res.Network.IpRange) + state.Gateway = types.StringValue(res.Network.Gateway) + // Save updated data into Terraform state - resp.Diagnostics.Append(resp.State.Set(ctx, NetworkResourceModel{ - ID: types.StringValue(res.Network.Id), - DataCenterId: types.StringValue(res.Network.DataCenterId), - ExternalIPAddress: types.StringValue(res.Network.ExternalIpAddress), - InternalIPAddress: types.StringValue(res.Network.InternalIpAddress), - IPRange: types.StringValue(res.Network.IpRange), - Gateway: types.StringValue(res.Network.Gateway), - })...) + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *NetworkResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { diff --git a/internal/provider/security_group_data_source.go b/internal/provider/security_group_data_source.go index 1c363db..e398e1e 100644 --- a/internal/provider/security_group_data_source.go +++ b/internal/provider/security_group_data_source.go @@ -137,11 +137,10 @@ func (d *SecurityGroupDataSource) Read(ctx context.Context, req datasource.ReadR return } - // Save data into Terraform state - resp.Diagnostics.Append(resp.State.Set(ctx, SecurityGroupDataSourceModel{ - ID: types.StringValue(res.SecurityGroup.Id), - DataCenterID: types.StringValue(res.SecurityGroup.DataCenterId), - Description: types.StringValue(res.SecurityGroup.Description), - Rules: getRuleModels(res.SecurityGroup.Rules), - })...) + sg := res.SecurityGroup + state.ID = types.StringValue(sg.Id) + state.DataCenterID = types.StringValue(sg.DataCenterId) + state.Description = types.StringValue(sg.Description) + state.Rules = getRuleModels(sg.Rules) + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } diff --git a/internal/provider/security_group_resource.go b/internal/provider/security_group_resource.go index 13143a6..a4c6ea0 100644 --- a/internal/provider/security_group_resource.go +++ b/internal/provider/security_group_resource.go @@ -304,13 +304,14 @@ func (r *SecurityGroupResource) Read(ctx context.Context, req resource.ReadReque return } + state.Id = types.StringValue(res.SecurityGroup.Id) + state.Description = types.StringValue(res.SecurityGroup.Description) + state.DataCenterID = types.StringValue(res.SecurityGroup.DataCenterId) + state.Id = types.StringValue(res.SecurityGroup.Id) + state.Rules = getRuleModels(res.SecurityGroup.Rules) + // Save updated data into Terraform state - resp.Diagnostics.Append(resp.State.Set(ctx, SecurityGroupResourceModel{ - Id: types.StringValue(res.SecurityGroup.Id), - Description: types.StringValue(res.SecurityGroup.Description), - DataCenterID: types.StringValue(res.SecurityGroup.DataCenterId), - Rules: getRuleModels(res.SecurityGroup.Rules), - })...) + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *SecurityGroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { diff --git a/internal/provider/vm_data_source.go b/internal/provider/vm_data_source.go index 0ec391b..f09c46d 100644 --- a/internal/provider/vm_data_source.go +++ b/internal/provider/vm_data_source.go @@ -154,24 +154,22 @@ func (d *VMDataSource) Read(ctx context.Context, req datasource.ReadRequest, res imageID = res.VM.PublicImageId } + state.BootDiskSizeGib = types.Int64Value(int64(res.VM.BootDiskSizeGib)) + state.CPUModel = types.StringValue(res.VM.CpuModel) + state.DatacenterID = types.StringValue(res.VM.DatacenterId) + state.GpuModel = types.StringValue(res.VM.GpuModel) + state.Gpus = types.Int64Value(int64(res.VM.GpuQuantity)) + state.ImageID = types.StringValue(imageID) + state.InternalIPAddress = types.StringValue(res.VM.InternalIpAddress) + state.ExternalIPAddress = types.StringValue(res.VM.ExternalIpAddress) + state.Memory = types.Int64Value(int64(res.VM.Memory)) + state.PriceHr = types.Float64Value(float64(res.VM.PriceHr)) + state.Vcpus = types.Int64Value(int64(res.VM.Vcpus)) + // Write logs using the tflog package // Documentation: https://terraform.io/plugin/log tflog.Trace(ctx, "read a data source") // Save data into Terraform state - resp.Diagnostics.Append(resp.State.Set(ctx, &VMDataSourceModel{ - ProjectID: types.StringValue(projectId), - Id: types.StringValue(vmId), - BootDiskSizeGib: types.Int64Value(int64(res.VM.BootDiskSizeGib)), - CPUModel: types.StringValue(res.VM.CpuModel), - DatacenterID: types.StringValue(res.VM.DatacenterId), - GpuModel: types.StringValue(res.VM.GpuModel), - Gpus: types.Int64Value(int64(res.VM.GpuQuantity)), - ImageID: types.StringValue(imageID), - InternalIPAddress: types.StringValue(res.VM.InternalIpAddress), - ExternalIPAddress: types.StringValue(res.VM.ExternalIpAddress), - Memory: types.Int64Value(int64(res.VM.Memory)), - PriceHr: types.Float64Value(float64(res.VM.PriceHr)), - Vcpus: types.Int64Value(int64(res.VM.Vcpus)), - })...) + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } diff --git a/internal/provider/vm_image_resource.go b/internal/provider/vm_image_resource.go index ff0928c..d49f0d1 100644 --- a/internal/provider/vm_image_resource.go +++ b/internal/provider/vm_image_resource.go @@ -137,10 +137,9 @@ func (r *VMImageResource) Create(ctx context.Context, req resource.CreateRequest return } - resp.Diagnostics.Append(resp.State.Set(ctx, VMImageResourceModel{ - DataCenterId: types.StringValue(res.Image.DataCenterId), - SizeGib: types.Int64Value(int64(res.Image.SizeGib)), - })...) + state.DataCenterId = types.StringValue(res.Image.DataCenterId) + state.SizeGib = types.Int64Value(int64(res.Image.SizeGib)) + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *VMImageResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { diff --git a/internal/provider/vm_resource.go b/internal/provider/vm_resource.go index a41285c..6810f7f 100644 --- a/internal/provider/vm_resource.go +++ b/internal/provider/vm_resource.go @@ -307,7 +307,7 @@ func (r *VMResource) Configure(ctx context.Context, req resource.ConfigureReques } func (r *VMResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var state *VMResourceModel + var state VMResourceModel // Read Terraform plan data into the model resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...) @@ -409,12 +409,12 @@ func (r *VMResource) Create(ctx context.Context, req resource.CreateRequest, res ) return } - fillVmState(vm.VM, state) + appendVmState(vm.VM, &state) resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *VMResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var state *VMResourceModel + var state VMResourceModel // Read Terraform prior state data into the model resp.Diagnostics.Append(req.State.Get(ctx, &state)...) @@ -441,7 +441,7 @@ func (r *VMResource) Read(ctx context.Context, req resource.ReadRequest, resp *r return } - fillVmState(vm.VM, state) + appendVmState(vm.VM, &state) resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } @@ -467,7 +467,7 @@ func (r *VMResource) Update(ctx context.Context, req resource.UpdateRequest, res } func (r *VMResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var state *VMResourceModel + var state VMResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) projectId := r.client.DefaultProjectID @@ -594,7 +594,7 @@ func waitForVmDelete(ctx context.Context, projectId string, vmID string, c vm.VM } } -func fillVmState(vm *vm.VM, state *VMResourceModel) { +func appendVmState(vm *vm.VM, state *VMResourceModel) { state.DataCenterID = types.StringValue(vm.DatacenterId) state.CPUModel = types.StringValue(vm.CpuModel) state.GPUs = types.Int64Value(int64(vm.GpuQuantity))