Skip to content

Commit dcb0c6e

Browse files
author
Paola Nicosia
committed
Group common code in marketplace and catalog commands
1 parent 58d80e6 commit dcb0c6e

30 files changed

+655
-1313
lines changed

internal/cmd/catalog/apply/apply.go

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import (
2525

2626
"github.com/mia-platform/miactl/internal/client"
2727
"github.com/mia-platform/miactl/internal/clioptions"
28+
commonMarketplace "github.com/mia-platform/miactl/internal/cmd/common/marketplace"
2829
"github.com/mia-platform/miactl/internal/encoding"
2930
"github.com/mia-platform/miactl/internal/files"
3031
"github.com/mia-platform/miactl/internal/resources/catalog"
32+
"github.com/mia-platform/miactl/internal/resources/marketplace"
3133
"github.com/mia-platform/miactl/internal/util"
3234
"github.com/spf13/cobra"
3335
)
@@ -65,36 +67,6 @@ miactl catalog apply -f ./path/to/myFantasticGoTemplate.json -f ./path/to/myFant
6567
miactl catalog apply -f myFantasticGoTemplates`
6668

6769
applyEndpointTemplate = "/api/tenants/%s/marketplace/items"
68-
69-
imageAssetType = "imageAssetType"
70-
imageKey = "image"
71-
imageURLKey = "imageUrl"
72-
73-
itemIDKey = "itemId"
74-
75-
supportedByImageAssetType = "supportedByImageAssetType"
76-
supportedByImageKey = "supportedByImage"
77-
supportedByImageURLKey = "supportedByImageUrl"
78-
)
79-
80-
var (
81-
errCompanyIDNotDefined = errors.New("companyID must be defined")
82-
83-
errResWithoutName = errors.New(`the required field "name" was not found in the resource`)
84-
errResWithoutItemID = errors.New(`the required field "itemId" was not found in the resource`)
85-
errNoValidFilesProvided = errors.New("no valid files were provided")
86-
87-
errResNameNotAString = errors.New(`the field "name" must be a string`)
88-
errResItemIDNotAString = errors.New(`the field "itemId" must be a string`)
89-
90-
errDuplicatedResIdentifier = errors.New("some resources have duplicated itemId-version tuple")
91-
errUnknownAssetType = errors.New("unknown asset type")
92-
93-
errUploadingImage = errors.New("error while uploading image")
94-
errBuildingFilesList = errors.New("error processing files")
95-
errBuildingApplyReq = errors.New("error preparing apply request")
96-
errProcessingImages = errors.New("error processing images")
97-
errApplyingResources = errors.New("error applying items")
9870
)
9971

10072
// ApplyCmd returns a new cobra command for adding or updating catalog resources
@@ -117,7 +89,7 @@ func ApplyCmd(options *clioptions.CLIOptions) *cobra.Command {
11789

11890
companyID := restConfig.CompanyID
11991
if len(companyID) == 0 {
120-
return catalog.ErrMissingCompanyID
92+
return marketplace.ErrMissingCompanyID
12193
}
12294

12395
outcome, err := applyItemsFromPaths(
@@ -142,23 +114,23 @@ func ApplyCmd(options *clioptions.CLIOptions) *cobra.Command {
142114
func applyItemsFromPaths(ctx context.Context, client *client.APIClient, companyID string, filePaths []string) (string, error) {
143115
resourceFilesPaths, err := buildFilePathsList(filePaths)
144116
if err != nil {
145-
return "", fmt.Errorf("%w: %s", errBuildingFilesList, err)
117+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrBuildingFilesList, err)
146118
}
147119

148120
applyReq, identifierToFilePathMap, err := buildApplyRequest(resourceFilesPaths)
149121
if err != nil {
150-
return "", fmt.Errorf("%w: %s", errBuildingApplyReq, err)
122+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrBuildingApplyReq, err)
151123
}
152124

153125
for _, item := range applyReq.Resources {
154126
if err := processItemImages(ctx, client, companyID, item, identifierToFilePathMap); err != nil {
155-
return "", fmt.Errorf("%w: %s", errProcessingImages, err)
127+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrProcessingImages, err)
156128
}
157129
}
158130

159131
outcome, err := applyMarketplaceResource(ctx, client, companyID, applyReq)
160132
if err != nil {
161-
return "", fmt.Errorf("%w: %s", errApplyingResources, err)
133+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrApplyingResources, err)
162134
}
163135

164136
return buildOutcomeSummaryAsTables(outcome), nil
@@ -178,21 +150,21 @@ func processItemImages(
178150
ctx context.Context,
179151
client *client.APIClient,
180152
companyID string,
181-
item *catalog.Item,
153+
item *marketplace.Item,
182154
itemIDToFilePathMap map[string]string,
183155
) error {
184156
processImage := func(imageObjKey, urlKey string, assetType string) error {
185-
localPath, err := getAndValidateImageLocalPath(item, imageObjKey, urlKey)
186-
if assetType != imageAssetType && assetType != supportedByImageAssetType {
187-
return fmt.Errorf("%w: %s", errUnknownAssetType, assetType)
157+
localPath, err := commonMarketplace.GetAndValidateImageLocalPath(item, imageObjKey, urlKey)
158+
if assetType != commonMarketplace.ImageAssetType && assetType != commonMarketplace.SupportedByImageAssetType {
159+
return fmt.Errorf("%w: %s", commonMarketplace.ErrUnknownAssetType, assetType)
188160
}
189161
if err != nil {
190162
return err
191163
}
192164
if localPath == "" {
193165
return nil
194166
}
195-
itemID := item.Get(itemIDKey).(string)
167+
itemID := item.Get(commonMarketplace.ItemIDKey).(string)
196168
identifier, err := buildItemIdentifier(item)
197169
if err != nil {
198170
return err
@@ -204,7 +176,7 @@ func processItemImages(
204176
if err != nil {
205177
return err
206178
}
207-
imageURL, err := uploadImageFileAndGetURL(
179+
imageURL, err := commonMarketplace.UploadImageFileAndGetURL(
208180
ctx,
209181
client,
210182
companyID,
@@ -214,18 +186,18 @@ func processItemImages(
214186
versionName,
215187
)
216188
if err != nil {
217-
return fmt.Errorf("%w: %s: %s", errUploadingImage, imageFilePath, err)
189+
return fmt.Errorf("%w: %s: %s", commonMarketplace.ErrUploadingImage, imageFilePath, err)
218190
}
219191

220192
item.Del(imageObjKey)
221193
item.Set(urlKey, imageURL)
222194
return nil
223195
}
224196

225-
if err := processImage(imageKey, imageURLKey, imageAssetType); err != nil {
197+
if err := processImage(commonMarketplace.ImageKey, commonMarketplace.ImageURLKey, commonMarketplace.ImageAssetType); err != nil {
226198
return err
227199
}
228-
err := processImage(supportedByImageKey, supportedByImageURLKey, supportedByImageAssetType)
200+
err := processImage(commonMarketplace.SupportedByImageKey, commonMarketplace.SupportedByImageURLKey, commonMarketplace.SupportedByImageAssetType)
229201
return err
230202
}
231203

@@ -253,11 +225,11 @@ func buildFilePathsList(paths []string) ([]string, error) {
253225
}
254226

255227
func buildApplyRequest(pathList []string) (*catalog.ApplyRequest, map[string]string, error) {
256-
resources := []*catalog.Item{}
228+
resources := []*marketplace.Item{}
257229
// the identifier is the concatenation of itemID and, if present, version.name
258230
resIdentifierToFilePath := map[string]string{}
259231
for _, filePath := range pathList {
260-
marketplaceItem := &catalog.Item{}
232+
marketplaceItem := &marketplace.Item{}
261233
if err := files.ReadFile(filePath, marketplaceItem); err != nil {
262234
if errors.Is(err, files.ErrUnsupportedFile) {
263235
continue
@@ -279,25 +251,25 @@ func buildApplyRequest(pathList []string) (*catalog.ApplyRequest, map[string]str
279251
}
280252

281253
if _, alreadyExists := resIdentifierToFilePath[resIdentifier]; alreadyExists {
282-
return nil, nil, fmt.Errorf("%w: %s", errDuplicatedResIdentifier, itemID)
254+
return nil, nil, fmt.Errorf("%w: %s", commonMarketplace.ErrDuplicatedResIdentifier, itemID)
283255
}
284256

285257
resources = append(resources, marketplaceItem)
286258

287259
resIdentifierToFilePath[resIdentifier] = filePath
288260
}
289261
if len(resources) == 0 {
290-
return nil, nil, errNoValidFilesProvided
262+
return nil, nil, commonMarketplace.ErrNoValidFilesProvided
291263
}
292264
return &catalog.ApplyRequest{
293265
Resources: resources,
294266
}, resIdentifierToFilePath, nil
295267
}
296268

297-
func buildItemIdentifier(item *catalog.Item) (string, error) {
298-
itemID, ok := item.Get(itemIDKey).(string)
269+
func buildItemIdentifier(item *marketplace.Item) (string, error) {
270+
itemID, ok := item.Get(commonMarketplace.ItemIDKey).(string)
299271
if !ok {
300-
return "", errResItemIDNotAString
272+
return "", commonMarketplace.ErrResItemIDNotAString
301273
}
302274

303275
versionName, err := item.GetVersionName()
@@ -308,26 +280,26 @@ func buildItemIdentifier(item *catalog.Item) (string, error) {
308280
return itemID + versionName, nil
309281
}
310282

311-
func validateItemName(marketplaceItem *catalog.Item, filePath string) (string, error) {
283+
func validateItemName(marketplaceItem *marketplace.Item, filePath string) (string, error) {
312284
itemName, ok := (*marketplaceItem)["name"]
313285
if !ok {
314-
return "", fmt.Errorf("%w: %s", errResWithoutName, filePath)
286+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrResWithoutName, filePath)
315287
}
316288
itemNameStr, ok := itemName.(string)
317289
if !ok {
318-
return "", fmt.Errorf("%w: %s", errResNameNotAString, filePath)
290+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrResNameNotAString, filePath)
319291
}
320292
return itemNameStr, nil
321293
}
322294

323-
func validateItemHumanReadableID(marketplaceItem *catalog.Item, filePath string) (string, error) {
324-
itemID, ok := (*marketplaceItem)[itemIDKey]
295+
func validateItemHumanReadableID(marketplaceItem *marketplace.Item, filePath string) (string, error) {
296+
itemID, ok := (*marketplaceItem)[commonMarketplace.ItemIDKey]
325297
if !ok {
326-
return "", fmt.Errorf("%w: %s", errResWithoutItemID, filePath)
298+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrResWithoutItemID, filePath)
327299
}
328300
itemIDStr, ok := itemID.(string)
329301
if !ok {
330-
return "", fmt.Errorf("%w: %s", errResItemIDNotAString, filePath)
302+
return "", fmt.Errorf("%w: %s", commonMarketplace.ErrResItemIDNotAString, filePath)
331303
}
332304
return itemIDStr, nil
333305
}
@@ -339,7 +311,7 @@ func applyMarketplaceResource(
339311
request *catalog.ApplyRequest,
340312
) (*catalog.ApplyResponse, error) {
341313
if companyID == "" {
342-
return nil, errCompanyIDNotDefined
314+
return nil, commonMarketplace.ErrCompanyIDNotDefined
343315
}
344316

345317
bodyBytes, err := json.Marshal(request)

0 commit comments

Comments
 (0)