Skip to content

Commit

Permalink
Merge pull request #128 from Cloud-Temple/bugfix/125
Browse files Browse the repository at this point in the history
Fixed a bug causing backup module not to find a virtual disk after running inventory
  • Loading branch information
KChapron committed Feb 8, 2024
2 parents 0d3c311 + 339eeba commit a55bc30
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.12.4-rc.2 (February 8th, 2024)

BUG FIXES :

* Fixed a bug causing backup module not to find the virtual disk after running hypervisor inventory.

## 0.12.4-rc.1 (February 2nd, 2024)

BUG FIXED :
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/compute_datastore.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ data "cloudtemple_compute_datastore" "name" {
- `hosts_names` (List of String)
- `hosts_number` (Number)
- `id` (String) The ID of this resource.
- `maintenance_status` (Boolean)
- `maintenance_status` (String)
- `max_capacity` (Number)
- `moref` (String)
- `type` (String)
Expand Down
98 changes: 98 additions & 0 deletions internal/client/backup_virtual_disk.go
Original file line number Diff line number Diff line change
@@ -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

}
2 changes: 1 addition & 1 deletion internal/client/compute_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Datastore struct {
MaxCapacity int `terraform:"max_capacity"`
FreeCapacity int `terraform:"free_capacity"`
Accessible int `terraform:"accessible"`
MaintenanceStatus bool `terraform:"maintenance_status"`
MaintenanceStatus string `terraform:"maintenance_status"`
UniqueId string `terraform:"unique_id"`
MachineManagerId string `terraform:"machine_manager_id"`
Type string `terraform:"type"`
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/data_source_compute_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func dataSourceDatastore() *schema.Resource {
Computed: true,
},
"maintenance_status": {
Type: schema.TypeBool,
Type: schema.TypeString,
Computed: true,
},
"unique_id": {
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_compute_virtual_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit a55bc30

Please sign in to comment.