Adding list by server operation
diff --git a/openstack/compute/v2/extensions/secgroups/fixtures.go b/openstack/compute/v2/extensions/secgroups/fixtures.go
index 3986eb0..168bbdd 100644
--- a/openstack/compute/v2/extensions/secgroups/fixtures.go
+++ b/openstack/compute/v2/extensions/secgroups/fixtures.go
@@ -11,6 +11,20 @@
const rootPath = "/os-security-groups"
+const listGroupsJSON = `
+{
+ "security_groups": [
+ {
+ "description": "default",
+ "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
+ "name": "default",
+ "rules": [],
+ "tenant_id": "openstack"
+ }
+ ]
+}
+`
+
func mockListGroupsResponse(t *testing.T) {
th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
@@ -19,19 +33,20 @@
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `
-{
- "security_groups": [
- {
- "description": "default",
- "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
- "name": "default",
- "rules": [],
- "tenant_id": "openstack"
- }
- ]
+ fmt.Fprintf(w, listGroupsJSON)
+ })
}
-`)
+
+func mockListGroupsByServerResponse(t *testing.T, serverID string) {
+ url := fmt.Sprintf("%s/servers/%s%s", rootPath, serverID, rootPath)
+ th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, listGroupsJSON)
})
}
diff --git a/openstack/compute/v2/extensions/secgroups/requests.go b/openstack/compute/v2/extensions/secgroups/requests.go
index 7b92606..0c14716 100644
--- a/openstack/compute/v2/extensions/secgroups/requests.go
+++ b/openstack/compute/v2/extensions/secgroups/requests.go
@@ -7,12 +7,20 @@
"github.com/rackspace/gophercloud/pagination"
)
-func List(client *gophercloud.ServiceClient) pagination.Pager {
+func commonList(client *gophercloud.ServiceClient, url string) pagination.Pager {
createPage := func(r pagination.PageResult) pagination.Page {
return SecurityGroupPage{pagination.SinglePageBase(r)}
}
- return pagination.NewPager(client, rootURL(client), createPage)
+ return pagination.NewPager(client, url, createPage)
+}
+
+func List(client *gophercloud.ServiceClient) pagination.Pager {
+ return commonList(client, rootURL(client))
+}
+
+func ListByServer(client *gophercloud.ServiceClient, serverID string) pagination.Pager {
+ return commonList(client, listByServerURL(client, serverID))
}
type CreateOpts struct {
diff --git a/openstack/compute/v2/extensions/secgroups/requests_test.go b/openstack/compute/v2/extensions/secgroups/requests_test.go
index c2a3e12..311fc4d 100644
--- a/openstack/compute/v2/extensions/secgroups/requests_test.go
+++ b/openstack/compute/v2/extensions/secgroups/requests_test.go
@@ -8,6 +8,8 @@
"github.com/rackspace/gophercloud/testhelper/client"
)
+const serverID = "{serverID}"
+
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
@@ -43,6 +45,41 @@
th.AssertEquals(t, 1, count)
}
+func TestListByServer(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockListGroupsByServerResponse(t, serverID)
+
+ count := 0
+
+ err := ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractSecurityGroups(page)
+ if err != nil {
+ t.Errorf("Failed to extract users: %v", err)
+ return false, err
+ }
+
+ expected := []SecurityGroup{
+ SecurityGroup{
+ ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
+ Description: "default",
+ Name: "default",
+ Rules: []Rule{},
+ TenantID: "openstack",
+ },
+ }
+
+ th.CheckDeepEquals(t, expected, actual)
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, 1, count)
+}
+
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
diff --git a/openstack/compute/v2/extensions/secgroups/urls.go b/openstack/compute/v2/extensions/secgroups/urls.go
index bce07d7..9c1859f 100644
--- a/openstack/compute/v2/extensions/secgroups/urls.go
+++ b/openstack/compute/v2/extensions/secgroups/urls.go
@@ -13,3 +13,7 @@
func rootURL(c *gophercloud.ServiceClient) string {
return c.ServiceURL(secgrouppath)
}
+
+func listByServerURL(c *gophercloud.ServiceClient, serverID string) string {
+ return c.ServiceURL(secgrouppath, "servers", serverID, secgrouppath)
+}