-
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.
Merge pull request #81 from cudoventures/storage-disks
Storage disks
- Loading branch information
Showing
16 changed files
with
868 additions
and
28 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
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,35 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "cudo_storage_disk Data Source - terraform-provider-cudo" | ||
subcategory: "" | ||
description: |- | ||
Disk data source | ||
--- | ||
|
||
# cudo_storage_disk (Data Source) | ||
|
||
Disk data source | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "cudo_storage_disk" "storage_disk_datasource" { | ||
id = "my-disk" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `id` (String) Storage disk ID. | ||
|
||
### Optional | ||
|
||
- `project_id` (String) The unique identifier of the project the disk is in. | ||
|
||
### Read-Only | ||
|
||
- `data_center_id` (String) The unique identifier of the datacenter where the disk is located. | ||
- `size_gib` (Number) Size of the storage disk in GiB |
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,34 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "cudo_storage_disk Resource - terraform-provider-cudo" | ||
subcategory: "" | ||
description: |- | ||
Storage disk resource | ||
--- | ||
|
||
# cudo_storage_disk (Resource) | ||
|
||
Storage disk resource | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "cudo_storage_disk" "my-storage-disk" { | ||
data_center_id = "gb-bournemouth-1" | ||
id = "my-disk" | ||
size_gib = 100 | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `data_center_id` (String) The unique identifier of the datacenter where the disk is located. | ||
- `id` (String) The unique identifier of the storage disk | ||
- `size_gib` (Number) Size of the storage disk in GiB | ||
|
||
### Optional | ||
|
||
- `project_id` (String) The project the storage disk is in. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "cudo_storage_disk" "storage_disk_datasource" { | ||
id = "my-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resource "cudo_storage_disk" "my-storage-disk" { | ||
data_center_id = "gb-bournemouth-1" | ||
id = "my-disk" | ||
size_gib = 100 | ||
} |
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
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
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,112 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/CudoVentures/terraform-provider-cudo/internal/compute/vm" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ datasource.DataSource = &StorageDiskDataSource{} | ||
|
||
func NewStorageDiskDataSource() datasource.DataSource { | ||
return &StorageDiskDataSource{} | ||
} | ||
|
||
// SecurityGroupsDataSource defines the data source implementation. | ||
type StorageDiskDataSource struct { | ||
client *CudoClientData | ||
} | ||
|
||
// SecurityGroupDataSourceModel describes the resource data model. | ||
type StorageDiskDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
ProjectID types.String `tfsdk:"project_id"` | ||
DataCenterID types.String `tfsdk:"data_center_id"` | ||
SizeGib types.Int64 `tfsdk:"size_gib"` | ||
} | ||
|
||
func (d *StorageDiskDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = "cudo_storage_disk" | ||
} | ||
|
||
func (d *StorageDiskDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "Disk data source", | ||
Description: "Gets a Disk", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Description: "Storage disk ID.", | ||
Required: true, | ||
}, | ||
"project_id": schema.StringAttribute{ | ||
Description: "The unique identifier of the project the disk is in.", | ||
Optional: true, | ||
}, | ||
"data_center_id": schema.StringAttribute{ | ||
Description: "The unique identifier of the datacenter where the disk is located.", | ||
Computed: true, | ||
}, | ||
"size_gib": schema.Int64Attribute{ | ||
Description: "Size of the storage disk in GiB", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *StorageDiskDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*CudoClientData) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *CudoClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *StorageDiskDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state StorageDiskDataSourceModel | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
projectId := d.client.DefaultProjectID | ||
if !state.ProjectID.IsNull() { | ||
projectId = state.ProjectID.ValueString() | ||
} | ||
|
||
res, err := d.client.VMClient.GetDisk(ctx, &vm.GetDiskRequest{ | ||
ProjectId: projectId, | ||
Id: state.ID.ValueString(), | ||
}) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Unable to read storage disks", | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
state.DataCenterID = types.StringValue(res.Disk.DataCenterId) | ||
state.SizeGib = types.Int64Value(int64(res.Disk.SizeGib)) | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) | ||
} |
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,71 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/CudoVentures/terraform-provider-cudo/internal/compute/vm" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
) | ||
|
||
func TestAcc_StorageDiskDataSource(t *testing.T) { | ||
var cancel context.CancelFunc | ||
ctx := context.Background() | ||
deadline, ok := t.Deadline() | ||
if ok { | ||
ctx, cancel = context.WithDeadline(ctx, deadline) | ||
defer cancel() | ||
} | ||
name := "storage-disk-data-source" + testRunID | ||
|
||
resourcesConfig := fmt.Sprintf(` | ||
resource "cudo_storage_disk" "disk_ds" { | ||
data_center_id = "black-mesa" | ||
id = "%s" | ||
size_gib = 15 | ||
}`, name) | ||
|
||
testAccStorageDiskDataSourceConfig := fmt.Sprintf(` | ||
data "cudo_storage_disk" "storage_disk_datasource" { | ||
id = "%s" | ||
}`, name) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
CheckDestroy: func(state *terraform.State) error { | ||
cl, _ := getClients(t) | ||
|
||
ins, err := cl.GetDisk(ctx, &vm.GetDiskRequest{ | ||
Id: name, | ||
ProjectId: projectID, | ||
}) | ||
|
||
if err == nil && ins.Disk.DiskState != vm.Disk_DISK_STATE_DELETE { | ||
res, err := cl.DeleteStorageDisk(ctx, &vm.DeleteStorageDiskRequest{ | ||
Id: name, | ||
ProjectId: projectID, | ||
}) | ||
t.Logf("(%s) %#v: %v", ins.Disk.DiskState, res, err) | ||
|
||
return fmt.Errorf("disk resource not destroyed %s , %s", ins.Disk.Id, ins.Disk.DiskState) | ||
} | ||
return nil | ||
}, | ||
|
||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getProviderConfig() + resourcesConfig, | ||
}, | ||
{ | ||
Config: getProviderConfig() + resourcesConfig + testAccStorageDiskDataSourceConfig, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.cudo_storage_disk.storage_disk_datasource", "id", name), | ||
resource.TestCheckResourceAttr("data.cudo_storage_disk.storage_disk_datasource", "size_gib", "15"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.