forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aa2176
commit a91f788
Showing
7 changed files
with
192 additions
and
7 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
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,100 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
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/power/models" | ||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" | ||
"github.com/hashicorp/go-uuid" | ||
) | ||
|
||
func DataSourceIBMPIStorageTiers() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceIBMPIStorageTiersRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
// Arguments | ||
Arg_CloudInstanceID: { | ||
Description: "The GUID of the service instance associated with an account.", | ||
Required: true, | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
// Attributes | ||
Attr_RegionStorageTiers: { | ||
Computed: true, | ||
Description: "An array of of storage tiers supported in a region.", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
Attr_Description: { | ||
Computed: true, | ||
Description: "Description, storage tier label.", | ||
Type: schema.TypeString, | ||
}, | ||
Attr_Name: { | ||
Computed: true, | ||
Description: "Name of the storage tier.", | ||
Type: schema.TypeString, | ||
}, | ||
Attr_State: { | ||
Computed: true, | ||
Description: "State of the storage tier (active or inactive).", | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
Type: schema.TypeList, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceIBMPIStorageTiersRead(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(Arg_CloudInstanceID).(string) | ||
|
||
client := instance.NewIBMPIStorageTierClient(ctx, sess, cloudInstanceID) | ||
rst, err := client.GetAll() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
var genID, _ = uuid.GenerateUUID() | ||
d.SetId(genID) | ||
|
||
regionStorageTiers := []map[string]interface{}{} | ||
if len(rst) > 0 { | ||
for _, storageTier := range rst { | ||
regionStorageTier := storageTierToMap(storageTier) | ||
regionStorageTiers = append(regionStorageTiers, regionStorageTier) | ||
} | ||
} | ||
d.Set(Attr_RegionStorageTiers, regionStorageTiers) | ||
|
||
return nil | ||
} | ||
|
||
func storageTierToMap(storageTier *models.StorageTier) map[string]interface{} { | ||
storageTierMap := make(map[string]interface{}) | ||
if storageTier.Description != "" { | ||
storageTierMap[Attr_Description] = storageTier.Description | ||
} | ||
if storageTier.Name != "" { | ||
storageTierMap[Attr_Name] = storageTier.Name | ||
} | ||
if storageTier.State != nil { | ||
storageTierMap[Attr_State] = storageTier.State | ||
} | ||
return storageTierMap | ||
} |
35 changes: 35 additions & 0 deletions
35
ibm/service/power/data_source_ibm_pi_storage_tiers_test.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,35 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package power_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" | ||
) | ||
|
||
func TestAccIBMPIStorageTiersDataSourceBasic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.TestAccPreCheck(t) }, | ||
Providers: acc.TestAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckIBMPIStorageTiersDataSourceConfigBasic(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.ibm_pi_storage_tiers.storage_tiers", "id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMPIStorageTiersDataSourceConfigBasic() string { | ||
return fmt.Sprintf(` | ||
data "ibm_pi_storage_tiers" "storage_tiers" { | ||
pi_cloud_instance_id = "%s" | ||
}`, acc.Pi_cloud_instance_id) | ||
} |
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,52 @@ | ||
--- | ||
layout: "ibm" | ||
page_title: "IBM : ibm_pi_storage_tiers" | ||
description: |- | ||
Get information about a storage tiers for a pi cloud instance. | ||
subcategory: "Power Systems" | ||
--- | ||
|
||
# ibm_pi_storage_tiers | ||
|
||
Retrieve information about supported storage tiers for a pi cloud instance. For more information, see [storage tiers docs](https://cloud.ibm.com/apidocs/power-cloud#pcloud-cloudinstances-storagetiers-getall). | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "ibm_pi_storage_tiers" "pi_storage_tiers" { | ||
pi_cloud_instance_id = "<value of the cloud_instance_id>" | ||
} | ||
``` | ||
|
||
### 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" | ||
zone = "lon04" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
Review the argument references that you can specify for your data source. | ||
|
||
- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all argument reference list, you can access the following attribute references after your data source is created. | ||
|
||
- `id` - (String) The unique identifier of the storage tiers. | ||
- `region_storage_tiers` - (List) An array of of storage tiers supported in a region. | ||
Nested schema for `region_storage_tiers`: | ||
- `description` - (String) Description, storage tier label. | ||
- `name` - (String) Name of the storage tier. | ||
- `state` - (String) State of the storage tier, `active` or `inactive`. |