This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ttnctl] Add commands to manage components collaborators
- Loading branch information
1 parent
77f98c0
commit f92f632
Showing
5 changed files
with
148 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright © 2017 The Things Network | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var componentsCollaboratorsCmd = &cobra.Command{ | ||
Use: "collaborators", | ||
Short: "Manage collaborators of a network component.", | ||
Long: `components collaborators can be used to manage the collaborators of a network component.`, | ||
} | ||
|
||
func init() { | ||
componentsCmd.AddCommand(componentsCollaboratorsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright © 2017 The Things Network | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
ttnlog "github.com/TheThingsNetwork/go-utils/log" | ||
"github.com/TheThingsNetwork/ttn/ttnctl/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var componentsCollaboratorsDeleteCmd = &cobra.Command{ | ||
Use: "delete [Type] [ComponentID] [Username]", | ||
Short: "Retracts an user as component collaborator.", | ||
Long: `components collaborators delete can be used to retract users as collaborators.`, | ||
Example: `$ ttnctl components collaborators delete handler ttn-handler-dev gomezjdaniel | ||
INFO Successfully user retracted as component collaborator ComponentID=dev-handler Type=handler Username=gomezjdaniel | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
assertArgsLength(cmd, args, 3, 3) | ||
|
||
account := util.GetAccount(ctx) | ||
|
||
if err := account.RetractComponentRights(args[0], args[1], args[2]); err != nil { | ||
ctx.WithError(err).Fatal("Could not retract user as component collaborator") | ||
} | ||
|
||
ctx.WithFields(ttnlog.Fields{ | ||
"Type": args[0], | ||
"ComponentID": args[1], | ||
"Username": args[2], | ||
}).Info("Successfully user retracted as component collaborator") | ||
}, | ||
} | ||
|
||
func init() { | ||
componentsCollaboratorsCmd.AddCommand(componentsCollaboratorsDeleteCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright © 2017 The Things Network | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
ttnlog "github.com/TheThingsNetwork/go-utils/log" | ||
"github.com/TheThingsNetwork/ttn/core/types" | ||
"github.com/TheThingsNetwork/ttn/ttnctl/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var componentsCollaboratorsSetCmd = &cobra.Command{ | ||
Use: "set [Type] [ComponentID] [Username]", | ||
Short: "Grant an user with rights over a network component.", | ||
Long: `components collaborators set can be used to add an username as collaborator or edit his rights.`, | ||
Example: `$ ttnctl components collaborators set handler ttn-handler-dev gomezjdaniel --rights='component:settings,component:delete' | ||
INFO Successfully user granted with collaborator rights ComponentID=ttn-dev-handler Type=handler Username=gomezjdaniel | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
assertArgsLength(cmd, args, 3, 3) | ||
|
||
var rights []types.Right | ||
rightsStrings, err := cmd.Flags().GetStringSlice("rights") | ||
if err != nil { | ||
ctx.WithError(err).Fatal("Could not read rights options") | ||
} | ||
if len(rightsStrings) == 0 { | ||
ctx.Fatal("Rights has not been provided through --rights flag") | ||
} | ||
|
||
for _, rightString := range rightsStrings { | ||
rights = append(rights, types.Right(rightString)) | ||
} | ||
|
||
account := util.GetAccount(ctx) | ||
|
||
if err := account.GrantComponentRights(args[0], args[1], args[2], rights); err != nil { | ||
ctx.WithError(err).Fatal("Could not grant user with collaborator rights") | ||
} | ||
|
||
ctx.WithFields(ttnlog.Fields{ | ||
"Type": args[0], | ||
"ComponentID": args[1], | ||
"Username": args[2], | ||
}).Info("Successfully user granted with component rights") | ||
}, | ||
} | ||
|
||
func init() { | ||
componentsCollaboratorsCmd.AddCommand(componentsCollaboratorsSetCmd) | ||
componentsCollaboratorsSetCmd.Flags().StringSlice("rights", []string{}, "Rights to be granted") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters