From e9db27a97287372990f50f52dcab3011f7ccb3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Fern=C3=A1ndez?= Date: Sun, 10 Nov 2024 17:31:11 -0300 Subject: [PATCH] feat: Refactor vm update (#15) --- README.md | 3 +- docs/resources/vm.md | 31 +++++++++++++++++- examples/resources/crunchloop_vm/resource.tf | 31 +++++++++++++++++- internal/services/vm_service.go | 33 ++------------------ 4 files changed, 64 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 25b5e76..5456dac 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,8 @@ resource "crunchloop_vm" "vm" { If you wish to contribute to the provider, follow these steps: 1. Clone the repository -2. Build the provider using Go: `go build ./...` +2. Build the provider using Go: `go build -o terraform-provider-crunchloop` +3. Use the `examples` folder to test managing resources with your instance ## Documentation diff --git a/docs/resources/vm.md b/docs/resources/vm.md index 1d1816f..4bfac9e 100644 --- a/docs/resources/vm.md +++ b/docs/resources/vm.md @@ -53,7 +53,36 @@ resource "crunchloop_vm" "without_host" { cores = 1 memory_megabytes = 1024 root_volume_size_gigabytes = 10 - user_data = base64encode("echo 'Hello, World!'") +} + +# Use a cloud-init configuration to configure the VM +# +data "cloudinit_config" "cloudinit" { + gzip = false + base64_encode = true + + part { + filename = "cloud-config.yaml" + content_type = "text/cloud-config" + + content = <<-EOF + #cloud-config + cloud_final_modules: + - [scripts-user, always] + + runcmd: + - echo "Hello, World!" > /tmp/hello.txt + EOF + } +} + +resource "crunchloop_vm" "with_cloudinit" { + name = "terraform-with-cloudinit" + vmi_id = data.crunchloop_vmi.ubuntu.id + cores = 1 + memory_megabytes = 1024 + root_volume_size_gigabytes = 10 + user_data = data.cloudinit_config.cloudinit.rendered } ``` diff --git a/examples/resources/crunchloop_vm/resource.tf b/examples/resources/crunchloop_vm/resource.tf index aacfa7f..69e0a06 100644 --- a/examples/resources/crunchloop_vm/resource.tf +++ b/examples/resources/crunchloop_vm/resource.tf @@ -38,5 +38,34 @@ resource "crunchloop_vm" "without_host" { cores = 1 memory_megabytes = 1024 root_volume_size_gigabytes = 10 - user_data = base64encode("echo 'Hello, World!'") } + +# Use a cloud-init configuration to configure the VM +# +data "cloudinit_config" "cloudinit" { + gzip = false + base64_encode = true + + part { + filename = "cloud-config.yaml" + content_type = "text/cloud-config" + + content = <<-EOF + #cloud-config + cloud_final_modules: + - [scripts-user, always] + + runcmd: + - echo "Hello, World!" > /tmp/hello.txt + EOF + } +} + +resource "crunchloop_vm" "with_cloudinit" { + name = "terraform-with-cloudinit" + vmi_id = data.crunchloop_vmi.ubuntu.id + cores = 1 + memory_megabytes = 1024 + root_volume_size_gigabytes = 10 + user_data = data.cloudinit_config.cloudinit.rendered +} \ No newline at end of file diff --git a/internal/services/vm_service.go b/internal/services/vm_service.go index 5fd80f9..fc76911 100644 --- a/internal/services/vm_service.go +++ b/internal/services/vm_service.go @@ -73,21 +73,6 @@ func (s *VmService) UpdateVm(ctx context.Context, id int32, options client.Updat return nil, err } - var wasRunning = *vm.Status == client.VirtualMachineStatusRunning - - // We need to stop the vm if it's running to update it. - if wasRunning { - _, err = s.client.StopVm(ctx, id) - if err != nil { - return nil, fmt.Errorf("failed to stop vm before updating. Error: %s", err) - } - - err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "stopped") - if err != nil { - return nil, err - } - } - // We are ready to update the vm now. updateResponse, err := s.client.UpdateVmWithResponse(ctx, id, options) if err != nil { @@ -99,27 +84,13 @@ func (s *VmService) UpdateVm(ctx context.Context, id int32, options client.Updat } // After we issue an update, the vm is going to transition to `updating` state - // and eventually will be back to `stopped` state, we need to wait for that + // and eventually will be back to `currentStatus` state, we need to wait for that // state before moving forward. - err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "stopped") + err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, *vm.Status) if err != nil { return nil, err } - // If the vm was running we should turn it on again. - // - if wasRunning { - _, err = s.client.StartVm(ctx, id) - if err != nil { - return nil, fmt.Errorf("failed to start vm after updating. Error: %s", err) - } - - err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "running") - if err != nil { - return nil, err - } - } - vm, err = s.GetVm(ctx, id) if err != nil { return nil, err