diff --git a/manage/account.go b/manage/account.go index 979800e9..da688b8e 100644 --- a/manage/account.go +++ b/manage/account.go @@ -22,4 +22,5 @@ package manage // AccountCommand is the main command for account management type AccountCommand struct { Cache AccountCacheCommand `command:"cache" alias:"c" description:"Cache the account assertions from the store in the database"` + Add AccountAddCommand `command:"add" alias:"a" description:"Add a new account"` } diff --git a/manage/account_test.go b/manage/account_test.go index 967618ef..284ab7de 100644 --- a/manage/account_test.go +++ b/manage/account_test.go @@ -40,13 +40,22 @@ func (s *AccountSuite) TestAccount(c *check.C) { tests := []manTest{ { Args: []string{"serial-vault-admin", "account"}, - ErrorMessage: "Please specify the cache command"}, + ErrorMessage: "Please specify one command of: add or cache"}, { Args: []string{"serial-vault-admin", "account", "invalid"}, - ErrorMessage: "Unknown command `invalid'. You should use the cache command"}, + ErrorMessage: "Unknown command `invalid'. Please specify one command of: add or cache"}, { Args: []string{"serial-vault-admin", "account", "cache"}, - ErrorMessage: ""}, + ErrorMessage: "", + }, + { + Args: []string{"serial-vault-admin", "account", "add", "acc123"}, + ErrorMessage: "", + }, + { + Args: []string{"serial-vault-admin", "account", "add", "acc123", "-r"}, + ErrorMessage: "", + }, } for _, t := range tests { diff --git a/manage/accountadd.go b/manage/accountadd.go new file mode 100644 index 00000000..b2982d2c --- /dev/null +++ b/manage/accountadd.go @@ -0,0 +1,43 @@ +package manage + +import ( + "fmt" + "github.com/CanonicalLtd/serial-vault/datastore" +) + +// AccountAddCommand handles adding a new account for the serial-vault-admin command +type AccountAddCommand struct { + ResellerAPI bool `short:"r" long:"reseller" description:"Enable the reseller API"` +} + +// Execute the adding of an account +func (cmd AccountAddCommand) Execute(args []string) error { + err := checkAccountIDArg(args, "Add") + if err != nil { + return err + } + + // Open the database and create the account + openDatabase() + account := datastore.Account{ + AuthorityID: args[0], + ResellerAPI: cmd.ResellerAPI, + } + if err := datastore.Environ.DB.CreateAccount(account); err != nil { + return fmt.Errorf("error creating the account: %v", err) + } + + fmt.Printf("Account '%s' created successfully\n", account.AuthorityID) + return nil +} + +func checkAccountIDArg(args []string, action string) error { + switch len(args) { + case 0: + return fmt.Errorf("%s account expects an 'account ID' argument", action) + case 1: + return nil + default: + return fmt.Errorf("%s account expects a single 'account ID' argument", action) + } +}