Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle namespace API listing on the client. #191

Merged
merged 4 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ javascript/
wsk
scripts
Godeps/_workspace
.idea/
*.iml
.gradle
62 changes: 27 additions & 35 deletions commands/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,50 +61,35 @@ var namespaceListCmd = &cobra.Command{
}

var namespaceGetCmd = &cobra.Command{
Use: "get [NAMESPACE]",
Short: wski18n.T("get triggers, actions, and rules in the registry for a namespace"),
Use: "get",
Short: wski18n.T("get triggers, actions, and rules in the registry for namespace"),
SilenceUsage: true,
SilenceErrors: true,
PreRunE: SetupClientConfig,
RunE: func(cmd *cobra.Command, args []string) error {
var qualifiedName = new(QualifiedName)
var err error
var namespace string = getClientNamespace()

if whiskErr := CheckArgs(args, 0, 1, "Namespace get",
wski18n.T("An optional namespace is the only valid argument.")); whiskErr != nil {
return whiskErr
}

// Namespace argument is optional; defaults to configured property namespace
if len(args) == 1 {
if qualifiedName, err = NewQualifiedName(args[0]); err != nil {
return NewQualifiedNameError(args[0], err)
}

if len(qualifiedName.GetEntityName()) > 0 {
return entityNameError(qualifiedName.GetEntityName())
if (!(len(args) == 1 && args[0] == "/_")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to clash a little with the usage info that states the namespace parameter is optional.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, there isn't a test for wsk namespace get /_.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is, the CLI tests already do this: wsk namespace get /_ (that's how I found it).
I have to add support for /_ because there's a circular dependence between this repo and the main repo and can't break it otherwise.

if whiskErr := CheckArgs(args, 0, 0, "Namespace get",
wski18n.T("No arguments are required.")); whiskErr != nil {
return whiskErr
}
}

namespace, _, err := Client.Namespaces.Get(qualifiedName.GetNamespace())
actions, _, err := Client.Actions.List("", &whisk.ActionListOptions{ Skip: 0, Limit: 0 })
if err != nil { return entityListError(err, namespace,"Actions") }

if err != nil {
whisk.Debug(whisk.DbgError, "Client.Namespaces.Get(%s) error: %s\n", getClientNamespace(), err)
errStr := wski18n.T("Unable to obtain the list of entities for namespace '{{.namespace}}': {{.err}}",
map[string]interface{}{"namespace": getClientNamespace(), "err": err})
werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXIT_CODE_ERR_NETWORK,
whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
return werr
}
packages, _, err := Client.Packages.List(&whisk.PackageListOptions{ Skip: 0, Limit: 0 })
if err != nil { return entityListError(err, namespace,"Packages") }

fmt.Fprintf(color.Output, wski18n.T("Entities in namespace: {{.namespace}}\n",
map[string]interface{}{"namespace": boldString(getClientNamespace())}))
sortByName := Flags.common.nameSort
printList(namespace.Contents.Packages, sortByName)
printList(namespace.Contents.Actions, sortByName)
printList(namespace.Contents.Triggers, sortByName)
//No errors, lets attempt to retrieve the status of each rule #312
for index, rule := range namespace.Contents.Rules {
triggers, _, err := Client.Triggers.List(&whisk.TriggerListOptions{ Skip: 0, Limit: 0 })
if err != nil { return entityListError(err, namespace,"Triggers") }

rules, _, err := Client.Rules.List(&whisk.RuleListOptions{ Skip: 0, Limit: 0 })
if err != nil { return entityListError(err, namespace,"Rules") }
//No errors, lets attempt to retrieve the status of each rule
for index, rule := range rules {
ruleStatus, _, err := Client.Rules.Get(rule.Name)
if err != nil {
errStr := wski18n.T("Unable to get status of rule '{{.name}}': {{.err}}",
Expand All @@ -113,9 +98,16 @@ var namespaceGetCmd = &cobra.Command{
werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
return werr
}
namespace.Contents.Rules[index].Status = ruleStatus.Status
rules[index].Status = ruleStatus.Status
}
printList(namespace.Contents.Rules, sortByName)

fmt.Fprintf(color.Output, wski18n.T("Entities in namespace: {{.namespace}}\n",
map[string]interface{}{"namespace": boldString(getClientNamespace())}))
sortByName := Flags.common.nameSort
printList(packages, sortByName)
printList(actions, sortByName)
printList(triggers, sortByName)
printList(rules, sortByName)

return nil
},
Expand Down
9 changes: 9 additions & 0 deletions commands/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ func entityNameError(entityName string) (error) {

return whisk.MakeWskError(errors.New(errMsg), whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
}

func entityListError(err error, namespace string, kind string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

whisk.Debug(whisk.DbgError, "Client.%s.List(%s) error: %s\n", kind, namespace, err)
errStr := wski18n.T("Unable to obtain the list of entities for namespace '{{.namespace}}': {{.err}}",
map[string]interface{}{"namespace": namespace, "err": err})
return whisk.MakeWskErrorFromWskError(errors.New(errStr), err,
whisk.EXIT_CODE_ERR_NETWORK, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
}

4 changes: 2 additions & 2 deletions tests/src/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func initInvalidArgs() {
Err: tooManyArgsMsg + invalidArg + ". " + noArgsReqMsg,
},
common.InvalidArg {
Cmd: []string{"namespace", "get", "namespace", invalidArg},
Err: tooManyArgsMsg + invalidArg + ". " + optNamespaceMsg,
Cmd: []string{"namespace", "get", invalidArg},
Err: tooManyArgsMsg + invalidArg + ". " + noArgsReqMsg,
},
common.InvalidArg {
Cmd: []string{"package", "create"},
Expand Down
10 changes: 1 addition & 9 deletions tests/src/test/scala/system/basic/WskBasicTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -776,18 +776,10 @@ class WskBasicTests extends TestHelpers with WskTestHelpers {
}

it should "list entities in default namespace" in {
// use a fresh wsk props instance that is guaranteed to use
// the default namespace
// use a fresh wsk props instance that is guaranteed to use the default namespace
wsk.namespace.get(expectedExitCode = SUCCESS_EXIT)(WskProps()).stdout should include("default")
}

it should "not list entities with an invalid namespace" in {
val namespace = "fakeNamespace"
val stderr = wsk.namespace.get(Some(s"/${namespace}"), expectedExitCode = FORBIDDEN).stderr

stderr should include(s"Unable to obtain the list of entities for namespace '${namespace}'")
}

behavior of "Wsk Activation CLI"

it should "create a trigger, and fire a trigger to get its individual fields from an activation" in withAssetCleaner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ class WskBasicUsageTests extends TestHelpers with WskTestHelpers {
(Seq("activation", "result", "activationID", invalidArg), s"${tooManyArgsMsg}${invalidArg}."),
(Seq("activation", "poll", "activationID", invalidArg), s"${tooManyArgsMsg}${invalidArg}. ${optNamespaceMsg}"),
(Seq("namespace", "list", invalidArg), s"${tooManyArgsMsg}${invalidArg}. ${noArgsReqMsg}"),
(Seq("namespace", "get", "namespace", invalidArg), s"${tooManyArgsMsg}${invalidArg}. ${optNamespaceMsg}"),
(Seq("namespace", "get", invalidArg), s"${tooManyArgsMsg}${invalidArg}. ${noArgsReqMsg}"),
(Seq("package", "create"), s"${tooFewArgsMsg} ${packageNameReqMsg}"),
(Seq("package", "create", "packageName", invalidArg), s"${tooManyArgsMsg}${invalidArg}."),
(Seq("package", "create", "packageName", "--shared", invalidArg), invalidShared),
Expand Down
4 changes: 2 additions & 2 deletions wski18n/resources/en_US.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
"translation": "Unable to obtain the list of available namespaces: {{.err}}"
},
{
"id": "get triggers, actions, and rules in the registry for a namespace",
"translation": "get triggers, actions, and rules in the registry for a namespace"
"id": "get triggers, actions, and rules in the registry for namespace",
"translation": "get triggers, actions, and rules in the registry for namespace"
},
{
"id": "'{{.name}}' is not a valid qualified name: {{.err}}",
Expand Down