Skip to content

Commit

Permalink
Add volume-onboarding & dr-locations methods (#188)
Browse files Browse the repository at this point in the history
* Add volume-onboarding & dr-locations methods

* feedback resolved
  • Loading branch information
dhirendersingh19 authored Aug 30, 2022
1 parent 0216680 commit 2e528d5
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 5 deletions.
53 changes: 53 additions & 0 deletions clients/instance/ibm-pi-dr-location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package instance

import (
"context"
"fmt"

"github.com/IBM-Cloud/power-go-client/errors"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_disaster_recovery"
"github.com/IBM-Cloud/power-go-client/power/models"
)

// IBMPIDisasterRecoveryLocationClient
type IBMPIDisasterRecoveryLocationClient struct {
IBMPIClient
}

// NewIBMPIDisasterRecoveryLocationClient
func NewIBMPIDisasterRecoveryLocationClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIDisasterRecoveryLocationClient {
return &IBMPIDisasterRecoveryLocationClient{
*NewIBMPIClient(ctx, sess, cloudInstanceID),
}
}

// Get the disaster recovery site details for the current location
func (f *IBMPIDisasterRecoveryLocationClient) Get() (*models.DisasterRecoveryLocation, error) {
params := p_cloud_disaster_recovery.NewPcloudLocationsDisasterrecoveryGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID)
resp, err := f.session.Power.PCloudDisasterRecovery.PcloudLocationsDisasterrecoveryGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf(errors.GetDisasterRecoveryLocationOperationFailed, f.cloudInstanceID, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to perform the Get Disaster Recovery Location for the cloud instance %s", f.cloudInstanceID)
}
return resp.Payload, nil
}

// Get all disaster recovery locations supported by Power Virtual Server
func (f *IBMPIDisasterRecoveryLocationClient) GetAll() (*models.DisasterRecoveryLocations, error) {
params := p_cloud_disaster_recovery.NewPcloudLocationsDisasterrecoveryGetallParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut)
resp, err := f.session.Power.PCloudDisasterRecovery.PcloudLocationsDisasterrecoveryGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf(errors.GetAllDisasterRecoveryLocationOperationFailed, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get All Disaster Recovery Location of Power Virtual Server")
}
return resp.Payload, nil
}
69 changes: 69 additions & 0 deletions clients/instance/ibm-pi-volume-onboarding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package instance

import (
"context"
"fmt"

"github.com/IBM-Cloud/power-go-client/errors"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_volume_onboarding"
"github.com/IBM-Cloud/power-go-client/power/models"
)

// IBMPIVolumeOnboardingClient
type IBMPIVolumeOnboardingClient struct {
IBMPIClient
}

// NewIBMPIVolumeOnboardingClient
func NewIBMPIVolumeOnboardingClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIVolumeOnboardingClient {
return &IBMPIVolumeOnboardingClient{
*NewIBMPIClient(ctx, sess, cloudInstanceID),
}
}

// Get the information of volume onboarding operation
func (f *IBMPIVolumeOnboardingClient) Get(id string) (*models.VolumeOnboarding, error) {
params := p_cloud_volume_onboarding.NewPcloudVolumeOnboardingGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeOnboardingID(id)
resp, err := f.session.Power.PCloudVolumeOnboarding.PcloudVolumeOnboardingGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf(errors.GetVolumeOnboardingOperationFailed, id, f.cloudInstanceID, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get Volume Onboarding for volume-onboarding ID:%s", id)
}
return resp.Payload, nil
}

// Get All volume onboardings for this cloud instance
func (f *IBMPIVolumeOnboardingClient) GetAll() (*models.VolumeOnboardings, error) {
params := p_cloud_volume_onboarding.NewPcloudVolumeOnboardingGetallParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID)
resp, err := f.session.Power.PCloudVolumeOnboarding.PcloudVolumeOnboardingGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf(errors.GetAllVolumeOnboardingsOperationFailed, f.cloudInstanceID, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get All Volume Onboardings for the cloud instance %s", f.cloudInstanceID)
}
return resp.Payload, nil
}

// Onboard auxiliary volumes to target site
func (f *IBMPIVolumeOnboardingClient) CreateVolumeOnboarding(body *models.VolumeOnboardingCreate) (*models.VolumeOnboardingCreateResponse, error) {
params := p_cloud_volume_onboarding.NewPcloudVolumeOnboardingPostParams().
WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithBody(body)
resp, err := f.session.Power.PCloudVolumeOnboarding.PcloudVolumeOnboardingPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf(errors.CreateVolumeOnboardingsOperationFailed, f.cloudInstanceID, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Create Volume Onboarding")
}
return resp.Payload, nil
}
30 changes: 30 additions & 0 deletions clients/instance/ibm-pi-volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,33 @@ func (f *IBMPIVolumeClient) VolumeAction(id string, body *models.VolumeAction) e
}
return nil
}

// Get remote copy relationship of a volume
func (f *IBMPIVolumeClient) GetVolumeRemoteCopyRelationships(id string) (*models.VolumeRemoteCopyRelationship, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesRemoteCopyRelationshipGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesRemoteCopyRelationshipGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf(errors.GetVolumeRemoteCopyRelationshipsOperationFailed, id, f.cloudInstanceID, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get remote copy relationships of a volume %s", id)
}
return resp.Payload, nil
}

// Get a list of flashcopy mappings of a given volume
func (f *IBMPIVolumeClient) GetVolumeFlashCopyMappings(id string) (models.FlashCopyMappings, error) {
params := p_cloud_volumes.NewPcloudCloudinstancesVolumesFlashCopyMappingsGetParams().
WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).
WithCloudInstanceID(f.cloudInstanceID).WithVolumeID(id)
resp, err := f.session.Power.PCloudVolumes.PcloudCloudinstancesVolumesFlashCopyMappingsGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, fmt.Errorf(errors.GetVolumeFlashCopyMappingOperationFailed, id, f.cloudInstanceID, err)
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get flash copy mapping of a volume %s", id)
}
return resp.Payload, nil
}
11 changes: 11 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ const CreateVolumeOperationFailed = "failed to perform the Create volume Operati
const CreateVolumeV2OperationFailed = "failed to perform the Create volume Operation V2 for volume %s with error %w"
const AttachVolumeOperationFailed = "failed to perform the Attach volume Operation for volume %s with error %w"
const DetachVolumeOperationFailed = "failed to perform the Detach volume Operation for volume %s with error %w"
const GetVolumeRemoteCopyRelationshipsOperationFailed = "failed to Get remote copy relationships of a volume %s for the cloud instance %s with error %w"
const GetVolumeFlashCopyMappingOperationFailed = "failed to Get flash copy mapping of a volume %s for the cloud instance %s with error %w"

// start of volume onboarding
const GetVolumeOnboardingOperationFailed = "failed to perform the Get Volume Onboarding Operation for volume-onboarding ID:%s for the cloud instance %s with error %w"
const GetAllVolumeOnboardingsOperationFailed = "failed to perform the Get All Volume Onboardings Operation for the cloud instance %s with error %w"
const CreateVolumeOnboardingsOperationFailed = "failed to perform the Create Volume Onboarding Operation for the cloud instance %s with error %w"

// start of disaster recovery
const GetDisasterRecoveryLocationOperationFailed = "failed to perform the Get Disaster Recovery Location Operation for the cloud instance %s with error %w"
const GetAllDisasterRecoveryLocationOperationFailed = "failed to perform the Get All Disaster Recovery Location Operation of power virtual server with error %w"

// start of Clone Messages
const StartCloneOperationFailed = "failed to start the clone operation for volumes-clone %s with error %w"
Expand Down
61 changes: 61 additions & 0 deletions examples/dr-locations/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"context"
"log"

v "github.com/IBM-Cloud/power-go-client/clients/instance"
ps "github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM/go-sdk-core/v5/core"
)

func main() {

//session Inputs
token := " < IAM TOKEN > "
region := " < REGION > "
zone := " < ZONE > "
accountID := " < ACCOUNT ID > "
url := region + ".power-iaas.test.cloud.ibm.com"

// dr location inputs
piID := " < POWER INSTANCE ID > "

authenticator := &core.BearerTokenAuthenticator{
BearerToken: token,
}
// authenticator := &core.IamAuthenticator{
// ApiKey: "< API KEY >",
// // Uncomment for test environment
// URL: "https://iam.test.cloud.ibm.com",
// }

// Create the session
options := &ps.IBMPIOptions{
Authenticator: authenticator,
UserAccount: accountID,
Zone: zone,
URL: url,
Debug: true,
}
session, err := ps.NewIBMPISession(options)
if err != nil {
log.Fatal(err)
}
powerClient := v.NewIBMPIDisasterRecoveryLocationClient(context.Background(), session, piID)
if err != nil {
log.Fatal(err)
}

getResp, err := powerClient.Get()
if err != nil {
log.Fatal(err)
}
log.Printf("***************[1]****************** %+v\n", *getResp)

getAllResp, err := powerClient.GetAll()
if err != nil {
log.Fatal(err)
}
log.Printf("***************[2]****************** %+v\n", *getAllResp)
}
88 changes: 88 additions & 0 deletions examples/volume-onboarding/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"context"
"log"

v "github.com/IBM-Cloud/power-go-client/clients/instance"
ps "github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM/go-sdk-core/v5/core"
)

func main() {

//session Inputs
token := " < IAM TOKEN > "
region := " < REGION > "
zone := " < ZONE > "
accountID := " < ACCOUNT ID > "
url := region + ".power-iaas.test.cloud.ibm.com"

// volume-onboarding inputs
piID := " < POWER INSTANCE ID > "
auxVolName := " < NAME OF THE auxiliary volume > "
sourceCRN := " < SOURCE CRN > "
onboardingName := " < ONBOARDING NAME > "

authenticator := &core.BearerTokenAuthenticator{
BearerToken: token,
}

// authenticator := &core.IamAuthenticator{
// ApiKey: "< API KEY >",
// // Uncomment for test environment
// URL: "https://iam.test.cloud.ibm.com",
// }

// Create the session
options := &ps.IBMPIOptions{
Authenticator: authenticator,
UserAccount: accountID,
Zone: zone,
URL: url,
Debug: true,
}
session, err := ps.NewIBMPISession(options)
if err != nil {
log.Fatal(err)
}
powerClient := v.NewIBMPIVolumeOnboardingClient(context.Background(), session, piID)
if err != nil {
log.Fatal(err)
}

auxVols := []*models.AuxiliaryVolumeForOnboarding{}
auxVols = append(auxVols, &models.AuxiliaryVolumeForOnboarding{
AuxVolumeName: &auxVolName,
Name: onboardingName,
})

vols := []*models.AuxiliaryVolumesForOnboarding{}
vols = append(vols, &models.AuxiliaryVolumesForOnboarding{
AuxiliaryVolumes: auxVols,
SourceCRN: &sourceCRN,
})
body := &models.VolumeOnboardingCreate{
Volumes: vols,
}

createRespOk, err := powerClient.CreateVolumeOnboarding(body)
if err != nil {
log.Fatal(err)
}
log.Printf("***************[1]****************** %+v\n", *createRespOk)

onboardingID := createRespOk.ID
getResp, err := powerClient.Get(onboardingID)
if err != nil {
log.Fatal(err)
}
log.Printf("***************[2]****************** %+v \n", *getResp)

getallResp, err := powerClient.GetAll()
if err != nil {
log.Fatal(err)
}
log.Printf("***************[3]****************** %+v \n", *getallResp)
}
25 changes: 20 additions & 5 deletions examples/volume/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func main() {
//session Inputs

//session Inputs
token := " < IAM TOKEN > "
region := " < REGION > "
Expand All @@ -25,6 +25,7 @@ func main() {
size := 20.0
vtype := "tier1"
sharable := true
replicationEnabled := false

authenticator := &core.BearerTokenAuthenticator{
BearerToken: token,
Expand All @@ -34,6 +35,7 @@ func main() {
// // Uncomment for test environment
// URL: "https://iam.test.cloud.ibm.com",
// }

// Create the session
options := &ps.IBMPIOptions{
Authenticator: authenticator,
Expand All @@ -52,10 +54,11 @@ func main() {
}

body := &models.CreateDataVolume{
Name: &name,
Size: &size,
DiskType: vtype,
Shareable: &sharable,
Name: &name,
Size: &size,
DiskType: vtype,
Shareable: &sharable,
ReplicationEnabled: &replicationEnabled,
}
createRespOk, err := powerClient.CreateVolume(body)
if err != nil {
Expand All @@ -76,6 +79,18 @@ func main() {
}
log.Printf("***************[2]****************** %+v \n", *getallResp)

getVolRCRelationships, err := powerClient.GetVolumeRemoteCopyRelationships(volumeID)
if err != nil {
log.Fatal(err)
}
log.Printf("***************[3]****************** %+v \n", *getVolRCRelationships)

getVolFCMappings, err := powerClient.GetVolumeFlashCopyMappings(volumeID)
if err != nil {
log.Fatal(err)
}
log.Printf("***************[4]****************** %+v \n", getVolFCMappings)

err = powerClient.DeleteVolume(volumeID)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 2e528d5

Please sign in to comment.