Skip to content

Commit e9db27a

Browse files
authored
feat: Refactor vm update (#15)
1 parent d6b9579 commit e9db27a

File tree

4 files changed

+64
-34
lines changed

4 files changed

+64
-34
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ resource "crunchloop_vm" "vm" {
5151
If you wish to contribute to the provider, follow these steps:
5252

5353
1. Clone the repository
54-
2. Build the provider using Go: `go build ./...`
54+
2. Build the provider using Go: `go build -o terraform-provider-crunchloop`
55+
3. Use the `examples` folder to test managing resources with your instance
5556

5657
## Documentation
5758

docs/resources/vm.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,36 @@ resource "crunchloop_vm" "without_host" {
5353
cores = 1
5454
memory_megabytes = 1024
5555
root_volume_size_gigabytes = 10
56-
user_data = base64encode("echo 'Hello, World!'")
56+
}
57+
58+
# Use a cloud-init configuration to configure the VM
59+
#
60+
data "cloudinit_config" "cloudinit" {
61+
gzip = false
62+
base64_encode = true
63+
64+
part {
65+
filename = "cloud-config.yaml"
66+
content_type = "text/cloud-config"
67+
68+
content = <<-EOF
69+
#cloud-config
70+
cloud_final_modules:
71+
- [scripts-user, always]
72+
73+
runcmd:
74+
- echo "Hello, World!" > /tmp/hello.txt
75+
EOF
76+
}
77+
}
78+
79+
resource "crunchloop_vm" "with_cloudinit" {
80+
name = "terraform-with-cloudinit"
81+
vmi_id = data.crunchloop_vmi.ubuntu.id
82+
cores = 1
83+
memory_megabytes = 1024
84+
root_volume_size_gigabytes = 10
85+
user_data = data.cloudinit_config.cloudinit.rendered
5786
}
5887
```
5988

examples/resources/crunchloop_vm/resource.tf

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,34 @@ resource "crunchloop_vm" "without_host" {
3838
cores = 1
3939
memory_megabytes = 1024
4040
root_volume_size_gigabytes = 10
41-
user_data = base64encode("echo 'Hello, World!'")
4241
}
42+
43+
# Use a cloud-init configuration to configure the VM
44+
#
45+
data "cloudinit_config" "cloudinit" {
46+
gzip = false
47+
base64_encode = true
48+
49+
part {
50+
filename = "cloud-config.yaml"
51+
content_type = "text/cloud-config"
52+
53+
content = <<-EOF
54+
#cloud-config
55+
cloud_final_modules:
56+
- [scripts-user, always]
57+
58+
runcmd:
59+
- echo "Hello, World!" > /tmp/hello.txt
60+
EOF
61+
}
62+
}
63+
64+
resource "crunchloop_vm" "with_cloudinit" {
65+
name = "terraform-with-cloudinit"
66+
vmi_id = data.crunchloop_vmi.ubuntu.id
67+
cores = 1
68+
memory_megabytes = 1024
69+
root_volume_size_gigabytes = 10
70+
user_data = data.cloudinit_config.cloudinit.rendered
71+
}

internal/services/vm_service.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,6 @@ func (s *VmService) UpdateVm(ctx context.Context, id int32, options client.Updat
7373
return nil, err
7474
}
7575

76-
var wasRunning = *vm.Status == client.VirtualMachineStatusRunning
77-
78-
// We need to stop the vm if it's running to update it.
79-
if wasRunning {
80-
_, err = s.client.StopVm(ctx, id)
81-
if err != nil {
82-
return nil, fmt.Errorf("failed to stop vm before updating. Error: %s", err)
83-
}
84-
85-
err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "stopped")
86-
if err != nil {
87-
return nil, err
88-
}
89-
}
90-
9176
// We are ready to update the vm now.
9277
updateResponse, err := s.client.UpdateVmWithResponse(ctx, id, options)
9378
if err != nil {
@@ -99,27 +84,13 @@ func (s *VmService) UpdateVm(ctx context.Context, id int32, options client.Updat
9984
}
10085

10186
// After we issue an update, the vm is going to transition to `updating` state
102-
// and eventually will be back to `stopped` state, we need to wait for that
87+
// and eventually will be back to `currentStatus` state, we need to wait for that
10388
// state before moving forward.
104-
err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "stopped")
89+
err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, *vm.Status)
10590
if err != nil {
10691
return nil, err
10792
}
10893

109-
// If the vm was running we should turn it on again.
110-
//
111-
if wasRunning {
112-
_, err = s.client.StartVm(ctx, id)
113-
if err != nil {
114-
return nil, fmt.Errorf("failed to start vm after updating. Error: %s", err)
115-
}
116-
117-
err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "running")
118-
if err != nil {
119-
return nil, err
120-
}
121-
}
122-
12394
vm, err = s.GetVm(ctx, id)
12495
if err != nil {
12596
return nil, err

0 commit comments

Comments
 (0)