Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tenant, storage pool, storage type, and volume flash copy data source and documentation #41

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 29 additions & 34 deletions ibm/service/power/data_source_ibm_pi_storage_pool_capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,53 @@ package power
import (
"context"
"fmt"

"log"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"

"github.com/IBM-Cloud/power-go-client/helpers"
"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"
)

const (
PIPoolName = "pi_storage_pool"
)

func DataSourceIBMPIStoragePoolCapacity() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIStoragePoolCapacityRead,
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,
},
PIPoolName: {
Type: schema.TypeString,
Arg_StoragePool: {
Description: "The storage pool name.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
Description: "Storage pool name",
},
// Computed Attributes
MaxAllocationSize: {
Type: schema.TypeInt,

// Attributes
Attr_MaxAllocationSize: {
Computed: true,
Description: "Maximum allocation storage size (GB)",
Description: "Maximum allocation storage size (GB).",
Type: schema.TypeInt,
},
StorageType: {
Type: schema.TypeString,
Attr_ReplicationEnabled: {
Computed: true,
Description: "Storage type of the storage pool",
Description: "Replication status of the storage pool.",
Type: schema.TypeBool,
},
TotalCapacity: {
Type: schema.TypeInt,
Attr_StorageType: {
Computed: true,
Description: "Total pool capacity (GB)",
Description: "Storage type of the storage pool.",
Type: schema.TypeString,
},
ReplicationEnabled: {
Type: schema.TypeBool,
Attr_TotalCapacity: {
Computed: true,
Description: "Replication status of the storage pool",
Description: "Total pool capacity (GB).",
Type: schema.TypeInt,
},
},
}
Expand All @@ -69,20 +64,20 @@ func dataSourceIBMPIStoragePoolCapacityRead(ctx context.Context, d *schema.Resou
return diag.FromErr(err)
}

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

client := st.NewIBMPIStorageCapacityClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPIStorageCapacityClient(ctx, sess, cloudInstanceID)
sp, err := client.GetStoragePoolCapacity(storagePool)
if err != nil {
log.Printf("[ERROR] get storage pool capacity failed %v", err)
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, storagePool))
d.Set(MaxAllocationSize, *sp.MaxAllocationSize)
d.Set(StorageType, sp.StorageType)
d.Set(TotalCapacity, sp.TotalCapacity)
d.Set(ReplicationEnabled, *sp.ReplicationEnabled)
d.Set(Attr_MaxAllocationSize, *sp.MaxAllocationSize)
d.Set(Attr_ReplicationEnabled, *sp.ReplicationEnabled)
d.Set(Attr_StorageType, sp.StorageType)
d.Set(Attr_TotalCapacity, sp.TotalCapacity)
return nil
}
94 changes: 40 additions & 54 deletions ibm/service/power/data_source_ibm_pi_storage_pools_capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,82 @@ package power

import (
"context"

"log"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"

"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/hashicorp/go-uuid"
"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"
)

const (
MaximumStorageAllocation = "maximum_storage_allocation"
StoragePoolsCapacity = "storage_pools_capacity"
MaxAllocationSize = "max_allocation_size"
PoolName = "pool_name"
StoragePool = "storage_pool"
StorageType = "storage_type"
TotalCapacity = "total_capacity"
ReplicationEnabled = "replication_enabled"
)

func DataSourceIBMPIStoragePoolsCapacity() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIStoragePoolsCapacityRead,
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,
},
// Computed Attributes
MaximumStorageAllocation: {
Type: schema.TypeMap,

// Attributes
Attr_MaximumStorageAllocation: {
Computed: true,
Description: "Maximum storage allocation",
Description: "Maximum storage allocation.",
Type: schema.TypeMap,
},
StoragePoolsCapacity: {
Type: schema.TypeList,
Attr_StoragePoolsCapacity: {
Computed: true,
Description: "Storage pools capacity",
Description: "List of storage pools capacity.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
MaxAllocationSize: {
Type: schema.TypeInt,
Attr_MaxAllocationSize: {
Computed: true,
Description: "Maximum allocation storage size (GB)",
Description: "Maximum allocation storage size (GB).",
Type: schema.TypeInt,
},
PoolName: {
Type: schema.TypeString,
Attr_PoolName: {
Computed: true,
Description: "Pool name",
},
StorageType: {
Description: "The pool name.",
Type: schema.TypeString,
},
Attr_ReplicationEnabled: {
Computed: true,
Description: "Storage type of the storage pool",
Description: "Replication status of the storage pool.",
Type: schema.TypeBool,
},
TotalCapacity: {
Type: schema.TypeInt,
Attr_StorageType: {
Computed: true,
Description: "Total pool capacity (GB)",
Description: "Storage type of the storage pool.",
Type: schema.TypeString,
},
ReplicationEnabled: {
Type: schema.TypeBool,
Attr_TotalCapacity: {
Computed: true,
Description: "Replication status of the storage pool",
Description: "Total pool capacity (GB).",
Type: schema.TypeInt,
},
},
},
Type: schema.TypeList,
},
},
}
}

func dataSourceIBMPIStoragePoolsCapacityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

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

client := st.NewIBMPIStorageCapacityClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPIStorageCapacityClient(ctx, sess, cloudInstanceID)
spc, err := client.GetAllStoragePoolsCapacity()
if err != nil {
log.Printf("[ERROR] get all storage pools capacity failed %v", err)
Expand All @@ -106,26 +93,25 @@ func dataSourceIBMPIStoragePoolsCapacityRead(ctx context.Context, d *schema.Reso
if spc.MaximumStorageAllocation != nil {
msa := spc.MaximumStorageAllocation
data := map[string]interface{}{
MaxAllocationSize: *msa.MaxAllocationSize,
StoragePool: *msa.StoragePool,
StorageType: *msa.StorageType,
Attr_MaxAllocationSize: *msa.MaxAllocationSize,
Attr_StoragePool: *msa.StoragePool,
Attr_StorageType: *msa.StorageType,
}
d.Set(MaximumStorageAllocation, flex.Flatten(data))
d.Set(Attr_MaximumStorageAllocation, flex.Flatten(data))
}

result := make([]map[string]interface{}, 0, len(spc.StoragePoolsCapacity))
for _, sp := range spc.StoragePoolsCapacity {
data := map[string]interface{}{
MaxAllocationSize: *sp.MaxAllocationSize,
PoolName: sp.PoolName,
StorageType: sp.StorageType,
TotalCapacity: sp.TotalCapacity,
ReplicationEnabled: *sp.ReplicationEnabled,
Attr_MaxAllocationSize: *sp.MaxAllocationSize,
Attr_PoolName: sp.PoolName,
Attr_ReplicationEnabled: *sp.ReplicationEnabled,
Attr_StorageType: sp.StorageType,
Attr_TotalCapacity: sp.TotalCapacity,
}

result = append(result, data)
}
d.Set(StoragePoolsCapacity, result)
d.Set(Attr_StoragePoolsCapacity, result)

return nil
}
Loading
Loading