From e07f58dbc6bed808408b525aa98c88e0650b00cc Mon Sep 17 00:00:00 2001 From: Mike Peter <103668254+m1kepeter@users.noreply.github.com> Date: Wed, 27 Sep 2023 15:18:45 +0200 Subject: [PATCH 1/4] suggest size --- .gitignore | 1 + Makefile | 4 ++-- cmd/common_test.go | 2 +- cmd/size.go | 37 ++++++++++++++++++++++++++++++++++++- cmd/size_test.go | 35 +++++++++++++++++++++++++++++++++++ go.mod | 4 +++- go.sum | 6 ++---- pkg/api/issues_test.go | 2 +- 8 files changed, 81 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index f78d60ba..bfc5c400 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ coverage.out metalctl result tmp +.DS_Store diff --git a/Makefile b/Makefile index 2ea69536..b00cdce7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -GOOS := linux -GOARCH := amd64 +GOOS := darwin +GOARCH := arm64 CGO_ENABLED := 1 TAGS := -tags 'netgo' BINARY := metalctl-$(GOOS)-$(GOARCH) diff --git a/cmd/common_test.go b/cmd/common_test.go index c63b9132..368c139a 100644 --- a/cmd/common_test.go +++ b/cmd/common_test.go @@ -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" diff --git a/cmd/size.go b/cmd/size.go index 83086fdb..92b8e5c0 100644 --- a/cmd/size.go +++ b/cmd/size.go @@ -71,7 +71,18 @@ 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 name of the suggested size") + + return genericcli.NewCmds(cmdsConfig, newSizeImageConstraintCmd(c), tryCmd, suggestCmd) } func (c sizeCmd) Get(id string) (*models.V1SizeResponse, error) { @@ -202,3 +213,27 @@ 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") + ) + + 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, + Constraints: resp.Payload, + }) + //return nil +} diff --git a/cmd/size_test.go b/cmd/size_test.go index 653d9632..a7006a6c 100644 --- a/cmd/size_test.go +++ b/cmd/size_test.go @@ -270,6 +270,41 @@ 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"} + + assertExhaustiveArgs(t, args, commonExcludedFileArgs()...) + return args + }, + mocks: &client.MetalMockFns{ + Size: func(mock *mock.Mock) { + mock.On("FindSize", testcommon.MatchIgnoreContext(t, size.NewFindSizeParams().WithID(*size1.ID)), nil).Return(&size.FindSizeOK{ + Payload: size1, + }, nil) + }, + }, + want: size1, + wantTable: pointer.Pointer(` +ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE +1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B +`), + wantWideTable: pointer.Pointer(` +ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE +1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B +`), + template: pointer.Pointer("{{ .id }} {{ .name }}"), + wantTemplate: pointer.Pointer(` +1 size-1 +`), + wantMarkdown: pointer.Pointer(` +| ID | NAME | DESCRIPTION | CPU RANGE | MEMORY RANGE | STORAGE RANGE | +|----|--------|-------------|-----------|--------------|---------------| +| 1 | size-1 | size 1 | 5 - 6 | 3 B - 4 B | 1 B - 2 B | +`), + }, } for _, tt := range tests { tt.testCmd(t) diff --git a/go.mod b/go.mod index c77efc58..54e39629 100644 --- a/go.mod +++ b/go.mod @@ -2,13 +2,15 @@ module github.com/metal-stack/metalctl go 1.21 +replace github.com/metal-stack/metal-go => ../metal-go + 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 diff --git a/go.sum b/go.sum index 4f3bb6e4..9f626dca 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/pkg/api/issues_test.go b/pkg/api/issues_test.go index 1470b938..3a31ecd2 100644 --- a/pkg/api/issues_test.go +++ b/pkg/api/issues_test.go @@ -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" From 940fc6fd90f3cda7d7826de0b1c285132b4db4de Mon Sep 17 00:00:00 2001 From: Mike Peter Date: Tue, 17 Oct 2023 12:07:02 +0200 Subject: [PATCH 2/4] size suggest test implemented --- cmd/size.go | 12 ++++++--- cmd/size_test.go | 64 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/cmd/size.go b/cmd/size.go index 92b8e5c0..1cfe473a 100644 --- a/cmd/size.go +++ b/cmd/size.go @@ -80,7 +80,9 @@ func newSizeCmd(c *config) *cobra.Command { } suggestCmd.Flags().String("machine-id", "", "Machine id used to create the size suggestion. [required]") - suggestCmd.Flags().String("id", "my-new-size", "The name of the suggested size") + 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) } @@ -216,8 +218,10 @@ func (c *sizeCmd) try() error { func (c *sizeCmd) suggest() error { var ( - machineid = viper.GetString("machine-id") - sizeid = viper.GetString("id") + machineid = viper.GetString("machine-id") + sizeid = viper.GetString("id") + name = viper.GetString("name") + description = viper.GetString("description") ) if machineid == "" { @@ -233,6 +237,8 @@ func (c *sizeCmd) suggest() error { return c.describePrinter.Print(&models.V1SizeResponse{ ID: &sizeid, + Name: name, + Description: description, Constraints: resp.Payload, }) //return nil diff --git a/cmd/size_test.go b/cmd/size_test.go index a7006a6c..a69aa1bd 100644 --- a/cmd/size_test.go +++ b/cmd/size_test.go @@ -274,36 +274,58 @@ ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE name: "suggest", cmd: func(want *models.V1SizeResponse) []string { - args := []string{"size", "suggest", "--machine-id=1", "--id=c1-large-x86"} + 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("FindSize", testcommon.MatchIgnoreContext(t, size.NewFindSizeParams().WithID(*size1.ID)), nil).Return(&size.FindSizeOK{ - Payload: size1, + 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: size1, - wantTable: pointer.Pointer(` -ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE -1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B -`), - wantWideTable: pointer.Pointer(` -ID NAME DESCRIPTION CPU RANGE MEMORY RANGE STORAGE RANGE -1 size-1 size 1 5 - 6 3 B - 4 B 1 B - 2 B -`), - template: pointer.Pointer("{{ .id }} {{ .name }}"), - wantTemplate: pointer.Pointer(` -1 size-1 -`), - wantMarkdown: pointer.Pointer(` -| ID | NAME | DESCRIPTION | CPU RANGE | MEMORY RANGE | STORAGE RANGE | -|----|--------|-------------|-----------|--------------|---------------| -| 1 | size-1 | size 1 | 5 - 6 | 3 B - 4 B | 1 B - 2 B | -`), + 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 { From c3836b1590b0f86002bb523eff729a38890f157e Mon Sep 17 00:00:00 2001 From: Mike Peter Date: Mon, 13 Nov 2023 10:13:44 +0100 Subject: [PATCH 3/4] updated makefile to linux again --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b00cdce7..2ea69536 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -GOOS := darwin -GOARCH := arm64 +GOOS := linux +GOARCH := amd64 CGO_ENABLED := 1 TAGS := -tags 'netgo' BINARY := metalctl-$(GOOS)-$(GOARCH) From 383cdf69127a971db651872caaca64e97a4eb28e Mon Sep 17 00:00:00 2001 From: Gerrit Date: Mon, 6 May 2024 09:20:10 +0200 Subject: [PATCH 4/4] Remove metal-go override. --- go.mod | 4 +--- go.sum | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a95ffcd9..35825dda 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/metal-stack/metalctl go 1.22 -replace github.com/metal-stack/metal-go => ../metal-go - require ( github.com/Masterminds/semver/v3 v3.2.1 github.com/dustin/go-humanize v1.0.1 @@ -11,6 +9,7 @@ require ( github.com/go-openapi/runtime v0.28.0 github.com/go-openapi/strfmt v0.23.0 github.com/google/go-cmp v0.6.0 + github.com/google/uuid v1.6.0 github.com/metal-stack/metal-go v0.28.3 github.com/metal-stack/metal-lib v0.16.1 github.com/metal-stack/updater v1.2.1 @@ -84,7 +83,6 @@ require ( github.com/google/go-github/v56 v56.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/nftables v0.2.0 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/gorilla/csrf v1.7.2 // indirect github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/securecookie v1.1.2 // indirect diff --git a/go.sum b/go.sum index ee9edbbc..c964bb50 100644 --- a/go.sum +++ b/go.sum @@ -240,6 +240,8 @@ 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.5.0 h1:ilICZmJcQz70vrWVes1MFera4jGiWNocSkykwwoy3XI= github.com/mdlayher/socket v0.5.0/go.mod h1:WkcBFfvyG8QENs5+hfQPl1X6Jpd2yeLIYgrGFmJiJxI= +github.com/metal-stack/metal-go v0.28.3 h1:mK9PMGbT8Ea/h/3QlhwH5zt3Ffi2rP2U/dqZ5uJ3mk4= +github.com/metal-stack/metal-go v0.28.3/go.mod h1:gYLZX3umsoZLWZ5d4MJdVbnR8eFXUTlLTK7tyx638As= github.com/metal-stack/metal-lib v0.16.1 h1:4iw2cQxS1pcQnuTVlXRvtmR0oCa6OCX6rIiZalfwSZY= github.com/metal-stack/metal-lib v0.16.1/go.mod h1:nyNGI4DZFOcWbSoq2Y6V3SHpFxuXBIqYBZHTb6cy//s= github.com/metal-stack/security v0.8.0 h1:tVaSDB9m5clwYrnLyaXfPy7mQlJTnmeoHscG+RUy/xo=