浏览代码

feat(cmd): impl. bulk user update

Mustafa Arici 7 年之前
父节点
当前提交
e8f5852ba0
共有 3 个文件被更改,包括 70 次插入27 次删除
  1. 48 26
      cmd/ovpm/action_user.go
  2. 16 1
      cmd/ovpm/cmd_user.go
  3. 6 0
      cmd/ovpm/user_test.go

+ 48 - 26
cmd/ovpm/action_user.go

@@ -171,7 +171,7 @@ func userCreateAction(rpcSrvURLStr string, username string, password string, ipA
 }
 }
 
 
 // userUpdateAction creates a new VPN user from the terminal.
 // 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.
 	// Parse RPC Server's URL.
 	rpcSrvURL, err := url.Parse(rpcSrvURLStr)
 	rpcSrvURL, err := url.Parse(rpcSrvURLStr)
 	if err != nil {
 	if err != nil {
@@ -195,18 +195,20 @@ func userUpdateAction(rpcSrvURLStr string, username string, password *string, ip
 	// Set targeted static IP addr.
 	// Set targeted static IP addr.
 	targetHostid := uint32(0)
 	targetHostid := uint32(0)
 	targetStaticPref := pb.UserUpdateRequest_NOPREFSTATIC
 	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
 		}
 		}
 	}
 	}
 
 
@@ -233,22 +235,42 @@ func userUpdateAction(rpcSrvURLStr string, username string, password *string, ip
 	// Prepare a service caller.
 	// Prepare a service caller.
 	var userSvc = pb.NewUserServiceClient(rpcConn)
 	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
 	return nil
 }
 }
 
 

+ 16 - 1
cmd/ovpm/cmd_user.go

@@ -161,6 +161,10 @@ var userUpdateCmd = cli.Command{
 	Action: func(c *cli.Context) error {
 	Action: func(c *cli.Context) error {
 		action = "user:update"
 		action = "user:update"
 
 
+		// inBulk means opeation needs to be done on
+		// all users.
+		var inBulk bool
+
 		// Use default port if no port is specified.
 		// Use default port if no port is specified.
 		daemonPort := ovpm.DefaultDaemonPort
 		daemonPort := ovpm.DefaultDaemonPort
 		if port := c.GlobalInt("daemon-port"); port != 0 {
 		if port := c.GlobalInt("daemon-port"); port != 0 {
@@ -169,12 +173,16 @@ var userUpdateCmd = cli.Command{
 
 
 		// Validate username and maybe password if set.
 		// Validate username and maybe password if set.
 		if govalidator.IsNull(c.String("username")) {
 		if govalidator.IsNull(c.String("username")) {
-			fmt.Printf("HERE")
 			err := errors.EmptyValue("username", c.String("username"))
 			err := errors.EmptyValue("username", c.String("username"))
 			exit(1)
 			exit(1)
 			return err
 			return err
 		}
 		}
 
 
+		// Check if bulk update is set.
+		if c.String("username") == "*" {
+			inBulk = true
+		}
+
 		// Set password if it's provided.
 		// Set password if it's provided.
 		var password *string
 		var password *string
 		if passwordStr := c.String("password"); len(passwordStr) > 0 {
 		if passwordStr := c.String("password"); len(passwordStr) > 0 {
@@ -184,7 +192,13 @@ var userUpdateCmd = cli.Command{
 		// Set isStatic if it's provided.
 		// Set isStatic if it's provided.
 		var isStatic *bool
 		var isStatic *bool
 		var ipAddr *net.IP
 		var ipAddr *net.IP
+
 		// Check mutex options.
 		// 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 {
 		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)")
 			err := errors.ConflictingDemands("--static and --no-static options are mutually exclusive (can not be used together)")
 			exit(1)
 			exit(1)
@@ -258,6 +272,7 @@ var userUpdateCmd = cli.Command{
 			isStatic,
 			isStatic,
 			noGW,
 			noGW,
 			isAdmin,
 			isAdmin,
+			inBulk,
 		)
 		)
 	},
 	},
 }
 }

+ 6 - 0
cmd/ovpm/user_test.go

@@ -120,6 +120,12 @@ func TestUserUpdateCmd(t *testing.T) {
 	if err == nil {
 	if err == nil {
 		t.Fatal("error is expected about static being malformed ip, but we didn't got error")
 		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) {
 func TestUserDeleteCmd(t *testing.T) {