diff --git a/clients/instance/ibm-pi-workspaces.go b/clients/instance/ibm-pi-workspaces.go new file mode 100644 index 00000000..6771c7ba --- /dev/null +++ b/clients/instance/ibm-pi-workspaces.go @@ -0,0 +1,40 @@ +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/workspaces" + "github.com/IBM-Cloud/power-go-client/power/models" +) + +// IBMPIWorkspacesClient +type IBMPIWorkspacesClient struct { + IBMPIClient +} + +// NewIBMPIWorkspacesClient +func NewIBMPIWorkspacesClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIWorkspacesClient { + return &IBMPIWorkspacesClient{ + *NewIBMPIClient(ctx, sess, cloudInstanceID), + } +} + +// Get a workspace +func (f *IBMPIWorkspacesClient) Get() (*models.Workspace, error) { + if f.session.IsOnPrem() { + return nil, fmt.Errorf("operation not supported in satellite location, check documentation") + } + params := workspaces.NewV1WorkspacesGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).WithWorkspaceID(f.cloudInstanceID) + resp, err := f.session.Power.Workspaces.V1WorkspacesGet(params, f.session.AuthInfo(f.cloudInstanceID)) + if err != nil { + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf(errors.GetWorkspaceOperationFailed, f.cloudInstanceID, err)) + } + if resp == nil || resp.Payload == nil { + return nil, fmt.Errorf("failed to Get Workspace %s", f.cloudInstanceID) + } + return resp.Payload, nil +} diff --git a/errors/errors.go b/errors/errors.go index 4ce4f612..5096b466 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -119,6 +119,9 @@ const DeleteSPPPlacementGroupOperationFailed = "failed to perform Delete Shared const AddMemberSPPPlacementGroupOperationFailed = "failed to perform Add Member Operation for pool %s and shared processor pool placement group %s with error %w" const DeleteMemberSPPPlacementGroupOperationFailed = "failed to perform Delete Member Operation for pool %s and shared processor pool placement group %s with error %w" +// start of workspaces +const GetWorkspaceOperationFailed = "failed to perform Get Workspace Operation for id %s with error %w" + // ErrorTarget ... type ErrorTarget struct { Name string diff --git a/examples/workspaces/main.go b/examples/workspaces/main.go new file mode 100644 index 00000000..8c323d31 --- /dev/null +++ b/examples/workspaces/main.go @@ -0,0 +1,52 @@ +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() { + // token := "" + apiKey := "" + region := "" + zone := "" + accountID := "" + url := region + ".power-iaas.test.cloud.ibm.com" + + piID := "" + // authenticator := &core.BearerTokenAuthenticator{ + // BearerToken: token, + // } + authenticator := &core.IamAuthenticator{ + ApiKey: apiKey, + // 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.NewIBMPIWorkspacesClient(context.Background(), session, piID) + if err != nil { + log.Fatal(err) + } + getWorkspace, err := powerClient.Get() + if err != nil { + log.Fatal(err) + } + log.Printf("***************[0]****************** %+v \n", *getWorkspace) +}