Skip to content

Commit

Permalink
Refactor Image Export
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Kita committed Jan 22, 2025
1 parent d841442 commit 928af69
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 66 deletions.
4 changes: 4 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
102 changes: 53 additions & 49 deletions ibm/service/power/resource_ibm_pi_image_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
},
}
Expand All @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions ibm/service/power/resource_ibm_pi_image_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions website/docs/r/pi_image_export.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 928af69

Please sign in to comment.