Skip to content

Commit

Permalink
Catalog-refactor image 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 d113b02 commit 422e5d0
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 123 deletions.
94 changes: 50 additions & 44 deletions ibm/service/power/data_source_ibm_pi_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,72 @@ package power
import (
"context"

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

//"fmt"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func DataSourceIBMPIImage() *schema.Resource {

return &schema.Resource{
ReadContext: dataSourceIBMPIImagesRead,
Schema: map[string]*schema.Schema{

helpers.PIImageName: {
Type: schema.TypeString,
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Description: "Imagename Name to be used for pvminstances",
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Arg_ImageName: {
Description: "The ID of the image.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

"state": {
Type: schema.TypeString,
Computed: true,
// Attributes
Attr_Architecture: {
Computed: true,
Description: "The CPU architecture that the image is designed for. ",
Type: schema.TypeString,
},
"size": {
Type: schema.TypeInt,
Computed: true,
Attr_Hypervisor: {
Computed: true,
Description: "Hypervision Type.",
Type: schema.TypeString,
},
"architecture": {
Type: schema.TypeString,
Computed: true,
Attr_ImageType: {
Computed: true,
Description: "The identifier of this image type.",
Type: schema.TypeString,
},
// TODO: Relabel this one "operating_system" to match catalog images
"operatingsystem": {
Type: schema.TypeString,
Computed: true,
Computed: true,
Description: "The operating system that is installed with the image.",
Type: schema.TypeString,
},
"hypervisor": {
Type: schema.TypeString,
Computed: true,
Attr_Size: {
Computed: true,
Description: "The size of the image in megabytes.",
Type: schema.TypeInt,
},
"storage_type": {
Type: schema.TypeString,
Computed: true,
Attr_State: {
Computed: true,
Description: "The state for this image. ",
Type: schema.TypeString,
},
"storage_pool": {
Type: schema.TypeString,
Computed: true,
Attr_StoragePool: {
Computed: true,
Description: "Storage pool where image resides.",
Type: schema.TypeString,
},
"image_type": {
Type: schema.TypeString,
Computed: true,
Attr_StorageType: {
Computed: true,
Description: "The storage type for this image.",
Type: schema.TypeString,
},
},
}
Expand All @@ -76,24 +83,23 @@ func dataSourceIBMPIImagesRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(err)
}

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

imageC := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
imagedata, err := imageC.Get(d.Get(helpers.PIImageName).(string))
imagedata, err := imageC.Get(d.Get(Arg_ImageName).(string))
if err != nil {
return diag.FromErr(err)
}

d.SetId(*imagedata.ImageID)
d.Set("state", imagedata.State)
d.Set("size", imagedata.Size)
d.Set("architecture", imagedata.Specifications.Architecture)
d.Set("hypervisor", imagedata.Specifications.HypervisorType)
d.Set(Attr_Architecture, imagedata.Specifications.Architecture)
d.Set(Attr_Hypervisor, imagedata.Specifications.HypervisorType)
d.Set(Attr_ImageType, imagedata.Specifications.ImageType)
d.Set("operatingsystem", imagedata.Specifications.OperatingSystem)
d.Set("storage_type", imagedata.StorageType)
d.Set("storage_pool", imagedata.StoragePool)
d.Set("image_type", imagedata.Specifications.ImageType)
d.Set(Attr_Size, imagedata.Size)
d.Set(Attr_State, imagedata.State)
d.Set(Attr_StoragePool, imagedata.StoragePool)
d.Set(Attr_StorageType, imagedata.StorageType)

return nil

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

func TestAccIBMPIImageDataSource_basic(t *testing.T) {

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

func testAccCheckIBMPIImageDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_image" "testacc_ds_image" {
pi_image_name = "%s"
pi_cloud_instance_id = "%s"
}`, acc.Pi_image, acc.Pi_cloud_instance_id)

data "ibm_pi_image" "testacc_ds_image" {
pi_image_name = "%s"
pi_cloud_instance_id = "%s"
}`, acc.Pi_image, acc.Pi_cloud_instance_id)
}
98 changes: 48 additions & 50 deletions ibm/service/power/data_source_ibm_pi_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,72 @@ package power
import (
"context"

"github.com/IBM-Cloud/power-go-client/helpers"
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

//"fmt"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
Datasource to get the list of images that are available when a power instance is created
*/
// Datasource to list images that are available when a power instance is created
func DataSourceIBMPIImages() *schema.Resource {

return &schema.Resource{
ReadContext: dataSourceIBMPIImagesAllRead,
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

"image_info": {
Type: schema.TypeList,
Computed: true,
// Attributes
Attr_ImageInfo: {
Computed: true,
Description: "List of all supported images.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Attr_Href: {
Computed: true,
Description: "The hyper link of an image.",
Type: schema.TypeString,
},
"name": {
Type: schema.TypeString,
Computed: true,
Attr_ID: {
Computed: true,
Description: "The unique identifier of an image.",
Type: schema.TypeString,
},
"href": {
Type: schema.TypeString,
Computed: true,
Attr_ImageType: {
Computed: true,
Description: "The identifier of this image type.",
Type: schema.TypeString,
},
"state": {
Type: schema.TypeString,
Computed: true,
Attr_Name: {
Computed: true,
Description: "The name of an image.",
Type: schema.TypeString,
},
"storage_type": {
Type: schema.TypeString,
Computed: true,
Attr_State: {
Computed: true,
Description: "The state of an image.",
Type: schema.TypeString,
},
"storage_pool": {
Type: schema.TypeString,
Computed: true,
Attr_StoragePool: {
Computed: true,
Description: "Storage pool where image resides.",
Type: schema.TypeString,
},
"image_type": {
Type: schema.TypeString,
Computed: true,
Attr_StorageType: {
Computed: true,
Description: "The storage type of an image.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
}
Expand All @@ -81,7 +83,7 @@ func dataSourceIBMPIImagesAllRead(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}

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

imageC := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
imagedata, err := imageC.GetAll()
Expand All @@ -91,28 +93,24 @@ func dataSourceIBMPIImagesAllRead(ctx context.Context, d *schema.ResourceData, m

var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)
d.Set("image_info", flattenStockImages(imagedata.Images))
d.Set(Attr_ImageInfo, flattenStockImages(imagedata.Images))

return nil

}

func flattenStockImages(list []*models.ImageReference) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {

l := map[string]interface{}{
"id": *i.ImageID,
"state": *i.State,
"href": *i.Href,
"name": *i.Name,
"storage_type": *i.StorageType,
"storage_pool": *i.StoragePool,
"image_type": i.Specifications.ImageType,
Attr_Href: *i.Href,
Attr_ID: *i.ImageID,
Attr_ImageType: i.Specifications.ImageType,
Attr_Name: *i.Name,
Attr_State: *i.State,
Attr_StoragePool: *i.StoragePool,
Attr_StorageType: *i.StorageType,
}

result = append(result, l)

}
return result
}
8 changes: 3 additions & 5 deletions ibm/service/power/data_source_ibm_pi_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

func TestAccIBMPIImagesDataSource_basic(t *testing.T) {

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

func testAccCheckIBMPIImagesDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_images" "testacc_ds_image" {
pi_cloud_instance_id = "%s"
}`, acc.Pi_cloud_instance_id)

data "ibm_pi_images" "testacc_ds_image" {
pi_cloud_instance_id = "%s"
}`, acc.Pi_cloud_instance_id)
}
17 changes: 8 additions & 9 deletions website/docs/d/pi_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ description: |-
Import the details of an existing IBM Power Virtual Server Cloud image 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_image" "ds_image" {
pi_image_name = "7200-03-03"
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`

Example usage:
**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 @@ -44,10 +42,11 @@ Review the argument references that you can specify for your data source.
In addition to all argument reference list, you can access the following attribute references after your data source is created.

- `architecture` - (String) The CPU architecture that the image is designed for.
- `hypervisor` - (String) Hypervisor type.
- `id` - (String) The unique identifier of the image.
- `image_type` - (String) The identifier of this image type.
- `operatingsystem` - (String) The operating system that is installed with the image.
- `size` - (String) The size of the image in megabytes.
- `state` - (String) The state for this image.
- `storage_type` - (String) The storage type for this image.
- `storage_pool` - (String) Storage pool where image resides.
- `image_type` - (String) The identifier of this image type.
Loading

0 comments on commit 422e5d0

Please sign in to comment.