working commit
This commit is contained in:
+35
-52
@@ -16,7 +16,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"mstore/app/descr"
|
||||
"mstore/pkg/client"
|
||||
@@ -55,75 +54,59 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Password, "pass", "p", "", "Password")
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Hostname, "host", "x", defaultHostname, "Hostname")
|
||||
subCmd.PersistentFlags().Uint64VarP(&util.commonAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
subCmd.MarkPersistentFlagRequired("host")
|
||||
subCmd.MarkFlagsRequiredTogether("user", "pass")
|
||||
|
||||
// CreateAccount
|
||||
var createAccountCmd = &cobra.Command{
|
||||
Use: "create",
|
||||
Use: "create host username password",
|
||||
Short: "Create user account",
|
||||
Args: cobra.ExactArgs(3),
|
||||
Run: util.CreateAccount,
|
||||
}
|
||||
createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewUsername, "newuser", "U", "", "New account username")
|
||||
createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewPassword, "newpass", "P", "", "New account password")
|
||||
createAccountCmd.MarkFlagsRequiredTogether("newuser", "newpass")
|
||||
subCmd.AddCommand(createAccountCmd)
|
||||
|
||||
// GetAccount
|
||||
var getAccountCmd = &cobra.Command{
|
||||
Use: "get",
|
||||
Use: "get hostname accountId|username",
|
||||
Short: "Get account info",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.GetAccount,
|
||||
}
|
||||
getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "id", "I", "", "Account ID or name")
|
||||
getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "name", "n", "", "Account ID or name")
|
||||
getAccountCmd.MarkFlagsOneRequired("id", "name")
|
||||
subCmd.AddCommand(getAccountCmd)
|
||||
|
||||
// UpdateAccount
|
||||
var updateAccountCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Use: "update hostname username|accounId",
|
||||
Short: "Update account parameters",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.UpdateAccount,
|
||||
}
|
||||
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "id", "I", "", "Account ID or username")
|
||||
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username")
|
||||
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "N", "", "New username")
|
||||
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "P", "", "New password")
|
||||
updateAccountCmd.MarkFlagsOneRequired("id", "name")
|
||||
updateAccountCmd.MarkFlagsOneRequired("newname", "newpass")
|
||||
subCmd.AddCommand(updateAccountCmd)
|
||||
|
||||
// DeleteAccount
|
||||
var deleteAccountCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Use: "delete hostname username|accountId",
|
||||
Short: "Delete account",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.DeleteAccount,
|
||||
}
|
||||
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.AccountID, "id", "I", "", "Account ID")
|
||||
deleteAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username")
|
||||
deleteAccountCmd.MarkFlagsOneRequired("id", "name")
|
||||
subCmd.AddCommand(deleteAccountCmd)
|
||||
|
||||
// ListAccount
|
||||
var listAccountsCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Use: "list hostname",
|
||||
Short: "list accounts",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: util.ListAccounts,
|
||||
}
|
||||
listAccountsCmd.Flags().BoolVarP(&util.listAccountsParams.Detail, "detail", "d", false, "Show detail information")
|
||||
listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Regex, "regex", "r", "", "Output regexp for usernames")
|
||||
listAccountsCmd.MarkFlagRequired("host")
|
||||
subCmd.AddCommand(listAccountsCmd)
|
||||
|
||||
viper.BindPFlags(subCmd.Flags())
|
||||
viper.SetEnvPrefix("MSTORE")
|
||||
|
||||
// Bind environment variables
|
||||
viper.BindEnv("user")
|
||||
viper.BindEnv("pass")
|
||||
viper.BindEnv("host")
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
@@ -133,11 +116,14 @@ type CreateAccountParams struct {
|
||||
NewPassword string
|
||||
}
|
||||
type CreateAccountResult struct {
|
||||
AccountID string `yaml:"accountId"`
|
||||
GrantIDs []string `yaml:"grantsIds,omitempty"`
|
||||
AccountID string `yaml:"accountId"`
|
||||
Grants map[string]string `yaml:"grantsIds,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) {
|
||||
util.commonAccountParams.Hostname = args[0]
|
||||
util.createAccountParams.NewUsername = args[1]
|
||||
util.createAccountParams.NewPassword = args[2]
|
||||
res, err := util.createAccount(&util.commonAccountParams, &util.createAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
@@ -145,7 +131,7 @@ func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) {
|
||||
func (util *AccountUtil) createAccount(common *CommonAccountParams, params *CreateAccountParams) (*CreateAccountResult, error) {
|
||||
var err error
|
||||
res := &CreateAccountResult{
|
||||
GrantIDs: make([]string, 0),
|
||||
Grants: make(map[string]string, 0),
|
||||
}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
@@ -167,11 +153,11 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea
|
||||
descr.RightReadImages,
|
||||
}
|
||||
for _, right := range fullRights {
|
||||
id, err := client.NewClient().CreateGrant(ctx, hostname, accountID, right, ".*")
|
||||
id, err := client.NewClient().CreateGrantByAccountID(ctx, hostname, accountID, right, ".*")
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.GrantIDs = append(res.GrantIDs, id)
|
||||
res.Grants[id] = right
|
||||
}
|
||||
res.AccountID = accountID
|
||||
return res, err
|
||||
@@ -179,9 +165,6 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea
|
||||
|
||||
// UpdateAccount
|
||||
type UpdateAccountParams struct {
|
||||
Hostname string
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
AccountID string
|
||||
NewUsername string
|
||||
@@ -192,6 +175,8 @@ type UpdateAccountResult struct {
|
||||
}
|
||||
|
||||
func (util *AccountUtil) UpdateAccount(cmd *cobra.Command, args []string) {
|
||||
util.commonAccountParams.Hostname = args[0]
|
||||
util.updateAccountParams.AccountID = args[1]
|
||||
res, err := util.updateAccount(&util.commonAccountParams, &util.updateAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
@@ -220,14 +205,13 @@ func (util *AccountUtil) updateAccount(common *CommonAccountParams, params *Upda
|
||||
|
||||
// Get file
|
||||
type GetAccountParams struct {
|
||||
Hostname string
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
AccountID string
|
||||
}
|
||||
|
||||
func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) {
|
||||
util.commonAccountParams.Hostname = args[0]
|
||||
util.getAccountParams.AccountID = args[1]
|
||||
res, err := util.getAccount(&util.commonAccountParams, &util.getAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
@@ -263,9 +247,6 @@ func (util *AccountUtil) getAccount(common *CommonAccountParams, params *GetAcco
|
||||
|
||||
// DeleteAccount
|
||||
type DeleteAccountParams struct {
|
||||
Hostname string
|
||||
Username string
|
||||
Password string
|
||||
AccountID string
|
||||
Timeout uint64
|
||||
}
|
||||
@@ -273,6 +254,8 @@ type DeleteAccountParams struct {
|
||||
type DeleteAccountResult struct{}
|
||||
|
||||
func (util *AccountUtil) DeleteAccount(cmd *cobra.Command, args []string) {
|
||||
util.commonAccountParams.Hostname = args[0]
|
||||
util.deleteAccountParams.AccountID = args[1]
|
||||
res, err := util.deleteAccount(&util.commonAccountParams, &util.deleteAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
@@ -300,17 +283,15 @@ func (util *AccountUtil) deleteAccount(common *CommonAccountParams, params *Dele
|
||||
|
||||
// ListAccounts
|
||||
type ListAccountsParams struct {
|
||||
Hostname string
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
Detail bool
|
||||
Regex string
|
||||
Timeout uint64
|
||||
Detail bool
|
||||
Regex string
|
||||
}
|
||||
|
||||
type Userinfo struct {
|
||||
Username string `yaml:"username,omitempty"`
|
||||
Rights []string `yaml:"rights,omitempty"`
|
||||
Username string `yaml:"username,omitempty"`
|
||||
AccountID string `yaml:"accountId,omitempty"`
|
||||
Rights map[string]string `yaml:"rights,omitempty"`
|
||||
}
|
||||
|
||||
type ListAccountsResult struct {
|
||||
@@ -319,6 +300,7 @@ type ListAccountsResult struct {
|
||||
}
|
||||
|
||||
func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) {
|
||||
util.commonAccountParams.Hostname = args[0]
|
||||
res, err := util.listAccounts(&util.commonAccountParams, &util.listAccountsParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
@@ -356,11 +338,12 @@ func (util *AccountUtil) listAccounts(common *CommonAccountParams, params *ListA
|
||||
res.Users = make([]Userinfo, 0)
|
||||
for _, account := range outAccounts {
|
||||
userinfo := Userinfo{
|
||||
Username: account.Username,
|
||||
Rights: make([]string, 0),
|
||||
Username: account.Username,
|
||||
AccountID: account.ID,
|
||||
Rights: make(map[string]string, 0),
|
||||
}
|
||||
for _, grant := range account.Grants {
|
||||
userinfo.Rights = append(userinfo.Rights, grant.Right)
|
||||
userinfo.Rights[grant.ID] = grant.Right
|
||||
}
|
||||
res.Users = append(res.Users, userinfo)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user