|  | 
|  | 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 | +	"encoding/json" | 
|  | 21 | +	"errors" | 
|  | 22 | +	"fmt" | 
|  | 23 | +	"io/fs" | 
|  | 24 | +	"path/filepath" | 
|  | 25 | + | 
|  | 26 | +	"github.com/mia-platform/miactl/internal/client" | 
|  | 27 | +	"github.com/mia-platform/miactl/internal/clioptions" | 
|  | 28 | +	"github.com/mia-platform/miactl/internal/encoding" | 
|  | 29 | +	"github.com/mia-platform/miactl/internal/files" | 
|  | 30 | +	itd "github.com/mia-platform/miactl/internal/resources/item-type-definition" | 
|  | 31 | +	"github.com/mia-platform/miactl/internal/util" | 
|  | 32 | +	"github.com/spf13/cobra" | 
|  | 33 | +) | 
|  | 34 | + | 
|  | 35 | +var ( | 
|  | 36 | +	ErrInvalidFilePath        = errors.New("invalid file path") | 
|  | 37 | +	ErrPathIsFolder           = errors.New("path must be a file path, not a folder") | 
|  | 38 | +	ErrFileFormatNotSupported = errors.New("file format not supported, supported formats are: .json, .yaml, .yml") | 
|  | 39 | +	ErrResWithoutName         = errors.New("name field is required") | 
|  | 40 | +	ErrResNameNotAString      = errors.New("name field must be string") | 
|  | 41 | +	ErrPuttingResources       = errors.New("cannot save the item type definition") | 
|  | 42 | +) | 
|  | 43 | + | 
|  | 44 | +const ( | 
|  | 45 | +	putItdEndpoint = "/api/tenants/%s/marketplace/item-type-definitions/" | 
|  | 46 | + | 
|  | 47 | +	cmdPutLongDescription = ` Create or update an Item Type Definition. It works with Mia-Platform Console v14.1.0 or later. | 
|  | 48 | +
 | 
|  | 49 | +  You need to specify the flag --file or -f that accepts a file and companyId. | 
|  | 50 | +
 | 
|  | 51 | +  Supported formats are JSON (.json files) and YAML (.yaml or .yml files). The company-id flag can be omitted if it is already set in the context.` | 
|  | 52 | + | 
|  | 53 | +	putExample = ` | 
|  | 54 | +  # Create the item type definition in file myFantasticGoTemplate.json located in the current directory | 
|  | 55 | +  miactl catalog apply --file myFantasticGoTemplate.json | 
|  | 56 | +
 | 
|  | 57 | +  # Create the item type definition in file myFantasticGoTemplate.json, with relative path | 
|  | 58 | +  miactl catalog apply --file ./path/to/myFantasticGoTemplate.json` | 
|  | 59 | + | 
|  | 60 | +	putCmdUse = "delete { --file file-path }" | 
|  | 61 | +) | 
|  | 62 | + | 
|  | 63 | +func PutCmd(options *clioptions.CLIOptions) *cobra.Command { | 
|  | 64 | +	cmd := &cobra.Command{ | 
|  | 65 | +		Use:     putCmdUse, | 
|  | 66 | +		Short:   "Create or update an Item Type Definition", | 
|  | 67 | +		Long:    cmdPutLongDescription, | 
|  | 68 | +		Example: putExample, | 
|  | 69 | +		RunE: func(cmd *cobra.Command, _ []string) error { | 
|  | 70 | +			restConfig, err := options.ToRESTConfig() | 
|  | 71 | +			cobra.CheckErr(err) | 
|  | 72 | +			client, err := client.APIClientForConfig(restConfig) | 
|  | 73 | +			cobra.CheckErr(err) | 
|  | 74 | + | 
|  | 75 | +			canUseNewAPI, versionError := util.VersionCheck(cmd.Context(), client, 14, 1) | 
|  | 76 | +			if !canUseNewAPI || versionError != nil { | 
|  | 77 | +				return itd.ErrUnsupportedCompanyVersion | 
|  | 78 | +			} | 
|  | 79 | + | 
|  | 80 | +			companyID := restConfig.CompanyID | 
|  | 81 | +			if len(companyID) == 0 { | 
|  | 82 | +				return itd.ErrMissingCompanyID | 
|  | 83 | +			} | 
|  | 84 | + | 
|  | 85 | +			outcome, err := putItemFromPath( | 
|  | 86 | +				cmd.Context(), | 
|  | 87 | +				client, | 
|  | 88 | +				companyID, | 
|  | 89 | +				options.ItemTypeDefinitionFilePath, | 
|  | 90 | +				options.OutputFormat, | 
|  | 91 | +			) | 
|  | 92 | +			cobra.CheckErr(err) | 
|  | 93 | + | 
|  | 94 | +			fmt.Println(outcome) | 
|  | 95 | + | 
|  | 96 | +			return nil | 
|  | 97 | +		}, | 
|  | 98 | +	} | 
|  | 99 | + | 
|  | 100 | +	options.AddItemTypeDefinitionFileFlag(cmd) | 
|  | 101 | + | 
|  | 102 | +	return cmd | 
|  | 103 | +} | 
|  | 104 | + | 
|  | 105 | +func putItemFromPath(ctx context.Context, client *client.APIClient, companyID string, filePath string, outputFormat string) (string, error) { | 
|  | 106 | +	_, err := checkFilePath(filePath) | 
|  | 107 | +	if err != nil { | 
|  | 108 | +		return "", fmt.Errorf("%w: %s", err, err) | 
|  | 109 | +	} | 
|  | 110 | + | 
|  | 111 | +	outcome, err := putItemTypeDefinition(ctx, client, companyID, filePath) | 
|  | 112 | +	if err != nil { | 
|  | 113 | +		return "", fmt.Errorf("%w: %s", ErrPuttingResources, err) | 
|  | 114 | +	} | 
|  | 115 | + | 
|  | 116 | +	data, err := outcome.Marshal(outputFormat) | 
|  | 117 | +	if err != nil { | 
|  | 118 | +		return "", err | 
|  | 119 | +	} | 
|  | 120 | + | 
|  | 121 | +	return string(data), nil | 
|  | 122 | +} | 
|  | 123 | + | 
|  | 124 | +func checkFilePath(rootPath string) (string, error) { | 
|  | 125 | +	err := filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error { | 
|  | 126 | +		if err != nil { | 
|  | 127 | +			return err | 
|  | 128 | +		} | 
|  | 129 | +		if info.IsDir() { | 
|  | 130 | +			return ErrPathIsFolder | 
|  | 131 | +		} | 
|  | 132 | +		extension := filepath.Ext(path) | 
|  | 133 | +		if extension == encoding.YmlExtension || extension == encoding.YamlExtension || | 
|  | 134 | +			extension == encoding.JSONExtension { | 
|  | 135 | +			return nil | 
|  | 136 | +		} | 
|  | 137 | +		return ErrFileFormatNotSupported | 
|  | 138 | +	}) | 
|  | 139 | +	if err != nil { | 
|  | 140 | +		return "", err | 
|  | 141 | +	} | 
|  | 142 | +	return rootPath, nil | 
|  | 143 | +} | 
|  | 144 | + | 
|  | 145 | +func putItemTypeDefinition( | 
|  | 146 | +	ctx context.Context, | 
|  | 147 | +	client *client.APIClient, | 
|  | 148 | +	companyID string, | 
|  | 149 | +	filePath string, | 
|  | 150 | +) (*itd.GenericItemTypeDefinition, error) { | 
|  | 151 | +	if companyID == "" { | 
|  | 152 | +		return nil, itd.ErrMissingCompanyID | 
|  | 153 | +	} | 
|  | 154 | + | 
|  | 155 | +	itemTypeDefinition := &itd.GenericItemTypeDefinition{} | 
|  | 156 | +	if err := files.ReadFile(filePath, itemTypeDefinition); err != nil { | 
|  | 157 | +		return nil, err | 
|  | 158 | +	} | 
|  | 159 | + | 
|  | 160 | +	bodyBytes, err := json.Marshal(itemTypeDefinition) | 
|  | 161 | +	if err != nil { | 
|  | 162 | +		return nil, err | 
|  | 163 | +	} | 
|  | 164 | + | 
|  | 165 | +	resp, err := client.Put(). | 
|  | 166 | +		APIPath(fmt.Sprintf(putItdEndpoint, companyID)). | 
|  | 167 | +		Body(bodyBytes). | 
|  | 168 | +		Do(ctx) | 
|  | 169 | +	if err != nil { | 
|  | 170 | +		return nil, err | 
|  | 171 | +	} | 
|  | 172 | +	if err := resp.Error(); err != nil { | 
|  | 173 | +		return nil, err | 
|  | 174 | +	} | 
|  | 175 | + | 
|  | 176 | +	putResponse := &itd.GenericItemTypeDefinition{} | 
|  | 177 | +	err = resp.ParseResponse(putResponse) | 
|  | 178 | +	if err != nil { | 
|  | 179 | +		return nil, err | 
|  | 180 | +	} | 
|  | 181 | + | 
|  | 182 | +	return putResponse, nil | 
|  | 183 | +} | 
0 commit comments