|
| 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 | +} |
0 commit comments