|  | 
|  | 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