Skip to content

Commit

Permalink
Refactor placement groups and others
Browse files Browse the repository at this point in the history
Signed-off-by: Yussuf Shaikh <[email protected]>
  • Loading branch information
yussufsh committed Dec 9, 2021
1 parent 7f83d6e commit ebcc0b9
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 73 deletions.
58 changes: 39 additions & 19 deletions clients/instance/ibm-pi-cloud-instance.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
111 changes: 69 additions & 42 deletions clients/instance/ibm-pi-placement-groups.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -13,83 +16,107 @@ 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)
}
return nil
}

// 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
}
32 changes: 23 additions & 9 deletions clients/instance/ibm-pi-system-pools.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ebcc0b9

Please sign in to comment.