Skip to content

Commit

Permalink
fix weird updating of state
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurVerrept committed Mar 7, 2024
1 parent 26c099b commit 0e33e2c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 62 deletions.
17 changes: 9 additions & 8 deletions internal/provider/network_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}
29 changes: 13 additions & 16 deletions internal/provider/network_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions internal/provider/security_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}
13 changes: 7 additions & 6 deletions internal/provider/security_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 13 additions & 15 deletions internal/provider/vm_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}
7 changes: 3 additions & 4 deletions internal/provider/vm_image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions internal/provider/vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand Down Expand Up @@ -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)...)
Expand All @@ -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)...)
}

Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 0e33e2c

Please sign in to comment.