Skip to content

Commit

Permalink
Refactor Image Export
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkad authored and Alexander-Kita committed Aug 17, 2024
1 parent 045433e commit 7946ba2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 58 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 @@ -31,6 +31,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
97 changes: 51 additions & 46 deletions ibm/service/power/resource_ibm_pi_image_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"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"

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"
Expand All @@ -31,46 +31,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 +88,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: 14 additions & 12 deletions website/docs/r/pi_image_export.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ description: |-
---

# ibm_pi_image_export

Export an image to IBM Cloud Object Storage for Power Systems Virtual Server instance. 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

The following example enables you to export an image:

```terraform
Expand All @@ -24,7 +26,8 @@ resource "ibm_pi_image_export" "testacc_image_export"{
}
```

**Note**
### Notes

* Ensure the exported file is cleaned up manually from the Cloud Object Storage when no longer needed. Power Systems Virtual Server does not support deleting the exported image. Updating any attribute will result in creating a new Export job.
* 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:
Expand All @@ -44,22 +47,21 @@ resource "ibm_pi_image_export" "testacc_image_export"{

The ibm_pi_image 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.

## 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_region` - (Required, String) The Cloud Object Storage region. Supported COS regions are:`au-syd`, `br-sao`, `ca-tor`, `eu-de`, `eu-es`, `eu-gb`, `jp-osa`, `jp-tok`, `us-east`, `us-south`.
- `pi_image_secret_key` - (Required, String, Sensitive) The Cloud Object Storage secret key; required for buckets with private access.

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_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`, `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
In addition to all argument reference list, you can access the following attribute reference after your resource is created.

- `id` - (String) The unique identifier of an image export resource. The ID is composed of `<image_id>/<bucket_name>/<bucket_region>`.
In addition to all argument reference list, you can access the following attribute reference after your resource is created.

* `id` - (String) The unique identifier of an image export resource. The ID is composed of `<image_id>/<bucket_name>/<bucket_region>`.

0 comments on commit 7946ba2

Please sign in to comment.