File: layer3.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 0.0~git20180917.45f1c769-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,768 kB
  • sloc: sh: 98; makefile: 14
file content (250 lines) | stat: -rw-r--r-- 7,111 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package layer3

import (
	"testing"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/acceptance/clients"
	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
	"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
)

// CreateFloatingIP creates a floating IP on a given network and port. An error
// will be returned if the creation failed.
func CreateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, networkID, portID string) (*floatingips.FloatingIP, error) {
	t.Logf("Attempting to create floating IP on port: %s", portID)

	createOpts := &floatingips.CreateOpts{
		FloatingNetworkID: networkID,
		PortID:            portID,
	}

	floatingIP, err := floatingips.Create(client, createOpts).Extract()
	if err != nil {
		return floatingIP, err
	}

	t.Logf("Created floating IP.")

	return floatingIP, err
}

// CreateExternalRouter creates a router on the external network. This requires
// the OS_EXTGW_ID environment variable to be set. An error is returned if the
// creation failed.
func CreateExternalRouter(t *testing.T, client *gophercloud.ServiceClient) (*routers.Router, error) {
	var router *routers.Router
	choices, err := clients.AcceptanceTestChoicesFromEnv()
	if err != nil {
		return router, err
	}

	routerName := tools.RandomString("TESTACC-", 8)

	t.Logf("Attempting to create external router: %s", routerName)

	adminStateUp := true
	enableSNAT := false
	gatewayInfo := routers.GatewayInfo{
		NetworkID:  choices.ExternalNetworkID,
		EnableSNAT: &enableSNAT,
	}

	createOpts := routers.CreateOpts{
		Name:         routerName,
		AdminStateUp: &adminStateUp,
		GatewayInfo:  &gatewayInfo,
	}

	router, err = routers.Create(client, createOpts).Extract()
	if err != nil {
		return router, err
	}

	if err := WaitForRouterToCreate(client, router.ID, 60); err != nil {
		return router, err
	}

	t.Logf("Created router: %s", routerName)

	return router, nil
}

// CreateRouter creates a router on a specified Network ID. An error will be
// returned if the creation failed.
func CreateRouter(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*routers.Router, error) {
	routerName := tools.RandomString("TESTACC-", 8)

	t.Logf("Attempting to create router: %s", routerName)

	adminStateUp := true
	gatewayInfo := routers.GatewayInfo{
		NetworkID: networkID,
	}

	createOpts := routers.CreateOpts{
		Name:         routerName,
		AdminStateUp: &adminStateUp,
		GatewayInfo:  &gatewayInfo,
	}

	router, err := routers.Create(client, createOpts).Extract()
	if err != nil {
		return router, err
	}

	if err := WaitForRouterToCreate(client, router.ID, 60); err != nil {
		return router, err
	}

	t.Logf("Created router: %s", routerName)

	return router, nil
}

// CreateRouterInterface will attach a subnet to a router. An error will be
// returned if the operation fails.
func CreateRouterInterface(t *testing.T, client *gophercloud.ServiceClient, portID, routerID string) (*routers.InterfaceInfo, error) {
	t.Logf("Attempting to add port %s to router %s", portID, routerID)

	aiOpts := routers.AddInterfaceOpts{
		PortID: portID,
	}

	iface, err := routers.AddInterface(client, routerID, aiOpts).Extract()
	if err != nil {
		return iface, err
	}

	if err := WaitForRouterInterfaceToAttach(client, portID, 60); err != nil {
		return iface, err
	}

	t.Logf("Successfully added port %s to router %s", portID, routerID)
	return iface, nil
}

// DeleteRouter deletes a router of a specified ID. A fatal error will occur
// if the deletion failed. This works best when used as a deferred function.
func DeleteRouter(t *testing.T, client *gophercloud.ServiceClient, routerID string) {
	t.Logf("Attempting to delete router: %s", routerID)

	err := routers.Delete(client, routerID).ExtractErr()
	if err != nil {
		t.Fatalf("Error deleting router: %v", err)
	}

	if err := WaitForRouterToDelete(client, routerID, 60); err != nil {
		t.Fatalf("Error waiting for router to delete: %v", err)
	}

	t.Logf("Deleted router: %s", routerID)
}

// DeleteRouterInterface will detach a subnet to a router. A fatal error will
// occur if the deletion failed. This works best when used as a deferred
// function.
func DeleteRouterInterface(t *testing.T, client *gophercloud.ServiceClient, portID, routerID string) {
	t.Logf("Attempting to detach port %s from router %s", portID, routerID)

	riOpts := routers.RemoveInterfaceOpts{
		PortID: portID,
	}

	_, err := routers.RemoveInterface(client, routerID, riOpts).Extract()
	if err != nil {
		t.Fatalf("Failed to detach port %s from router %s", portID, routerID)
	}

	if err := WaitForRouterInterfaceToDetach(client, portID, 60); err != nil {
		t.Fatalf("Failed to wait for port %s to detach from router %s", portID, routerID)
	}

	t.Logf("Successfully detached port %s from router %s", portID, routerID)
}

// DeleteFloatingIP deletes a floatingIP of a specified ID. A fatal error will
// occur if the deletion failed. This works best when used as a deferred
// function.
func DeleteFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIPID string) {
	t.Logf("Attempting to delete floating IP: %s", floatingIPID)

	err := floatingips.Delete(client, floatingIPID).ExtractErr()
	if err != nil {
		t.Fatalf("Failed to delete floating IP: %v", err)
	}

	t.Logf("Deleted floating IP: %s", floatingIPID)
}

func WaitForRouterToCreate(client *gophercloud.ServiceClient, routerID string, secs int) error {
	return gophercloud.WaitFor(secs, func() (bool, error) {
		r, err := routers.Get(client, routerID).Extract()
		if err != nil {
			return false, err
		}

		if r.Status == "ACTIVE" {
			return true, nil
		}

		return false, nil
	})
}

func WaitForRouterToDelete(client *gophercloud.ServiceClient, routerID string, secs int) error {
	return gophercloud.WaitFor(secs, func() (bool, error) {
		_, err := routers.Get(client, routerID).Extract()
		if err != nil {
			if _, ok := err.(gophercloud.ErrDefault404); ok {
				return true, nil
			}

			return false, err
		}

		return false, nil
	})
}

func WaitForRouterInterfaceToAttach(client *gophercloud.ServiceClient, routerInterfaceID string, secs int) error {
	return gophercloud.WaitFor(secs, func() (bool, error) {
		r, err := ports.Get(client, routerInterfaceID).Extract()
		if err != nil {
			return false, err
		}

		if r.Status == "ACTIVE" {
			return true, nil
		}

		return false, nil
	})
}

func WaitForRouterInterfaceToDetach(client *gophercloud.ServiceClient, routerInterfaceID string, secs int) error {
	return gophercloud.WaitFor(secs, func() (bool, error) {
		r, err := ports.Get(client, routerInterfaceID).Extract()
		if err != nil {
			if _, ok := err.(gophercloud.ErrDefault404); ok {
				return true, nil
			}

			if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
				if errCode.Actual == 409 {
					return false, nil
				}
			}

			return false, err
		}

		if r.Status == "ACTIVE" {
			return true, nil
		}

		return false, nil
	})
}