Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 7cfd38c

Browse files
committed
Merge pull request #488 from trumant/allowed_address_pairs
Allowed address pairs support for Neutron Port
2 parents 680aa02 + 0c24665 commit 7cfd38c

File tree

4 files changed

+83
-21
lines changed

4 files changed

+83
-21
lines changed

acceptance/openstack/networking/v2/port_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,20 @@ func TestPortCRUD(t *testing.T) {
4545
th.AssertEquals(t, p.ID, portID)
4646

4747
// Update port
48-
p, err = ports.Update(Client, portID, ports.UpdateOpts{Name: "new_port_name"}).Extract()
48+
updateOpts := ports.UpdateOpts{
49+
Name: "new_port_name",
50+
AllowedAddressPairs: []ports.AddressPair{
51+
ports.AddressPair{IPAddress: "192.168.199.201"},
52+
},
53+
}
54+
p, err = ports.Update(Client, portID, updateOpts).Extract()
55+
4956
th.AssertNoErr(t, err)
5057
th.AssertEquals(t, p.Name, "new_port_name")
5158

59+
updatedPort, err := ports.Get(Client, portID).Extract()
60+
th.AssertEquals(t, updatedPort.AllowedAddressPairs[0].IPAddress, "192.168.199.201")
61+
5262
// Delete port
5363
res := ports.Delete(Client, portID)
5464
th.AssertNoErr(t, res.Err)
@@ -82,8 +92,8 @@ func listPorts(t *testing.T) {
8292
th.AssertNoErr(t, err)
8393

8494
for _, p := range portList {
85-
t.Logf("Port: ID [%s] Name [%s] Status [%s] MAC addr [%s] Fixed IPs [%#v] Security groups [%#v]",
86-
p.ID, p.Name, p.Status, p.MACAddress, p.FixedIPs, p.SecurityGroups)
95+
t.Logf("Port: ID [%s] Name [%s] Status [%s] MAC addr [%s] Fixed IPs [%#v] Security groups [%#v] Allowed Address Pairs [%#v]",
96+
p.ID, p.Name, p.Status, p.MACAddress, p.FixedIPs, p.SecurityGroups, p.AllowedAddressPairs)
8797
}
8898

8999
return true, nil
@@ -108,6 +118,9 @@ func createSubnet(networkID string) (string, error) {
108118
IPVersion: subnets.IPv4,
109119
Name: "my_subnet",
110120
EnableDHCP: subnets.Down,
121+
AllocationPools: []subnets.AllocationPool{
122+
subnets.AllocationPool{Start: "192.168.199.2", End: "192.168.199.200"},
123+
},
111124
}).Extract()
112125
return s.ID, err
113126
}

openstack/networking/v2/ports/requests.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ type CreateOptsBuilder interface {
9595

9696
// CreateOpts represents the attributes used when creating a new port.
9797
type CreateOpts struct {
98-
NetworkID string
99-
Name string
100-
AdminStateUp *bool
101-
MACAddress string
102-
FixedIPs interface{}
103-
DeviceID string
104-
DeviceOwner string
105-
TenantID string
106-
SecurityGroups []string
98+
NetworkID string
99+
Name string
100+
AdminStateUp *bool
101+
MACAddress string
102+
FixedIPs interface{}
103+
DeviceID string
104+
DeviceOwner string
105+
TenantID string
106+
SecurityGroups []string
107+
AllowedAddressPairs []AddressPair
107108
}
108109

109110
// ToPortCreateMap casts a CreateOpts struct to a map.
@@ -139,6 +140,9 @@ func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
139140
if opts.MACAddress != "" {
140141
p["mac_address"] = opts.MACAddress
141142
}
143+
if opts.AllowedAddressPairs != nil {
144+
p["allowed_address_pairs"] = opts.AllowedAddressPairs
145+
}
142146

143147
return map[string]interface{}{"port": p}, nil
144148
}
@@ -168,12 +172,13 @@ type UpdateOptsBuilder interface {
168172

169173
// UpdateOpts represents the attributes used when updating an existing port.
170174
type UpdateOpts struct {
171-
Name string
172-
AdminStateUp *bool
173-
FixedIPs interface{}
174-
DeviceID string
175-
DeviceOwner string
176-
SecurityGroups []string
175+
Name string
176+
AdminStateUp *bool
177+
FixedIPs interface{}
178+
DeviceID string
179+
DeviceOwner string
180+
SecurityGroups []string
181+
AllowedAddressPairs []AddressPair
177182
}
178183

179184
// ToPortUpdateMap casts an UpdateOpts struct to a map.
@@ -198,6 +203,9 @@ func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
198203
if opts.Name != "" {
199204
p["name"] = opts.Name
200205
}
206+
if opts.AllowedAddressPairs != nil {
207+
p["allowed_address_pairs"] = opts.AllowedAddressPairs
208+
}
201209

202210
return map[string]interface{}{"port": p}, nil
203211
}

openstack/networking/v2/ports/requests_test.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ func TestCreate(t *testing.T) {
164164
"ip_address": "10.0.0.2"
165165
}
166166
],
167-
"security_groups": ["foo"]
167+
"security_groups": ["foo"],
168+
"allowed_address_pairs": [
169+
{
170+
"ip_address": "10.0.0.4",
171+
"mac_address": "fa:16:3e:c9:cb:f0"
172+
}
173+
]
168174
}
169175
}
170176
`)
@@ -177,7 +183,6 @@ func TestCreate(t *testing.T) {
177183
"port": {
178184
"status": "DOWN",
179185
"name": "private-port",
180-
"allowed_address_pairs": [],
181186
"admin_state_up": true,
182187
"network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
183188
"tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa",
@@ -193,6 +198,12 @@ func TestCreate(t *testing.T) {
193198
"security_groups": [
194199
"f0ac4394-7e4a-4409-9701-ba8be283dbc3"
195200
],
201+
"allowed_address_pairs": [
202+
{
203+
"ip_address": "10.0.0.4",
204+
"mac_address": "fa:16:3e:c9:cb:f0"
205+
}
206+
],
196207
"device_id": ""
197208
}
198209
}
@@ -208,6 +219,9 @@ func TestCreate(t *testing.T) {
208219
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
209220
},
210221
SecurityGroups: []string{"foo"},
222+
AllowedAddressPairs: []AddressPair{
223+
AddressPair{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
224+
},
211225
}
212226
n, err := Create(fake.ServiceClient(), options).Extract()
213227
th.AssertNoErr(t, err)
@@ -224,6 +238,9 @@ func TestCreate(t *testing.T) {
224238
})
225239
th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
226240
th.AssertDeepEquals(t, n.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
241+
th.AssertDeepEquals(t, n.AllowedAddressPairs, []AddressPair{
242+
AddressPair{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
243+
})
227244
}
228245

229246
func TestRequiredCreateOpts(t *testing.T) {
@@ -251,6 +268,12 @@ func TestUpdate(t *testing.T) {
251268
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
252269
"ip_address": "10.0.0.3"
253270
}
271+
],
272+
"allowed_address_pairs": [
273+
{
274+
"ip_address": "10.0.0.4",
275+
"mac_address": "fa:16:3e:c9:cb:f0"
276+
}
254277
],
255278
"security_groups": [
256279
"f0ac4394-7e4a-4409-9701-ba8be283dbc3"
@@ -278,6 +301,12 @@ func TestUpdate(t *testing.T) {
278301
"ip_address": "10.0.0.3"
279302
}
280303
],
304+
"allowed_address_pairs": [
305+
{
306+
"ip_address": "10.0.0.4",
307+
"mac_address": "fa:16:3e:c9:cb:f0"
308+
}
309+
],
281310
"id": "65c0ee9f-d634-4522-8954-51021b570b0d",
282311
"security_groups": [
283312
"f0ac4394-7e4a-4409-9701-ba8be283dbc3"
@@ -294,6 +323,9 @@ func TestUpdate(t *testing.T) {
294323
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
295324
},
296325
SecurityGroups: []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"},
326+
AllowedAddressPairs: []AddressPair{
327+
AddressPair{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
328+
},
297329
}
298330

299331
s, err := Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
@@ -303,6 +335,9 @@ func TestUpdate(t *testing.T) {
303335
th.AssertDeepEquals(t, s.FixedIPs, []IP{
304336
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
305337
})
338+
th.AssertDeepEquals(t, s.AllowedAddressPairs, []AddressPair{
339+
AddressPair{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
340+
})
306341
th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
307342
}
308343

openstack/networking/v2/ports/results.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func (r commonResult) Extract() (*Port, error) {
1919
var res struct {
2020
Port *Port `json:"port"`
2121
}
22-
2322
err := mapstructure.Decode(r.Body, &res)
2423

2524
return res.Port, err
@@ -51,6 +50,11 @@ type IP struct {
5150
IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"`
5251
}
5352

53+
type AddressPair struct {
54+
IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"`
55+
MACAddress string `mapstructure:"mac_address" json:"mac_address,omitempty"`
56+
}
57+
5458
// Port represents a Neutron port. See package documentation for a top-level
5559
// description of what this is.
5660
type Port struct {
@@ -78,6 +82,8 @@ type Port struct {
7882
SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"`
7983
// Identifies the device (e.g., virtual server) using this port.
8084
DeviceID string `mapstructure:"device_id" json:"device_id"`
85+
// Identifies the list of IP addresses the port will recognize/accept
86+
AllowedAddressPairs []AddressPair `mapstructure:"allowed_address_pairs" json:"allowed_address_pairs"`
8187
}
8288

8389
// PortPage is the page returned by a pager when traversing over a collection

0 commit comments

Comments
 (0)