Skip to content

Commit

Permalink
feat(cmd): impl. bulk user update
Browse files Browse the repository at this point in the history
  • Loading branch information
cad committed May 13, 2018
1 parent b2d6bdc commit e8f5852
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 deletions.
74 changes: 48 additions & 26 deletions cmd/ovpm/action_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func userCreateAction(rpcSrvURLStr string, username string, password string, ipA
}

// userUpdateAction creates a new VPN user from the terminal.
func userUpdateAction(rpcSrvURLStr string, username string, password *string, ipAddr *net.IP, isStatic *bool, noGW *bool, isAdmin *bool) error {
func userUpdateAction(rpcSrvURLStr string, username string, password *string, ipAddr *net.IP, isStatic *bool, noGW *bool, isAdmin *bool, inBulk bool) error {
// Parse RPC Server's URL.
rpcSrvURL, err := url.Parse(rpcSrvURLStr)
if err != nil {
Expand All @@ -195,18 +195,20 @@ func userUpdateAction(rpcSrvURLStr string, username string, password *string, ip
// Set targeted static IP addr.
targetHostid := uint32(0)
targetStaticPref := pb.UserUpdateRequest_NOPREFSTATIC
if isStatic != nil {
if *isStatic {
targetHostid = ovpm.IP2HostID(ipAddr.To4())
if targetHostid == 0 {
// hostid being 0 means dynamic ip addr(no static ip address provided),
// hence ambiguous meaning here.
// This is perceived as an error.
return errors.ConflictingDemands("hostid is 0, but user is trying to allocate a static ip addr")
if !inBulk {
if isStatic != nil {
if *isStatic {
targetHostid = ovpm.IP2HostID(ipAddr.To4())
if targetHostid == 0 {
// hostid being 0 means dynamic ip addr(no static ip address provided),
// hence ambiguous meaning here.
// This is perceived as an error.
return errors.ConflictingDemands("hostid is 0, but user is trying to allocate a static ip addr")
}
targetStaticPref = pb.UserUpdateRequest_STATIC
} else {
targetStaticPref = pb.UserUpdateRequest_NOSTATIC
}
targetStaticPref = pb.UserUpdateRequest_STATIC
} else {
targetStaticPref = pb.UserUpdateRequest_NOSTATIC
}
}

Expand All @@ -233,22 +235,42 @@ func userUpdateAction(rpcSrvURLStr string, username string, password *string, ip
// Prepare a service caller.
var userSvc = pb.NewUserServiceClient(rpcConn)

// Send a user update request to the server.
userUpdateResp, err := userSvc.Update(context.Background(), &pb.UserUpdateRequest{
Username: username,
Password: targetPassword,
Gwpref: targetGWPref,
StaticPref: targetStaticPref,
HostId: targetHostid,
AdminPref: targetAdminPref,
})
if err != nil {
err := errors.UnknownGRPCError(err)
exit(1)
return err
userNames := []string{username}

// Mark all users to update if working in bulk.
if inBulk {
userListResp, err := userSvc.List(context.Background(), &pb.UserListRequest{})
if err != nil {
err := errors.UnknownGRPCError(err)
exit(1)
return err
}

uNames := []string{}
for _, u := range userListResp.Users {
uNames = append(uNames, u.Username)
}
userNames = uNames
}

for _, userName := range userNames {
// Send a user update request to the server.
userUpdateResp, err := userSvc.Update(context.Background(), &pb.UserUpdateRequest{
Username: userName,
Password: targetPassword,
Gwpref: targetGWPref,
StaticPref: targetStaticPref,
HostId: targetHostid,
AdminPref: targetAdminPref,
})
if err != nil {
err := errors.UnknownGRPCError(err)
exit(1)
return err
}
logrus.Infof("user updated: %s", userUpdateResp.Users[0].Username)
}

logrus.Infof("user updated: %s", userUpdateResp.Users[0].Username)
return nil
}

Expand Down
17 changes: 16 additions & 1 deletion cmd/ovpm/cmd_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ var userUpdateCmd = cli.Command{
Action: func(c *cli.Context) error {
action = "user:update"

// inBulk means opeation needs to be done on
// all users.
var inBulk bool

// Use default port if no port is specified.
daemonPort := ovpm.DefaultDaemonPort
if port := c.GlobalInt("daemon-port"); port != 0 {
Expand All @@ -169,12 +173,16 @@ var userUpdateCmd = cli.Command{

// Validate username and maybe password if set.
if govalidator.IsNull(c.String("username")) {
fmt.Printf("HERE")
err := errors.EmptyValue("username", c.String("username"))
exit(1)
return err
}

// Check if bulk update is set.
if c.String("username") == "*" {
inBulk = true
}

// Set password if it's provided.
var password *string
if passwordStr := c.String("password"); len(passwordStr) > 0 {
Expand All @@ -184,7 +192,13 @@ var userUpdateCmd = cli.Command{
// Set isStatic if it's provided.
var isStatic *bool
var ipAddr *net.IP

// Check mutex options.
if !govalidator.IsNull(c.String("static")) == true && inBulk {
err := errors.ConflictingDemands("--static and --user * (bulk) options are mutually exclusive (can not be used together)")
exit(1)
return err
}
if !govalidator.IsNull(c.String("static")) == true && c.Bool("no-static") == true {
err := errors.ConflictingDemands("--static and --no-static options are mutually exclusive (can not be used together)")
exit(1)
Expand Down Expand Up @@ -258,6 +272,7 @@ var userUpdateCmd = cli.Command{
isStatic,
noGW,
isAdmin,
inBulk,
)
},
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/ovpm/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func TestUserUpdateCmd(t *testing.T) {
if err == nil {
t.Fatal("error is expected about static being malformed ip, but we didn't got error")
}

// Bulk update mutex
err = app.Run([]string{"ovpm", "user", "update", "--username", "*", "--static", "12.12.12.12"})
if err == nil {
t.Fatal("error is expected about bulk and --static conflict")
}
}

func TestUserDeleteCmd(t *testing.T) {
Expand Down

0 comments on commit e8f5852

Please sign in to comment.