github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/vbs/v2/policies/requests.go (about) 1 package policies 2 3 import ( 4 "reflect" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/pagination" 8 ) 9 10 // CreateOptsBuilder allows extensions to add additional parameters to the 11 // Create request. 12 type CreateOptsBuilder interface { 13 ToPolicyCreateMap() (map[string]interface{}, error) 14 } 15 16 // CreateOpts contains the options for create a Policy. This object is 17 // passed to Create(). For more information about these parameters, 18 // please refer to the Policy object, or the volume backup service API v2 19 // documentation 20 type CreateOpts struct { 21 //Backup policy name.It cannot start with default. 22 Name string `json:"backup_policy_name" required:"true"` 23 //Details about the scheduling policy 24 ScheduledPolicy ScheduledPolicy `json:"scheduled_policy" required:"true"` 25 // Tags to be configured for the backup policy 26 Tags []Tag `json:"tags,omitempty"` 27 } 28 29 // ScheduledPolicy defines the details about scheduling policy for create 30 type ScheduledPolicy struct { 31 //Start time of the backup job. 32 StartTime string `json:"start_time" required:"true"` 33 //Backup interval (1 to 14 days) 34 Frequency int `json:"frequency,omitempty"` 35 //Specifies on which days of each week backup jobs are ececuted. 36 WeekFrequency []string `json:"week_frequency,omitempty"` 37 //Number of retained backups, minimum 2. 38 RententionNum int `json:"rentention_num,omitempty"` 39 //Days of retained backups, minimum 2. 40 RententionDay int `json:"rentention_day,omitempty"` 41 //Whether to retain the first backup in the current month, possible values Y or N 42 RemainFirstBackup string `json:"remain_first_backup_of_curMonth" required:"true"` 43 //Backup policy status, ON or OFF 44 Status string `json:"status" required:"true"` 45 } 46 47 type Tag struct { 48 //Tag key. A tag key consists of up to 36 characters 49 Key string `json:"key" required:"true"` 50 //Tag value. A tag value consists of 0 to 43 characters 51 Value string `json:"value" required:"true"` 52 } 53 54 // ToPolicyCreateMap assembles a request body based on the contents of a 55 // CreateOpts. 56 func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) { 57 return golangsdk.BuildRequestBody(opts, "") 58 } 59 60 // Create will create a new Policy based on the values in CreateOpts. To extract 61 // the Policy object from the response, call the Extract method on the 62 // CreateResult. 63 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 64 b, err := opts.ToPolicyCreateMap() 65 if err != nil { 66 r.Err = err 67 return 68 } 69 _, r.Err = client.Post(commonURL(client), b, &r.Body, &golangsdk.RequestOpts{ 70 OkCodes: []int{200}, 71 }) 72 return 73 } 74 75 // UpdateOptsBuilder allows extensions to add additional parameters to the 76 // Update request. 77 type UpdateOptsBuilder interface { 78 ToPolicyUpdateMap() (map[string]interface{}, error) 79 } 80 81 // UpdateOpts contains the options for Update a Policy. 82 type UpdateOpts struct { 83 //Backup policy name.It cannot start with default. 84 Name string `json:"backup_policy_name,omitempty"` 85 //Details about the scheduling policy 86 ScheduledPolicy UpdateSchedule `json:"scheduled_policy,omitempty"` 87 } 88 89 // UpdateSchedule defiens the details about scheduling policy for update. 90 type UpdateSchedule struct { 91 //Start time of the backup job. 92 StartTime string `json:"start_time,omitempty"` 93 //Backup interval (1 to 14 days) 94 Frequency int `json:"frequency,omitempty"` 95 //Specifies on which days of each week backup jobs are ececuted. 96 WeekFrequency []string `json:"week_frequency,omitempty"` 97 //Number of retained backups, minimum 2. 98 RententionNum int `json:"rentention_num,omitempty"` 99 //Days of retained backups, minimum 2. 100 RententionDay int `json:"rentention_day,omitempty"` 101 //Number of retained backups, minimum 2. 102 RemainFirstBackup string `json:"remain_first_backup_of_curMonth,omitempty"` 103 //Backup policy status, ON or OFF 104 Status string `json:"status,omitempty"` 105 } 106 107 // ToPolicyUpdateMap assembles a request body based on the contents of a 108 // UpdateOpts. 109 func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) { 110 return golangsdk.BuildRequestBody(opts, "") 111 } 112 113 //Update will Update an existing backup Policy based on the values in UpdateOpts.To extract 114 // the Policy object from the response, call the Extract method on the 115 // UpdateResult. 116 func Update(c *golangsdk.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) { 117 b, err := opts.ToPolicyUpdateMap() 118 if err != nil { 119 r.Err = err 120 return 121 } 122 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 123 _, r.Err = c.Put(resourceURL(c, policyID), b, &r.Body, reqOpt) 124 return 125 } 126 127 //Delete will delete the specified backup policy. 128 func Delete(c *golangsdk.ServiceClient, policyID string) (r DeleteResult) { 129 _, r.Err = c.Delete(resourceURL(c, policyID), nil) 130 return 131 } 132 133 //ListOpts allows filtering policies 134 type ListOpts struct { 135 //Backup policy ID 136 ID string 137 //Backup policy name 138 Name string 139 //Backup policy status 140 Status string 141 } 142 143 // List returns a Pager which allows you to iterate over a collection of 144 // Policies. It accepts a ListOpts struct, which allows you to 145 // filter the returned collection for greater efficiency. 146 func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Policy, error) { 147 148 pages, err := pagination.NewPager(c, commonURL(c), func(r pagination.PageResult) pagination.Page { 149 return PolicyPage{pagination.LinkedPageBase{PageResult: r}} 150 }).AllPages() 151 if err != nil { 152 return nil, err 153 } 154 155 allPolicies, err := ExtractPolicies(pages) 156 if err != nil { 157 return nil, err 158 } 159 160 return FilterPolicies(allPolicies, opts) 161 } 162 163 func FilterPolicies(policies []Policy, opts ListOpts) ([]Policy, error) { 164 165 var refinedPolicies []Policy 166 var matched bool 167 168 m := map[string]FilterStruct{} 169 170 if opts.ID != "" { 171 m["ID"] = FilterStruct{Value: opts.ID} 172 } 173 if opts.Name != "" { 174 m["Name"] = FilterStruct{Value: opts.Name} 175 } 176 177 if opts.Status != "" { 178 m["Status"] = FilterStruct{Value: opts.Status, Driller: []string{"ScheduledPolicy"}} 179 } 180 181 if len(m) > 0 && len(policies) > 0 { 182 for _, policies := range policies { 183 matched = true 184 185 for key, value := range m { 186 if sVal := GetStructNestedField(&policies, key, value.Driller); !(sVal == value.Value) { 187 matched = false 188 } 189 } 190 if matched { 191 refinedPolicies = append(refinedPolicies, policies) 192 } 193 } 194 } else { 195 refinedPolicies = policies 196 } 197 return refinedPolicies, nil 198 } 199 200 func GetStructNestedField(v *Policy, field string, structDriller []string) string { 201 r := reflect.ValueOf(v) 202 for _, drillField := range structDriller { 203 f := reflect.Indirect(r).FieldByName(drillField).Interface() 204 r = reflect.ValueOf(f) 205 } 206 f1 := reflect.Indirect(r).FieldByName(field) 207 return string(f1.String()) 208 } 209 210 type FilterStruct struct { 211 Value string 212 Driller []string 213 } 214 215 // AssociateOptsBuilder allows extensions to add additional parameters to the 216 // Associate request. 217 type AssociateOptsBuilder interface { 218 ToPolicyAssociateMap() (map[string]interface{}, error) 219 } 220 221 // AssociateOpts contains the options to associate a resource to a Policy. 222 type AssociateOpts struct { 223 //Backup policy ID, to which the resource is to be associated. 224 PolicyID string `json:"backup_policy_id" required:"true"` 225 //Details about the resources to associate with the policy. 226 Resources []AssociateResource `json:"resources" required:"true"` 227 } 228 229 type AssociateResource struct { 230 //The ID of the resource to associate with policy. 231 ResourceID string `json:"resource_id" required:"true"` 232 //Type of the resource , e.g. volume. 233 ResourceType string `json:"resource_type" required:"true"` 234 } 235 236 // ToPolicyAssociateMap assembles a request body based on the contents of a 237 // AssociateOpts. 238 func (opts AssociateOpts) ToPolicyAssociateMap() (map[string]interface{}, error) { 239 return golangsdk.BuildRequestBody(opts, "") 240 } 241 242 // Associate will associate a resource tp a backup policy based on the values in AssociateOpts. To extract 243 // the associated resources from the response, call the ExtractResource method on the 244 // ResourceResult. 245 func Associate(client *golangsdk.ServiceClient, opts AssociateOpts) (r ResourceResult) { 246 b, err := opts.ToPolicyAssociateMap() 247 if err != nil { 248 r.Err = err 249 return 250 } 251 _, r.Err = client.Post(associateURL(client), b, &r.Body, &golangsdk.RequestOpts{ 252 OkCodes: []int{200}, 253 }) 254 return 255 } 256 257 // DisassociateOptsBuilder allows extensions to add additional parameters to the 258 // Disassociate request. 259 type DisassociateOptsBuilder interface { 260 ToPolicyDisassociateMap() (map[string]interface{}, error) 261 } 262 263 // DisassociateOpts contains the options disassociate a resource from a Policy. 264 type DisassociateOpts struct { 265 //Disassociate Resources 266 Resources []DisassociateResource `json:"resources" required:"true"` 267 } 268 269 type DisassociateResource struct { 270 //ResourceID 271 ResourceID string `json:"resource_id" required:"true"` 272 } 273 274 // ToPolicyDisassociateMap assembles a request body based on the contents of a 275 // DisassociateOpts. 276 func (opts DisassociateOpts) ToPolicyDisassociateMap() (map[string]interface{}, error) { 277 return golangsdk.BuildRequestBody(opts, "") 278 } 279 280 // Disassociate will disassociate a resource from a backup policy based on the values in DisassociateOpts. To extract 281 // the disassociated resources from the response, call the ExtractResource method on the 282 // ResourceResult. 283 func Disassociate(client *golangsdk.ServiceClient, policyID string, opts DisassociateOpts) (r ResourceResult) { 284 b, err := opts.ToPolicyDisassociateMap() 285 if err != nil { 286 r.Err = err 287 return 288 } 289 _, r.Err = client.Post(disassociateURL(client, policyID), b, &r.Body, &golangsdk.RequestOpts{ 290 OkCodes: []int{200}, 291 }) 292 return 293 }