Skip to content

Commit

Permalink
Added support for "GetWorkloadsAttached" for rightsizing rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadkesarwani committed Sep 17, 2024
1 parent 1b86c76 commit 828fe73
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/service/ocean/right_sizing/getWorkloadsAttached/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"github.com/spotinst/spotinst-sdk-go/service/ocean"
"github.com/spotinst/spotinst-sdk-go/service/ocean/right_sizing"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil"
"log"
)

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()

// Read an existing right sizing rule
out, err := svc.RightSizing().GetWorkloadsAttached(ctx, &right_sizing.GetWorkloadsAttachedInput{
RuleName: spotinst.String("demo-rule"),
OceanId: spotinst.String("o-123456"),
})
if err != nil {
log.Fatalf("spotinst: failed to read attached workload: %v", err)
}

// Output.
if out.Workloads != nil {
log.Printf("Workloads %q",
stringutil.Stringify(out.Workloads))

}
}
83 changes: 83 additions & 0 deletions service/ocean/right_sizing/right_sizing.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ type Workload struct {
forceSendFields []string
nullFields []string
}
type Workloads struct {
Name *string `json:"name,omitempty"`
Type *string `json:"type,omitempty"`
Namespace *string `json:"namespace,omitempty"`

forceSendFields []string
nullFields []string
}

type RecommendationApplicationOverheadValues struct {
CpuPercentage *float64 `json:"cpuPercentage,omitempty"`
Expand Down Expand Up @@ -157,6 +165,15 @@ type ReadRightsizingRuleOutput struct {
RightsizingRule *RightsizingRule `json:"rightsizingRule,omitempty"`
}

type GetWorkloadsAttachedInput struct {
RuleName *string `json:"ruleName,omitempty"`
OceanId *string `json:"oceanId,omitempty"`
}

type GetWorkloadsAttachedOutput struct {
Workloads []*Workloads `json:"workloads,omitempty"`
}

type UpdateRightsizingRuleInput struct {
RuleName *string `json:"ruleName,omitempty"`
RightsizingRule *RightsizingRule `json:"rightsizingRule,omitempty"`
Expand Down Expand Up @@ -216,6 +233,42 @@ func rightsizingRulesFromHttpResponse(resp *http.Response) ([]*RightsizingRule,
return rightsizingRulesFromJSON(body)
}

// ======================================
func workloadsFromHttpResponse(resp *http.Response) ([]*Workloads, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return workloadsFromJSON(body)
}

func workloadsFromJSON(in []byte) ([]*Workloads, error) {
var rw client.Response
if err := json.Unmarshal(in, &rw); err != nil {
return nil, err
}
out := make([]*Workloads, len(rw.Response.Items))
if len(out) == 0 {
return out, nil
}
for i, rb := range rw.Response.Items {
b, err := workloadFromJSON(rb)
if err != nil {
return nil, err
}
out[i] = b
}
return out, nil
}

func workloadFromJSON(in []byte) (*Workloads, error) {
b := new(Workloads)
if err := json.Unmarshal(in, b); err != nil {
return nil, err
}
return b, nil
}

func (s *ServiceOp) ListRightsizingRules(ctx context.Context, input *ListRightsizingRulesInput) (*ListRightsizingRulesOutput, error) {
path, err := uritemplates.Expand("/ocean/{oceanId}/rightSizing/rule", uritemplates.Values{
"oceanId": spotinst.StringValue(input.OceanId),
Expand Down Expand Up @@ -401,6 +454,36 @@ func (s *ServiceOp) DetachRightSizingRule(ctx context.Context, input *RightSizin
return &RightSizingAttachDetachOutput{}, nil
}

func (s *ServiceOp) GetWorkloadsAttached(ctx context.Context, input *GetWorkloadsAttachedInput) (*GetWorkloadsAttachedOutput, error) {
path, err := uritemplates.Expand("/ocean/{oceanId}/rightSizing/rule/{ruleName}/workloads", uritemplates.Values{
"oceanId": spotinst.StringValue(input.OceanId),
"ruleName": spotinst.StringValue(input.RuleName),
})

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()

gs, err := workloadsFromHttpResponse(resp)
if err != nil {
return nil, err
}

output := new(GetWorkloadsAttachedOutput)
if len(gs) > 0 {
output.Workloads = gs
}

return output, nil
}

// region RightsizingRule

func (o RightsizingRule) MarshalJSON() ([]byte, error) {
Expand Down
1 change: 1 addition & 0 deletions service/ocean/right_sizing/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Service interface {
DeleteRightsizingRules(context.Context, *DeleteRightsizingRuleInput) (*DeleteRightsizingRuleOutput, error)
AttachRightSizingRule(context.Context, *RightSizingAttachDetachInput) (*RightSizingAttachDetachOutput, error)
DetachRightSizingRule(context.Context, *RightSizingAttachDetachInput) (*RightSizingAttachDetachOutput, error)
GetWorkloadsAttached(context.Context, *GetWorkloadsAttachedInput) (*GetWorkloadsAttachedOutput, error)
}

type ServiceOp struct {
Expand Down

0 comments on commit 828fe73

Please sign in to comment.