Skip to content

Commit

Permalink
Add SDK workspace create and deletion commands (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
ismirlia authored Nov 20, 2023
1 parent cba7ddf commit 981aecd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
71 changes: 71 additions & 0 deletions clients/instance/ibm-pi-workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@ import (
"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"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
)

// IBMPIWorkspacesClient
type IBMPIWorkspacesClient struct {
IBMPIClient
}

// Convert into valid plan or return empty (resulting in error)
func translatePlan(plan string) string {
planID := ""
switch plan {
case "public":
planID = "f165dd34-3a40-423b-9d95-e90a23f724dd"
case "private":
planID = "1112d6a9-71d6-4968-956b-eb3edbf0225b"
default:
planID = ""
}
return planID
}

// NewIBMPIWorkspacesClient
func NewIBMPIWorkspacesClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIWorkspacesClient {
return &IBMPIWorkspacesClient{
Expand Down Expand Up @@ -54,3 +70,58 @@ func (f *IBMPIWorkspacesClient) GetAll() (*models.Workspaces, error) {
}
return resp.Payload, nil
}

// Create a workspace
func (f *IBMPIWorkspacesClient) Create(name, location, groupID, plan string) (*resourcecontrollerv2.ResourceInstance, *core.DetailedResponse, error) {
resourceController, err := ibmpisession.CreateResourceControllerV2(f.session.Options.URL, f.session.Options.Authenticator)
if err != nil {
return nil, nil, fmt.Errorf("error creating Resource Controller client: %v", err)
}
planID := translatePlan(plan)
if planID == "" {
return nil, nil, fmt.Errorf("workspace creation error, incorrect plan value; either \"public\" or \"private\" is allowed")
}
params := resourceController.NewCreateResourceInstanceOptions(name, location, groupID, planID)
workspace, response, err := resourceController.CreateResourceInstance(params)
if err != nil {
return nil, response, fmt.Errorf("error creating workspace: workspace %v response %v err %v", workspace, response, err)
}
if response.StatusCode >= 400 {
return nil, response, fmt.Errorf("error creating resource instance. Status code: %d", response.StatusCode)
}
return workspace, response, nil
}

// Delete a workspace
func (f *IBMPIWorkspacesClient) Delete(workspaceID string) (*core.DetailedResponse, error) {
resourceController, err := ibmpisession.CreateResourceControllerV2(f.session.Options.URL, f.session.Options.Authenticator)
if err != nil {
return nil, fmt.Errorf("error creating Resource Controller client: %v", err)
}
params := resourceController.NewDeleteResourceInstanceOptions(workspaceID)
response, err := resourceController.DeleteResourceInstance(params)
if err != nil {
return response, fmt.Errorf("error deleting workspace: response %v, err %v", response, err)
}
if response.StatusCode >= 400 {
return response, fmt.Errorf("error deleting resource instance. Status code: %d", response.StatusCode)
}
return response, nil
}

// Get a resourceController
func (f *IBMPIWorkspacesClient) GetRC(rcWorkspaceID string) (*resourcecontrollerv2.ResourceInstance, *core.DetailedResponse, error) {
resourceController, err := ibmpisession.CreateResourceControllerV2(f.session.Options.URL, f.session.Options.Authenticator)
if err != nil {
return nil, nil, fmt.Errorf("error creating Resource Controller client: %v", err)
}
params := resourceController.NewGetResourceInstanceOptions(rcWorkspaceID)
workspace, response, err := resourceController.GetResourceInstance(params)
if err != nil {
return nil, response, fmt.Errorf("error creating workspace: workspace %v response %v err %v", workspace, response, err)
}
if response.StatusCode >= 400 {
return nil, response, fmt.Errorf("error getting resource instance. Status code: %d", response.StatusCode)
}
return workspace, response, nil
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/IBM/go-sdk-core/v5 v5.15.0
github.com/IBM/platform-services-go-sdk v0.54.0
github.com/apparentlymart/go-cidr v1.1.0
github.com/go-openapi/errors v0.20.4
github.com/go-openapi/runtime v0.26.0
Expand All @@ -16,7 +17,7 @@ require (
require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
Expand All @@ -26,13 +27,15 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.13.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/leodido/go-urn v1.2.3 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/gomega v1.29.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.11.3 // indirect
Expand Down
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/IBM/go-sdk-core/v5 v5.15.0 h1:AhFoWVk3i58f9vnDoEoZumI/zbtRoP5moWIz5YQOmZg=
github.com/IBM/go-sdk-core/v5 v5.15.0/go.mod h1:5Obavm/s1Tc2PxivEIfgCvj/HJ5h3QIOjLHS5y8QJf0=
github.com/IBM/platform-services-go-sdk v0.54.0 h1:WjHWm9ZAJvlq07E1WXXtEe+d/B0sazWD6cEWVT7EMLU=
github.com/IBM/platform-services-go-sdk v0.54.0/go.mod h1:CWSprvsCsXWvujmBzbtoJSmbRZS9FVV3O594b0t/GiM=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
Expand All @@ -14,8 +16,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-openapi/analysis v0.21.2/go.mod h1:HZwRk4RRisyG8vx2Oe6aqeSQcoxRp47Xkp3+K6q+LdY=
Expand Down Expand Up @@ -87,7 +89,8 @@ github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/V
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
Expand Down Expand Up @@ -130,7 +133,8 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
Expand Down
16 changes: 16 additions & 0 deletions ibmpisession/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/IBM-Cloud/power-go-client/power/client"
"github.com/IBM/go-sdk-core/v5/core"
rc "github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
)
Expand Down Expand Up @@ -106,3 +107,18 @@ func SDKFailWithAPIError(err error, origErr error) error {
}
return origErr
}

// CreateResourceControllerV2 returns a resourceControllerV2
func CreateResourceControllerV2(url string, authenticator core.Authenticator) (service *rc.ResourceControllerV2, err error) {
env := ""
if strings.Contains(url, "test") {
env = ".test"
}
rcUrl := "https://resource-controller" + env + ".cloud.ibm.com"

resourceController, err := rc.NewResourceControllerV2(&rc.ResourceControllerV2Options{
Authenticator: authenticator,
URL: rcUrl,
})
return resourceController, err
}

0 comments on commit 981aecd

Please sign in to comment.