Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 0b01aeb

Browse files
committed
feat: new CBR policy sdk supported
1 parent f1dcb14 commit 0b01aeb

File tree

5 files changed

+501
-0
lines changed

5 files changed

+501
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package policies
2+
3+
import (
4+
"github.com/huaweicloud/golangsdk"
5+
"github.com/huaweicloud/golangsdk/pagination"
6+
)
7+
8+
type CreateOpts struct {
9+
Name string `json:"name" required:"true"`
10+
OperationDefinition *PolicyODCreate `json:"operation_definition" required:"true"`
11+
OperationType string `json:"operation_type" required:"true"`
12+
Trigger *Trigger `json:"trigger" required:"true"`
13+
Enabled *bool `json:"enabled,omitempty"`
14+
}
15+
16+
// PolicyODCreate is policy operation definition
17+
type PolicyODCreate struct {
18+
DailyBackups int `json:"day_backups,omitempty"`
19+
WeekBackups int `json:"week_backups,omitempty"`
20+
YearBackups int `json:"year_backups,omitempty"`
21+
MonthBackups int `json:"month_backups,omitempty"`
22+
MaxBackups int `json:"max_backups,omitempty"`
23+
RetentionDurationDays int `json:"retention_duration_days,omitempty"`
24+
Timezone string `json:"timezone,omitempty"`
25+
EnableAcceleration bool `json:"enable_acceleration,omitempty"`
26+
DestinationProjectID string `json:"destination_project_id,omitempty"`
27+
DestinationRegion string `json:"destination_region,omitempty"`
28+
}
29+
30+
type TriggerProperties struct {
31+
Pattern []string `json:"pattern" required:"true"`
32+
}
33+
34+
type Trigger struct {
35+
Properties TriggerProperties `json:"properties" required:"true"`
36+
}
37+
38+
type CreateOptsBuilder interface {
39+
ToPolicyCreateMap() (map[string]interface{}, error)
40+
}
41+
42+
func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
43+
return golangsdk.BuildRequestBody(opts, "policy")
44+
}
45+
46+
//Create is a method by which to create function that create a CBR policy
47+
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
48+
reqBody, err := opts.ToPolicyCreateMap()
49+
if err != nil {
50+
r.Err = err
51+
return
52+
}
53+
_, err = client.Post(rootURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{
54+
OkCodes: []int{200},
55+
})
56+
r.Err = err
57+
return
58+
}
59+
60+
type ListOpts struct {
61+
OperationType string `q:"operation_type"`
62+
VaultID string `q:"vault_id"`
63+
}
64+
65+
type ListOptsBuilder interface {
66+
ToPolicyListQuery() (string, error)
67+
}
68+
69+
func (opts ListOpts) ToPolicyListQuery() (string, error) {
70+
q, err := golangsdk.BuildQueryString(opts)
71+
if err != nil {
72+
return "", err
73+
}
74+
return q.String(), err
75+
}
76+
77+
//List is a method to obtain the specified CBR policy according to the vault ID or operation type.
78+
//This method can also obtain all the CBR policies through the default parameter settings.
79+
func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
80+
url := rootURL(client)
81+
if opts != nil {
82+
query, err := opts.ToPolicyListQuery()
83+
if err != nil {
84+
return pagination.Pager{Err: err}
85+
}
86+
url += query
87+
}
88+
89+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
90+
return PolicyPage{pagination.SinglePageBase(r)}
91+
})
92+
}
93+
94+
//Get is a method to obtain the specified CBR policy according to the policy ID.
95+
func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
96+
_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
97+
return
98+
}
99+
100+
type UpdateOpts struct {
101+
Enabled *bool `json:"enabled,omitempty"`
102+
Name string `json:"name,omitempty"`
103+
OperationDefinition *PolicyODCreate `json:"operation_definition,omitempty"`
104+
Trigger *Trigger `json:"trigger,omitempty"`
105+
}
106+
107+
type UpdateOptsBuilder interface {
108+
ToPolicyUpdateMap() (map[string]interface{}, error)
109+
}
110+
111+
func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
112+
return golangsdk.BuildRequestBody(opts, "policy")
113+
}
114+
115+
//Delete is a method to update an existing CBR policy
116+
func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
117+
reqBody, err := opts.ToPolicyUpdateMap()
118+
if err != nil {
119+
r.Err = err
120+
return
121+
}
122+
_, err = client.Put(resourceURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
123+
OkCodes: []int{200},
124+
})
125+
r.Err = err
126+
return
127+
}
128+
129+
//Delete is a method to delete an existing CBR policy
130+
func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
131+
_, r.Err = client.Delete(resourceURL(client, id), nil)
132+
return
133+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package policies
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/huaweicloud/golangsdk"
7+
"github.com/huaweicloud/golangsdk/pagination"
8+
)
9+
10+
type commonResult struct {
11+
golangsdk.Result
12+
}
13+
14+
type CreateResult struct {
15+
commonResult
16+
}
17+
18+
type GetResult struct {
19+
commonResult
20+
}
21+
22+
type UpdateResult struct {
23+
commonResult
24+
}
25+
26+
type DeleteResult struct {
27+
golangsdk.ErrResult
28+
}
29+
30+
type Policy struct {
31+
ID string `json:"id"`
32+
Name string `json:"name"`
33+
Enabled bool `json:"enabled"`
34+
OperationDefinition *PolicyODCreate `json:"operation_definition"`
35+
OperationType string `json:"operation_type"`
36+
Trigger *PolicyTriggerResp `json:"trigger"`
37+
AssociatedVaults []PolicyAssociateVault `json:"associated_vaults"`
38+
}
39+
40+
type PolicyTriggerResp struct {
41+
TriggerID string `json:"id"`
42+
Name string `json:"name"`
43+
Type string `json:"type"`
44+
Properties PolicyTriggerPropertiesResp `json:"properties"`
45+
}
46+
47+
type PolicyTriggerPropertiesResp struct {
48+
Pattern []string `json:"pattern"`
49+
StartTime string `json:"start_time"`
50+
}
51+
52+
type PolicyAssociateVault struct {
53+
VaultID string `json:"vault_id"`
54+
DestinationVaultID string `json:"destination_vault_id"`
55+
}
56+
57+
func (r commonResult) Extract() (*Policy, error) {
58+
var s struct {
59+
Policy *Policy `json:"policy"`
60+
}
61+
if r.Err != nil {
62+
return nil, r.Err
63+
}
64+
err := r.ExtractInto(&s)
65+
if err != nil {
66+
return nil, fmt.Errorf("error extracting policy from create response: %s", err)
67+
}
68+
return s.Policy, err
69+
}
70+
71+
type PolicyPage struct {
72+
pagination.SinglePageBase
73+
}
74+
75+
func ExtractPolicies(r pagination.Page) ([]Policy, error) {
76+
var s []Policy
77+
err := r.(PolicyPage).Result.ExtractIntoSlicePtr(&s, "policies")
78+
if err != nil {
79+
return nil, err
80+
}
81+
return s, nil
82+
}

0 commit comments

Comments
 (0)