Skip to content

Commit 99ef8a3

Browse files
LiliumCandidumPaola Nicosia
andauthored
feat: item type definition commands (#262)
Co-authored-by: Paola Nicosia <[email protected]>
1 parent 249b478 commit 99ef8a3

File tree

17 files changed

+1692
-1
lines changed

17 files changed

+1692
-1
lines changed

internal/clioptions/clioptions.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type CLIOptions struct {
6161
UserEmails []string
6262
UserIDs []string
6363

64+
Page int
65+
6466
ServiceAccountID string
6567

6668
BasicClientID string
@@ -79,6 +81,9 @@ type CLIOptions struct {
7981
MarketplaceItemObjectID string
8082
MarketplaceFetchPublicItems bool
8183

84+
ItemTypeDefinitionName string
85+
ItemTypeDefinitionFilePath string
86+
8287
FromCronJob string
8388

8489
FollowLogs bool
@@ -250,6 +255,12 @@ func (o *CLIOptions) AddPublicFlag(flags *pflag.FlagSet) (flagName string) {
250255
return
251256
}
252257

258+
func (o *CLIOptions) AddPageFlag(flags *pflag.FlagSet) (flagName string) {
259+
flagName = "page"
260+
flags.IntVar(&o.Page, flagName, 1, "specify the page to fetch")
261+
return
262+
}
263+
253264
func (o *CLIOptions) AddMarketplaceItemObjectIDFlag(flags *pflag.FlagSet) (flagName string) {
254265
flagName = "object-id"
255266
flags.StringVar(&o.MarketplaceItemObjectID, flagName, "", "The _id of the Marketplace item")
@@ -262,6 +273,21 @@ func (o *CLIOptions) AddMarketplaceVersionFlag(flags *pflag.FlagSet) (flagName s
262273
return
263274
}
264275

276+
func (o *CLIOptions) AddItemTypeDefinitionNameFlag(flags *pflag.FlagSet) (flagName string) {
277+
flagName = "name"
278+
flags.StringVarP(&o.ItemTypeDefinitionName, flagName, "i", "", "The name of the Item Type Definition")
279+
return
280+
}
281+
282+
func (o *CLIOptions) AddItemTypeDefinitionFileFlag(cmd *cobra.Command) {
283+
cmd.Flags().StringVarP(&o.ItemTypeDefinitionFilePath, "file", "f", "", "paths to JSON/YAML file containing an item type definition")
284+
err := cmd.MarkFlagRequired("file")
285+
if err != nil {
286+
// the error is only due to a programming error (missing command), hence panic
287+
panic(err)
288+
}
289+
}
290+
265291
func (o *CLIOptions) AddCreateJobFlags(flags *pflag.FlagSet) {
266292
flags.StringVar(&o.FromCronJob, "from", "", "name of the cronjob to create a Job from")
267293
}

internal/cmd/catalog/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
3737
This command works with Mia-Platform Console v14.0.0 or later.
3838
39-
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.
39+
You need to specify the itemId, via the respective flag. The company-id flag can be omitted if it is already set in the context.
4040
`
4141
cmdGetUse = "get { --item-id item-id --version version }"
4242
)

internal/cmd/catalog/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ const (
3030
3131
This command lists the Catalog items of a company. It works with Mia-Platform Console v14.0.0 or later.
3232
33+
Results are paginated. By default, only the first page is shown.
34+
3335
you can also specify the following flags:
3436
- --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.
37+
- -- page - specify the page to fetch, default is 1
3538
`
3639
listCmdUse = "list --company-id company-id"
3740
)
@@ -46,6 +49,7 @@ func ListCmd(options *clioptions.CLIOptions) *cobra.Command {
4649
}
4750

4851
options.AddPublicFlag(cmd.Flags())
52+
options.AddPageFlag(cmd.Flags())
4953

5054
return cmd
5155
}
@@ -65,6 +69,7 @@ func runListCmd(options *clioptions.CLIOptions) func(cmd *cobra.Command, args []
6569
marketplaceItemsOptions := commonMarketplace.GetMarketplaceItemsOptions{
6670
CompanyID: restConfig.CompanyID,
6771
Public: options.MarketplaceFetchPublicItems,
72+
Page: options.Page,
6873
}
6974

7075
err = commonMarketplace.PrintMarketplaceItems(cmd.Context(), apiClient, marketplaceItemsOptions, options.Printer(), listMarketplaceEndpoint)

internal/cmd/catalog/list_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func privateAndPublicMarketplaceHandler(t *testing.T) http.HandlerFunc {
157157
return func(w http.ResponseWriter, r *http.Request) {
158158
if strings.EqualFold(r.URL.Path, "/api/marketplace/") &&
159159
r.Method == http.MethodGet &&
160+
r.URL.Query().Get("page") == "1" &&
160161
r.URL.Query().Get("includeTenantId") == "my-company" {
161162
_, err := w.Write([]byte(marketplaceItemsBodyContent(t)))
162163
require.NoError(t, err)
@@ -172,6 +173,7 @@ func privateCompanyMarketplaceHandler(t *testing.T) http.HandlerFunc {
172173
return func(w http.ResponseWriter, r *http.Request) {
173174
if strings.EqualFold(r.URL.Path, "/api/marketplace/") &&
174175
r.Method == http.MethodGet &&
176+
r.URL.Query().Get("page") == "1" &&
175177
r.URL.Query().Get("tenantId") == "my-company" {
176178
_, err := w.Write([]byte(marketplacePrivateCompanyBodyContent(t)))
177179
require.NoError(t, err)

internal/cmd/common/marketplace/list.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package marketplace
1818
import (
1919
"context"
2020
"fmt"
21+
"strconv"
2122

2223
"github.com/mia-platform/miactl/internal/client"
2324
"github.com/mia-platform/miactl/internal/printer"
@@ -28,6 +29,7 @@ import (
2829
type GetMarketplaceItemsOptions struct {
2930
CompanyID string
3031
Public bool
32+
Page int
3133
}
3234

3335
func PrintMarketplaceItems(context context.Context, client *client.APIClient, options GetMarketplaceItemsOptions, p printer.IPrinter, endpoint string) error {
@@ -89,6 +91,15 @@ func buildRequest(client *client.APIClient, options GetMarketplaceItemsOptions,
8991
request.SetParam("tenantId", options.CompanyID)
9092
}
9193

94+
// marketplace command API does not support pagination
95+
if endpoint == "/api/marketplace/" {
96+
if options.Page <= 0 {
97+
request.SetParam("page", "1")
98+
} else {
99+
request.SetParam("page", strconv.Itoa(options.Page))
100+
}
101+
}
102+
92103
return request
93104
}
94105

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright Mia srl
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package itd
17+
18+
import (
19+
"context"
20+
"errors"
21+
"fmt"
22+
"net/http"
23+
24+
"github.com/mia-platform/miactl/internal/client"
25+
"github.com/mia-platform/miactl/internal/clioptions"
26+
itd "github.com/mia-platform/miactl/internal/resources/item-type-definition"
27+
"github.com/mia-platform/miactl/internal/util"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
var (
32+
ErrServerDeleteItem = errors.New("server error while deleting item type definition")
33+
ErrUnexpectedDeleteItem = errors.New("unexpected response while deleting item")
34+
)
35+
36+
const (
37+
deleteItdEndpoint = "/api/tenants/%s/marketplace/item-type-definitions/%s/"
38+
39+
cmdDeleteLongDescription = `Delete an Item Type Definition. It works with Mia-Platform Console v14.1.0 or later.
40+
41+
You need to specify the companyId and the item type definition name via the respective flags (recommended). The company-id flag can be omitted if it is already set in the context.
42+
`
43+
deleteCmdUse = "delete { --name name --version version }"
44+
)
45+
46+
func DeleteCmd(options *clioptions.CLIOptions) *cobra.Command {
47+
cmd := &cobra.Command{
48+
Use: deleteCmdUse,
49+
Short: "Delete an Item Type Definition",
50+
Long: cmdDeleteLongDescription,
51+
SuggestFor: []string{"rm"},
52+
RunE: func(cmd *cobra.Command, _ []string) error {
53+
restConfig, err := options.ToRESTConfig()
54+
cobra.CheckErr(err)
55+
client, err := client.APIClientForConfig(restConfig)
56+
cobra.CheckErr(err)
57+
58+
canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), client, 14, 1)
59+
if !canUseNewAPI || versionError != nil {
60+
return itd.ErrUnsupportedCompanyVersion
61+
}
62+
63+
companyID := restConfig.CompanyID
64+
if len(companyID) == 0 {
65+
return itd.ErrMissingCompanyID
66+
}
67+
68+
if options.MarketplaceItemVersion != "" && options.MarketplaceItemID != "" {
69+
err = deleteITD(
70+
cmd.Context(),
71+
client,
72+
companyID,
73+
options.ItemTypeDefinitionName,
74+
)
75+
cobra.CheckErr(err)
76+
return nil
77+
}
78+
79+
return errors.New("invalid input parameters")
80+
},
81+
}
82+
83+
ITDFlagName := options.AddItemTypeDefinitionNameFlag(cmd.Flags())
84+
85+
err := cmd.MarkFlagRequired(ITDFlagName)
86+
if err != nil {
87+
// the error is only due to a programming error (missing command flag), hence panic
88+
panic(err)
89+
}
90+
91+
return cmd
92+
}
93+
94+
func deleteITD(ctx context.Context, client *client.APIClient, companyID, name string) error {
95+
resp, err := client.
96+
Delete().
97+
APIPath(fmt.Sprintf(deleteItdEndpoint, companyID, name)).
98+
Do(ctx)
99+
100+
if err != nil {
101+
return fmt.Errorf("error executing request: %w", err)
102+
}
103+
104+
switch resp.StatusCode() {
105+
case http.StatusNoContent:
106+
fmt.Println("item deleted successfully")
107+
return nil
108+
case http.StatusNotFound:
109+
return itd.ErrItemNotFound
110+
default:
111+
if resp.StatusCode() >= http.StatusInternalServerError {
112+
return ErrServerDeleteItem
113+
}
114+
return fmt.Errorf("%w: %d", ErrUnexpectedDeleteItem, resp.StatusCode())
115+
}
116+
}

0 commit comments

Comments
 (0)