Skip to content

Commit

Permalink
Gnocchi: implement archive policy Update method
Browse files Browse the repository at this point in the history
Add support for updating Gnocchi archive policies.
Add unit test and example in documentation.
  • Loading branch information
ozerovandrei committed Jun 10, 2018
1 parent 5e68bc5 commit 558e4c0
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
19 changes: 19 additions & 0 deletions gnocchi/metric/v1/archivepolicies/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,24 @@ Example of Creating an archive policy
if err != nil {
panic(err)
}
Example of Updating an archive policy
updateOpts := archivepolicies.UpdateOpts{
Definition: []archivepolicies.ArchivePolicyDefinitionOpts{
{
Granularity: "12:00:00",
TimeSpan: "30 days, 0:00:00",
},
{
Granularity: "1 day, 0:00:00",
TimeSpan: "90 days, 0:00:00",
},
},
}
archivePolicy, err := archivepolicies.Update(gnocchiClient, "test_policy", updateOpts).Extract()
if err != nil {
panic(err)
}
*/
package archivepolicies
31 changes: 31 additions & 0 deletions gnocchi/metric/v1/archivepolicies/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,34 @@ func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r Create

return
}

// UpdateOptsBuilder allows extensions to add additional parameters to the Update request.
type UpdateOptsBuilder interface {
ToArchivePolicyUpdateMap() (map[string]interface{}, error)
}

// UpdateOpts represents options used to update an archive policy.
type UpdateOpts struct {
// Definition is a list of parameters that configures
// archive policy precision and timespan.
Definition []ArchivePolicyDefinitionOpts `json:"definition"`
}

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

// Update accepts a UpdateOpts and updates an existing Gnocchi archive policy using the values provided.
func Update(client *gophercloud.ServiceClient, archivePolicyName string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToArchivePolicyUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Patch(updateURL(client, archivePolicyName), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})

return
}
6 changes: 6 additions & 0 deletions gnocchi/metric/v1/archivepolicies/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type CreateResult struct {
commonResult
}

// UpdateResult represents the result of an update operation. Call its Extract
// method to interpret it as a Gnocchi archive policy.
type UpdateResult struct {
commonResult
}

// ArchivePolicy represents a Gnocchi archive policy.
// Archive policy is an aggregate storage policy attached to a metric.
// It determines how long aggregates will be kept in a metric and how they will be aggregated.
Expand Down
39 changes: 39 additions & 0 deletions gnocchi/metric/v1/archivepolicies/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,42 @@ const ArchivePolicyCreateResponse = `
"name": "test_policy"
}
`

// ArchivePolicyUpdateRequest represents a raw update request.
const ArchivePolicyUpdateRequest = `
{
"definition": [
{
"timespan": "30 days, 0:00:00",
"granularity": "12:00:00"
},
{
"timespan": "90 days, 0:00:00",
"granularity": "1 day, 0:00:00"
}
]
}
`

// ArchivePolicyUpdateResponse represents a raw server response from a server to an update request.
const ArchivePolicyUpdateResponse = `
{
"definition": [
{
"points": 60,
"timespan": "30 days, 0:00:00",
"granularity": "12:00:00"
},
{
"points": 90,
"timespan": "90 days, 0:00:00",
"granularity": "1 day, 0:00:00"
}
],
"back_window": 0,
"name": "test_policy",
"aggregation_methods": [
"max"
]
}
`
51 changes: 51 additions & 0 deletions gnocchi/metric/v1/archivepolicies/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,54 @@ func TestCreate(t *testing.T) {
})
th.AssertEquals(t, s.Name, "test_policy")
}

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

th.Mux.HandleFunc("/v1/archive_policy/test_policy", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PATCH")
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, ArchivePolicyUpdateRequest)

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

fmt.Fprintf(w, ArchivePolicyUpdateResponse)
})

updateOpts := archivepolicies.UpdateOpts{
Definition: []archivepolicies.ArchivePolicyDefinitionOpts{
{
Granularity: "12:00:00",
TimeSpan: "30 days, 0:00:00",
},
{
Granularity: "1 day, 0:00:00",
TimeSpan: "90 days, 0:00:00",
},
},
}
s, err := archivepolicies.Update(fake.ServiceClient(), "test_policy", updateOpts).Extract()
th.AssertNoErr(t, err)

th.AssertDeepEquals(t, s.AggregationMethods, []string{
"max",
})
th.AssertEquals(t, s.BackWindow, 0)
th.AssertDeepEquals(t, s.Definition, []archivepolicies.ArchivePolicyDefinition{
{
Granularity: "12:00:00",
Points: 60,
TimeSpan: "30 days, 0:00:00",
},
{
Granularity: "1 day, 0:00:00",
Points: 90,
TimeSpan: "90 days, 0:00:00",
},
})
th.AssertEquals(t, s.Name, "test_policy")
}
4 changes: 4 additions & 0 deletions gnocchi/metric/v1/archivepolicies/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ func getURL(c *gophercloud.ServiceClient, archivePolicyName string) string {
func createURL(c *gophercloud.ServiceClient) string {
return rootURL(c)
}

func updateURL(c *gophercloud.ServiceClient, archivePolicyName string) string {
return resourceURL(c, archivePolicyName)
}

0 comments on commit 558e4c0

Please sign in to comment.