Skip to content

Commit

Permalink
Added support to get workload attached to rightSizing rule. (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadkesarwani authored Nov 13, 2024
1 parent e9de40f commit b11169e
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/service/ocean/right_sizing/readAttachedWorkloads/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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().ReadRightsizingRuleAttachedWorkloads(ctx, &right_sizing.ReadRightsizingRuleAttachedWorkloadsInput{
RuleName: spotinst.String("test-rule"),
OceanId: spotinst.String("o-123456"),
})
if err != nil {
log.Fatalf("spotinst: failed to read the right sizing rule: %v", err)
}
// Output.
if out.RightsizingRuleAttachedWorkloads != nil {
log.Printf("Attached workloads : %s",
stringutil.Stringify(out.RightsizingRuleAttachedWorkloads))
}
}
113 changes: 113 additions & 0 deletions service/ocean/right_sizing/right_sizing.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ type ReadRightsizingRuleOutput struct {
RightsizingRule *RightsizingRule `json:"rightsizingRule,omitempty"`
}

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

type ReadRightsizingRuleAttachedWorkloadsOutput struct {
RightsizingRuleAttachedWorkloads *RightsizingRuleAttachedWorkloads `json:"rightsizingRule,omitempty"`

forceSendFields []string
nullFields []string
}

type UpdateRightsizingRuleInput struct {
RuleName *string `json:"ruleName,omitempty"`
RightsizingRule *RightsizingRule `json:"rightsizingRule,omitempty"`
Expand All @@ -181,6 +193,42 @@ type CreateRightsizingRuleOutput struct {
RightsizingRule *RightsizingRule `json:"rightsizingRule,omitempty"`
}

type RightsizingRuleAttachedWorkloads struct {
RightsizingRuleWorkloads []*RightsizingRuleWorkloads `json:"workloads,omitempty"`
RightsizingRuleLabels []*RightsizingRuleLabels `json:"labels,omitempty"`
RightsizingRuleRegex []*RightsizingRuleRegex `json:"regexes,omitempty"`

forceSendFields []string
nullFields []string
}

type RightsizingRuleWorkloads struct {
Namespace *string `json:"namespace,omitempty"`
Type *string `json:"type,omitempty"`
Name *string `json:"name,omitempty"`

forceSendFields []string
nullFields []string
}

type RightsizingRuleLabels struct {
Key *string `json:"key,omitempty"`
Value *string `json:"value,omitempty"`
Namespace *string `json:"namespace,omitempty"`

forceSendFields []string
nullFields []string
}

type RightsizingRuleRegex struct {
Name *string `json:"name,omitempty"`
WorkloadType *string `json:"workloadType,omitempty"`
Namespace *string `json:"namespace,omitempty"`

forceSendFields []string
nullFields []string
}

func rightsizingRuleFromJSON(in []byte) (*RightsizingRule, error) {
b := new(RightsizingRule)
if err := json.Unmarshal(in, b); err != nil {
Expand Down Expand Up @@ -216,6 +264,41 @@ func rightsizingRulesFromHttpResponse(resp *http.Response) ([]*RightsizingRule,
return rightsizingRulesFromJSON(body)
}

func rightsizingRulesAttachedWorkloadsFromHttpResponse(resp *http.Response) ([]*RightsizingRuleAttachedWorkloads, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return rightsizingRulesAttachedWorkloadsFromJSON(body)
}

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

func rightsizingRulesAttachedWorkloadFromJSON(in []byte) (*RightsizingRuleAttachedWorkloads, error) {
b := new(RightsizingRuleAttachedWorkloads)
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 @@ -301,6 +384,36 @@ func (s *ServiceOp) ReadRightsizingRule(ctx context.Context, input *ReadRightsiz
return output, nil
}

func (s *ServiceOp) ReadRightsizingRuleAttachedWorkloads(ctx context.Context, input *ReadRightsizingRuleAttachedWorkloadsInput) (*ReadRightsizingRuleAttachedWorkloadsOutput, 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 := rightsizingRulesAttachedWorkloadsFromHttpResponse(resp)
if err != nil {
return nil, err
}

output := new(ReadRightsizingRuleAttachedWorkloadsOutput)
if len(gs) > 0 {
output.RightsizingRuleAttachedWorkloads = gs[0]
}

return output, nil
}

func (s *ServiceOp) UpdateRightsizingRule(ctx context.Context, input *UpdateRightsizingRuleInput) (*UpdateRightsizingRuleOutput, error) {
path, err := uritemplates.Expand("/ocean/{oceanId}/rightSizing/rule/{ruleName}", uritemplates.Values{
"oceanId": spotinst.StringValue(input.RightsizingRule.OceanId),
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)
ReadRightsizingRuleAttachedWorkloads(context.Context, *ReadRightsizingRuleAttachedWorkloadsInput) (*ReadRightsizingRuleAttachedWorkloadsOutput, error)
}

type ServiceOp struct {
Expand Down

0 comments on commit b11169e

Please sign in to comment.