Skip to content

Commit

Permalink
Catalog-refactor cloud instance snapshot data source and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Diptipowervs committed Dec 28, 2023
1 parent d751593 commit 246110f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 101 deletions.
183 changes: 98 additions & 85 deletions ibm/service/power/data_source_ibm_pi_cloud_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,96 +6,111 @@ package power
import (
"context"

"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"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"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 DataSourceIBMPICloudInstance() *schema.Resource {

return &schema.Resource{
ReadContext: dataSourceIBMPICloudInstanceRead,
Schema: map[string]*schema.Schema{
helpers.PICloudInstanceId: {
Type: schema.TypeString,
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Start of Computed Attributes
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"capabilities": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"total_processors_consumed": {
Type: schema.TypeFloat,
Computed: true,
// Attributes
Attr_Capabilities: {
Computed: true,
Description: "Lists the capabilities for this cloud instance.",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
},
"total_instances": {
Type: schema.TypeFloat,
Computed: true,
Attr_Enabled: {
Computed: true,
Description: "Indicates whether the tenant is enabled.",
Type: schema.TypeBool,
},
"total_memory_consumed": {
Type: schema.TypeFloat,
Computed: true,
},
"total_ssd_storage_consumed": {
Type: schema.TypeFloat,
Computed: true,
},
"total_standard_storage_consumed": {
Type: schema.TypeFloat,
Computed: true,
},
"pvm_instances": {
Type: schema.TypeList,
Computed: true,
Attr_PVMInstances: {
Computed: true,
Description: "PVM instances owned by the Cloud Instance.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Attr_CreationDate: {
Computed: true,
Description: "Date of PVM instance creation.",
Type: schema.TypeString,
},
"name": {
Type: schema.TypeString,
Computed: true,
Attr_Href: {
Computed: true,
Description: "Link to Cloud Instance resource.",
Type: schema.TypeString,
},
"href": {
Type: schema.TypeString,
Computed: true,
Attr_ID: {
Computed: true,
Description: "PVM Instance ID.",
Type: schema.TypeString,
},
"status": {
Type: schema.TypeString,
Computed: true,
Attr_Name: {
Computed: true,
Description: "Name of the server.",
Type: schema.TypeString,
},
"systype": {
Type: schema.TypeString,
Computed: true,
Attr_Status: {
Computed: true,
Description: "The status of the instance.",
Type: schema.TypeString,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
Attr_SysType: {
Computed: true,
Description: "System type used to host the instance.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
Attr_Region: {
Computed: true,
Description: "The region the cloud instance lives.",
Type: schema.TypeString,
},
Attr_TenantID: {
Computed: true,
Description: "The tenant ID that owns this cloud instance.",
Type: schema.TypeString,
},
Attr_TotalInstances: {
Computed: true,
Description: "The count of lpars that belong to this specific cloud instance.",
Type: schema.TypeFloat,
},
Attr_TotalMemoryConsumed: {
Computed: true,
Description: "The total memory consumed by this service instance.",
Type: schema.TypeFloat,
},
Attr_TotalProcessorsConsumed: {
Computed: true,
Description: "The total processors consumed by this service instance.",
Type: schema.TypeFloat,
},
Attr_TotalSSDStorageConsumed: {
Computed: true,
Description: "The total SSD Storage consumed by this service instance.",
Type: schema.TypeFloat,
},
Attr_TotalStandardStorageConsumed: {
Computed: true,
Description: "The total Standard Storage consumed by this service instance.",
Type: schema.TypeFloat,
},
},
}
Expand All @@ -107,7 +122,7 @@ func dataSourceIBMPICloudInstanceRead(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

cloud_instance := instance.NewIBMPICloudInstanceClient(ctx, sess, cloudInstanceID)
cloud_instance_data, err := cloud_instance.Get(cloudInstanceID)
Expand All @@ -116,35 +131,33 @@ func dataSourceIBMPICloudInstanceRead(ctx context.Context, d *schema.ResourceDat
}

d.SetId(*cloud_instance_data.CloudInstanceID)
d.Set("tenant_id", (cloud_instance_data.TenantID))
d.Set("enabled", cloud_instance_data.Enabled)
d.Set("region", cloud_instance_data.Region)
d.Set("capabilities", cloud_instance_data.Capabilities)
d.Set("pvm_instances", flattenpvminstances(cloud_instance_data.PvmInstances))
d.Set("total_ssd_storage_consumed", cloud_instance_data.Usage.StorageSSD)
d.Set("total_instances", cloud_instance_data.Usage.Instances)
d.Set("total_standard_storage_consumed", cloud_instance_data.Usage.StorageStandard)
d.Set("total_processors_consumed", cloud_instance_data.Usage.Processors)
d.Set("total_memory_consumed", cloud_instance_data.Usage.Memory)

return nil
d.Set(Attr_Capabilities, cloud_instance_data.Capabilities)
d.Set(Attr_Enabled, cloud_instance_data.Enabled)
d.Set(Attr_PVMInstances, flattenpvminstances(cloud_instance_data.PvmInstances))
d.Set(Attr_Region, cloud_instance_data.Region)
d.Set(Attr_TenantID, (cloud_instance_data.TenantID))
d.Set(Attr_TotalInstances, cloud_instance_data.Usage.Instances)
d.Set(Attr_TotalMemoryConsumed, cloud_instance_data.Usage.Memory)
d.Set(Attr_TotalProcessorsConsumed, cloud_instance_data.Usage.Processors)
d.Set(Attr_TotalSSDStorageConsumed, cloud_instance_data.Usage.StorageSSD)
d.Set(Attr_TotalStandardStorageConsumed, cloud_instance_data.Usage.StorageStandard)

return nil
}

func flattenpvminstances(list []*models.PVMInstanceReference) []map[string]interface{} {
pvms := make([]map[string]interface{}, 0)
for _, lpars := range list {

l := map[string]interface{}{
"id": *lpars.PvmInstanceID,
"name": *lpars.ServerName,
"href": *lpars.Href,
"status": *lpars.Status,
"systype": lpars.SysType,
"creation_date": lpars.CreationDate.String(),
Attr_CreationDate: lpars.CreationDate.String(),
Attr_ID: *lpars.PvmInstanceID,
Attr_Href: *lpars.Href,
Attr_Name: *lpars.ServerName,
Attr_Status: *lpars.Status,
Attr_SysType: lpars.SysType,
}
pvms = append(pvms, l)

}
return pvms
}
11 changes: 4 additions & 7 deletions ibm/service/power/data_source_ibm_pi_cloud_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

func TestAccIBMPICloudInstanceDataSource_basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Expand All @@ -29,10 +28,8 @@ func TestAccIBMPICloudInstanceDataSource_basic(t *testing.T) {
}

func testAccCheckIBMPICloudInstanceDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_cloud_instance" "testacc_ds_cloud_instance" {
pi_cloud_instance_id = "%s"
}`, acc.Pi_cloud_instance_id)

return fmt.Sprintf(`
data "ibm_pi_cloud_instance" "testacc_ds_cloud_instance" {
pi_cloud_instance_id = "%s"
}`, acc.Pi_cloud_instance_id)
}
15 changes: 6 additions & 9 deletions website/docs/d/pi_cloud_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ description: |-
Retrieve information about an existing IBM Power Virtual Server Cloud Instance as a read-only data source. For more information, about IBM power virtual server cloud, see [getting started with IBM Power Systems Virtual Servers](https://cloud.ibm.com/docs/power-iaas?topic=power-iaas-getting-started).

## Example usage

```terraform
data "ibm_pi_cloud_instance" "ds_cloud_instance" {
pi_cloud_instance_id = "49fba6c9-23f8-40bc-9899-aca322ee7d5b"
}
```

## Notes:
* Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
* If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
* `region` - `lon`
* `zone` - `lon04`
**Notes**
- Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
- If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
- `region` - `lon`
- `zone` - `lon04`

Example usage:

```terraform
provider "ibm" {
region = "lon"
Expand All @@ -46,7 +44,7 @@ In addition to the argument reference list, you can access the following attribu
- `pvm_instances` - (List) PVM instances owned by the Cloud Instance.

Nested scheme for `pvm_instances`:
- `creation_date` - (String) Date/Time of PVM creation.
- `creation_date` - (String) Date of PVM instance creation.
- `href` - (String) Link to Cloud Instance resource.
- `id` - (String) PVM Instance ID.
- `name` - (String) Name of the server.
Expand All @@ -59,4 +57,3 @@ In addition to the argument reference list, you can access the following attribu
- `total_processors_consumed` - (String) The total processors consumed by this service instance.
- `total_ssd_storage_consumed` - (String) The total SSD Storage consumed by this service instance.
- `total_standard_storage_consumed` - (String) The total Standard Storage consumed by this service instance.

0 comments on commit 246110f

Please sign in to comment.