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 resource image_export and documentation #68

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const (
// Arguments
Arg_CloudConnectionName = "pi_cloud_connection_name"
Arg_CloudInstanceID = "pi_cloud_instance_id"
Arg_ImageAccessKey = "pi_image_access_key"
Arg_ImageBucketName = "pi_image_bucket_name"
Arg_ImageBucketRegion = "pi_image_bucket_region"
Arg_ImageID = "pi_image_id"
Arg_ImageName = "pi_image_name"
Arg_ImageSecretKey = "pi_image_secret_key"
Arg_InstanceName = "pi_instance_name"
Arg_Key = "pi_ssh_key"
Arg_KeyName = "pi_key_name"
Expand Down
79 changes: 38 additions & 41 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,12 @@ 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"
)

func ResourceIBMPIImageExport() *schema.Resource {
Expand All @@ -31,46 +29,45 @@ func ResourceIBMPIImageExport() *schema.Resource {
},

Schema: map[string]*schema.Schema{
//required attributes
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
//Arguments
Arg_CloudInstanceID: {
Description: "PI cloud instance ID",
ForceNew: true,
},
helpers.PIImageId: {
Type: schema.TypeString,
Required: true,
Description: "Instance image id",
DiffSuppressFunc: flex.ApplyOnce,
ForceNew: true,
},
helpers.PIImageBucketName: {
Type: schema.TypeString,
Required: true,
Description: "Cloud Object Storage bucket name; bucket-name[/optional/folder]",
ForceNew: true,
},
helpers.PIImageAccessKey: {
Type: schema.TypeString,
Required: true,
},
Arg_ImageAccessKey: {
Description: "Cloud Object Storage access key; required for buckets with private access",
Sensitive: true,
ForceNew: true,
},

helpers.PIImageSecretKey: {
Type: schema.TypeString,
Required: true,
Description: "Cloud Object Storage secret key; required for buckets with private access",
Sensitive: true,
ForceNew: true,
Type: schema.TypeString,
},
helpers.PIImageBucketRegion: {
Arg_ImageBucketName: {
Description: "Cloud Object Storage bucket name; bucket-name[/optional/folder]",
ForceNew: true,
Type: schema.TypeString,
Required: true,
},
Arg_ImageBucketRegion: {
Description: "Cloud Object Storage region",
ForceNew: true,
Required: true,
Type: schema.TypeString,
},
Arg_ImageID: {
Description: "Instance image id",
DiffSuppressFunc: flex.ApplyOnce,
ForceNew: true,
Required: true,
Type: schema.TypeString,
},
Arg_ImageSecretKey: {
Description: "Cloud Object Storage secret key; required for buckets with private access",
ForceNew: true,
Required: true,
Sensitive: true,
Type: schema.TypeString,
},
},
}
Expand All @@ -83,28 +80,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
21 changes: 10 additions & 11 deletions ibm/service/power/resource_ibm_pi_image_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ 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"
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)
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)
}
5 changes: 2 additions & 3 deletions website/docs/r/pi_image_export.html.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

subcategory: "Power Systems"
layout: "ibm"
page_title: "IBM: pi_image_export"
Expand All @@ -24,14 +23,14 @@ 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:
* `region` - `lon`
* `zone` - `lon04`

Example usage:
Example usage:

```terraform
provider "ibm" {
Expand Down
Loading