Skip to content

Commit

Permalink
feat(cmd): imp. bulk network assoc/dissoc w/ users
Browse files Browse the repository at this point in the history
  • Loading branch information
cad committed May 13, 2018
1 parent e8f5852 commit f8556d7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
64 changes: 49 additions & 15 deletions cmd/ovpm/action_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
18 changes: 16 additions & 2 deletions cmd/ovpm/cmd_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
},
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
},
}

Expand Down

0 comments on commit f8556d7

Please sign in to comment.