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 Capture #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ var (
Pi_capture_storage_image_path string
Pi_capture_cloud_storage_access_key string
Pi_capture_cloud_storage_secret_key string
Pi_capture_cloud_storage_region string
)

var ISDelegegatedVPC string
Expand Down Expand Up @@ -1301,6 +1302,12 @@ func init() {
fmt.Println("[INFO] Set the environment variable PI_CAPTURE_CLOUD_STORAGE_SECRET_KEY for testing Pi_capture_cloud_storage_secret_key resource else it is set to default value 'terraform-test-power'")
}

Pi_capture_cloud_storage_region = os.Getenv("PI_CAPTURE_CLOUD_STORAGE_REGION")
if Pi_capture_cloud_storage_region == "" {
Pi_capture_cloud_storage_region = "us-south"
fmt.Println("[INFO] Set the environment variable PI_CAPTURE_CLOUD_STORAGE_REGION for testing Pi_capture_cloud_storage_region resource else it is set to default value 'us-south'")
}

Pi_shared_processor_pool_id = os.Getenv("PI_SHARED_PROCESSOR_POOL_ID")
if Pi_shared_processor_pool_id == "" {
Pi_shared_processor_pool_id = "tf-pi-shared-processor-pool"
Expand Down
11 changes: 11 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package power
import "time"

const (

// Arguments
Arg_Action = "pi_action"
Arg_AffinityInstance = "pi_affinity_instance"
Expand All @@ -11,6 +12,13 @@ const (
Arg_AntiAffinityInstances = "pi_anti_affinity_instances"
Arg_AntiAffinityVolumes = "pi_anti_affinity_volumes"
Arg_BootVolumeReplicationEnabled = "pi_boot_volume_replication_enabled"
Arg_CaptureCloudStorageAccessKey = "pi_capture_cloud_storage_access_key"
Arg_CaptureCloudStorageRegion = "pi_capture_cloud_storage_region"
Arg_CaptureCloudStorageSecretKey = "pi_capture_cloud_storage_secret_key"
Arg_CaptureDestination = "pi_capture_destination"
Arg_CaptureName = "pi_capture_name"
Arg_CaptureStorageImagePath = "pi_capture_storage_image_path"
Arg_CaptureVolumeIDs = "pi_capture_volume_ids"
Arg_Cidr = "pi_cidr"
Arg_CloudConnectionID = "pi_cloud_connection_id"
Arg_CloudConnectionName = "pi_cloud_connection_name"
Expand Down Expand Up @@ -447,8 +455,10 @@ const (
Allow = "allow"
AntiAffinity = "anti-affinity"
Attach = "attach"
Both = "both"
BYOL = "byol"
Capped = "capped"
CloudStorage = "cloud-storage"
Critical = "CRITICAL"
CUSTOM_VIRTUAL_CORES = "custom-virtualcores"
Dedicated = "dedicated"
Expand All @@ -467,6 +477,7 @@ const (
Host = "host"
HostGroup = "hostGroup"
ICMP = "icmp"
ImageCatalog = "image-catalog"
IPV4_Address = "ipv4-address"
NAG = "network-address-group"
MaxVolumeSupport = "maxVolumeSupport"
Expand Down
163 changes: 79 additions & 84 deletions ibm/service/power/resource_ibm_pi_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ import (
"strings"
"time"

"github.com/IBM-Cloud/power-go-client/clients/instance"
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/power/client/p_cloud_images"
"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/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"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"
)

const cloudStorageDestination string = "cloud-storage"
const imageCatalogDestination string = "image-catalog"

func ResourceIBMPICapture() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPICaptureCreate,
Expand All @@ -40,72 +38,69 @@ func ResourceIBMPICapture() *schema.Resource {
},

Schema: map[string]*schema.Schema{

helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
// Arguments
Arg_CaptureCloudStorageAccessKey: {
Description: "Name of Cloud Storage Access Key",
ForceNew: true,
Description: " Cloud Instance ID - This is the service_instance_id.",
},

helpers.PIInstanceName: {
Optional: true,
Sensitive: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Instance Name of the Power VM",
},

helpers.PIInstanceCaptureName: {
Arg_CaptureCloudStorageRegion: {
Description: "List of Regions to use",
ForceNew: true,
Optional: true,
Type: schema.TypeString,
Required: true,
},
Arg_CaptureCloudStorageSecretKey: {
Description: "Name of the Cloud Storage Secret Key",
ForceNew: true,
Description: "Name of the capture to create. Note : this must be unique",
Optional: true,
Sensitive: true,
Type: schema.TypeString,
},

helpers.PIInstanceCaptureDestination: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Arg_CaptureDestination: {
Description: "Destination for the deployable image",
ValidateFunc: validate.ValidateAllowedStringValues([]string{"image-catalog", "cloud-storage", "both"}),
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validate.ValidateAllowedStringValues([]string{ImageCatalog, CloudStorage, Both}),
},

helpers.PIInstanceCaptureVolumeIds: {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
ForceNew: true,
DiffSuppressFunc: flex.ApplyOnce,
Description: "List of Data volume IDs",
Arg_CaptureName: {
Description: "Name of the capture to create. Note : this must be unique",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

helpers.PIInstanceCaptureCloudStorageRegion: {
Type: schema.TypeString,
Optional: true,
Arg_CaptureStorageImagePath: {
Description: "Cloud Storage Image Path (bucket-name [/folder/../..])",
ForceNew: true,
Description: "List of Regions to use",
},

helpers.PIInstanceCaptureCloudStorageAccessKey: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
Description: "Name of Cloud Storage Access Key",
},
helpers.PIInstanceCaptureCloudStorageSecretKey: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
Description: "Name of the Cloud Storage Secret Key",
},
helpers.PIInstanceCaptureCloudStorageImagePath: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Cloud Storage Image Path (bucket-name [/folder/../..])",
Arg_CaptureVolumeIDs: {
Description: "List of Data volume IDs",
DiffSuppressFunc: flex.ApplyOnce,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Optional: true,
Set: schema.HashString,
Type: schema.TypeSet,
},
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_InstanceName: {
Description: "Instance Name of the Power VM",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_UserTags: {
Description: "List of user tags attached to the resource.",
Expand All @@ -120,10 +115,10 @@ func ResourceIBMPICapture() *schema.Resource {
Description: "The CRN of the resource.",
Type: schema.TypeString,
},
"image_id": {
Type: schema.TypeString,
Attr_ImageID: {
Computed: true,
Description: "Image ID of Capture Instance",
Description: "The image id of the capture instance.",
Type: schema.TypeString,
},
},
}
Expand All @@ -135,41 +130,41 @@ func resourceIBMPICaptureCreate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

name := d.Get(helpers.PIInstanceName).(string)
capturename := d.Get(helpers.PIInstanceCaptureName).(string)
capturedestination := d.Get(helpers.PIInstanceCaptureDestination).(string)
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
name := d.Get(Arg_InstanceName).(string)
capturename := d.Get(Arg_CaptureName).(string)
capturedestination := d.Get(Arg_CaptureDestination).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

client := st.NewIBMPIInstanceClient(context.Background(), sess, cloudInstanceID)
client := instance.NewIBMPIInstanceClient(context.Background(), sess, cloudInstanceID)

captureBody := &models.PVMInstanceCapture{
CaptureDestination: &capturedestination,
CaptureName: &capturename,
}
if capturedestination != imageCatalogDestination {
if v, ok := d.GetOk(helpers.PIInstanceCaptureCloudStorageRegion); ok {
if capturedestination != ImageCatalog {
if v, ok := d.GetOk(Arg_CaptureCloudStorageRegion); ok {
captureBody.CloudStorageRegion = v.(string)
} else {
return diag.Errorf("%s is required when capture destination is %s", helpers.PIInstanceCaptureCloudStorageRegion, capturedestination)
return diag.Errorf("%s is required when capture destination is %s", Arg_CaptureCloudStorageRegion, capturedestination)
}
if v, ok := d.GetOk(helpers.PIInstanceCaptureCloudStorageAccessKey); ok {
if v, ok := d.GetOk(Arg_CaptureCloudStorageAccessKey); ok {
captureBody.CloudStorageAccessKey = v.(string)
} else {
return diag.Errorf("%s is required when capture destination is %s ", helpers.PIInstanceCaptureCloudStorageAccessKey, capturedestination)
return diag.Errorf("%s is required when capture destination is %s ", Arg_CaptureCloudStorageAccessKey, capturedestination)
}
if v, ok := d.GetOk(helpers.PIInstanceCaptureCloudStorageImagePath); ok {
if v, ok := d.GetOk(Arg_CaptureStorageImagePath); ok {
captureBody.CloudStorageImagePath = v.(string)
} else {
return diag.Errorf("%s is required when capture destination is %s ", helpers.PIInstanceCaptureCloudStorageImagePath, capturedestination)
return diag.Errorf("%s is required when capture destination is %s ", Arg_CaptureStorageImagePath, capturedestination)
}
if v, ok := d.GetOk(helpers.PIInstanceCaptureCloudStorageSecretKey); ok {
if v, ok := d.GetOk(Arg_CaptureCloudStorageSecretKey); ok {
captureBody.CloudStorageSecretKey = v.(string)
} else {
return diag.Errorf("%s is required when capture destination is %s ", helpers.PIInstanceCaptureCloudStorageSecretKey, capturedestination)
return diag.Errorf("%s is required when capture destination is %s ", Arg_CaptureCloudStorageSecretKey, capturedestination)
}
}

if v, ok := d.GetOk(helpers.PIInstanceCaptureVolumeIds); ok {
if v, ok := d.GetOk(Arg_CaptureVolumeIDs); ok {
volids := flex.ExpandStringList((v.(*schema.Set)).List())
if len(volids) > 0 {
captureBody.CaptureVolumeIDs = volids
Expand All @@ -187,13 +182,13 @@ func resourceIBMPICaptureCreate(ctx context.Context, d *schema.ResourceData, met
}

d.SetId(fmt.Sprintf("%s/%s/%s", cloudInstanceID, capturename, capturedestination))
jobClient := st.NewIBMPIJobClient(ctx, sess, cloudInstanceID)
jobClient := instance.NewIBMPIJobClient(ctx, sess, cloudInstanceID)
_, err = waitForIBMPIJobCompleted(ctx, jobClient, *captureResponse.ID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}

if _, ok := d.GetOk(Arg_UserTags); ok && capturedestination != cloudStorageDestination {
if _, ok := d.GetOk(Arg_UserTags); ok && capturedestination != CloudStorage {
imageClient := st.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
imagedata, err := imageClient.Get(capturename)
if err != nil {
Expand Down Expand Up @@ -226,8 +221,8 @@ func resourceIBMPICaptureRead(ctx context.Context, d *schema.ResourceData, meta
cloudInstanceID := parts[0]
captureID := parts[1]
capturedestination := parts[2]
if capturedestination != cloudStorageDestination {
imageClient := st.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
if capturedestination != CloudStorage {
imageClient := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
imagedata, err := imageClient.Get(captureID)
if err != nil {
uErr := errors.Unwrap(err)
Expand All @@ -241,6 +236,7 @@ func resourceIBMPICaptureRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(err)
}
imageid := *imagedata.ImageID
d.Set(Attr_ImageID, imageid)
if imagedata.Crn != "" {
d.Set(Attr_CRN, imagedata.Crn)
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(imagedata.Crn), "", UserTagType)
Expand All @@ -249,9 +245,8 @@ func resourceIBMPICaptureRead(ctx context.Context, d *schema.ResourceData, meta
}
d.Set(Arg_UserTags, tags)
}
d.Set("image_id", imageid)
}
d.Set(helpers.PICloudInstanceId, cloudInstanceID)
d.Set(Arg_CloudInstanceID, cloudInstanceID)
return nil
}

Expand All @@ -267,8 +262,8 @@ func resourceIBMPICaptureDelete(ctx context.Context, d *schema.ResourceData, met
cloudInstanceID := parts[0]
captureID := parts[1]
capturedestination := parts[2]
if capturedestination != cloudStorageDestination {
imageClient := st.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
if capturedestination != CloudStorage {
imageClient := instance.NewIBMPIImageClient(ctx, sess, cloudInstanceID)
err = imageClient.Delete(captureID)
if err != nil {
uErr := errors.Unwrap(err)
Expand All @@ -294,7 +289,7 @@ func resourceIBMPICaptureUpdate(ctx context.Context, d *schema.ResourceData, met
captureID := parts[1]
capturedestination := parts[2]

if capturedestination != cloudStorageDestination && d.HasChange(Arg_UserTags) {
if capturedestination != CloudStorage && d.HasChange(Arg_UserTags) {
if crn, ok := d.GetOk(Attr_CRN); ok {
oldList, newList := d.GetChange(Arg_UserTags)
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, crn.(string), "", UserTagType)
Expand Down
Loading
Loading