Skip to content

Commit

Permalink
Add Get All Workspaces (#254)
Browse files Browse the repository at this point in the history
* Add Get All Workspaces

* Update timeout
  • Loading branch information
michaelkad authored Oct 18, 2023
1 parent 9863b8d commit 750fdf5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
20 changes: 18 additions & 2 deletions clients/instance/ibm-pi-workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func NewIBMPIWorkspacesClient(ctx context.Context, sess *ibmpisession.IBMPISessi
}

// Get a workspace
func (f *IBMPIWorkspacesClient) Get() (*models.Workspace, error) {
func (f *IBMPIWorkspacesClient) Get(cloudInstanceID string) (*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)
params := workspaces.NewV1WorkspacesGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).WithWorkspaceID(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))
Expand All @@ -38,3 +38,19 @@ func (f *IBMPIWorkspacesClient) Get() (*models.Workspace, error) {
}
return resp.Payload, nil
}

// Get all workspaces
func (f *IBMPIWorkspacesClient) GetAll() (*models.Workspaces, error) {
if f.session.IsOnPrem() {
return nil, fmt.Errorf("operation not supported in satellite location, check documentation")
}
params := workspaces.NewV1WorkspacesGetallParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut)
resp, err := f.session.Power.Workspaces.V1WorkspacesGetall(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to Get all Workspaces: %w", err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to Get all Workspaces")
}
return resp.Payload, nil
}
58 changes: 58 additions & 0 deletions examples/datacenters/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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.NewIBMPIDatacenterClient(context.Background(), session, piID)
if err != nil {
log.Fatal(err)
}
getDatacenter, err := powerClient.Get("dal12")
if err != nil {
log.Fatal(err)
}
log.Printf("***************[0]****************** %+v \n", getDatacenter)
getDatacenters, err := powerClient.GetAll()
if err != nil {
log.Fatal(err)
}
log.Printf("***************[1]****************** %+v \n", getDatacenters)
}
7 changes: 6 additions & 1 deletion examples/workspaces/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ func main() {
if err != nil {
log.Fatal(err)
}
getWorkspace, err := powerClient.Get()
getWorkspace, err := powerClient.Get(piID)
if err != nil {
log.Fatal(err)
}
log.Printf("***************[0]****************** %+v \n", *getWorkspace)
getWorkspaces, err := powerClient.GetAll()
if err != nil {
log.Fatal(err)
}
log.Printf("***************[1]****************** %+v \n", *getWorkspaces)
}

0 comments on commit 750fdf5

Please sign in to comment.