Add security group :zap:
diff --git a/openstack/compute/v2/extensions/secgroups/fixtures.go b/openstack/compute/v2/extensions/secgroups/fixtures.go
index d87e76b..3986eb0 100644
--- a/openstack/compute/v2/extensions/secgroups/fixtures.go
+++ b/openstack/compute/v2/extensions/secgroups/fixtures.go
@@ -9,8 +9,10 @@
fake "github.com/rackspace/gophercloud/testhelper/client"
)
+const rootPath = "/os-security-groups"
+
func mockListGroupsResponse(t *testing.T) {
- th.Mux.HandleFunc("/os-security-groups", func(w http.ResponseWriter, r *http.Request) {
+ th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
@@ -32,3 +34,34 @@
`)
})
}
+
+func mockCreateGroupResponse(t *testing.T) {
+ th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ th.TestJSONRequest(t, r, `
+{
+ "security_group": {
+ "name": "test",
+ "description": "something"
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "security_group": {
+ "description": "something",
+ "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
+ "name": "test",
+ "rules": [],
+ "tenant_id": "openstack"
+ }
+}
+`)
+ })
+}
diff --git a/openstack/compute/v2/extensions/secgroups/requests.go b/openstack/compute/v2/extensions/secgroups/requests.go
index a9b8f30..7b92606 100644
--- a/openstack/compute/v2/extensions/secgroups/requests.go
+++ b/openstack/compute/v2/extensions/secgroups/requests.go
@@ -1,6 +1,8 @@
package secgroups
import (
+ "github.com/racker/perigee"
+
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -12,3 +14,30 @@
return pagination.NewPager(client, rootURL(client), createPage)
}
+
+type CreateOpts struct {
+ // Optional - the name of your security group. If no value provided, null
+ // will be set.
+ Name string `json:"name,omitempty"`
+
+ // Optional - the description of your security group. If no value provided,
+ // null will be set.
+ Description string `json:"description,omitempty"`
+}
+
+func Create(client *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
+ var result CreateResult
+
+ reqBody := struct {
+ CreateOpts `json:"security_group"`
+ }{opts}
+
+ _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{
+ Results: &result.Body,
+ ReqBody: &reqBody,
+ MoreHeaders: client.AuthenticatedHeaders(),
+ OkCodes: []int{200},
+ })
+
+ return result
+}
diff --git a/openstack/compute/v2/extensions/secgroups/requests_test.go b/openstack/compute/v2/extensions/secgroups/requests_test.go
index a3b9ba0..c2a3e12 100644
--- a/openstack/compute/v2/extensions/secgroups/requests_test.go
+++ b/openstack/compute/v2/extensions/secgroups/requests_test.go
@@ -42,3 +42,27 @@
th.AssertNoErr(t, err)
th.AssertEquals(t, 1, count)
}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockCreateGroupResponse(t)
+
+ opts := CreateOpts{
+ Name: "test",
+ Description: "something",
+ }
+
+ group, err := Create(client.ServiceClient(), opts).Extract()
+ th.AssertNoErr(t, err)
+
+ expected := &SecurityGroup{
+ ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
+ Name: "test",
+ Description: "something",
+ TenantID: "openstack",
+ Rules: []Rule{},
+ }
+ th.AssertDeepEquals(t, expected, group)
+}
diff --git a/openstack/compute/v2/extensions/secgroups/results.go b/openstack/compute/v2/extensions/secgroups/results.go
index 8d9a4e9..8f5477e 100644
--- a/openstack/compute/v2/extensions/secgroups/results.go
+++ b/openstack/compute/v2/extensions/secgroups/results.go
@@ -3,6 +3,7 @@
import (
"github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -50,3 +51,25 @@
err := mapstructure.Decode(casted, &response)
return response.SecurityGroups, err
}
+
+type commonResult struct {
+ gophercloud.Result
+}
+
+type CreateResult struct {
+ commonResult
+}
+
+func (r commonResult) Extract() (*SecurityGroup, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var response struct {
+ SecurityGroup SecurityGroup `mapstructure:"security_group"`
+ }
+
+ err := mapstructure.Decode(r.Body, &response)
+
+ return &response.SecurityGroup, err
+}