diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-10-18 20:37:36 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-10-20 10:33:10 +0900 |
commit | 95188d08e1f53b8393659a7fe8c0cb57799ce1d9 (patch) | |
tree | 28cdba61858fcb48e5172bd3cdf10c24c1403661 | |
parent | 9362bba919c03b2f193714b47a7432ab521ecf98 (diff) |
cli: support policy configuration
- create a policy
$ gobgp policy add p0
- add statements to a policy
$ gobgp policy add p0 stmt0 stmt1
- remove statements from a policy
$ gobgp policy del p0 stmt1
- set statements for a policy
$ gobgp policy set p0 stmt2 stmt3
- delete a policy
$ gobgp policy del
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | gobgp/cmd/policy.go | 150 |
1 files changed, 50 insertions, 100 deletions
diff --git a/gobgp/cmd/policy.go b/gobgp/cmd/policy.go index 69dff538..589ecac0 100644 --- a/gobgp/cmd/policy.go +++ b/gobgp/cmd/policy.go @@ -755,106 +755,6 @@ func parseActions() (*api.Actions, error) { return actions, nil } -func modPolicy(resource api.Resource, op api.Operation, data interface{}) error { - pd := &api.Policy{} - if resource != api.Resource_POLICY_ROUTEPOLICY { - co := &api.Conditions{} - switch resource { - case api.Resource_POLICY_PREFIX: - co.PrefixSet = data.(*api.MatchSet) - case api.Resource_POLICY_NEIGHBOR: - co.NeighborSet = data.(*api.MatchSet) - case api.Resource_POLICY_ASPATH: - co.AsPathSet = data.(*api.MatchSet) - case api.Resource_POLICY_COMMUNITY: - co.CommunitySet = data.(*api.MatchSet) - case api.Resource_POLICY_EXTCOMMUNITY: - co.ExtCommunitySet = data.(*api.MatchSet) - } - pd.Statements = []*api.Statement{{Conditions: co}} - } else { - pd = data.(*api.Policy) - } - arg := &api.PolicyArguments{ - Resource: resource, - Operation: op, - PolicyDefinition: pd, - } - stream, err := client.ModPolicyRoutePolicy(context.Background()) - if err != nil { - return err - } - err = stream.Send(arg) - if err != nil { - return err - } - stream.CloseSend() - - res, e := stream.Recv() - if e != nil { - return e - } - if res.Code != api.Error_SUCCESS { - return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg) - } - return nil -} - -func modPolicyRoutePolicy(modtype string, eArgs []string) error { - var operation api.Operation - pd := &api.Policy{} - if len(eArgs) > 0 { - pd.Name = eArgs[0] - } - - switch modtype { - case CMD_ADD: - if len(eArgs) != 2 { - return fmt.Errorf("usage: gobgp policy routepoilcy add <route policy name> <statement name>") - } - stmt := &api.Statement{ - Name: eArgs[1], - } - conditions, err := parseConditions() - if err != nil { - return err - } - actions, err := parseActions() - if err != nil { - return err - } - stmt.Conditions = conditions - stmt.Actions = actions - - pd.Statements = []*api.Statement{stmt} - operation = api.Operation_ADD - - case CMD_DEL: - if len(eArgs) == 0 { - return fmt.Errorf("usage: policy neighbor del <route policy name> [<statement name>]") - } else if len(eArgs) == 1 { - operation = api.Operation_DEL - } else if len(eArgs) == 2 { - stmt := &api.Statement{ - Name: eArgs[1], - } - pd.Statements = []*api.Statement{stmt} - operation = api.Operation_DEL - } - case CMD_ALL: - if len(eArgs) > 0 { - return fmt.Errorf("Argument can not be entered: %s", eArgs[0:]) - } - operation = api.Operation_DEL_ALL - default: - return fmt.Errorf("invalid modType %s", modtype) - } - if e := modPolicy(api.Resource_POLICY_ROUTEPOLICY, operation, pd); e != nil { - return e - } - return nil -} - func showStatement(args []string) error { m := []*api.Statement{} if len(args) > 0 { @@ -1188,6 +1088,42 @@ func modAction(name, op string, args []string) error { return err } +func modPolicy(modtype string, args []string) error { + if len(args) < 1 { + return fmt.Errorf("usage: gobgp policy %s <name> [<statement name>...]", modtype) + } + name := args[0] + args = args[1:] + var op api.Operation + switch modtype { + case CMD_ADD: + op = api.Operation_ADD + case CMD_DEL: + if len(args) < 1 { + op = api.Operation_DEL_ALL + } else { + op = api.Operation_DEL + } + case CMD_SET: + op = api.Operation_REPLACE + } + stmts := make([]*api.Statement, 0, len(args)) + for _, n := range args { + stmts = append(stmts, &api.Statement{Name: n}) + } + arg := &api.ModPolicyArguments{ + Operation: op, + Policy: &api.Policy{ + Name: name, + Statements: stmts, + }, + ReferExistingStatements: true, + PreserveStatements: true, + } + _, err := client.ModPolicy(context.Background(), arg) + return err +} + func NewPolicyCmd() *cobra.Command { policyCmd := &cobra.Command{ Use: CMD_POLICY, @@ -1285,5 +1221,19 @@ func NewPolicyCmd() *cobra.Command { } policyCmd.AddCommand(stmtCmd) + for _, v := range []string{CMD_ADD, CMD_DEL, CMD_SET} { + cmd := &cobra.Command{ + Use: v, + Run: func(c *cobra.Command, args []string) { + err := modPolicy(c.Use, args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, + } + policyCmd.AddCommand(cmd) + } + return policyCmd } |