blob: 4f50739109327ec689fb1c9ec565110c0f46c877 [file] [log] [blame]
Jamie Hannafordc5afec42014-11-20 12:01:08 +01001// +build acceptance compute secgroups
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/acceptance/tools"
10 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
11 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
12 "github.com/rackspace/gophercloud/pagination"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func TestSecGroups(t *testing.T) {
17 client, err := newClient()
18 th.AssertNoErr(t, err)
19
20 serverID, needsDeletion := findServer(t, client)
21
22 groupID := createSecGroup(t, client)
23
24 listSecGroups(t, client)
25
26 newName := tools.RandomString("secgroup_", 5)
27 updateSecGroup(t, client, groupID, newName)
28
29 getSecGroup(t, client, groupID)
30
31 addRemoveRules(t, client, groupID)
32
33 addServerToSecGroup(t, client, serverID, newName)
34
35 removeServerFromSecGroup(t, client, serverID, newName)
36
37 if needsDeletion {
38 servers.Delete(client, serverID)
39 }
40
41 deleteSecGroup(t, client, groupID)
42}
43
Jamie Hannafordaea96c62014-11-25 12:05:04 +010044func createSecGroup(t *testing.T, client *gophercloud.ServiceClient) string {
Jamie Hannafordc5afec42014-11-20 12:01:08 +010045 opts := secgroups.CreateOpts{
46 Name: tools.RandomString("secgroup_", 5),
47 Description: "something",
48 }
49
50 group, err := secgroups.Create(client, opts).Extract()
51 th.AssertNoErr(t, err)
52
53 t.Logf("Created secgroup %s %s", group.ID, group.Name)
54
55 return group.ID
56}
57
58func listSecGroups(t *testing.T, client *gophercloud.ServiceClient) {
59 err := secgroups.List(client).EachPage(func(page pagination.Page) (bool, error) {
60 secGrpList, err := secgroups.ExtractSecurityGroups(page)
61 th.AssertNoErr(t, err)
62
63 for _, sg := range secGrpList {
64 t.Logf("Listing secgroup %s: Name [%s] Desc [%s] TenantID [%s]", sg.ID,
65 sg.Name, sg.Description, sg.TenantID)
66 }
67
68 return true, nil
69 })
70
71 th.AssertNoErr(t, err)
72}
73
Jamie Hannafordaea96c62014-11-25 12:05:04 +010074func updateSecGroup(t *testing.T, client *gophercloud.ServiceClient, id, newName string) {
Jamie Hannaford19460b22014-11-24 16:04:17 +010075 opts := secgroups.UpdateOpts{
76 Name: newName,
77 Description: tools.RandomString("dec_", 10),
78 }
Jamie Hannafordc5afec42014-11-20 12:01:08 +010079 group, err := secgroups.Update(client, id, opts).Extract()
80 th.AssertNoErr(t, err)
81
82 t.Logf("Updated %s's name to %s", group.ID, group.Name)
83}
84
Jamie Hannafordaea96c62014-11-25 12:05:04 +010085func getSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannafordc5afec42014-11-20 12:01:08 +010086 group, err := secgroups.Get(client, id).Extract()
87 th.AssertNoErr(t, err)
88
Jamie Hannafordaea96c62014-11-25 12:05:04 +010089 t.Logf("Getting %s: %#v", id, group)
Jamie Hannafordc5afec42014-11-20 12:01:08 +010090}
91
Jamie Hannafordaea96c62014-11-25 12:05:04 +010092func addRemoveRules(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannaford04abbc72014-11-21 11:27:57 +010093 opts := secgroups.CreateRuleOpts{
Jamie Hannafordc5afec42014-11-20 12:01:08 +010094 ParentGroupID: id,
95 FromPort: 22,
96 ToPort: 22,
97 IPProtocol: "TCP",
98 CIDR: "0.0.0.0/0",
99 }
100
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100101 rule, err := secgroups.CreateRule(client, opts).Extract()
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100102 th.AssertNoErr(t, err)
103
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100104 t.Logf("Adding rule %s to group %s", rule.ID, id)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100105
106 err = secgroups.DeleteRule(client, rule.ID).ExtractErr()
107 th.AssertNoErr(t, err)
108
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100109 t.Logf("Deleted rule %s from group %s", rule.ID, id)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100110}
111
112func findServer(t *testing.T, client *gophercloud.ServiceClient) (string, bool) {
113 var serverID string
114 var needsDeletion bool
115
116 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
117 sList, err := servers.ExtractServers(page)
118 th.AssertNoErr(t, err)
119
120 for _, s := range sList {
121 serverID = s.ID
122 needsDeletion = false
123
124 t.Logf("Found an existing server: ID [%s]", serverID)
125 break
126 }
127
128 return true, nil
129 })
130 th.AssertNoErr(t, err)
131
132 if serverID == "" {
133 t.Log("No server found, creating one")
134
135 choices, err := ComputeChoicesFromEnv()
136 th.AssertNoErr(t, err)
137
138 opts := &servers.CreateOpts{
139 Name: tools.RandomString("secgroup_test_", 5),
140 ImageRef: choices.ImageID,
141 FlavorRef: choices.FlavorID,
142 }
143
144 s, err := servers.Create(client, opts).Extract()
145 th.AssertNoErr(t, err)
146 serverID = s.ID
147
148 t.Logf("Created server %s, waiting for it to build", s.ID)
149 err = servers.WaitForStatus(client, serverID, "ACTIVE", 300)
150 th.AssertNoErr(t, err)
151
152 needsDeletion = true
153 }
154
155 return serverID, needsDeletion
156}
157
158func addServerToSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) {
159 err := secgroups.AddServerToGroup(client, serverID, groupName).ExtractErr()
160 th.AssertNoErr(t, err)
161
162 t.Logf("Adding group %s to server %s", groupName, serverID)
163}
164
165func removeServerFromSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) {
166 err := secgroups.RemoveServerFromGroup(client, serverID, groupName).ExtractErr()
167 th.AssertNoErr(t, err)
168
169 t.Logf("Removing group %s from server %s", groupName, serverID)
170}
171
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100172func deleteSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100173 err := secgroups.Delete(client, id).ExtractErr()
174 th.AssertNoErr(t, err)
175
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100176 t.Logf("Deleted group %s", id)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100177}