-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added resource 'cloudtemple_compute_iaas_opensource_virtual_disk'
- Loading branch information
Showing
5 changed files
with
228 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "cloudtemple_compute_iaas_opensource_virtual_disk Resource - terraform-provider-cloudtemple" | ||
subcategory: "Compute" | ||
description: |- | ||
To manage this resource you will need the following roles: | ||
- compute_iaas_opensource_management | ||
- compute_iaas_opensource_read | ||
- activity_read | ||
--- | ||
|
||
# cloudtemple_compute_iaas_opensource_virtual_disk (Resource) | ||
|
||
To manage this resource you will need the following roles: | ||
- `compute_iaas_opensource_management` | ||
- `compute_iaas_opensource_read` | ||
- `activity_read` | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `mode` (String) The mode of the virtual disk. | ||
- `name` (String) The name of the virtual disk. | ||
- `size` (Number) The size of the virtual disk in bytes. | ||
- `storage_repository_id` (String) The ID of the storage repository where the virtual disk is stored. | ||
- `virtual_machine_id` (String) The ID of the virtual machine to which the virtual disk is attached. | ||
|
||
### Optional | ||
|
||
- `bootable` (Boolean) Whether the virtual disk is bootable. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of the virtual disk. | ||
- `usage` (String) The usage of the virtual disk. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
internal/provider/resource_compute_openiaas_virtual_disk.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloud-temple/terraform-provider-cloudtemple/internal/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func resourceOpenIaasVirtualDisk() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: openIaasVirtualDiskCreate, | ||
ReadContext: openIaasVirtualDiskRead, | ||
UpdateContext: openIaasVirtualDiskUpdate, | ||
DeleteContext: openIaasVirtualDiskDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
// In | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "The name of the virtual disk.", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"size": { | ||
Type: schema.TypeInt, | ||
Description: "The size of the virtual disk in bytes.", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"mode": { | ||
Type: schema.TypeString, | ||
Description: "The mode of the virtual disk.", | ||
ValidateFunc: validation.StringInSlice([]string{"RW", "RO"}, false), | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"storage_repository_id": { | ||
Type: schema.TypeString, | ||
Description: "The ID of the storage repository where the virtual disk is stored.", | ||
Required: true, | ||
ValidateFunc: validation.IsUUID, | ||
ForceNew: true, | ||
}, | ||
"virtual_machine_id": { | ||
Type: schema.TypeString, | ||
Description: "The ID of the virtual machine to which the virtual disk is attached.", | ||
Required: true, | ||
ValidateFunc: validation.IsUUID, | ||
ForceNew: true, | ||
}, | ||
"bootable": { | ||
Type: schema.TypeBool, | ||
Description: "Whether the virtual disk is bootable.", | ||
Optional: true, | ||
Default: false, | ||
ForceNew: true, | ||
}, | ||
|
||
// Out | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The ID of the virtual disk.", | ||
}, | ||
"usage": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The usage of the virtual disk.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func openIaasVirtualDiskCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := getClient(meta) | ||
|
||
activityId, err := c.Compute().OpenIaaS().VirtualDisk().Create(ctx, &client.OpenIaaSVirtualDiskCreateRequest{ | ||
Name: d.Get("name").(string), | ||
Size: d.Get("size").(int), | ||
Mode: d.Get("mode").(string), | ||
StorageRepositoryID: d.Get("storage_repository_id").(string), | ||
VirtualMachineID: d.Get("virtual_machine_id").(string), | ||
Bootable: d.Get("bootable").(bool), | ||
}) | ||
if err != nil { | ||
return diag.Errorf("the virtual disk could not be created: %s", err) | ||
} | ||
activity, err := c.Activity().WaitForCompletion(ctx, activityId, getWaiterOptions((ctx))) | ||
setIdFromActivityState(d, activity) | ||
if err != nil { | ||
return diag.Errorf("the virtual disk could not be created: %s", err) | ||
} | ||
|
||
return openIaasVirtualDiskUpdate(ctx, d, meta) | ||
} | ||
|
||
func openIaasVirtualDiskRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := getClient(meta) | ||
virtualDisk, err := c.Compute().OpenIaaS().VirtualDisk().Read(ctx, d.Id()) | ||
if err != nil { | ||
return diag.Errorf("the virtual disk could not be read: %s", err) | ||
} | ||
if virtualDisk == nil { | ||
return diag.Errorf("the virtual disk could not be found: %s", err) | ||
} | ||
|
||
// Set the retrieved data to the schema | ||
d.Set("name", virtualDisk.Name) | ||
d.Set("size", virtualDisk.Size) | ||
d.Set("usage", virtualDisk.Usage) | ||
d.Set("storage_repository_id", virtualDisk.StorageRepository.ID) | ||
|
||
return nil | ||
} | ||
|
||
func openIaasVirtualDiskUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
// TODO : Implement Detach on API to be able to change a virtual disk VM attachment | ||
|
||
// TODO : Implement this on Compute API | ||
// // Update the virtual disk properties if they have changed | ||
// if d.HasChange("name") || d.HasChange("size") || d.HasChange("mode") || d.HasChange("storage_repository_id") || d.HasChange("bootable") { | ||
// _, err := c.Compute().OpenIaaS().VirtualDisk().Update(ctx, d.Id(), &client.OpenIaaSVirtualDiskUpdateRequest{ | ||
// Name: d.Get("name").(string), | ||
// Size: d.Get("size").(int), | ||
// Mode: d.Get("mode").(string), | ||
// StorageRepositoryID: d.Get("storage_repository_id").(string), | ||
// Bootable: d.Get("bootable").(bool), | ||
// }) | ||
// if err != nil { | ||
// return diag.Errorf("failed to update virtual disk: %s", err) | ||
// } | ||
// } | ||
|
||
return openIaasVirtualDiskRead(ctx, d, meta) | ||
} | ||
|
||
func openIaasVirtualDiskDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := getClient(meta) | ||
|
||
activityId, err := c.Compute().OpenIaaS().VirtualDisk().Delete(ctx, d.Id()) | ||
if err != nil { | ||
return diag.Errorf("failed to delete virtual disk: %s", err) | ||
} | ||
if _, err = c.Activity().WaitForCompletion(ctx, activityId, getWaiterOptions(ctx)); err != nil { | ||
return diag.Errorf("failed to delete virtual disk, %s", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters