-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnet_ctl_delete.go
165 lines (158 loc) · 4.1 KB
/
gnet_ctl_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"net/http"
log "github.com/gopher-net/gnet-ctl/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"github.com/gopher-net/gnet-ctl/Godeps/_workspace/src/github.com/codegangsta/cli"
)
var GnetCtlDelete = cli.Command{
Name: "delete",
Usage: "gnet-ctl delete <option>",
Subcommands: []cli.Command{
{
Usage: "use 'gnet-ctl delete help' for subcommand usage",
Action: cli.ShowSubcommandHelp,
},
{
Name: "neighbor",
Usage: "Delete an existing BGP Neighbor from the neighbor list of the specified node: " +
"'gnet-ctl delete neighbor --neighbor-ip=<ip-address>' delete a bgp neighbor\n " +
"\t(Example): 'gnet-ctl delete neighbor --neighbor-ip=172.16.100.1'",
Flags: []cli.Flag{
NeighborIpFlag,
},
Action: DelNeighdor,
},
{
Name: "route",
Usage: "Delete an existing route advertisement from the specified node: " +
"'gnet-ctl delete route --prefix=<ip_network/prefix>' \n" +
"\t(Example): 'gnet-ctl delete route --nexthop=172.16.100.1'",
Flags: []cli.Flag{
RouteIpPrefix,
},
Action: DelRoute,
},
},
}
func DelNeighdor(c *cli.Context) {
client := NewClient()
bgpNeighbor := new(Neighbor)
rawIp := c.String("neighbor-ip")
var neighborIp net.IP
if rawIp != "" {
neighborIp = net.ParseIP(rawIp)
bgpNeighbor.NeighborIP = neighborIp
} else {
log.Error("a peer IP address is required to add a bgp neighbor")
return
}
j, err := json.Marshal(&bgpNeighbor)
if err != nil {
log.Println(err)
return
}
req, err := http.NewRequest("POST", "http://127.0.0.1:8080"+NEIGHBOR+DEL, bytes.NewBuffer(j))
if err != nil {
log.Errorf("No answer from the bgp daemon: %s \n", err)
log.Error(error(err))
return
}
parseFormErr := req.ParseForm()
if parseFormErr != nil {
log.Error(error(parseFormErr))
return
}
// Get Request
resp, err := client.Do(req)
if err != nil {
log.Errorf("No answer from the bgp daemon: %s \n", err)
return
}
// Read Response
respBody, e := ioutil.ReadAll(resp.Body)
if e != nil {
return
}
if resp.Status == "404 Not Found" {
log.Println("requested data type not supported")
}
// Display Results
log.Debugln("response Status : ", resp.Status)
log.Debugln("response Headers : ", resp.Header)
log.Debugln("response Body : ", string(respBody))
log.Infoln("results: ", string(respBody))
return
}
func DelRoute(c *cli.Context) {
var (
cidr *net.IPNet
err error
preflen uint8
mlen int
)
client := NewClient()
ipRoute := new(IpRoute)
// Parse cli flag input
strIp := c.String("prefix")
if strIp != "" {
cidr, err = GetCidr(strIp)
if err != nil {
log.Error("Error parsing a valid ip prefix.")
return
}
} else {
log.Error("a valid ip prefix or resolvable hostname is required")
return
}
// validate/type the provided ip via net.IP then cast back to string
if cidr.Mask != nil {
mlen, _ = cidr.Mask.Size()
}
preflen = uint8(mlen)
ipRoute.IpPrefix = cidr.IP.String()
ipRoute.PrefixMask = preflen
log.Debugf("sending route addition request for Prefix: %s/%d Nexthop: %s",
ipRoute.IpPrefix, ipRoute.PrefixMask, ipRoute.NextHop)
j, err := json.Marshal(&ipRoute)
if err != nil {
log.Println(err)
return
}
log.Debugf("marshalled route rest request: %s ", j)
// http post rout request
req, err := http.NewRequest("POST", "http://127.0.0.1:8080"+ROUTE_TABLES+DEL, bytes.NewBuffer(j))
if err != nil {
log.Errorf("no answer from the bgp daemon: %s \n", err)
log.Error(error(err))
return
}
parseFormErr := req.ParseForm()
if parseFormErr != nil {
log.Error(error(parseFormErr))
return
}
// Get Request
resp, err := client.Do(req)
if err != nil {
log.Errorf("no answer from the bgp daemon: %s \n", err)
return
}
// Read Response
respBody, e := ioutil.ReadAll(resp.Body)
if e != nil {
return
}
if resp.Status == "404 Not Found" {
log.Error("requested data type not supported")
}
// Display Results for debugging
log.Debugln("response Status : ", resp.Status)
log.Debugln("response Headers : ", resp.Header)
log.Debugln("response Body : ", string(respBody))
log.Infoln("results: ", string(respBody))
return
}