|  | 
|  | 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 | +	"fmt" | 
|  | 21 | +	"strconv" | 
|  | 22 | + | 
|  | 23 | +	"github.com/mia-platform/miactl/internal/client" | 
|  | 24 | +	"github.com/mia-platform/miactl/internal/clioptions" | 
|  | 25 | +	"github.com/mia-platform/miactl/internal/printer" | 
|  | 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 | +type GetItdsOptions struct { | 
|  | 32 | +	CompanyID string | 
|  | 33 | +	Public    bool | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +const ( | 
|  | 37 | +	listItdEndpoint = "/api/marketplace/item-type-definitions/" | 
|  | 38 | +	listCmdLong     = `List Item Type Definitions | 
|  | 39 | +
 | 
|  | 40 | +    This command lists the Item Type Definitions of a company. It works with Mia-Platform Console v14.1.0 or later. | 
|  | 41 | +
 | 
|  | 42 | +    you can also specify the following flags: | 
|  | 43 | +    - --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. | 
|  | 44 | +    ` | 
|  | 45 | +	listCmdUse = "list --company-id company-id" | 
|  | 46 | +) | 
|  | 47 | + | 
|  | 48 | +func ListCmd(options *clioptions.CLIOptions) *cobra.Command { | 
|  | 49 | +	cmd := &cobra.Command{ | 
|  | 50 | +		Use:   listCmdUse, | 
|  | 51 | +		Short: "List item type definitions", | 
|  | 52 | +		Long:  listCmdLong, | 
|  | 53 | +		RunE:  runListCmd(options), | 
|  | 54 | +	} | 
|  | 55 | + | 
|  | 56 | +	options.AddPublicFlag(cmd.Flags()) | 
|  | 57 | + | 
|  | 58 | +	return cmd | 
|  | 59 | +} | 
|  | 60 | + | 
|  | 61 | +func runListCmd(options *clioptions.CLIOptions) func(cmd *cobra.Command, args []string) error { | 
|  | 62 | +	return func(cmd *cobra.Command, _ []string) error { | 
|  | 63 | +		restConfig, err := options.ToRESTConfig() | 
|  | 64 | +		cobra.CheckErr(err) | 
|  | 65 | +		apiClient, err := client.APIClientForConfig(restConfig) | 
|  | 66 | +		cobra.CheckErr(err) | 
|  | 67 | + | 
|  | 68 | +		canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), apiClient, 14, 1) | 
|  | 69 | +		if !canUseNewAPI || versionError != nil { | 
|  | 70 | +			return itd.ErrUnsupportedCompanyVersion | 
|  | 71 | +		} | 
|  | 72 | + | 
|  | 73 | +		marketplaceItemsOptions := GetItdsOptions{ | 
|  | 74 | +			CompanyID: restConfig.CompanyID, | 
|  | 75 | +			Public:    options.MarketplaceFetchPublicItems, | 
|  | 76 | +		} | 
|  | 77 | + | 
|  | 78 | +		err = PrintItds(cmd.Context(), apiClient, marketplaceItemsOptions, options.Printer(), listItdEndpoint) | 
|  | 79 | +		cobra.CheckErr(err) | 
|  | 80 | + | 
|  | 81 | +		return nil | 
|  | 82 | +	} | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | +func PrintItds(context context.Context, client *client.APIClient, options GetItdsOptions, p printer.IPrinter, endpoint string) error { | 
|  | 86 | +	itds, err := fetchItds(context, client, options, endpoint) | 
|  | 87 | +	if err != nil { | 
|  | 88 | +		return err | 
|  | 89 | +	} | 
|  | 90 | + | 
|  | 91 | +	p.Keys("Name", "Display Name", "Visibility", "Publisher", "Versioning Supported") | 
|  | 92 | +	for _, itd := range itds { | 
|  | 93 | +		publisher := itd.Metadata.Publisher.Name | 
|  | 94 | +		if publisher == "" { | 
|  | 95 | +			publisher = "-" | 
|  | 96 | +		} | 
|  | 97 | + | 
|  | 98 | +		p.Record( | 
|  | 99 | +			itd.Metadata.Name, | 
|  | 100 | +			itd.Metadata.DisplayName, | 
|  | 101 | +			itd.Metadata.Visibility.Scope, | 
|  | 102 | +			publisher, | 
|  | 103 | +			strconv.FormatBool(itd.Spec.IsVersioningSupported), | 
|  | 104 | +		) | 
|  | 105 | +	} | 
|  | 106 | +	p.Print() | 
|  | 107 | +	return nil | 
|  | 108 | +} | 
|  | 109 | + | 
|  | 110 | +func fetchItds(ctx context.Context, client *client.APIClient, options GetItdsOptions, endpoint string) ([]*itd.ItemTypeDefinition, error) { | 
|  | 111 | +	err := validateOptions(options) | 
|  | 112 | +	if err != nil { | 
|  | 113 | +		return nil, err | 
|  | 114 | +	} | 
|  | 115 | + | 
|  | 116 | +	request := buildRequest(client, options, endpoint) | 
|  | 117 | +	resp, err := executeRequest(ctx, request) | 
|  | 118 | +	if err != nil { | 
|  | 119 | +		return nil, err | 
|  | 120 | +	} | 
|  | 121 | + | 
|  | 122 | +	marketplaceItems, err := parseResponse(resp) | 
|  | 123 | +	if err != nil { | 
|  | 124 | +		return nil, err | 
|  | 125 | +	} | 
|  | 126 | + | 
|  | 127 | +	return marketplaceItems, nil | 
|  | 128 | +} | 
|  | 129 | + | 
|  | 130 | +func validateOptions(options GetItdsOptions) error { | 
|  | 131 | +	requestedSpecificCompany := len(options.CompanyID) > 0 | 
|  | 132 | + | 
|  | 133 | +	if !requestedSpecificCompany { | 
|  | 134 | +		return itd.ErrMissingCompanyID | 
|  | 135 | +	} | 
|  | 136 | + | 
|  | 137 | +	return nil | 
|  | 138 | +} | 
|  | 139 | + | 
|  | 140 | +func buildRequest(client *client.APIClient, options GetItdsOptions, endpoint string) *client.Request { | 
|  | 141 | +	request := client.Get().APIPath(endpoint) | 
|  | 142 | +	switch { | 
|  | 143 | +	case options.Public: | 
|  | 144 | +		request.SetParam("visibility", "console"+","+options.CompanyID) | 
|  | 145 | +	case !options.Public: | 
|  | 146 | +		request.SetParam("visibility", options.CompanyID) | 
|  | 147 | +	} | 
|  | 148 | + | 
|  | 149 | +	return request | 
|  | 150 | +} | 
|  | 151 | + | 
|  | 152 | +func executeRequest(ctx context.Context, request *client.Request) (*client.Response, error) { | 
|  | 153 | +	resp, err := request.Do(ctx) | 
|  | 154 | +	if err != nil { | 
|  | 155 | +		return nil, fmt.Errorf("error executing request: %w", err) | 
|  | 156 | +	} | 
|  | 157 | +	if err := resp.Error(); err != nil { | 
|  | 158 | +		return nil, err | 
|  | 159 | +	} | 
|  | 160 | + | 
|  | 161 | +	return resp, nil | 
|  | 162 | +} | 
|  | 163 | + | 
|  | 164 | +func parseResponse(resp *client.Response) ([]*itd.ItemTypeDefinition, error) { | 
|  | 165 | +	marketplaceItems := make([]*itd.ItemTypeDefinition, 0) | 
|  | 166 | +	if err := resp.ParseResponse(&marketplaceItems); err != nil { | 
|  | 167 | +		return nil, fmt.Errorf("error parsing response body: %w", err) | 
|  | 168 | +	} | 
|  | 169 | + | 
|  | 170 | +	return marketplaceItems, nil | 
|  | 171 | +} | 
0 commit comments