Skip to content

Commit

Permalink
fix(resource/virtual_machine) Start a stopped VM
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre PÉRONNET <pierre.peronnet@datadoghq.com>
  • Loading branch information
holyhope authored and NikolaLohinski committed Oct 19, 2024
1 parent 9adcc84 commit 0827c88
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/resource_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ func (v *virtualMachineResource) Update(ctx context.Context, req resource.Update
return
}

expectedStatus := model.Status.ValueString()

payload, diagnostics := model.toClientPayload(ctx)
if diagnostics.HasError() {
resp.Diagnostics.Append(diagnostics...)
Expand Down Expand Up @@ -669,7 +671,7 @@ func (v *virtualMachineResource) Update(ctx context.Context, req resource.Update
}

// Start if needed
if model.Status.ValueString() == freeboxTypes.RunningStatus {
if expectedStatus == freeboxTypes.RunningStatus {
status, err := v.start(ctx, virtualMachine.ID)
model.Status = basetypes.NewStringValue(status)
if err != nil {
Expand Down
45 changes: 42 additions & 3 deletions internal/resource_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var _ = Context("resource \"freebox_virtual_machine\" { ... }", Ordered, func()
memory = 300
name = "` + name + `"
disk_type = "qcow2"
status = "stopped"
disk_path = "` + existingDisk.filepath + `"
timeouts = {
kill = "500ms" // The image used for tests hangs on SIGTERM and needs a SIGKILL to terminate
Expand All @@ -43,7 +42,7 @@ var _ = Context("resource \"freebox_virtual_machine\" { ... }", Ordered, func()
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "memory", "300"),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "disk_type", types.QCow2Disk),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "disk_path", existingDisk.filepath),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "status", "stopped"),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "status", "running"),
func(s *terraform.State) error {
identifier, err := strconv.Atoi(s.RootModule().Resources["freebox_virtual_machine."+name].Primary.Attributes["id"])
Expect(err).To(BeNil())
Expand All @@ -54,7 +53,7 @@ var _ = Context("resource \"freebox_virtual_machine\" { ... }", Ordered, func()
Expect(vm.Name).To(Equal(name))
Expect(vm.DiskType).To(Equal(types.QCow2Disk))
Expect(vm.DiskPath).To(Equal(types.Base64Path(existingDisk.filepath)))
Expect(vm.Status).To(BeEquivalentTo(types.StoppedStatus))
Expect(vm.Status).To(BeEquivalentTo(types.RunningStatus))
return nil
},
),
Expand Down Expand Up @@ -98,6 +97,7 @@ var _ = Context("resource \"freebox_virtual_machine\" { ... }", Ordered, func()
memory = 300
name = "` + name + `"
disk_type = "qcow2"
status = "stopped"
disk_path = "` + existingDisk.filepath + `"
timeouts = {
kill = "500ms" // The image used for tests hangs on SIGTERM and needs a SIGKILL to terminate
Expand All @@ -109,12 +109,51 @@ var _ = Context("resource \"freebox_virtual_machine\" { ... }", Ordered, func()
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "name", name),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "enable_cloudinit", "false"),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "status", "stopped"),
func(s *terraform.State) error {
identifier, err := strconv.Atoi(s.RootModule().Resources["freebox_virtual_machine."+name].Primary.Attributes["id"])
Expect(err).To(BeNil())
vm, err := freeboxClient.GetVirtualMachine(ctx, int64(identifier))
Expect(err).To(BeNil())
Expect(vm.Status).To(BeEquivalentTo(types.StoppedStatus))
return nil
},
),
},
{
Config: providerBlock + `
resource "freebox_virtual_machine" "` + name + `" {
vcpus = 1
memory = 300
name = "` + name + `"
disk_type = "qcow2"
status = "running"
disk_path = "` + existingDisk.filepath + `"
enable_cloudinit = true
cloudinit_hostname = "` + name + `"
cloudinit_userdata = yamlencode(jsondecode(<<EOF
` + cloudInitConfig + `
EOF
))
timeouts = {
kill = "500ms" // The image used for tests hangs on SIGTERM and needs a SIGKILL to terminate
networking = "0s" // The image used for tests does not register to the network
}
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "name", name),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "enable_cloudinit", "true"),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "cloudinit_hostname", name),
resource.TestCheckResourceAttr("freebox_virtual_machine."+name, "status", "running"),
func(s *terraform.State) error {
identifier, err := strconv.Atoi(s.RootModule().Resources["freebox_virtual_machine."+name].Primary.Attributes["id"])
Expect(err).To(BeNil())
vm, err := freeboxClient.GetVirtualMachine(ctx, int64(identifier))
Expect(err).To(BeNil())
Expect(vm.EnableCloudInit).To(BeTrue())
Expect(vm.CloudHostName).To(Equal(name))
Expect(vm.CloudInitUserData).To(MatchYAML(cloudInitConfig))
Expect(vm.Status).To(BeEquivalentTo(types.RunningStatus))
return nil
},
Expand Down

0 comments on commit 0827c88

Please sign in to comment.