Skip to content

Instantly share code, notes, and snippets.

@ishidawataru
Last active October 19, 2016 20:02
Show Gist options
  • Save ishidawataru/26daf32b7badafbab7c9 to your computer and use it in GitHub Desktop.
Save ishidawataru/26daf32b7badafbab7c9 to your computer and use it in GitHub Desktop.
show how to configure global policy via gRPC api
package main
import (
gobgp "github.com/osrg/gobgp/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"time"
)
func main() {
timeout := grpc.WithTimeout(time.Second)
conn, _ := grpc.Dial("127.0.0.1:8080", timeout, grpc.WithBlock(), grpc.WithInsecure())
client := gobgp.NewGobgpApiClient(conn)
client.ModDefinedSet(context.Background(), &gobgp.ModDefinedSetArguments{
Operation: gobgp.Operation_ADD,
Set: &gobgp.DefinedSet{
Type: gobgp.DefinedType_NEIGHBOR,
Name: "ns0",
// neighbor's ip address
List: []string{"10.0.0.1"},
},
})
stmt := &gobgp.Statement{
Conditions: &gobgp.Conditions{
NeighborSet: &gobgp.MatchSet{
Type: gobgp.MatchType_ANY,
Name: "ns0",
},
},
Actions: &gobgp.Actions{
Community: &gobgp.CommunityAction{
Type: gobgp.CommunityActionType_COMMUNITY_ADD,
Communities: []string{"100:100"},
},
},
}
policy := &gobgp.Policy{
Name: "policy0",
Statements: []*gobgp.Statement{stmt},
}
client.ModPolicy(context.Background(), &gobgp.ModPolicyArguments{
Operation: gobgp.Operation_ADD,
Policy: policy,
})
client.ModPolicyAssignment(context.Background(), &gobgp.ModPolicyAssignmentArguments{
Operation: gobgp.Operation_ADD,
Assignment: &gobgp.PolicyAssignment{
Type: gobgp.PolicyType_EXPORT,
Resource: gobgp.Resource_GLOBAL,
Policies: []*gobgp.Policy{policy},
Default: gobgp.RouteAction_ACCEPT,
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment