From f802cd0ba55e80e914d4be75e8d4052f74e7ca1d Mon Sep 17 00:00:00 2001 From: Paul BESRET Date: Thu, 25 Jan 2024 16:26:55 +0100 Subject: [PATCH] Fixed a bug causing backup module not to find a virtual disk after running inventory --- CHANGELOG.md | 6 ++ internal/client/backup_virtual_disk.go | 98 +++++++++++++++++++ .../provider/resource_compute_virtual_disk.go | 4 +- 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 internal/client/backup_virtual_disk.go diff --git a/CHANGELOG.md b/CHANGELOG.md index d64590e..df987db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.12.4 (January 25th, 2024) + +BUG FIXES : + + * Fixed a bug causing backup module not to find the virtual disk after running hypervisor inventory. + ## 0.12.3 (November 29th, 2023) BUG FIXES : diff --git a/internal/client/backup_virtual_disk.go b/internal/client/backup_virtual_disk.go new file mode 100644 index 0000000..c39de7d --- /dev/null +++ b/internal/client/backup_virtual_disk.go @@ -0,0 +1,98 @@ +package client + +import ( + "context" + "fmt" + "time" + + "github.com/sethvargo/go-retry" +) + +type BackupVirtualDiskClient struct { + c *Client +} + +func (c *BackupClient) VirtualDisk() *BackupVirtualDiskClient { + return &BackupVirtualDiskClient{c.c} +} + +type BackupVirtualDisk struct { + ID string `terraform:"id"` + Name string `terraform:"name"` + InternalId string `terraform:"internal_id"` + InstanceId string `terraform:"instance_id"` + SPPServerId string `terraform:"spp_server_id"` + VirtualMachineId string `terraform:"virtual_machine_id"` +} + +type BackupVirtualDiskNotFoundError struct { + message string + virtualDisk string +} + +const backupVirtualDiskNotFoundErrorMessage = ` + Message: %s + VirtualDisk: %s +` + +func (b *BackupVirtualDiskNotFoundError) Error() string { + if b.virtualDisk == "" { + return b.message + } + + return fmt.Sprintf( + backupVirtualDiskNotFoundErrorMessage, + b.message, + b.virtualDisk, + ) +} + +func (c *BackupVirtualDiskClient) Read(ctx context.Context, id string) (*BackupVirtualDisk, error) { + r := c.c.newRequest("GET", "/backup/v1/virtual_disks/%s", id) + resp, err := c.c.doRequest(ctx, r) + if err != nil { + return nil, err + } + defer closeResponseBody(resp) + found, err := requireNotFoundOrOK(resp, 404) + if err != nil { + return nil, err + } else if !found { + return nil, nil + } + + var out BackupVirtualDisk + if err := decodeBody(resp, &out); err != nil { + return nil, err + } + + return &out, nil +} + +func (c *BackupVirtualDiskClient) WaitForInventory(ctx context.Context, id string, options *WaiterOptions) (*BackupVirtualDisk, error) { + b := retry.NewFibonacci(1 * time.Second) + b = retry.WithCappedDuration(30*time.Second, b) + + var res *BackupVirtualDisk + var count int + + err := retry.Do(ctx, b, func(ctx context.Context) error { + count++ + virtualDisk, err := c.Read(ctx, id) + if err != nil { + return options.error(err) + } + if virtualDisk == nil { + err := &BackupVirtualDiskNotFoundError{ + message: fmt.Sprintf("the virtual disk %q could not be found", id), + virtualDisk: id, + } + return options.retryableError(err) + } + res = virtualDisk + options.log(fmt.Sprintf("the virtual disk %q has been found.", id)) + return nil + }) + return res, err + +} diff --git a/internal/provider/resource_compute_virtual_disk.go b/internal/provider/resource_compute_virtual_disk.go index e056f0a..59a4e11 100644 --- a/internal/provider/resource_compute_virtual_disk.go +++ b/internal/provider/resource_compute_virtual_disk.go @@ -169,9 +169,9 @@ func computeVirtualDiskCreate(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("failed to update catalog, %s", err) } - _, err = c.Backup().Job().WaitForCompletion(ctx, job.ID, getWaiterOptions(ctx)) + _, err = c.Backup().VirtualDisk().WaitForInventory(ctx, d.Id(), getWaiterOptions(ctx)) if err != nil { - return diag.Errorf("failed to update catalog, %s", err) + return diag.Errorf("failed to find virtual disk in backup inventory : %s", err) } slaPolicies := []string{}