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 image list-catalog data source and documentation #23

Closed
wants to merge 9 commits into from
256 changes: 134 additions & 122 deletions ibm/service/power/data_source_ibm_pi_catalog_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,108 +7,125 @@ import (
"context"
"time"

"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"
"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/terraform-provider-ibm/ibm/conns"
)

/*
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 DataSourceIBMPICatalogImages() *schema.Resource {

return &schema.Resource{
ReadContext: dataSourceIBMPICatalogImagesRead,
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,
},
"sap": {
Type: schema.TypeBool,
Optional: true,
Arg_SAP: {
Description: "Set true to include SAP images. The default value is false.",
Optional: true,
Type: schema.TypeBool,
},
"vtl": {
Type: schema.TypeBool,
Optional: true,
Arg_VTL: {
Description: "Set true to include VTL images. The default value is false.",
Optional: true,
Type: schema.TypeBool,
},
"images": {
Type: schema.TypeList,
Computed: true,

// Attributes
Attr_Images: {
Computed: true,
Description: "Lists all the images in the IBM Power Virtual Server Cloud.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"image_id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"storage_type": {
Type: schema.TypeString,
Computed: true,
},
"storage_pool": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"last_update_date": {
Type: schema.TypeString,
Computed: true,
},
"image_type": {
Type: schema.TypeString,
Computed: true,
},
"container_format": {
Type: schema.TypeString,
Computed: true,
},
"disk_format": {
Type: schema.TypeString,
Computed: true,
},
"operating_system": {
Type: schema.TypeString,
Computed: true,
},
"hypervisor_type": {
Type: schema.TypeString,
Computed: true,
},
"architecture": {
Type: schema.TypeString,
Computed: true,
},
"endianness": {
Type: schema.TypeString,
Computed: true,
},
"href": {
Type: schema.TypeString,
Computed: true,
Attr_Architecture: {
Computed: true,
Description: "The CPU architecture that the image is designed for.",
Type: schema.TypeString,
},
Attr_ContainerFormat: {
Computed: true,
Description: "The container format.",
Type: schema.TypeString,
},
Attr_CreationDate: {
Computed: true,
Description: "Date of image creation",
Type: schema.TypeString,
},
Attr_Description: {
Computed: true,
Description: "The description of an image.",
Type: schema.TypeString,
},
Attr_DiskFormat: {
Computed: true,
Description: "The disk format.",
Type: schema.TypeString,
},
Attr_Endianness: {
Computed: true,
Description: "The Endianness order.",
Type: schema.TypeString,
},
Attr_Href: {
Computed: true,
Description: "The href of an image.",
Type: schema.TypeString,
},
Attr_HypervisorType: {
Computed: true,
Description: "Hypervisor type.",
Type: schema.TypeString,
},
Attr_ImageID: {
Computed: true,
Description: "The unique identifier of an image.",
Type: schema.TypeString,
},
Attr_ImageType: {
Computed: true,
Description: "The identifier of this image type.",
Type: schema.TypeString,
},
Attr_LastUpdateDate: {
Computed: true,
Description: "The last updated date of an image.",
Type: schema.TypeString,
},
Attr_Name: {
Computed: true,
Description: "The name of the image.",
Type: schema.TypeString,
},
Attr_OperatingSystem: {
Computed: true,
Description: "Operating System.",
Type: schema.TypeString,
},
Attr_State: {
Computed: true,
Description: "The state of an Operating System.",
Type: schema.TypeString,
},
Attr_StoragePool: {
Computed: true,
Description: "Storage pool where image resides.",
Type: schema.TypeString,
},
Attr_StorageType: {
Computed: true,
Description: "The storage type of an image.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
}
Expand All @@ -120,15 +137,9 @@ func dataSourceIBMPICatalogImagesRead(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
includeSAP := false
if s, ok := d.GetOk("sap"); ok {
includeSAP = s.(bool)
}
includeVTL := false
if v, ok := d.GetOk("vtl"); ok {
includeVTL = v.(bool)
}
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
includeSAP := d.Get(Arg_SAP).(bool)
includeVTL := d.Get(Arg_VTL).(bool)
imageC := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
stockImages, err := imageC.GetAllStockImages(includeSAP, includeVTL)
if err != nil {
Expand All @@ -138,57 +149,58 @@ func dataSourceIBMPICatalogImagesRead(ctx context.Context, d *schema.ResourceDat
images := make([]map[string]interface{}, 0)
for _, i := range stockImages.Images {
image := make(map[string]interface{})
image["image_id"] = *i.ImageID
image["name"] = *i.Name
if i.State != nil {
image["state"] = *i.State
}
image[Attr_ImageID] = *i.ImageID
image[Attr_Name] = *i.Name

if i.Description != nil {
image["description"] = *i.Description
}
if i.StorageType != nil {
image["storage_type"] = *i.StorageType
}
if i.StoragePool != nil {
image["storage_pool"] = *i.StoragePool
image[Attr_Description] = *i.Description
}
if i.CreationDate != nil {
image["creation_date"] = i.CreationDate.String()
}
if i.LastUpdateDate != nil {
image["last_update_date"] = i.LastUpdateDate.String()
image[Attr_CreationDate] = i.CreationDate.String()
}
if i.Href != nil {
image["href"] = *i.Href
image[Attr_Href] = *i.Href
}
if i.LastUpdateDate != nil {
image[Attr_LastUpdateDate] = i.LastUpdateDate.String()
}
if i.Specifications != nil {
s := i.Specifications
if s.ImageType != "" {
image["image_type"] = s.ImageType
if s.Architecture != "" {
image[Attr_Architecture] = s.Architecture
}
if s.ContainerFormat != "" {
image["container_format"] = s.ContainerFormat
image[Attr_ContainerFormat] = s.ContainerFormat
}
if s.DiskFormat != "" {
image["disk_format"] = s.DiskFormat
image[Attr_DiskFormat] = s.DiskFormat
}
if s.OperatingSystem != "" {
image["operating_system"] = s.OperatingSystem
if s.Endianness != "" {
image[Attr_Endianness] = s.Endianness
}
if s.HypervisorType != "" {
image["hypervisor_type"] = s.HypervisorType
image[Attr_HypervisorType] = s.HypervisorType
}
if s.Architecture != "" {
image["architecture"] = s.Architecture
if s.ImageType != "" {
image[Attr_ImageType] = s.ImageType
}
if s.Endianness != "" {
image["endianness"] = s.Endianness
if s.OperatingSystem != "" {
image[Attr_OperatingSystem] = s.OperatingSystem
}
}
if i.State != nil {
image[Attr_State] = *i.State
}
if i.StoragePool != nil {
image[Attr_StoragePool] = *i.StoragePool
}
if i.StorageType != nil {
image[Attr_StorageType] = *i.StorageType
}
images = append(images, image)
}
d.SetId(time.Now().UTC().String())
d.Set("images", images)
return nil
d.Set(Attr_Images, images)

return nil
}
36 changes: 16 additions & 20 deletions ibm/service/power/data_source_ibm_pi_catalog_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,34 @@ import (

func testAccCheckIBMPICatalogImagesDataSourceBasicConfig() string {
return fmt.Sprintf(`
data "ibm_pi_catalog_images" "power_catalog_images_basic" {
pi_cloud_instance_id = "%s"
}
`, acc.Pi_cloud_instance_id)
data "ibm_pi_catalog_images" "power_catalog_images_basic" {
pi_cloud_instance_id = "%s"
}`, acc.Pi_cloud_instance_id)
}

func testAccCheckIBMPICatalogImagesDataSourceSAPConfig() string {
return fmt.Sprintf(`
data "ibm_pi_catalog_images" "power_catalog_images_sap" {
pi_cloud_instance_id = "%s"
sap = "true"
}
`, acc.Pi_cloud_instance_id)
data "ibm_pi_catalog_images" "power_catalog_images_sap" {
pi_cloud_instance_id = "%s"
sap = "true"
}`, acc.Pi_cloud_instance_id)
}

func testAccCheckIBMPICatalogImagesDataSourceVTLConfig() string {
return fmt.Sprintf(`
data "ibm_pi_catalog_images" "power_catalog_images_vtl" {
pi_cloud_instance_id = "%s"
vtl = "true"
}
`, acc.Pi_cloud_instance_id)
data "ibm_pi_catalog_images" "power_catalog_images_vtl" {
pi_cloud_instance_id = "%s"
vtl = "true"
}`, acc.Pi_cloud_instance_id)
}

func testAccCheckIBMPICatalogImagesDataSourceSAP_And_VTLConfig() string {
return fmt.Sprintf(`
data "ibm_pi_catalog_images" "power_catalog_images_sap_and_vtl" {
pi_cloud_instance_id = "%s"
sap = "true"
vtl = "true"
}
`, acc.Pi_cloud_instance_id)
data "ibm_pi_catalog_images" "power_catalog_images_sap_and_vtl" {
pi_cloud_instance_id = "%s"
sap = "true"
vtl = "true"
}`, acc.Pi_cloud_instance_id)
}

func TestAccIBMPICatalogImagesDataSourceBasic(t *testing.T) {
Expand Down
Loading
Loading