Skip to content

Commit

Permalink
Networking V2: add QoS policy Create call
Browse files Browse the repository at this point in the history
Implement QoS policy Create function.

Add tests, docs and acceptance test helper.
  • Loading branch information
ozerovandrei committed Jun 2, 2019
1 parent 2d58570 commit ac621e5
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package policies

import (
"testing"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/policies"
th "github.com/gophercloud/gophercloud/testhelper"
)

// CreateQoSPolicy will create a QoS policy. An error will be returned if the
// QoS policy could not be created.
func CreateQoSPolicy(t *testing.T, client *gophercloud.ServiceClient) (*policies.Policy, error) {
policyName := tools.RandomString("TESTACC-", 8)
policyDescription := tools.RandomString("TESTACC-DESC-", 8)

createOpts := policies.CreateOpts{
Name: policyName,
Description: policyDescription,
}

t.Logf("Attempting to create a QoS policy: %s", policyName)

policy, err := policies.Create(client, createOpts).Extract()
if err != nil {
return nil, err
}

t.Logf("Succesfully created a QoS policy")

th.AssertEquals(t, policyName, policy.Name)
th.AssertEquals(t, policyDescription, policy.Description)

return policy, nil
}
15 changes: 15 additions & 0 deletions openstack/networking/v2/extensions/qos/policies/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@ Example to Get a specific QoS policy
panic(err)
}
fmt.Printf("%+v\n", policy)
Example to Create a QoS policy
opts := policies.CreateOpts{
Name: "shared-default-policy",
Shared: true,
IsDefault: true,
}
policy, err := policies.Create(networkClient, opts).Extract()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", policy)
*/
package policies
47 changes: 46 additions & 1 deletion openstack/networking/v2/extensions/qos/policies/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type PolicyListOptsBuilder interface {
// ListOpts allows the filtering and sorting of paginated collections through
// the Neutron API. Filtering is achieved by passing in struct field values
// that map to the Policy attributes you want to see returned.
// SortKey allows you to sort by a particular BandwidthLimitRule attribute.
// SortKey allows you to sort by a particular Policy attribute.
// SortDir sets the direction, and is either `asc' or `desc'.
// Marker and Limit are used for the pagination.
type ListOpts struct {
Expand Down Expand Up @@ -174,3 +174,48 @@ func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(getURL(c, id), &r.Body, nil)
return
}

// CreateOptsBuilder allows to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToPolicyCreateMap() (map[string]interface{}, error)
}

// CreateOpts specifies parameters of a new QoS policy.
type CreateOpts struct {
// Name is the human-readable name of the QoS policy.
Name string `json:"name"`

// TenantID is the id of the Identity project.
TenantID string `json:"tenant_id,omitempty"`

// ProjectID is the id of the Identity project.
ProjectID string `json:"project_id,omitempty"`

// Shared indicates whether this QoS policy is shared across all projects.
Shared bool `json:"shared,omitempty"`

// Description is the human-readable description for the QoS policy.
Description string `json:"description,omitempty"`

// IsDefault indicates if this QoS policy is default policy or not.
IsDefault bool `json:"is_default,omitempty"`
}

// ToPolicyCreateMap constructs a request body from CreateOpts.
func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "policy")
}

// Create requests the creation of a new QoS policy on the server.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToPolicyCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
return
}
6 changes: 6 additions & 0 deletions openstack/networking/v2/extensions/qos/policies/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type GetResult struct {
commonResult
}

// CreateResult represents the result of a Create operation. Call its Extract
// method to interpret it as a QoS policy.
type CreateResult struct {
commonResult
}

// Extract is a function that accepts a result and extracts a QoS policy resource.
func (r commonResult) Extract() (*Policy, error) {
var s struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,33 @@ const GetPolicyResponse = `
}
}
`

const CreatePolicyRequest = `
{
"policy": {
"name": "shared-default-policy",
"is_default": true,
"shared": true,
"description": "use-me"
}
}
`

const CreatePolicyResponse = `
{
"policy": {
"name": "shared-default-policy",
"tags": [],
"rules": [],
"tenant_id": "a77cbe0998374aed9a6798ad6c61677e",
"created_at": "2019-05-19T11:17:50Z",
"updated_at": "2019-05-19T11:17:57Z",
"is_default": true,
"revision_number": 0,
"shared": true,
"project_id": "a77cbe0998374aed9a6798ad6c61677e",
"id": "d6ae28ce-fcb5-4180-aa62-d260a27e09ae",
"description": "use-me"
}
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,41 @@ func TestGetPolicy(t *testing.T) {
th.AssertEquals(t, 1, p.RevisionNumber)
th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID)
}

func TestCreatePolicy(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()

th.Mux.HandleFunc("/v2.0/qos/policies", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, CreatePolicyRequest)

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)

fmt.Fprintf(w, CreatePolicyResponse)
})

opts := policies.CreateOpts{
Name: "shared-default-policy",
Shared: true,
IsDefault: true,
Description: "use-me",
}
p, err := policies.Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)

th.AssertEquals(t, "shared-default-policy", p.Name)
th.AssertEquals(t, true, p.Shared)
th.AssertEquals(t, true, p.IsDefault)
th.AssertEquals(t, "use-me", p.Description)
th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID)
th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID)
th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt)
th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 57, 0, time.UTC), p.UpdatedAt)
th.AssertEquals(t, 0, p.RevisionNumber)
th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID)
}
4 changes: 4 additions & 0 deletions openstack/networking/v2/extensions/qos/policies/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ func listURL(c *gophercloud.ServiceClient) string {
func getURL(c *gophercloud.ServiceClient, id string) string {
return resourceURL(c, id)
}

func createURL(c *gophercloud.ServiceClient) string {
return rootURL(c)
}

0 comments on commit ac621e5

Please sign in to comment.