Parcourir la source

feat(cmd): imp. bulk network assoc/dissoc w/ users

Mustafa Arici il y a 7 ans
Parent
commit
f8556d7113
2 fichiers modifiés avec 65 ajouts et 17 suppressions
  1. 49 15
      cmd/ovpm/action_net.go
  2. 16 2
      cmd/ovpm/cmd_net.go

+ 49 - 15
cmd/ovpm/action_net.go

@@ -202,7 +202,7 @@ func netDefAction(rpcServURLStr string, netName string, netCIDR string, netType
 	return nil
 }
 
-func netAssocAction(rpcServURLStr string, netName string, username string) error {
+func netAssocAction(rpcServURLStr string, netName string, username string, inBulk bool) error {
 	// Parse RPC Server's URL.
 	rpcSrvURL, err := url.Parse(rpcServURLStr)
 	if err != nil {
@@ -220,18 +220,35 @@ func netAssocAction(rpcServURLStr string, netName string, username string) error
 	// Prepare service callable.
 	var netSvc = pb.NewNetworkServiceClient(rpcConn)
 
+	userNames := []string{username}
+	if inBulk {
+		var userSvc = pb.NewUserServiceClient(rpcConn)
+		r, err := userSvc.List(context.Background(), &pb.UserListRequest{})
+		if err != nil {
+			errors.UnknownGRPCError(err)
+			exit(1)
+			return err
+		}
+		userNames = []string{}
+		for _, u := range r.Users {
+			userNames = append(userNames, u.Username)
+		}
+	}
+
 	// Call the service.
-	_, err = netSvc.Associate(context.Background(), &pb.NetworkAssociateRequest{Name: netName, Username: username})
-	if err != nil {
-		errors.UnknownGRPCError(err)
-		exit(1)
-		return err
+	for _, userName := range userNames {
+		_, err = netSvc.Associate(context.Background(), &pb.NetworkAssociateRequest{Name: netName, Username: userName})
+		if err != nil {
+			errors.UnknownGRPCError(err)
+			//exit(1)
+			//return err
+		}
+		logrus.Infof("network associated: user:%s <-> network:%s", userName, netName)
 	}
-	logrus.Infof("network associated: user:%s <-> network:%s", username, netName)
 	return nil
 }
 
-func netDissocAction(rpcServURLStr string, netName string, username string) error {
+func netDissocAction(rpcServURLStr string, netName string, username string, inBulk bool) error {
 	// Parse RPC Server's URL.
 	rpcSrvURL, err := url.Parse(rpcServURLStr)
 	if err != nil {
@@ -249,14 +266,31 @@ func netDissocAction(rpcServURLStr string, netName string, username string) erro
 	// Prepare service callable.
 	var netSvc = pb.NewNetworkServiceClient(rpcConn)
 
-	// Call the service.
-	_, err = netSvc.Dissociate(context.Background(), &pb.NetworkDissociateRequest{Name: netName, Username: username})
-	if err != nil {
-		errors.UnknownGRPCError(err)
-		exit(1)
-		return err
+	userNames := []string{username}
+	if inBulk {
+		var userSvc = pb.NewUserServiceClient(rpcConn)
+		r, err := userSvc.List(context.Background(), &pb.UserListRequest{})
+		if err != nil {
+			errors.UnknownGRPCError(err)
+			exit(1)
+			return err
+		}
+		userNames = []string{}
+		for _, u := range r.Users {
+			userNames = append(userNames, u.Username)
+		}
 	}
 
-	logrus.Infof("network dissociated: user:%s <-> network:%s", username, netName)
+	// Call the service.
+	for _, userName := range userNames {
+		_, err = netSvc.Dissociate(context.Background(), &pb.NetworkDissociateRequest{Name: netName, Username: userName})
+		if err != nil {
+			errors.UnknownGRPCError(err)
+			//exit(1)
+			//return err
+		}
+
+		logrus.Infof("network dissociated: user:%s <-> network:%s", userName, netName)
+	}
 	return nil
 }

+ 16 - 2
cmd/ovpm/cmd_net.go

@@ -187,6 +187,8 @@ var netAssociateCommand = cli.Command{
 			daemonPort = port
 		}
 
+		var inBulk bool
+
 		// Validate username and network name.
 		if netName := c.String("net"); govalidator.IsNull(netName) {
 			err := errors.EmptyValue("network", netName)
@@ -199,12 +201,17 @@ var netAssociateCommand = cli.Command{
 			return err
 		}
 
+		// Mark inBulk if username is set to asterisk.
+		if c.String("user") == "*" {
+			inBulk = true
+		}
+
 		// If dry run, then don't call the action, just preprocess.
 		if c.GlobalBool("dry-run") {
 			return nil
 		}
 
-		return netAssocAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("net"), c.String("user"))
+		return netAssocAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("net"), c.String("user"), inBulk)
 	},
 }
 
@@ -232,6 +239,8 @@ var netDissociateCommand = cli.Command{
 			daemonPort = port
 		}
 
+		var inBulk bool
+
 		// Validate username and network name.
 		if netName := c.String("net"); govalidator.IsNull(netName) {
 			err := errors.EmptyValue("network", netName)
@@ -244,12 +253,17 @@ var netDissociateCommand = cli.Command{
 			return err
 		}
 
+		// Mark inBulk if username is set to asterisk.
+		if c.String("user") == "*" {
+			inBulk = true
+		}
+
 		// If dry run, then don't call the action, just preprocess.
 		if c.GlobalBool("dry-run") {
 			return nil
 		}
 
-		return netDissocAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("net"), c.String("user"))
+		return netDissocAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("net"), c.String("user"), inBulk)
 	},
 }