Skip to content

Commit 10c25a5

Browse files
authored
Merge pull request #21 from pescetto/add_tunnels
Add tunnels
2 parents 041f608 + 8653982 commit 10c25a5

File tree

4 files changed

+220
-5
lines changed

4 files changed

+220
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
*.iml
33
*.back
4+
*.swp

app.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ func (b *BigIP) CreateAppsvc01(p *Appsvc01) error {
130130
log.Printf("++++++ Here is what terraform is sending to bigip ................ : %+v ", p)
131131
err := b.post(p, uriMgmt, uriSha, uriAppsvcs, uriDecl)
132132
if err != nil {
133-
log.Println(" API call not successfull %v ", err)
133+
log.Println(" API call not successfull ", err)
134134
}
135135
return nil
136136
}
137137
func (b *BigIP) CreateAppsvc02(p *Appsvc02) error {
138138
log.Printf("++++++ Here is what terraform is sending to bigip ................ : %+v ", p)
139139
err := b.post(p, uriMgmt, uriSha, uriAppsvcs, uriDecl)
140140
if err != nil {
141-
log.Println(" API call not successfull %v ", err)
141+
log.Println(" API call not successfull ", err)
142142
}
143143
return nil
144144
}
@@ -152,17 +152,17 @@ func (b *BigIP) DeleteAppsvc02() error {
152152
func (b *BigIP) ModifyAppsvc01(p *Appsvc01) error {
153153
log.Printf("++++++ Here is what terraform is sending to bigip ................ : %+v ", p)
154154
err := b.patch(p, uriMgmt, uriSha, uriAppsvcs, uriDecl)
155-
log.Printf("value of p in modify +++++++++++++++", p)
155+
log.Println("value of p in modify +++++++++++++++", p)
156156
if err != nil {
157-
log.Println(" API call not successfull %v ", err)
157+
log.Println(" API call not successfull ", err)
158158
}
159159
return nil
160160
}
161161
func (b *BigIP) ModifyAppsvc02(p *Appsvc02) error {
162162
log.Printf("++++++ Here is what terraform is sending to bigip ................ : %+v ", p)
163163
err := b.patch(p, uriMgmt, uriSha, uriAppsvcs, uriDecl)
164164
if err != nil {
165-
log.Println(" API call not successfull %v ", err)
165+
log.Println(" API call not successfull ", err)
166166
}
167167
return nil
168168
}

net.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,42 @@ type RouteDomain struct {
163163
Vlans []string `json:"vlans,omitempty"`
164164
}
165165

166+
// Tunnels contains a list of tunnel objects on the BIG-IP system.
167+
type Tunnels struct {
168+
Tunnels []Tunnel `json:"items"`
169+
}
170+
171+
// Tunnel contains information on the tunnel.
172+
// https://devcentral.f5.com/wiki/iControlREST.APIRef_tm_net_tunnels_tunnel.ashx
173+
type Tunnel struct {
174+
Name string `json:"name,omitempty"`
175+
AppService string `json:"appService,omitempty"`
176+
AutoLasthop string `json:"autoLasthop,omitempty"`
177+
Description string `json:"description,omitempty"`
178+
IdleTimeout int `json:"idleTimeout,omitempty"`
179+
IfIndex int `json:"ifIndex,omitempty"`
180+
Key int `json:"key,omitempty"`
181+
LocalAddress string `json:"localAddress,omitempty"`
182+
Mode string `json:"mode,omitempty"`
183+
Mtu int `json:"mtu,omitempty"`
184+
Partition string `json:"partition,omitempty"`
185+
Profile string `json:"profile,omitempty"`
186+
RemoteAddress string `json:"remoteAddress,omitempty"`
187+
SecondaryAddress string `json:"secondaryAddress,omitempty"`
188+
Tos string `json:"tos,omitempty"`
189+
TrafficGroup string `json:"trafficGroup,omitempty"`
190+
Transparent string `json:"transparent,omitempty"`
191+
UsePmtu string `json:"usePmtu,omitempty"`
192+
}
193+
166194
const (
167195
uriNet = "net"
168196
uriInterface = "interface"
169197
uriSelf = "self"
170198
uriTrunk = "trunk"
199+
uriTunnels = "tunnels"
200+
uriTunnel = "tunnel"
201+
uriVxlan = "vxlan"
171202
uriVlan = "vlan"
172203
uriVlanInterfaces = "interfaces"
173204
uriRoute = "route"
@@ -448,3 +479,57 @@ func (b *BigIP) DeleteRouteDomain(name string) error {
448479
func (b *BigIP) ModifyRouteDomain(name string, config *RouteDomain) error {
449480
return b.put(config, uriNet, uriRouteDomain, name)
450481
}
482+
483+
// Tunnels returns a list of tunnels.
484+
func (b *BigIP) Tunnels() (*Tunnels, error) {
485+
var tunnels Tunnels
486+
err, _ := b.getForEntity(&tunnels, uriNet, uriTunnels, uriTunnel)
487+
if err != nil {
488+
return nil, err
489+
}
490+
491+
return &tunnels, nil
492+
}
493+
494+
// GetTunnel fetches the tunnel by it's name.
495+
func (b *BigIP) GetTunnel(name string) (*Tunnel, error) {
496+
var tunnel Tunnel
497+
values := []string{}
498+
regex := regexp.MustCompile(`^(\/.+\/)?(.+)`)
499+
match := regex.FindStringSubmatch(name)
500+
if match[1] == "" {
501+
values = append(values, "~Common~")
502+
}
503+
values = append(values, name)
504+
// Join the strings into one.
505+
result := strings.Join(values, "")
506+
err, ok := b.getForEntity(&tunnel, uriNet, uriTunnels, uriTunnel, result)
507+
if err != nil {
508+
return nil, err
509+
}
510+
if !ok {
511+
return nil, nil
512+
}
513+
514+
return &tunnel, nil
515+
}
516+
517+
// CreateTunnel adds a new tunnel to the BIG-IP system.
518+
func (b *BigIP) CreateTunnel(name, profile string) error {
519+
config := &Tunnel{
520+
Name: name,
521+
Profile: profile,
522+
}
523+
524+
return b.post(config, uriNet, uriTunnels, uriTunnel)
525+
}
526+
527+
// DeleteTunnel removes a tunnel.
528+
func (b *BigIP) DeleteTunnel(name string) error {
529+
return b.delete(uriNet, uriTunnels, uriTunnel, name)
530+
}
531+
532+
// ModifyTunnel allows you to change any attribute of a tunnel.
533+
func (b *BigIP) ModifyTunnel(name string, config *Tunnel) error {
534+
return b.put(config, uriNet, uriTunnels, uriTunnel, name)
535+
}

net_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,132 @@ func assertRestCall(s *NetTestSuite, method, path, body string) {
380380
assert.JSONEq(s.T(), body, s.LastRequestBody)
381381
}
382382
}
383+
384+
func (s *NetTestSuite) TestTunnels() {
385+
s.ResponseFunc = func(w http.ResponseWriter, r *http.Request) {
386+
w.Write([]byte(`{
387+
"kind": "tm:net:tunnels:tunnel:tunnelcollectionstate",
388+
"selfLink": "https://localhost/mgmt/tm/net/tunnels/tunnel?ver=13.1.1.2",
389+
"items": [
390+
{
391+
"kind": "tm:net:tunnels:tunnel:tunnelstate",
392+
"name": "http-tunnel",
393+
"partition": "Common",
394+
"fullPath": "/Common/http-tunnel",
395+
"generation": 1,
396+
"selfLink": "https://localhost/mgmt/tm/net/tunnels/tunnel/~Common~http-tunnel?ver=13.1.1.2",
397+
"autoLasthop": "default",
398+
"description": "Tunnel for http-explicit profile",
399+
"idleTimeout": 300,
400+
"ifIndex": 912,
401+
"key": 0,
402+
"localAddress": "any6",
403+
"mode": "bidirectional",
404+
"mtu": 0,
405+
"profile": "/Common/tcp-forward",
406+
"profileReference": {
407+
"link": "https://localhost/mgmt/tm/net/tunnels/tcp-forward/~Common~tcp-forward?ver=13.1.1.2"
408+
},
409+
"remoteAddress": "any6",
410+
"secondaryAddress": "any6",
411+
"tos": "preserve",
412+
"transparent": "disabled",
413+
"usePmtu": "enabled"
414+
},
415+
{
416+
"kind": "tm:net:tunnels:tunnel:tunnelstate",
417+
"name": "socks-tunnel",
418+
"partition": "Common",
419+
"fullPath": "/Common/socks-tunnel",
420+
"generation": 1,
421+
"selfLink": "https://localhost/mgmt/tm/net/tunnels/tunnel/~Common~socks-tunnel?ver=13.1.1.2",
422+
"autoLasthop": "default",
423+
"description": "Tunnel for socks profile",
424+
"idleTimeout": 300,
425+
"ifIndex": 928,
426+
"key": 0,
427+
"localAddress": "any6",
428+
"mode": "bidirectional",
429+
"mtu": 0,
430+
"profile": "/Common/tcp-forward",
431+
"profileReference": {
432+
"link": "https://localhost/mgmt/tm/net/tunnels/tcp-forward/~Common~tcp-forward?ver=13.1.1.2"
433+
},
434+
"remoteAddress": "any6",
435+
"secondaryAddress": "any6",
436+
"tos": "preserve",
437+
"transparent": "disabled",
438+
"usePmtu": "enabled"
439+
}
440+
]
441+
}`))
442+
}
443+
444+
tunnels, err := s.Client.Tunnels()
445+
446+
assert.Nil(s.T(), err)
447+
assertRestCall(s, "GET", "/mgmt/tm/net/tunnels/tunnel", "")
448+
assert.Equal(s.T(), 2, len(tunnels.Tunnels))
449+
assert.Equal(s.T(), "http-tunnel", tunnels.Tunnels[0].Name)
450+
assert.Equal(s.T(), "socks-tunnel", tunnels.Tunnels[1].Name)
451+
}
452+
453+
func (s *NetTestSuite) TestGetTunnel() {
454+
s.ResponseFunc = func(w http.ResponseWriter, r *http.Request) {
455+
w.Write([]byte(`{
456+
"autoLasthop": "default",
457+
"description": "Tunnel for http-explicit profile",
458+
"fullPath": "/Common/http-tunnel",
459+
"generation": 1,
460+
"idleTimeout": 300,
461+
"ifIndex": 112,
462+
"key": 0,
463+
"kind": "tm:net:tunnels:tunnel:tunnelstate",
464+
"localAddress": "any6",
465+
"mode": "bidirectional",
466+
"mtu": 0,
467+
"name": "http-tunnel",
468+
"partition": "Common",
469+
"profile": "/Common/tcp-forward",
470+
"profileReference": {
471+
"link": "https://localhost/mgmt/tm/net/tunnels/tcp-forward/~Common~tcp-forward?ver=14.1.0.3"
472+
},
473+
"remoteAddress": "any6",
474+
"secondaryAddress": "any6",
475+
"selfLink": "https://localhost/mgmt/tm/net/tunnels/tunnel/~Common~http-tunnel?ver=14.1.0.3",
476+
"tos": "preserve",
477+
"transparent": "disabled",
478+
"usePmtu": "enabled"
479+
}`))
480+
}
481+
482+
tunnel, err := s.Client.GetTunnel("http-tunnel")
483+
484+
assert.Nil(s.T(), err)
485+
assertRestCall(s, "GET", "/mgmt/tm/net/tunnels/tunnel/~Common~http-tunnel", "")
486+
assert.Equal(s.T(), "http-tunnel", tunnel.Name)
487+
assert.Equal(s.T(), "/Common/tcp-forward", tunnel.Profile)
488+
}
489+
490+
func (s *NetTestSuite) TestCreateTunnel() {
491+
err := s.Client.CreateTunnel("some-foo-tunnel", "/Common/some-foo-profile")
492+
493+
assert.Nil(s.T(), err)
494+
assertRestCall(s, "POST", "/mgmt/tm/net/tunnels/tunnel", `{"name":"some-foo-tunnel", "profile":"/Common/some-foo-profile"}`)
495+
}
496+
497+
func (s *NetTestSuite) TestDeleteTunnel() {
498+
err := s.Client.DeleteTunnel("some-foo-tunnel")
499+
500+
assert.Nil(s.T(), err)
501+
assertRestCall(s, "DELETE", "/mgmt/tm/net/tunnels/tunnel/some-foo-tunnel", "")
502+
}
503+
504+
func (s *NetTestSuite) TestModifyTunnel() {
505+
tunnel := &Tunnel{Transparent: "enabled"}
506+
507+
err := s.Client.ModifyTunnel("some-foo-tunnel", tunnel)
508+
509+
assert.Nil(s.T(), err)
510+
assertRestCall(s, "PUT", "/mgmt/tm/net/tunnels/tunnel/some-foo-tunnel", `{"transparent":"enabled"}`)
511+
}

0 commit comments

Comments
 (0)