Skip to content

Commit f6ec560

Browse files
author
Paola Nicosia
committed
feat: add console version error for catalog commands
1 parent a979ca6 commit f6ec560

File tree

11 files changed

+157
-9
lines changed

11 files changed

+157
-9
lines changed

internal/cmd/catalog/apply/apply.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ import (
2828
"github.com/mia-platform/miactl/internal/encoding"
2929
"github.com/mia-platform/miactl/internal/files"
3030
"github.com/mia-platform/miactl/internal/resources/catalog"
31+
"github.com/mia-platform/miactl/internal/util"
3132
"github.com/spf13/cobra"
3233
)
3334

3435
const (
3536
applyLong = `Create or update one or more Catalog items.
3637
38+
This command works with Mia-Platform Console v14.0.0 or later.
39+
3740
The flag -f accepts either files or directories. In case of directories, it explores them recursively.
3841
3942
Supported formats are JSON (.json files) and YAML (.yaml or .yml files).
@@ -107,6 +110,11 @@ func ApplyCmd(options *clioptions.CLIOptions) *cobra.Command {
107110
client, err := client.APIClientForConfig(restConfig)
108111
cobra.CheckErr(err)
109112

113+
canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), client, 14, 0)
114+
if !canUseNewAPI || versionError != nil {
115+
return catalog.ErrUnsupportedCompanyVersion
116+
}
117+
110118
companyID := restConfig.CompanyID
111119
if len(companyID) == 0 {
112120
return catalog.ErrMissingCompanyID

internal/cmd/catalog/apply/apply_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,34 @@ import (
2222
"mime/multipart"
2323
"net/http"
2424
"net/http/httptest"
25+
"strings"
2526
"testing"
2627

2728
"github.com/mia-platform/miactl/internal/client"
29+
"github.com/mia-platform/miactl/internal/clioptions"
2830
"github.com/mia-platform/miactl/internal/resources/catalog"
2931
"github.com/stretchr/testify/assert"
3032
"github.com/stretchr/testify/require"
3133
"sigs.k8s.io/kustomize/kyaml/yaml"
3234
)
3335

36+
func TestApplyCommand(t *testing.T) {
37+
t.Run("test post run - shows deprecated command message", func(t *testing.T) {
38+
server := httptest.NewServer(unexecutedCmdMockServer(t))
39+
defer server.Close()
40+
41+
opts := clioptions.NewCLIOptions()
42+
opts.CompanyID = "company-id"
43+
opts.Endpoint = server.URL
44+
45+
cmd := ApplyCmd(opts)
46+
cmd.SetArgs([]string{"apply", "-f", "testdata/validItem1.json"})
47+
48+
err := cmd.Execute()
49+
require.ErrorIs(t, err, catalog.ErrUnsupportedCompanyVersion)
50+
})
51+
}
52+
3453
func TestApplyBuildPathsFromDir(t *testing.T) {
3554
t.Run("should read all files in dir retrieving paths", func(t *testing.T) {
3655
dirPath := "./testdata/subdir"
@@ -1069,3 +1088,16 @@ func applyMockServer(t *testing.T, statusCode int, mockResponse interface{}) *ht
10691088
applyRequestHandler(t, w, r, statusCode, mockResponse)
10701089
}))
10711090
}
1091+
1092+
func unexecutedCmdMockServer(t *testing.T) http.HandlerFunc {
1093+
t.Helper()
1094+
return func(w http.ResponseWriter, r *http.Request) {
1095+
if strings.EqualFold(r.URL.Path, "/api/version") && r.Method == http.MethodGet {
1096+
_, err := w.Write([]byte(`{"major": "13", "minor":"6"}`))
1097+
require.NoError(t, err)
1098+
} else {
1099+
w.WriteHeader(http.StatusNotFound)
1100+
assert.Fail(t, fmt.Sprintf("unexpected request: %s", r.URL.Path))
1101+
}
1102+
}
1103+
}

internal/cmd/catalog/delete.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/mia-platform/miactl/internal/client"
2525
"github.com/mia-platform/miactl/internal/clioptions"
2626
"github.com/mia-platform/miactl/internal/resources/catalog"
27+
"github.com/mia-platform/miactl/internal/util"
2728
"github.com/spf13/cobra"
2829
)
2930

@@ -33,6 +34,8 @@ const (
3334

3435
cmdDeleteLongDescription = `Delete a single Catalog item
3536
37+
This command works with Mia-Platform Console v14.0.0 or later.
38+
3639
You need to specify the companyId, itemId and version, via the respective flags (recommended). The company-id flag can be omitted if it is already set in the context.
3740
`
3841
cmdUse = "delete { --item-id item-id --version version }"
@@ -56,6 +59,11 @@ func DeleteCmd(options *clioptions.CLIOptions) *cobra.Command {
5659
client, err := client.APIClientForConfig(restConfig)
5760
cobra.CheckErr(err)
5861

62+
canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), client, 14, 0)
63+
if !canUseNewAPI || versionError != nil {
64+
return catalog.ErrUnsupportedCompanyVersion
65+
}
66+
5967
companyID := restConfig.CompanyID
6068
if len(companyID) == 0 {
6169
return catalog.ErrMissingCompanyID

internal/cmd/catalog/delete_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ func TestDeleteResourceCmd(t *testing.T) {
3737
cmd := DeleteCmd(opts)
3838
require.NotNil(t, cmd)
3939
})
40+
41+
t.Run("should not run command when Console version is lower than 14.0.0", func(t *testing.T) {
42+
server := httptest.NewServer(unexecutedCmdMockServer(t))
43+
defer server.Close()
44+
45+
opts := clioptions.NewCLIOptions()
46+
opts.CompanyID = mockDeleteCompanyID
47+
opts.Endpoint = server.URL
48+
49+
cmd := DeleteCmd(opts)
50+
cmd.SetArgs([]string{"delete", "--item-id", "some-item-id", "--version", "1.0.0"})
51+
52+
err := cmd.Execute()
53+
require.ErrorIs(t, err, catalog.ErrUnsupportedCompanyVersion)
54+
})
4055
}
4156

4257
func deleteByItemIDAndVersionMockServer(t *testing.T,

internal/cmd/catalog/get.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/mia-platform/miactl/internal/clioptions"
2424
"github.com/mia-platform/miactl/internal/encoding"
2525
"github.com/mia-platform/miactl/internal/resources/catalog"
26+
"github.com/mia-platform/miactl/internal/util"
2627
"github.com/spf13/cobra"
2728
)
2829

@@ -31,6 +32,8 @@ const (
3132

3233
cmdGetLongDescription = `Get a single Catalog item
3334
35+
This command works with Mia-Platform Console v14.0.0 or later.
36+
3437
You need to specify the companyId, itemId and version, via the respective flags. The company-id flag can be omitted if it is already set in the context.
3538
`
3639
cmdGetUse = "get { --item-id item-id --version version }"
@@ -48,6 +51,11 @@ func GetCmd(options *clioptions.CLIOptions) *cobra.Command {
4851
client, err := client.APIClientForConfig(restConfig)
4952
cobra.CheckErr(err)
5053

54+
canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), client, 14, 0)
55+
if !canUseNewAPI || versionError != nil {
56+
return catalog.ErrUnsupportedCompanyVersion
57+
}
58+
5159
serializedItem, err := getItemEncodedWithFormat(
5260
cmd.Context(),
5361
client,

internal/cmd/catalog/get_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/mia-platform/miactl/internal/client"
2525
"github.com/mia-platform/miactl/internal/clioptions"
2626
"github.com/mia-platform/miactl/internal/encoding"
27+
"github.com/mia-platform/miactl/internal/resources/catalog"
2728
"github.com/stretchr/testify/require"
2829
"sigs.k8s.io/kustomize/kyaml/yaml"
2930
)
@@ -81,6 +82,21 @@ func TestGetResourceCmd(t *testing.T) {
8182
cmd := GetCmd(opts)
8283
require.NotNil(t, cmd)
8384
})
85+
86+
t.Run("should not run command when Console version is lower than 14.0.0", func(t *testing.T) {
87+
server := httptest.NewServer(unexecutedCmdMockServer(t))
88+
defer server.Close()
89+
90+
opts := clioptions.NewCLIOptions()
91+
opts.CompanyID = mockCompanyID
92+
opts.Endpoint = server.URL
93+
94+
cmd := GetCmd(opts)
95+
cmd.SetArgs([]string{"get", "--item-id", mockItemID, "--version", mockVersion})
96+
97+
err := cmd.Execute()
98+
require.ErrorIs(t, err, catalog.ErrUnsupportedCompanyVersion)
99+
})
84100
}
85101

86102
func getItemByTupleMockServer(

internal/cmd/catalog/list.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import (
2424
"github.com/mia-platform/miactl/internal/printer"
2525
"github.com/mia-platform/miactl/internal/resources"
2626
"github.com/mia-platform/miactl/internal/resources/catalog"
27+
"github.com/mia-platform/miactl/internal/util"
2728
"github.com/spf13/cobra"
2829
)
2930

3031
const (
3132
listMarketplaceEndpoint = "/api/marketplace/"
3233
listCmdLong = `List Catalog items
3334
34-
This command lists the Catalog items of a company.
35+
This command lists the Catalog items of a company. It works with Mia-Platform Console v14.0.0 or later.
3536
3637
you can also specify the following flags:
3738
- --public - if this flag is set, the command fetches not only the items from the requested company, but also the public Catalog items from other companies.
@@ -45,28 +46,35 @@ func ListCmd(options *clioptions.CLIOptions) *cobra.Command {
4546
Use: listCmdUse,
4647
Short: "List catalog items",
4748
Long: listCmdLong,
48-
Run: runListCmd(options),
49+
RunE: runListCmd(options),
4950
}
5051

5152
options.AddPublicFlag(cmd.Flags())
5253

5354
return cmd
5455
}
5556

56-
func runListCmd(options *clioptions.CLIOptions) func(cmd *cobra.Command, args []string) {
57-
return func(cmd *cobra.Command, _ []string) {
57+
func runListCmd(options *clioptions.CLIOptions) func(cmd *cobra.Command, args []string) error {
58+
return func(cmd *cobra.Command, _ []string) error {
5859
restConfig, err := options.ToRESTConfig()
5960
cobra.CheckErr(err)
6061
apiClient, err := client.APIClientForConfig(restConfig)
6162
cobra.CheckErr(err)
6263

64+
canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), apiClient, 14, 0)
65+
if !canUseNewAPI || versionError != nil {
66+
return catalog.ErrUnsupportedCompanyVersion
67+
}
68+
6369
marketplaceItemsOptions := GetMarketplaceItemsOptions{
6470
companyID: restConfig.CompanyID,
6571
public: options.MarketplaceFetchPublicItems,
6672
}
6773

6874
err = printMarketplaceItems(cmd.Context(), apiClient, marketplaceItemsOptions, options.Printer())
6975
cobra.CheckErr(err)
76+
77+
return nil
7078
}
7179
}
7280

internal/cmd/catalog/list_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/mia-platform/miactl/internal/client"
2626
"github.com/mia-platform/miactl/internal/clioptions"
2727
"github.com/mia-platform/miactl/internal/printer"
28+
"github.com/mia-platform/miactl/internal/resources/catalog"
2829
"github.com/stretchr/testify/assert"
2930
"github.com/stretchr/testify/require"
3031
)
@@ -35,6 +36,21 @@ func TestNewGetCmd(t *testing.T) {
3536
cmd := ListCmd(opts)
3637
require.NotNil(t, cmd)
3738
})
39+
40+
t.Run("should not run command when Console version is lower than 14.0.0", func(t *testing.T) {
41+
server := httptest.NewServer(unexecutedCmdMockServer(t))
42+
defer server.Close()
43+
44+
opts := clioptions.NewCLIOptions()
45+
opts.CompanyID = "my-company"
46+
opts.Endpoint = server.URL
47+
48+
cmd := ListCmd(opts)
49+
cmd.SetArgs([]string{"list"})
50+
51+
err := cmd.Execute()
52+
require.ErrorIs(t, err, catalog.ErrUnsupportedCompanyVersion)
53+
})
3854
}
3955

4056
func TestBuildMarketplaceItemsList(t *testing.T) {
@@ -122,6 +138,19 @@ func TestBuildMarketplaceItemsList(t *testing.T) {
122138
}
123139
}
124140

141+
func unexecutedCmdMockServer(t *testing.T) http.HandlerFunc {
142+
t.Helper()
143+
return func(w http.ResponseWriter, r *http.Request) {
144+
if strings.EqualFold(r.URL.Path, "/api/version") && r.Method == http.MethodGet {
145+
_, err := w.Write([]byte(`{"major": "13", "minor":"6"}`))
146+
require.NoError(t, err)
147+
} else {
148+
w.WriteHeader(http.StatusNotFound)
149+
assert.Fail(t, fmt.Sprintf("unexpected request: %s", r.URL.Path))
150+
}
151+
}
152+
}
153+
125154
func privateAndPublicMarketplaceHandler(t *testing.T) http.HandlerFunc {
126155
t.Helper()
127156
return func(w http.ResponseWriter, r *http.Request) {

internal/cmd/catalog/list_versions.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/mia-platform/miactl/internal/clioptions"
2626
"github.com/mia-platform/miactl/internal/printer"
2727
"github.com/mia-platform/miactl/internal/resources/catalog"
28+
"github.com/mia-platform/miactl/internal/util"
2829
"github.com/spf13/cobra"
2930
)
3031

@@ -40,13 +41,18 @@ func ListVersionCmd(options *clioptions.CLIOptions) *cobra.Command {
4041
Use: "list-versions",
4142
Short: "List versions of a Marketplace item",
4243
Long: `List the currently available versions of a Marketplace item.
43-
The command will output a table with each version of the item.`,
44-
Run: func(cmd *cobra.Command, _ []string) {
44+
The command will output a table with each version of the item. It works with Mia-Platform Console v14.0.0 or later.`,
45+
RunE: func(cmd *cobra.Command, _ []string) error {
4546
restConfig, err := options.ToRESTConfig()
4647
cobra.CheckErr(err)
4748
client, err := client.APIClientForConfig(restConfig)
4849
cobra.CheckErr(err)
4950

51+
canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), client, 14, 0)
52+
if !canUseNewAPI || versionError != nil {
53+
return catalog.ErrUnsupportedCompanyVersion
54+
}
55+
5056
releases, err := getItemVersions(
5157
cmd.Context(),
5258
client,
@@ -58,6 +64,8 @@ The command will output a table with each version of the item.`,
5864
printItemVersionList(releases, options.Printer(
5965
clioptions.DisableWrapLines(true),
6066
))
67+
68+
return nil
6169
},
6270
}
6371

internal/cmd/catalog/list_versions_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ func TestNewListVersionsCmd(t *testing.T) {
6464
cmd := ListVersionCmd(opts)
6565
require.NotNil(t, cmd)
6666
})
67+
68+
t.Run("should not run command when Console version is lower than 14.0.0", func(t *testing.T) {
69+
server := httptest.NewServer(unexecutedCmdMockServer(t))
70+
defer server.Close()
71+
72+
opts := clioptions.NewCLIOptions()
73+
opts.CompanyID = "my-company"
74+
opts.Endpoint = server.URL
75+
76+
cmd := ListVersionCmd(opts)
77+
cmd.SetArgs([]string{"list-versions", "--item-id", "item-id"})
78+
79+
err := cmd.Execute()
80+
require.ErrorIs(t, err, catalog.ErrUnsupportedCompanyVersion)
81+
})
6782
}
6883

6984
func TestGetItemVersions(t *testing.T) {

0 commit comments

Comments
 (0)