Skip to content

Commit

Permalink
Fix(Ocean/Spark) - Add list VNGs API (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextarasov-spot authored Feb 9, 2023
1 parent 4ad43e8 commit a6201a9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
// Detach VNG.
ctx := context.Background()

// Delete an existing cluster.
// Detach Ocean VNG
_, err := svc.Spark().DetachVirtualNodeGroup(ctx, &spark.DetachVngInput{
ClusterID: spotinst.String("osc-12345"),
VngID: spotinst.String("ols-12345"),
Expand Down
47 changes: 47 additions & 0 deletions examples/service/ocean/spark/virtualNodeGroup/list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"log"

"github.com/spotinst/spotinst-sdk-go/service/ocean"
"github.com/spotinst/spotinst-sdk-go/service/ocean/spark"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil"
)

func main() {
// All clients require a Session. The Session provides the client with
// shared configuration such as account and credentials.
// A Session should be shared where possible to take advantage of
// configuration and credential caching. See the session package for
// more information.
sess := session.New()

// Create a new instance of the service's client with a Session.
// Optional spotinst.Config values can also be provided as variadic
// arguments to the New function. This option allows you to provide
// service specific configuration.
svc := ocean.New(sess)

// Create a new context.
ctx := context.Background()

// List dedicated VNGs.
out, err := svc.Spark().ListVirtualNodeGroups(ctx, &spark.ListVngsInput{
ClusterID: spotinst.String("osc-12345"),
})
if err != nil {
log.Fatalf("spotinst: failed to list VNGs: %v", err)
}

// Output dedicated VNGs, if any.
if len(out.VirtualNodeGroups) > 0 {
for _, vng := range out.VirtualNodeGroups {
log.Printf("VNG %q: %s",
spotinst.StringValue(vng.VngID),
stringutil.Stringify(vng))
}
}
}
1 change: 1 addition & 0 deletions service/ocean/spark/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Service interface {
DeleteCluster(context.Context, *DeleteClusterInput) (*DeleteClusterOutput, error)
CreateCluster(context.Context, *CreateClusterInput) (*CreateClusterOutput, error)
UpdateCluster(context.Context, *UpdateClusterInput) (*UpdateClusterOutput, error)
ListVirtualNodeGroups(context.Context, *ListVngsInput) (*ListVngsOutput, error)
DetachVirtualNodeGroup(context.Context, *DetachVngInput) (*DetachVngOutput, error)
AttachVirtualNodeGroup(context.Context, *AttachVngInput) (*AttachVngOutput, error)
}
Expand Down
32 changes: 32 additions & 0 deletions service/ocean/spark/spark.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,38 @@ func clusterFromJSON(in []byte) (*Cluster, error) {
//endregion

// region Virtual Node Group
func (s *ServiceOp) ListVirtualNodeGroups(ctx context.Context, input *ListVngsInput) (*ListVngsOutput, error) {
if input == nil {
return nil, fmt.Errorf("input is nil")
}

path, err := uritemplates.Expand("/ocean/spark/cluster/{clusterId}/virtualNodeGroup", uritemplates.Values{
"clusterId": spotinst.StringValue(input.ClusterID),
})
if err != nil {
return nil, err
}

r := client.NewRequest(http.MethodGet, path)
resp, err := client.RequireOK(s.Client.Do(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()

vngs, err := vngsFromHttpResponse(resp)
if err != nil {
return nil, err
}

output := new(ListVngsOutput)
if len(vngs) > 0 {
output.VirtualNodeGroups = vngs
}

return output, nil
}

func (s *ServiceOp) DetachVirtualNodeGroup(ctx context.Context, input *DetachVngInput) (*DetachVngOutput, error) {
if input == nil {
return nil, fmt.Errorf("input is nil")
Expand Down
8 changes: 8 additions & 0 deletions service/ocean/spark/vng.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ type DetachVngInput struct {
}

type DetachVngOutput struct{}

type ListVngsInput struct {
ClusterID *string `json:"clusterId,omitempty"`
}

type ListVngsOutput struct {
VirtualNodeGroups []*DedicatedVirtualNodeGroup `json:"virtualNodeGroups,omitempty"`
}

0 comments on commit a6201a9

Please sign in to comment.