Skip to content

Commit c268ac0

Browse files
Jscott377kayrus
authored andcommitted
Added address groups to Networking extensions, with tests. Field added to SecGroupRules
1 parent ac09823 commit c268ac0

10 files changed

Lines changed: 915 additions & 19 deletions

File tree

internal/acceptance/openstack/networking/v2/extensions/extensions.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/gophercloud/gophercloud/v2"
88
"github.com/gophercloud/gophercloud/v2/internal/acceptance/tools"
99
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/external"
10+
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/addressgroups"
1011
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/groups"
1112
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/rules"
1213
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/networks"
@@ -199,3 +200,45 @@ func DeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, ru
199200
t.Fatalf("Unable to delete security group rule: %v", err)
200201
}
201202
}
203+
204+
// CreateSecurityAddressGroup will create a security address group with a random name.
205+
func CreateSecurityAddressGroup(t *testing.T, client *gophercloud.ServiceClient) (*addressgroups.AddressGroup, error) {
206+
addressGroupName := tools.RandomString("TESTACC-", 8)
207+
addressGroupDescription := tools.RandomString("TESTACC-DESC-", 8)
208+
209+
t.Logf("Attempting to create security address group: %s", addressGroupName)
210+
211+
addresses := []string{
212+
"192.168.1.1/32",
213+
}
214+
createOpts := addressgroups.CreateOpts{
215+
Name: addressGroupName,
216+
Description: addressGroupDescription,
217+
Addresses: addresses,
218+
}
219+
220+
addressGroup, err := addressgroups.Create(context.TODO(), client, createOpts).Extract()
221+
if err != nil {
222+
return addressGroup, err
223+
}
224+
225+
t.Logf("Created security address group: %s", addressGroup.ID)
226+
227+
th.AssertEquals(t, addressGroupName, addressGroup.Name)
228+
th.AssertEquals(t, addressGroupDescription, addressGroup.Description)
229+
th.AssertDeepEquals(t, addresses, addressGroup.Addresses)
230+
231+
return addressGroup, nil
232+
}
233+
234+
// DeleteSecurityAddressGroup will delete a security address group of a specified ID.
235+
// A fatal error will occur if the deletion failed. This works best as a
236+
// deferred function
237+
func DeleteSecurityAddressGroup(t *testing.T, client *gophercloud.ServiceClient, addressGroupID string) {
238+
t.Logf("Attempting to delete security address group: %s", addressGroupID)
239+
240+
err := addressgroups.Delete(context.TODO(), client, addressGroupID).ExtractErr()
241+
if err != nil {
242+
t.Fatalf("Unable to delete security address group: %v", err)
243+
}
244+
}

internal/acceptance/openstack/networking/v2/extensions/security_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/gophercloud/gophercloud/v2/internal/acceptance/clients"
1111
networking "github.com/gophercloud/gophercloud/v2/internal/acceptance/openstack/networking/v2"
1212
"github.com/gophercloud/gophercloud/v2/internal/acceptance/tools"
13+
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/addressgroups"
1314
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/groups"
1415
th "github.com/gophercloud/gophercloud/v2/testhelper"
1516
)
@@ -156,3 +157,79 @@ func TestSecurityGroupsRevision(t *testing.T) {
156157
th.AssertEquals(t, group.Name, newName)
157158
th.AssertEquals(t, group.Description, newDescription)
158159
}
160+
161+
func TestSecurityAddressGroups(t *testing.T) {
162+
client, err := clients.NewNetworkV2Client()
163+
th.AssertNoErr(t, err)
164+
165+
group, err := CreateSecurityAddressGroup(t, client)
166+
th.AssertNoErr(t, err)
167+
defer DeleteSecurityAddressGroup(t, client, group.ID)
168+
169+
tools.PrintResource(t, group)
170+
171+
name := "Update group"
172+
description := ""
173+
updateOpts := addressgroups.UpdateOpts{
174+
Name: &name,
175+
Description: &description,
176+
}
177+
newGroup, err := addressgroups.Update(context.TODO(), client, group.ID, updateOpts).Extract()
178+
th.AssertNoErr(t, err)
179+
tools.PrintResource(t, newGroup)
180+
181+
th.AssertEquals(t, newGroup.Name, name)
182+
th.AssertEquals(t, newGroup.Description, description)
183+
184+
listOpts := addressgroups.ListOpts{}
185+
allPages, err := addressgroups.List(client, listOpts).AllPages(context.TODO())
186+
th.AssertNoErr(t, err)
187+
allGroups, err := addressgroups.ExtractGroups(allPages)
188+
th.AssertNoErr(t, err)
189+
190+
var found = -1
191+
for i, v := range allGroups {
192+
if v.ID == group.ID {
193+
found = i
194+
break
195+
}
196+
}
197+
if found == -1 {
198+
t.Fatalf("Expected to find group %s in the list of groups", group.ID)
199+
}
200+
201+
th.AssertEquals(t, allGroups[found].Name, newGroup.Name)
202+
th.AssertEquals(t, allGroups[found].Description, newGroup.Description)
203+
th.AssertDeepEquals(t, allGroups[found].Addresses, newGroup.Addresses)
204+
205+
// Test that we can add a new address to the group.
206+
newAddresses := []string{
207+
"192.168.170.0/24",
208+
}
209+
addAddressOpts := addressgroups.UpdateAddressesOpts{
210+
Addresses: newAddresses,
211+
}
212+
updatedGroup, err := addressgroups.AddAddresses(context.TODO(), client, group.ID, addAddressOpts).Extract()
213+
th.AssertNoErr(t, err)
214+
tools.PrintResource(t, updatedGroup)
215+
216+
// Check that the new address was added.
217+
expectedAddresses := append(group.Addresses, newAddresses...)
218+
th.AssertDeepEquals(t, updatedGroup.Addresses, expectedAddresses)
219+
220+
// Test that we can remove an address from the group.
221+
removeAddressOpts := addressgroups.UpdateAddressesOpts{
222+
Addresses: newAddresses,
223+
}
224+
updatedGroup, err = addressgroups.RemoveAddresses(context.TODO(), client, group.ID, removeAddressOpts).Extract()
225+
th.AssertNoErr(t, err)
226+
tools.PrintResource(t, updatedGroup)
227+
228+
// Check that the address was removed.
229+
expectedAddresses = group.Addresses
230+
th.AssertDeepEquals(t, updatedGroup.Addresses, expectedAddresses)
231+
232+
// Verify that the group exists.
233+
_, err = addressgroups.Get(context.TODO(), client, group.ID).Extract()
234+
th.AssertNoErr(t, err)
235+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Package addressgroups provides information and interaction with Address Groups
3+
for the OpenStack Networking services.
4+
5+
Example to List Address Groups
6+
7+
listOpts := addressgroups.ListOpts{
8+
}
9+
10+
allPages, err := addressgroups.List(networkClient, listOpts).AllPages(context.TODO())
11+
if err != nil {
12+
panic(err)
13+
}
14+
15+
allAddressGroups, err := addressgroups.ExtractGroups(allPages)
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
for _, addressGroup := range allAddressGroups {
21+
fmt.Printf("%+v\n", addressGroup)
22+
}
23+
24+
Example to Get an Address Group
25+
26+
groupID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
27+
addressGroup, err := addressgroups.Get(context.TODO(), networkClient, groupID).Extract()
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
Example to Create an Address Group
33+
34+
createOpts := addressgroups.CreateOpts{
35+
Name: "addressGroupName",
36+
Addresses: []string{"10.2.30.4/32", "10.2.30.6/32"},
37+
Description: "Created address group",
38+
}
39+
40+
addressGroup, err := addressgroups.Create(context.TODO(), networkClient, createOpts).Extract()
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
Example to Delete a Address Group
46+
47+
groupID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
48+
err := addressgroups.Delete(context.TODO(), computeClient, groupID).ExtractErr()
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
Example to update an existing Address Group
54+
55+
groupID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
56+
name := "ADDR_GP_2"
57+
description := "new description"
58+
updateOpts := addressgroups.UpdateOpts{
59+
Name: &name,
60+
Description: &description,
61+
}
62+
addressGroup, err := addressgroups.UpdateAddressGroup(context.TODO(), networkClient, groupID, createOpts).Extract()
63+
if err != nil {
64+
panic(err)
65+
}
66+
67+
Example to add addresses to an existing Address Group
68+
69+
groupID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
70+
createOpts := addressgroups.UpdateAddressesOpts{
71+
Addresses: []string{"10.2.30.4/32", "10.2.30.6/32"},
72+
}
73+
addressGroup, err := addressgroups.AddAddresses(context.TODO(), networkClient, groupID, createOpts).Extract()
74+
if err != nil {
75+
panic(err)
76+
}
77+
78+
Example to remove addresses from an existing Address Group
79+
80+
groupID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
81+
createOpts := addressgroups.UpdateAddressesOpts{
82+
Addresses: []string{"10.2.30.4/32", "10.2.30.6/32"},
83+
}
84+
addressGroup, err := addressgroups.RemoveAddresses(context.TODO(), networkClient, groupID, createOpts).Extract()
85+
if err != nil {
86+
panic(err)
87+
}
88+
89+
*/
90+
91+
package addressgroups

0 commit comments

Comments
 (0)