Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce size suggest command #221

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ coverage.out
metalctl
result
tmp
.DS_Store
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GOOS := linux
GOARCH := amd64
GOOS := darwin
GOARCH := arm64
m1kepeter marked this conversation as resolved.
Show resolved Hide resolved
CGO_ENABLED := 1
TAGS := -tags 'netgo'
BINARY := metalctl-$(GOOS)-$(GOARCH)
Expand Down
2 changes: 1 addition & 1 deletion cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"slices"

"bou.ke/monkey"
"github.com/google/go-cmp/cmp"
"github.com/markelog/monkey"
"github.com/metal-stack/metal-go/test/client"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/metal-stack/metal-lib/pkg/testcommon"
Expand Down
43 changes: 42 additions & 1 deletion cmd/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ func newSizeCmd(c *config) *cobra.Command {
tryCmd.Flags().StringP("memory", "M", "", "Memory of the hardware to try, can be given in bytes or any human readable size spec")
tryCmd.Flags().StringP("storagesize", "S", "", "Total storagesize of the hardware to try, can be given in bytes or any human readable size spec")

return genericcli.NewCmds(cmdsConfig, newSizeImageConstraintCmd(c), tryCmd)
suggestCmd := &cobra.Command{
Use: "suggest",
Short: "suggest size from a given machine id",
RunE: func(cmd *cobra.Command, args []string) error {
return w.suggest()
},
}

suggestCmd.Flags().String("machine-id", "", "Machine id used to create the size suggestion. [required]")
suggestCmd.Flags().String("id", "my-new-size", "The id of the suggested size")
suggestCmd.Flags().String("name", "mysize", "The name of the suggested size")
suggestCmd.Flags().String("description", "foo", "The description of the suggested size")

return genericcli.NewCmds(cmdsConfig, newSizeImageConstraintCmd(c), tryCmd, suggestCmd)
}

func (c sizeCmd) Get(id string) (*models.V1SizeResponse, error) {
Expand Down Expand Up @@ -202,3 +215,31 @@ func (c *sizeCmd) try() error {

return c.listPrinter.Print(resp.Payload)
}

func (c *sizeCmd) suggest() error {
var (
machineid = viper.GetString("machine-id")
sizeid = viper.GetString("id")
name = viper.GetString("name")
description = viper.GetString("description")
)

if machineid == "" {
return fmt.Errorf("machine-id flag is required")
}

resp, err := c.client.Size().Suggest(size.NewSuggestParams().WithBody(&models.V1SizeSuggestRequest{
MachineID: &machineid,
}), nil)
if err != nil {
return err
}

return c.describePrinter.Print(&models.V1SizeResponse{
ID: &sizeid,
Name: name,
Description: description,
Constraints: resp.Payload,
})
//return nil
}
57 changes: 57 additions & 0 deletions cmd/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,63 @@ ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE
},
want: size1,
},
{
name: "suggest",
cmd: func(want *models.V1SizeResponse) []string {

args := []string{"size", "suggest", "--machine-id=1", "--id=c1-large-x86", "--name=mysize", "--description=foo"}

assertExhaustiveArgs(t, args, commonExcludedFileArgs()...)
return args
},
mocks: &client.MetalMockFns{
Size: func(mock *mock.Mock) {
mock.On("Suggest", testcommon.MatchIgnoreContext(t, size.NewSuggestParams().WithBody(&models.V1SizeSuggestRequest{
MachineID: pointer.Pointer("1"),
})), nil).Return(&size.SuggestOK{
Payload: []*models.V1SizeConstraint{
{
Max: pointer.Pointer(int64(2)),
Min: pointer.Pointer(int64(1)),
Type: pointer.Pointer("storage"),
},
{
Max: pointer.Pointer(int64(4)),
Min: pointer.Pointer(int64(3)),
Type: pointer.Pointer("memory"),
},
{
Max: pointer.Pointer(int64(6)),
Min: pointer.Pointer(int64(5)),
Type: pointer.Pointer("cores"),
},
},
}, nil)
},
},
want: &models.V1SizeResponse{
Constraints: []*models.V1SizeConstraint{
{
Max: pointer.Pointer(int64(2)),
Min: pointer.Pointer(int64(1)),
Type: pointer.Pointer("storage"),
},
{
Max: pointer.Pointer(int64(4)),
Min: pointer.Pointer(int64(3)),
Type: pointer.Pointer("memory"),
},
{
Max: pointer.Pointer(int64(6)),
Min: pointer.Pointer(int64(5)),
Type: pointer.Pointer("cores"),
},
},
Description: "foo",
ID: pointer.Pointer("c1-large-x86"),
Name: "mysize",
},
},
}
for _, tt := range tests {
tt.testCmd(t)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ module github.com/metal-stack/metalctl

go 1.21

replace github.com/metal-stack/metal-go => ../metal-go
Gerrit91 marked this conversation as resolved.
Show resolved Hide resolved

require (
bou.ke/monkey v1.0.2
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.15.0
github.com/go-openapi/runtime v0.26.0
github.com/go-openapi/strfmt v0.21.7
github.com/google/go-cmp v0.5.9
github.com/markelog/monkey v1.1.2
github.com/metal-stack/metal-go v0.24.1
github.com/metal-stack/metal-lib v0.13.3
github.com/metal-stack/updater v1.1.5
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down Expand Up @@ -426,6 +424,8 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/markelog/monkey v1.1.2 h1:b41Jgmf0OGQ2uv3IHJIFBhgiro52upPgfvVt+imn/bc=
github.com/markelog/monkey v1.1.2/go.mod h1:kupJDTvVpJ2r5ZPWwjfnEJY7hO46SzwOv78PLkDXlYs=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
Expand All @@ -443,8 +443,6 @@ github.com/mdlayher/sdnotify v1.0.0 h1:Ma9XeLVN/l0qpyx1tNeMSeTjCPH6NtuD6/N9XdTlQ
github.com/mdlayher/sdnotify v1.0.0/go.mod h1:HQUmpM4XgYkhDLtd+Uad8ZFK1T9D5+pNxnXQjCeJlGE=
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA=
github.com/metal-stack/metal-go v0.24.1 h1:S4g3LXeWBELCKIyyCsRMa9B1pC5EClqcX35oV/8xv2U=
github.com/metal-stack/metal-go v0.24.1/go.mod h1:jNJ0dWIBRwKeJoP+RGqTyE5qLsdZFISFrNHU5m3IDwA=
github.com/metal-stack/metal-lib v0.13.3 h1:BOhwcKHILmBZd2pz2YMOhj8QxzDaz3G0F/CGuYhnu8o=
github.com/metal-stack/metal-lib v0.13.3/go.mod h1:BAR7fjdoV7DDg8i9GpJQBDaNSFirOcBs0vLYTBnhHQU=
github.com/metal-stack/security v0.6.7 h1:8wstGy0pdUmphVclAlT+9RKQmx9lF+cIGklJZAB5cIc=
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"testing"
"time"

"bou.ke/monkey"
"github.com/go-openapi/strfmt"
"github.com/google/go-cmp/cmp"
"github.com/markelog/monkey"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/stretchr/testify/require"
Expand Down