From 928af697545a0cf73c7c959d538b000e329d45c2 Mon Sep 17 00:00:00 2001 From: Alexander Kita Date: Wed, 22 Jan 2025 09:06:55 -0600 Subject: [PATCH] Refactor Image Export --- ibm/service/power/ibm_pi_constants.go | 4 + .../power/resource_ibm_pi_image_export.go | 102 +++++++++--------- .../resource_ibm_pi_image_export_test.go | 26 ++--- website/docs/r/pi_image_export.html.markdown | 8 +- 4 files changed, 74 insertions(+), 66 deletions(-) diff --git a/ibm/service/power/ibm_pi_constants.go b/ibm/service/power/ibm_pi_constants.go index 77ae403ce0..5b3142a3ec 100644 --- a/ibm/service/power/ibm_pi_constants.go +++ b/ibm/service/power/ibm_pi_constants.go @@ -34,6 +34,10 @@ const ( Arg_IBMiCSS = "pi_ibmi_css" Arg_IBMiPHA = "pi_ibmi_pha" Arg_IBMiRDSUsers = "pi_ibmi_rds_users" + Arg_ImageBucketName = "pi_image_bucket_name" + Arg_ImageBucketRegion = "pi_image_bucket_region" + Arg_ImageSecretKey = "pi_image_secret_key" + Arg_ImageAccessKey = "pi_image_access_key" Arg_ImageID = "pi_image_id" Arg_ImageImportDetails = "pi_image_import_details" Arg_ImageName = "pi_image_name" diff --git a/ibm/service/power/resource_ibm_pi_image_export.go b/ibm/service/power/resource_ibm_pi_image_export.go index ed3df33d6a..50f782d120 100644 --- a/ibm/service/power/resource_ibm_pi_image_export.go +++ b/ibm/service/power/resource_ibm_pi_image_export.go @@ -9,14 +9,13 @@ import ( "log" "time" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - - st "github.com/IBM-Cloud/power-go-client/clients/instance" - "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/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "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 ResourceIBMPIImageExport() *schema.Resource { @@ -31,46 +30,51 @@ func ResourceIBMPIImageExport() *schema.Resource { }, Schema: map[string]*schema.Schema{ - //required attributes - helpers.PICloudInstanceId: { - Type: schema.TypeString, - Required: true, - Description: "PI cloud instance ID", - ForceNew: true, + // Arguments + Arg_CloudInstanceID: { + Description: "The GUID of the service instance associated with an account.", + ForceNew: true, + Required: true, + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, }, - helpers.PIImageId: { - Type: schema.TypeString, - Required: true, - Description: "Instance image id", - DiffSuppressFunc: flex.ApplyOnce, - ForceNew: true, + Arg_ImageAccessKey: { + Description: "Cloud Object Storage access key; required for buckets with private access", + ForceNew: true, + Required: true, + Sensitive: true, + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, }, - helpers.PIImageBucketName: { - Type: schema.TypeString, - Required: true, - Description: "Cloud Object Storage bucket name; bucket-name[/optional/folder]", - ForceNew: true, + Arg_ImageBucketName: { + Description: "Cloud Object Storage bucket name; bucket-name[/optional/folder]", + ForceNew: true, + Required: true, + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, }, - helpers.PIImageAccessKey: { - Type: schema.TypeString, - Required: true, - Description: "Cloud Object Storage access key; required for buckets with private access", - Sensitive: true, - ForceNew: true, + Arg_ImageBucketRegion: { + Description: "Cloud Object Storage region", + ForceNew: true, + Required: true, + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, }, - - helpers.PIImageSecretKey: { - Type: schema.TypeString, - Required: true, - Description: "Cloud Object Storage secret key; required for buckets with private access", - Sensitive: true, - ForceNew: true, + Arg_ImageID: { + Description: "Instance image id", + DiffSuppressFunc: flex.ApplyOnce, + ForceNew: true, + Required: true, + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, }, - helpers.PIImageBucketRegion: { - Type: schema.TypeString, - Description: "Cloud Object Storage region", - ForceNew: true, - Required: true, + Arg_ImageSecretKey: { + Description: "Cloud Object Storage secret key; required for buckets with private access", + ForceNew: true, + Required: true, + Sensitive: true, + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, }, }, } @@ -83,28 +87,28 @@ func resourceIBMPIImageExportCreate(ctx context.Context, d *schema.ResourceData, return diag.FromErr(err) } - cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string) - imageid := d.Get(helpers.PIImageId).(string) - bucketName := d.Get(helpers.PIImageBucketName).(string) - accessKey := d.Get(helpers.PIImageAccessKey).(string) + cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) + imageid := d.Get(Arg_ImageID).(string) + bucketName := d.Get(Arg_ImageBucketName).(string) + accessKey := d.Get(Arg_ImageAccessKey).(string) - client := st.NewIBMPIImageClient(ctx, sess, cloudInstanceID) + client := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID) // image export var body = &models.ExportImage{ BucketName: &bucketName, AccessKey: &accessKey, - Region: d.Get(helpers.PIImageBucketRegion).(string), - SecretKey: d.Get(helpers.PIImageSecretKey).(string), + Region: d.Get(Arg_ImageBucketRegion).(string), + SecretKey: d.Get(Arg_ImageSecretKey).(string), } imageResponse, err := client.ExportImage(imageid, body) if err != nil { return diag.FromErr(err) } - d.SetId(fmt.Sprintf("%s/%s/%s", imageid, bucketName, d.Get(helpers.PIImageBucketRegion).(string))) + d.SetId(fmt.Sprintf("%s/%s/%s", imageid, bucketName, d.Get(Arg_ImageBucketRegion).(string))) - jobClient := st.NewIBMPIJobClient(ctx, sess, cloudInstanceID) + jobClient := instance.NewIBMPIJobClient(ctx, sess, cloudInstanceID) _, err = waitForIBMPIJobCompleted(ctx, jobClient, *imageResponse.ID, d.Timeout(schema.TimeoutCreate)) if err != nil { return diag.FromErr(err) diff --git a/ibm/service/power/resource_ibm_pi_image_export_test.go b/ibm/service/power/resource_ibm_pi_image_export_test.go index 82def95b39..419d959f91 100644 --- a/ibm/service/power/resource_ibm_pi_image_export_test.go +++ b/ibm/service/power/resource_ibm_pi_image_export_test.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func TestAccIBMPIImageEport(t *testing.T) { +func TestAccIBMPIImageExport(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, Providers: acc.TestAccProviders, @@ -28,17 +28,17 @@ func TestAccIBMPIImageEport(t *testing.T) { func testAccCheckIBMPIImageExportConfig() string { return fmt.Sprintf(` - data "ibm_pi_image" "power_image" { - pi_image_name = "%[6]s" - pi_cloud_instance_id = "%[1]s" - } - resource "ibm_pi_image_export" "power_image_export" { - pi_image_id = data.ibm_pi_image.power_image.id - pi_cloud_instance_id = "%[1]s" - pi_image_bucket_name = "%[2]s" - pi_image_access_key = "%[3]s" - pi_image_secret_key = "%[4]s" - pi_image_bucket_region = "%[5]s" - } + data "ibm_pi_image" "power_image" { + pi_image_name = "%[6]s" + pi_cloud_instance_id = "%[1]s" + } + resource "ibm_pi_image_export" "power_image_export" { + pi_image_id = data.ibm_pi_image.power_image.id + pi_cloud_instance_id = "%[1]s" + pi_image_bucket_name = "%[2]s" + pi_image_access_key = "%[3]s" + pi_image_secret_key = "%[4]s" + pi_image_bucket_region = "%[5]s" + } `, acc.Pi_cloud_instance_id, acc.Pi_image_bucket_name, acc.Pi_image_bucket_access_key, acc.Pi_image_bucket_secret_key, acc.Pi_image_bucket_region, acc.Pi_image) } diff --git a/website/docs/r/pi_image_export.html.markdown b/website/docs/r/pi_image_export.html.markdown index 2bb90e2517..a7f75a080a 100644 --- a/website/docs/r/pi_image_export.html.markdown +++ b/website/docs/r/pi_image_export.html.markdown @@ -44,19 +44,19 @@ Example usage: ## Timeouts -The ibm_pi_image provides the following [timeouts](https://www.terraform.io/docs/language/resources/syntax.html) configuration options: +The `ibm_pi_image_export` provides the following [timeouts](https://www.terraform.io/docs/language/resources/syntax.html) configuration options: -- **create** The export image to IBM Cloud Object Storage bucket is considered failed if no response is received for 60 minutes. +- **create** - (Default 60 minutes) used for exporting image to IBM Cloud Object Storage bucket is considered failed if no response is received. ## Argument reference Review the argument references that you can specify for your resource. - `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account. -- `pi_image_id` - (Required, String) The Image ID of existing source image; required for image export. -- `pi_image_bucket_name` - (Required, String) The Cloud Object Storage bucket name; `bucket-name[/optional/folder]` - `pi_image_access_key` - (Required, String, Sensitive) The Cloud Object Storage access key; required for buckets with private access. +- `pi_image_bucket_name` - (Required, String) The Cloud Object Storage bucket name; `bucket-name[/optional/folder]` - `pi_image_bucket_region` - (Required, String) The Cloud Object Storage region. Supported COS regions are:`au-syd`, `br-sao`, `ca-tor`, `che01`, `eu-de`, `eu-es`, `eu-gb`, `jp-osa`, `jp-tok`, `us-east`, `us-south`. +- `pi_image_id` - (Required, String) The Image ID of existing source image; required for image export. - `pi_image_secret_key` - (Required, String, Sensitive) The Cloud Object Storage secret key; required for buckets with private access. ## Attribute reference