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: 19 additions & 7 deletions cmd/delete-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (

func NewDeleteAllCommand(parentCmd *cobra.Command) func() {

var pageLength uint16 = 25

resetFunc := func() {
pageLength = 25
}

var deleteAll = &cobra.Command{
Use: "delete-all",
Short: "Deletes all of a resource",
Expand All @@ -37,6 +43,8 @@ func NewDeleteAllCommand(parentCmd *cobra.Command) func() {
},
}

deleteAll.PersistentFlags().Uint16VarP(&pageLength, "page-length", "", pageLength, "page length to use when deleting")

e := config.GetEnv()
hiddenResources := map[string]struct{}{}

Expand Down Expand Up @@ -71,16 +79,16 @@ func NewDeleteAllCommand(parentCmd *cobra.Command) func() {
Short: GetDeleteAllShort(resource),
Hidden: false,
RunE: func(cmd *cobra.Command, args []string) error {
return deleteAllInternal(clictx.Ctx, append([]string{resourceName}, args...))
return deleteAllInternal(clictx.Ctx, pageLength, append([]string{resourceName}, args...))
},
}
deleteAll.AddCommand(deleteAllResourceCmd)
}
parentCmd.AddCommand(deleteAll)
return func() {}
return resetFunc

}
func deleteAllInternal(ctx context.Context, args []string) error {
func deleteAllInternal(ctx context.Context, pageLength uint16, args []string) error {
// Find Resource
resource, ok := resources.GetResourceByName(args[0])
if !ok {
Expand All @@ -95,7 +103,7 @@ func deleteAllInternal(ctx context.Context, args []string) error {
return fmt.Errorf("resource %s doesn't support DELETE", args[0])
}

allParentEntityIds, err := getParentIds(ctx, resource)
allParentEntityIds, err := getParentIds(ctx, pageLength, resource)

if err != nil {
return fmt.Errorf("could not retrieve parent ids for for resource %s, error: %w", resource.PluralName, err)
Expand All @@ -117,7 +125,11 @@ func deleteAllInternal(ctx context.Context, args []string) error {
}

params := url.Values{}
params.Add("page[limit]", "25")
params.Add("page[limit]", fmt.Sprintf("%d", pageLength))

for k, v := range resource.GetCollectionInfo.DefaultQueryParams {
params.Add(k, v)
}

resp, err := httpclient.DoRequest(ctx, "GET", resourceURL, params.Encode(), nil)

Expand Down Expand Up @@ -175,7 +187,7 @@ func deleteAllInternal(ctx context.Context, args []string) error {
return aliases.ClearAllAliasesForJsonApiType(resource.JsonApiType)
}

func getParentIds(ctx context.Context, resource resources.Resource) ([][]id.IdableAttributes, error) {
func getParentIds(ctx context.Context, pageLength uint16, resource resources.Resource) ([][]id.IdableAttributes, error) {

myEntityIds := make([][]id.IdableAttributes, 0)
if resource.GetCollectionInfo == nil {
Expand All @@ -200,7 +212,7 @@ func getParentIds(ctx context.Context, resource resources.Resource) ([][]id.Idab
return myEntityIds, fmt.Errorf("could not find parent resource %s", immediateParentType)
}

return apihelper.GetAllIds(ctx, &parentResource)
return apihelper.GetAllIds(ctx, pageLength, &parentResource)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/reset-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func deleteAllResourceData(resourceNames []string) (error, []string) {

if myDepth == depth {
log.Infof("Processing resource %s", resourceName)
err := deleteAllInternal(clictx.Ctx, []string{resourceName})
err := deleteAllInternal(clictx.Ctx, 25, []string{resourceName})

if err != nil {
errors = append(errors, fmt.Errorf("error while deleting %s: %w", resourceName, err).Error())
Expand Down
13 changes: 13 additions & 0 deletions cmd/runbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,15 @@ func processRunbookVariablesOnCommand(runbookActionRunActionCommand *cobra.Comma
description = variable.Description.Short
}

// Add ENUM options to description
if strings.HasPrefix(variable.Type, "ENUM:") {
enumValues := strings.Split(variable.Type[5:], ",")
if description != "" {
description += ". "
}
description += "Options: [" + strings.Join(enumValues, ", ") + "]"
}

runbookActionRunActionCommand.Flags().StringVar(runbookStringArguments[key], key, templates.Render(variable.Default), description)
}

Expand All @@ -479,6 +488,10 @@ func processRunbookVariablesOnCommand(runbookActionRunActionCommand *cobra.Comma
})

}
} else if strings.HasPrefix(variable.Type, "ENUM:") {
// Extract enum values from "ENUM:val1,val2,val3"
enumValues := strings.Split(variable.Type[5:], ",")
return enumValues, cobra.ShellCompDirectiveNoFileComp
}
return []string{}, cobra.ShellCompDirectiveNoFileComp

Expand Down
15 changes: 10 additions & 5 deletions external/apihelper/get_all_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package apihelper
import (
"context"
"fmt"
"net/url"
"reflect"

"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/id"
"github.com/elasticpath/epcc-cli/external/resources"
log "github.com/sirupsen/logrus"
"net/url"
"reflect"
)

func GetAllIds(ctx context.Context, resource *resources.Resource) ([][]id.IdableAttributes, error) {
func GetAllIds(ctx context.Context, pageLength uint16, resource *resources.Resource) ([][]id.IdableAttributes, error) {
// TODO make this a channel based instead of array based
// This must be an unbuffered channel since the receiver won't get the channel until after we have sent in some cases.
//myEntityIds := make(chan<- []string, 1024)
Expand Down Expand Up @@ -49,7 +50,7 @@ func GetAllIds(ctx context.Context, resource *resources.Resource) ([][]id.Idable
parentResource = &myParentResource
}

myParentEntityIds, err := GetAllIds(ctx, parentResource)
myParentEntityIds, err := GetAllIds(ctx, pageLength, parentResource)
if err != nil {
return myEntityIds, err
}
Expand All @@ -66,9 +67,13 @@ func GetAllIds(ctx context.Context, resource *resources.Resource) ([][]id.Idable
lastPageIds := make([]id.IdableAttributes, 125)
for i := 0; i < 10000; i += 25 {
params := url.Values{}
params.Add("page[limit]", "25")
params.Add("page[limit]", fmt.Sprintf("%d", pageLength))
params.Add("page[offset]", fmt.Sprintf("%d", i))

for k, v := range resource.GetCollectionInfo.DefaultQueryParams {
params.Add(k, v)
}

resp, err := httpclient.DoRequest(ctx, "GET", resourceURL, params.Encode(), nil)

if resp != nil && resp.Body != nil {
Expand Down
2 changes: 2 additions & 0 deletions external/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func Complete(c Request) ([]string, cobra.ShellCompDirective) {

if regexOptions, err := rt.GetCompletionOptions(); err == nil {
autoCompleteAttributes = append(autoCompleteAttributes, regexOptions...)
} else {
log.Debugf("Could not complete options %v", err)
}

for _, k := range autoCompleteAttributes {
Expand Down
29 changes: 29 additions & 0 deletions external/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,35 @@ func TestCompleteAttributeKeyWithWhenAndSatisfiedExistingValuesReturnsSatisfiedC
require.Len(t, completions, 9)
}

// This test might be redundant but regex was broken for this resource at a time
func TestCompleteAttributeKeyWithEmptyExistingValuesReturnsAllIncludingRegex(t *testing.T) {
// Fixture Setup
toComplete := ""
manualOrder := resources.MustGetResourceByName("manual-order")
request := Request{
Type: CompleteAttributeKey,
Verb: Create,
ToComplete: toComplete,
Resource: manualOrder,
Attributes: map[string]string{
"meta.display_price.with_tax.currency": "USD",
"meta.display_price.balance_owing.formatted": "$10.00",
},
}

// Exercise SUT
completions, compDir := Complete(request)

// Verify Results
require.Equal(t, compDir, cobra.ShellCompDirectiveNoFileComp)

require.Contains(t, completions, "meta.display_price.with_tax.amount")
require.Contains(t, completions, "meta.display_price.balance_owing.currency")
require.NotContains(t, completions, "meta.display_price.with_tax.currency")
require.NotContains(t, completions, "meta.display_price.balance_owing.formatted")
require.Contains(t, completions, "meta.display_price.paid")
}

func TestCompleteAttributeKeyWithWhenSkippingWhen(t *testing.T) {
// Fixture Setup
toComplete := ""
Expand Down
2 changes: 2 additions & 0 deletions external/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ type CrudEntityInfo struct {

// Only valid on create, if set we report that the type created by this is different.
Creates string `yaml:"creates"`

DefaultQueryParams map[string]string `yaml:"default-query-params,omitempty"`
}

type CrudEntityAttribute struct {
Expand Down
11 changes: 10 additions & 1 deletion external/resources/resources_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@
"additionalProperties": false
}
},
"openapi-operation-id": { "type": "string" }
"openapi-operation-id": { "type": "string" },
"default-query-params": {
"type": "object",
"additionalProperties": true,
"patternProperties": {
"(.*?)": {
"type": "string"
}
}
}
},
"required": [ "url", "docs"]
},
Expand Down
5 changes: 3 additions & 2 deletions external/resources/uritemplates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package resources

import (
"fmt"
"net/url"
"strings"

"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/id"
log "github.com/sirupsen/logrus"
"github.com/yosida95/uritemplate/v3"
"net/url"
"strings"
)

func GenerateUrlViaIdableAttributes(urlInfo *CrudEntityInfo, args []id.IdableAttributes) (string, error) {
Expand Down
3 changes: 2 additions & 1 deletion external/resources/uritemplates_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package resources

import (
"github.com/elasticpath/epcc-cli/external/aliases"
"testing"

"github.com/elasticpath/epcc-cli/external/aliases"
)

func TestGetNumberOfVariablesReturnsErrorOnTemplate(t *testing.T) {
Expand Down
Loading
Loading