Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions internal/clioptions/clioptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type CLIOptions struct {
UserEmails []string
UserIDs []string

Page int

ServiceAccountID string

BasicClientID string
Expand All @@ -79,6 +81,9 @@ type CLIOptions struct {
MarketplaceItemObjectID string
MarketplaceFetchPublicItems bool

ItemTypeDefinitionName string
ItemTypeDefinitionFilePath string

FromCronJob string

FollowLogs bool
Expand Down Expand Up @@ -250,6 +255,12 @@ func (o *CLIOptions) AddPublicFlag(flags *pflag.FlagSet) (flagName string) {
return
}

func (o *CLIOptions) AddPageFlag(flags *pflag.FlagSet) (flagName string) {
flagName = "page"
flags.IntVar(&o.Page, flagName, 1, "specify the page to fetch")
return
}

func (o *CLIOptions) AddMarketplaceItemObjectIDFlag(flags *pflag.FlagSet) (flagName string) {
flagName = "object-id"
flags.StringVar(&o.MarketplaceItemObjectID, flagName, "", "The _id of the Marketplace item")
Expand All @@ -262,6 +273,21 @@ func (o *CLIOptions) AddMarketplaceVersionFlag(flags *pflag.FlagSet) (flagName s
return
}

func (o *CLIOptions) AddItemTypeDefinitionNameFlag(flags *pflag.FlagSet) (flagName string) {
flagName = "name"
flags.StringVarP(&o.ItemTypeDefinitionName, flagName, "i", "", "The name of the Item Type Definition")
return
}

func (o *CLIOptions) AddItemTypeDefinitionFileFlag(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.ItemTypeDefinitionFilePath, "file", "f", "", "paths to JSON/YAML file containing an item type definition")
err := cmd.MarkFlagRequired("file")
if err != nil {
// the error is only due to a programming error (missing command), hence panic
panic(err)
}
}

func (o *CLIOptions) AddCreateJobFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.FromCronJob, "from", "", "name of the cronjob to create a Job from")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/catalog/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (

This command works with Mia-Platform Console v14.0.0 or later.

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.
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.
`
cmdGetUse = "get { --item-id item-id --version version }"
)
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/catalog/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ const (

This command lists the Catalog items of a company. It works with Mia-Platform Console v14.0.0 or later.

Results are paginated. By default, only the first page is shown.

you can also specify the following flags:
- --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.
- -- page - specify the page to fetch, default is 1
`
listCmdUse = "list --company-id company-id"
)
Expand All @@ -46,6 +49,7 @@ func ListCmd(options *clioptions.CLIOptions) *cobra.Command {
}

options.AddPublicFlag(cmd.Flags())
options.AddPageFlag(cmd.Flags())

return cmd
}
Expand All @@ -65,6 +69,7 @@ func runListCmd(options *clioptions.CLIOptions) func(cmd *cobra.Command, args []
marketplaceItemsOptions := commonMarketplace.GetMarketplaceItemsOptions{
CompanyID: restConfig.CompanyID,
Public: options.MarketplaceFetchPublicItems,
Page: options.Page,
}

err = commonMarketplace.PrintMarketplaceItems(cmd.Context(), apiClient, marketplaceItemsOptions, options.Printer(), listMarketplaceEndpoint)
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/catalog/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func privateAndPublicMarketplaceHandler(t *testing.T) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.EqualFold(r.URL.Path, "/api/marketplace/") &&
r.Method == http.MethodGet &&
r.URL.Query().Get("page") == "1" &&
r.URL.Query().Get("includeTenantId") == "my-company" {
_, err := w.Write([]byte(marketplaceItemsBodyContent(t)))
require.NoError(t, err)
Expand All @@ -172,6 +173,7 @@ func privateCompanyMarketplaceHandler(t *testing.T) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.EqualFold(r.URL.Path, "/api/marketplace/") &&
r.Method == http.MethodGet &&
r.URL.Query().Get("page") == "1" &&
r.URL.Query().Get("tenantId") == "my-company" {
_, err := w.Write([]byte(marketplacePrivateCompanyBodyContent(t)))
require.NoError(t, err)
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/common/marketplace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package marketplace
import (
"context"
"fmt"
"strconv"

"github.com/mia-platform/miactl/internal/client"
"github.com/mia-platform/miactl/internal/printer"
Expand All @@ -28,6 +29,7 @@ import (
type GetMarketplaceItemsOptions struct {
CompanyID string
Public bool
Page int
}

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

// marketplace command API does not support pagination
if endpoint == "/api/marketplace/" {
if options.Page <= 0 {
request.SetParam("page", "1")
} else {
request.SetParam("page", strconv.Itoa(options.Page))
}
}

return request
}

Expand Down
116 changes: 116 additions & 0 deletions internal/cmd/item-type-definition/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Mia srl
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package itd

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/mia-platform/miactl/internal/client"
"github.com/mia-platform/miactl/internal/clioptions"
itd "github.com/mia-platform/miactl/internal/resources/item-type-definition"
"github.com/mia-platform/miactl/internal/util"
"github.com/spf13/cobra"
)

var (
ErrServerDeleteItem = errors.New("server error while deleting item type definition")
ErrUnexpectedDeleteItem = errors.New("unexpected response while deleting item")
)

const (
deleteItdEndpoint = "/api/tenants/%s/marketplace/item-type-definitions/%s/"

cmdDeleteLongDescription = `Delete an Item Type Definition. It works with Mia-Platform Console v14.1.0 or later.

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.
`
deleteCmdUse = "delete { --name name --version version }"
)

func DeleteCmd(options *clioptions.CLIOptions) *cobra.Command {
cmd := &cobra.Command{
Use: deleteCmdUse,
Short: "Delete an Item Type Definition",
Long: cmdDeleteLongDescription,
SuggestFor: []string{"rm"},
RunE: func(cmd *cobra.Command, _ []string) error {
restConfig, err := options.ToRESTConfig()
cobra.CheckErr(err)
client, err := client.APIClientForConfig(restConfig)
cobra.CheckErr(err)

canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), client, 14, 1)
if !canUseNewAPI || versionError != nil {
return itd.ErrUnsupportedCompanyVersion
}

companyID := restConfig.CompanyID
if len(companyID) == 0 {
return itd.ErrMissingCompanyID
}

if options.MarketplaceItemVersion != "" && options.MarketplaceItemID != "" {
err = deleteITD(
cmd.Context(),
client,
companyID,
options.ItemTypeDefinitionName,
)
cobra.CheckErr(err)
return nil
}

return errors.New("invalid input parameters")
},
}

ITDFlagName := options.AddItemTypeDefinitionNameFlag(cmd.Flags())

err := cmd.MarkFlagRequired(ITDFlagName)
if err != nil {
// the error is only due to a programming error (missing command flag), hence panic
panic(err)
}

return cmd
}

func deleteITD(ctx context.Context, client *client.APIClient, companyID, name string) error {
resp, err := client.
Delete().
APIPath(fmt.Sprintf(deleteItdEndpoint, companyID, name)).
Do(ctx)

if err != nil {
return fmt.Errorf("error executing request: %w", err)
}

switch resp.StatusCode() {
case http.StatusNoContent:
fmt.Println("item deleted successfully")
return nil
case http.StatusNotFound:
return itd.ErrItemNotFound
default:
if resp.StatusCode() >= http.StatusInternalServerError {
return ErrServerDeleteItem
}
return fmt.Errorf("%w: %d", ErrUnexpectedDeleteItem, resp.StatusCode())
}
}
Loading