From ebcc0b93842dc35320d44b7a7f77d061265b9a71 Mon Sep 17 00:00:00 2001 From: Yussuf Shaikh Date: Thu, 9 Dec 2021 21:49:05 +0530 Subject: [PATCH] Refactor placement groups and others Signed-off-by: Yussuf Shaikh --- clients/instance/ibm-pi-cloud-instance.go | 58 ++++++---- clients/instance/ibm-pi-placement-groups.go | 111 ++++++++++++-------- clients/instance/ibm-pi-system-pools.go | 32 ++++-- errors/errors.go | 6 +- 4 files changed, 134 insertions(+), 73 deletions(-) diff --git a/clients/instance/ibm-pi-cloud-instance.go b/clients/instance/ibm-pi-cloud-instance.go index 61d9b91f..85845e36 100644 --- a/clients/instance/ibm-pi-cloud-instance.go +++ b/clients/instance/ibm-pi-cloud-instance.go @@ -1,8 +1,12 @@ package instance import ( + "context" "fmt" + "github.com/IBM-Cloud/power-go-client/errors" + "github.com/IBM-Cloud/power-go-client/helpers" + "github.com/go-openapi/runtime" "github.com/IBM-Cloud/power-go-client/ibmpisession" "github.com/IBM-Cloud/power-go-client/power/client/p_cloud_instances" @@ -12,44 +16,60 @@ import ( // IBMPICloudInstanceClient ... type IBMPICloudInstanceClient struct { session *ibmpisession.IBMPISession - powerinstanceid string + cloudInstanceID string + authInfo runtime.ClientAuthInfoWriter + ctx context.Context } // NewIBMPICloudInstanceClient ... -func NewIBMPICloudInstanceClient(sess *ibmpisession.IBMPISession, powerinstanceid string) *IBMPICloudInstanceClient { +func NewIBMPICloudInstanceClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPICloudInstanceClient { + authInfo := ibmpisession.NewAuth(sess, cloudInstanceID) return &IBMPICloudInstanceClient{ - session: sess, - powerinstanceid: powerinstanceid, + sess, + cloudInstanceID, + authInfo, + ctx, } } // Get information about a cloud instance -func (f *IBMPICloudInstanceClient) Get(powerinstanceid string) (*models.CloudInstance, error) { - params := p_cloud_instances.NewPcloudCloudinstancesGetParams().WithCloudInstanceID(powerinstanceid) - resp, err := f.session.Power.PCloudInstances.PcloudCloudinstancesGet(params, ibmpisession.NewAuth(f.session, powerinstanceid)) +func (f *IBMPICloudInstanceClient) Get(id string) (*models.CloudInstance, error) { + params := p_cloud_instances.NewPcloudCloudinstancesGetParams(). + WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut). + WithCloudInstanceID(id) + resp, err := f.session.Power.PCloudInstances.PcloudCloudinstancesGet(params, f.authInfo) if err != nil { - return nil, fmt.Errorf(errors.GetCloudInstanceOperationFailed, powerinstanceid, err) + return nil, fmt.Errorf(errors.GetCloudInstanceOperationFailed, id, err) + } + if resp == nil || resp.Payload == nil { + return nil, fmt.Errorf("failed to Get Cloud Instance %s", id) } return resp.Payload, nil } // Update a cloud instance -func (f *IBMPICloudInstanceClient) Update(powerinstanceid string, updateparams *p_cloud_instances.PcloudCloudinstancesPutParams) (*models.CloudInstance, error) { - params := p_cloud_instances.NewPcloudCloudinstancesPutParamsWithTimeout(f.session.Timeout).WithCloudInstanceID(powerinstanceid).WithBody(updateparams.Body) - resp, err := f.session.Power.PCloudInstances.PcloudCloudinstancesPut(params, ibmpisession.NewAuth(f.session, powerinstanceid)) +func (f *IBMPICloudInstanceClient) Update(id string, body *models.CloudInstanceUpdate) (*models.CloudInstance, error) { + params := p_cloud_instances.NewPcloudCloudinstancesPutParams(). + WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut). + WithCloudInstanceID(id).WithBody(body) + resp, err := f.session.Power.PCloudInstances.PcloudCloudinstancesPut(params, f.authInfo) if err != nil { - return nil, fmt.Errorf(errors.UpdateCloudInstanceOperationFailed, powerinstanceid, err) - + return nil, fmt.Errorf(errors.UpdateCloudInstanceOperationFailed, id, err) + } + if resp == nil || resp.Payload == nil { + return nil, fmt.Errorf("failed to update the Cloud instance %s", id) } return resp.Payload, nil } // Delete a Cloud instance -func (f *IBMPICloudInstanceClient) Delete(powerinstanceid string) (models.Object, error) { - params := p_cloud_instances.NewPcloudCloudinstancesDeleteParams().WithCloudInstanceID(powerinstanceid) - resp, err := f.session.Power.PCloudInstances.PcloudCloudinstancesDelete(params, ibmpisession.NewAuth(f.session, powerinstanceid)) - if err != nil || resp.Payload == nil { - return nil, fmt.Errorf(errors.DeleteCloudInstanceOperationFailed, powerinstanceid, err) +func (f *IBMPICloudInstanceClient) Delete(id string) error { + params := p_cloud_instances.NewPcloudCloudinstancesDeleteParams(). + WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut). + WithCloudInstanceID(id) + _, err := f.session.Power.PCloudInstances.PcloudCloudinstancesDelete(params, f.authInfo) + if err != nil { + return fmt.Errorf(errors.DeleteCloudInstanceOperationFailed, id, err) } - return resp.Payload, nil + return nil } diff --git a/clients/instance/ibm-pi-placement-groups.go b/clients/instance/ibm-pi-placement-groups.go index e650ecee..c0923faa 100644 --- a/clients/instance/ibm-pi-placement-groups.go +++ b/clients/instance/ibm-pi-placement-groups.go @@ -1,9 +1,12 @@ package instance import ( + "context" "fmt" + "github.com/IBM-Cloud/power-go-client/errors" "github.com/IBM-Cloud/power-go-client/power/client/p_cloud_placement_groups" + "github.com/go-openapi/runtime" "github.com/IBM-Cloud/power-go-client/helpers" "github.com/IBM-Cloud/power-go-client/ibmpisession" @@ -13,57 +16,73 @@ import ( //IBMPIPlacementGroupClient ... type IBMPIPlacementGroupClient struct { session *ibmpisession.IBMPISession - powerinstanceid string + cloudInstanceID string + authInfo runtime.ClientAuthInfoWriter + ctx context.Context } -// NewIBMPIImageClient ... -func NewIBMPIPlacementGroupClient(sess *ibmpisession.IBMPISession, powerinstanceid string) *IBMPIPlacementGroupClient { +// NewIBMPIPlacementGroupClient ... +func NewIBMPIPlacementGroupClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIPlacementGroupClient { + authInfo := ibmpisession.NewAuth(sess, cloudInstanceID) return &IBMPIPlacementGroupClient{ session: sess, - powerinstanceid: powerinstanceid, + cloudInstanceID: cloudInstanceID, + authInfo: authInfo, + ctx: ctx, } } // Get PI Placementgroup -func (f *IBMPIPlacementGroupClient) Get(id, powerinstanceid string) (*models.PlacementGroup, error) { - - params := p_cloud_placement_groups.NewPcloudPlacementgroupsGetParamsWithTimeout(helpers.PIGetTimeOut).WithCloudInstanceID(powerinstanceid).WithPlacementGroupID(id) - resp, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsGet(params, ibmpisession.NewAuth(f.session, powerinstanceid)) - +func (f *IBMPIPlacementGroupClient) Get(id string) (*models.PlacementGroup, error) { + params := p_cloud_placement_groups.NewPcloudPlacementgroupsGetParams(). + WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut). + WithCloudInstanceID(f.cloudInstanceID).WithPlacementGroupID(id) + resp, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsGet(params, f.authInfo) if err != nil { return nil, fmt.Errorf(errors.GetPlacementGroupOperationFailed, id, err) } + if resp == nil || resp.Payload == nil { + return nil, fmt.Errorf("failed to Get Placement Group %s", id) + } return resp.Payload, nil } -// GEt All placement groups - -func (f *IBMPIPlacementGroupClient) GetAll(powerinstanceid string) (*models.PlacementGroups, error) { - params := p_cloud_placement_groups.NewPcloudPlacementgroupsGetallParamsWithTimeout(helpers.PIGetTimeOut).WithCloudInstanceID(powerinstanceid) - resp, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsGetall(params, ibmpisession.NewAuth(f.session, powerinstanceid)) - +// Get All placement groups +func (f *IBMPIPlacementGroupClient) GetAll() (*models.PlacementGroups, error) { + params := p_cloud_placement_groups.NewPcloudPlacementgroupsGetallParams(). + WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut). + WithCloudInstanceID(f.cloudInstanceID) + resp, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsGetall(params, f.authInfo) if err != nil { - return nil, fmt.Errorf(errors.GetPlacementGroupOperationFailed, powerinstanceid, err) + return nil, fmt.Errorf("failed to Get All Placement Groups: %w", err) + } + if resp == nil || resp.Payload == nil { + return nil, fmt.Errorf("failed to Get all Placement Groups") } return resp.Payload, nil } -//Create the placement group -func (f *IBMPIPlacementGroupClient) Create(powerdef *p_cloud_placement_groups.PcloudPlacementgroupsPostParams, powerinstanceid string) (*models.PlacementGroup, error) { - - params := p_cloud_placement_groups.NewPcloudPlacementgroupsPostParamsWithTimeout(helpers.PICreateTimeOut).WithCloudInstanceID(powerinstanceid).WithBody(powerdef.Body) - result, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsPost(params, ibmpisession.NewAuth(f.session, powerinstanceid)) - if err != nil || result == nil || result.Payload == nil { - return nil, fmt.Errorf(errors.CreatePlacementGroupOperationFailed, powerinstanceid, err) +// Create the placement group +func (f *IBMPIPlacementGroupClient) Create(body *models.PlacementGroupCreate) (*models.PlacementGroup, error) { + params := p_cloud_placement_groups.NewPcloudPlacementgroupsPostParams(). + WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut). + WithCloudInstanceID(f.cloudInstanceID).WithBody(body) + postok, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsPost(params, f.authInfo) + if err != nil { + return nil, fmt.Errorf(errors.CreatePlacementGroupOperationFailed, f.cloudInstanceID, err) } - return result.Payload, nil - + if postok == nil || postok.Payload == nil { + return nil, fmt.Errorf("failed to Create Placement Group") + } + return postok.Payload, nil } // Delete Placement Group -func (f *IBMPIPlacementGroupClient) Delete(id string, powerinstanceid string) error { - params := p_cloud_placement_groups.NewPcloudPlacementgroupsDeleteParamsWithTimeout(helpers.PIDeleteTimeOut).WithCloudInstanceID(powerinstanceid).WithPlacementGroupID(id) - _, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsDelete(params, ibmpisession.NewAuth(f.session, powerinstanceid)) +func (f *IBMPIPlacementGroupClient) Delete(id string) error { + params := p_cloud_placement_groups.NewPcloudPlacementgroupsDeleteParams(). + WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut). + WithCloudInstanceID(f.cloudInstanceID).WithPlacementGroupID(id) + _, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsDelete(params, f.authInfo) if err != nil { return fmt.Errorf(errors.DeletePlacementGroupOperationFailed, id, err) } @@ -71,25 +90,33 @@ func (f *IBMPIPlacementGroupClient) Delete(id string, powerinstanceid string) er } // Adding a member to a Placement Group -func (f *IBMPIPlacementGroupClient) Update(placementdef *p_cloud_placement_groups.PcloudPlacementgroupsMembersPostParams, placementgroupid, powerinstanceid string) (*models.PlacementGroup, error) { - - params := p_cloud_placement_groups.NewPcloudPlacementgroupsMembersPostParamsWithTimeout(helpers.PICreateTimeOut).WithCloudInstanceID(powerinstanceid).WithPlacementGroupID(placementgroupid).WithBody(placementdef.Body) - resp, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsMembersPost(params, ibmpisession.NewAuth(f.session, f.powerinstanceid)) - +func (f *IBMPIPlacementGroupClient) AddMember(id string, body *models.PlacementGroupServer) (*models.PlacementGroup, error) { + params := p_cloud_placement_groups.NewPcloudPlacementgroupsMembersPostParams(). + WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut). + WithCloudInstanceID(f.cloudInstanceID).WithPlacementGroupID(id). + WithBody(body) + postok, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsMembersPost(params, f.authInfo) if err != nil { - return nil, fmt.Errorf(errors.UpdatePlacementGroupOperationFailed, powerinstanceid, placementgroupid, err) + return nil, fmt.Errorf(errors.AddMemberPlacementGroupOperationFailed, *body.ID, id, err) } - return resp.Payload, nil + if postok == nil || postok.Payload == nil { + return nil, fmt.Errorf("failed to Add Member for instance %s and placement group %s", *body.ID, id) + } + return postok.Payload, nil } // Delete Member from Placement Group -func (f *IBMPIPlacementGroupClient) DeleteMember(placementdef *p_cloud_placement_groups.PcloudPlacementgroupsMembersPostParams, placementgroupid, powerinstanceid string) (*models.PlacementGroup, error) { - - params := p_cloud_placement_groups.NewPcloudPlacementgroupsMembersDeleteParamsWithTimeout(helpers.PICreateTimeOut).WithCloudInstanceID(powerinstanceid).WithPlacementGroupID(placementgroupid).WithBody(placementdef.Body) - resp, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsMembersDelete(params, ibmpisession.NewAuth(f.session, f.powerinstanceid)) - +func (f *IBMPIPlacementGroupClient) DeleteMember(id string, body *models.PlacementGroupServer) (*models.PlacementGroup, error) { + params := p_cloud_placement_groups.NewPcloudPlacementgroupsMembersDeleteParams(). + WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut). + WithCloudInstanceID(f.cloudInstanceID).WithPlacementGroupID(id). + WithBody(body) + delok, err := f.session.Power.PCloudPlacementGroups.PcloudPlacementgroupsMembersDelete(params, f.authInfo) if err != nil { - return nil, fmt.Errorf(errors.DeleteMemberPlacementGroupOperationFailed, powerinstanceid, placementgroupid, err) + return nil, fmt.Errorf(errors.DeleteMemberPlacementGroupOperationFailed, *body.ID, id, err) } - return resp.Payload, nil + if delok == nil || delok.Payload == nil { + return nil, fmt.Errorf("failed to Delete Member for instance %s and placement group %s", *body.ID, id) + } + return delok.Payload, nil } diff --git a/clients/instance/ibm-pi-system-pools.go b/clients/instance/ibm-pi-system-pools.go index c8ace83d..5fe32bfa 100644 --- a/clients/instance/ibm-pi-system-pools.go +++ b/clients/instance/ibm-pi-system-pools.go @@ -1,8 +1,12 @@ package instance import ( + "context" "fmt" + "github.com/IBM-Cloud/power-go-client/errors" + "github.com/IBM-Cloud/power-go-client/helpers" + "github.com/go-openapi/runtime" "github.com/IBM-Cloud/power-go-client/ibmpisession" "github.com/IBM-Cloud/power-go-client/power/client/p_cloud_system_pools" @@ -12,23 +16,33 @@ import ( // IBMPISystemPoolClient ... type IBMPISystemPoolClient struct { session *ibmpisession.IBMPISession - powerinstanceid string + cloudInstanceID string + authInfo runtime.ClientAuthInfoWriter + ctx context.Context } // NewIBMPISystemPoolClient ... -func NewIBMPISystemPoolClient(sess *ibmpisession.IBMPISession, powerinstanceid string) *IBMPISystemPoolClient { +func NewIBMPISystemPoolClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPISystemPoolClient { + authInfo := ibmpisession.NewAuth(sess, cloudInstanceID) return &IBMPISystemPoolClient{ - sess, powerinstanceid, + sess, + cloudInstanceID, + authInfo, + ctx, } } //Get the System Pools -func (f *IBMPISystemPoolClient) Get(powerinstanceid string) (models.SystemPools, error) { - params := p_cloud_system_pools.NewPcloudSystempoolsGetParamsWithTimeout(f.session.Timeout).WithCloudInstanceID(powerinstanceid) - resp, err := f.session.Power.PCloudSystemPools.PcloudSystempoolsGet(params, ibmpisession.NewAuth(f.session, powerinstanceid)) - - if err != nil || resp.Payload == nil { - return nil, fmt.Errorf(errors.GetSystemPoolsOperationFailed, powerinstanceid, err) +func (f *IBMPISystemPoolClient) Get(id string) (models.SystemPools, error) { + params := p_cloud_system_pools.NewPcloudSystempoolsGetParams(). + WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut). + WithCloudInstanceID(id) + resp, err := f.session.Power.PCloudSystemPools.PcloudSystempoolsGet(params, f.authInfo) + if err != nil { + return nil, fmt.Errorf(errors.GetSystemPoolsOperationFailed, id, err) + } + if resp == nil || resp.Payload == nil { + return nil, fmt.Errorf("failed to perform Get System Pools Operation for cloud instance id %s", id) } return resp.Payload, nil } diff --git a/errors/errors.go b/errors/errors.go index 1a024891..3cbe6299 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -10,11 +10,11 @@ import ( // start of Placementgroup Messages -const GetPlacementGroupOperationFailed = "failed to perform Get Placement Group Operation for placement group %s with error %w" +const GetPlacementGroupOperationFailed = "failed to perform Get Placement Group Operation for placement group %s with error %w" const CreatePlacementGroupOperationFailed = "failed to perform Create Placement Group Operation for powerinstanceid %s with error %w" const DeletePlacementGroupOperationFailed = "failed to perform Delete Placement Group Operation for placement group %s with error %w" -const UpdatePlacementGroupOperationFailed = "failed to perform Update Placement Group Operation for powerinstanceid %s and placement group %s with error %w" -const DeleteMemberPlacementGroupOperationFailed = "failed to perform Delete Placement Group Operation for powerinstanceid %s and placement group %s with error %w" +const AddMemberPlacementGroupOperationFailed = "failed to perform Add Member Operation for instance %s and placement group %s with error %w" +const DeleteMemberPlacementGroupOperationFailed = "failed to perform Delete Member Operation for instance %s and placement group %s with error %w" // start of Cloud Connection Messages