github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/csbs/v1/policies/List.go (about) 1 package policies 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" 6 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 7 ) 8 9 type ListOpts struct { 10 // Exact matching based on field name 11 Name string `q:"name"` 12 // The value of sort is a group of properties separated by commas (,) and sorting directions. 13 // The value format is <key1>[:<direction>],<key2>[:<direction>], where the value of direction is asc 14 // (in ascending order) or desc (in descending order). If the parameter direction is not specified, 15 // backup policies are sorted in descending order by time. The value of sort contains a maximum of 255 characters. 16 Sort string `q:"sort"` 17 // Number of resources displayed per page. The value must be a positive integer. The value defaults to 1000. 18 Limit int `q:"limit"` 19 // ID of the last record displayed on the previous page when pagination query is applied 20 Marker string `q:"marker"` 21 // Offset value, which is a positive integer. 22 Offset int `q:"offset"` 23 // Whether backup policies of all tenants can be queried 24 // This parameter is only available for administrators. 25 AllTenants string `q:"all_tenants"` 26 } 27 28 // List returns a Pager which allows you to iterate over a collection of 29 // backup policies. It accepts a ListOpts struct, which allows you to 30 // filter the returned collection for greater efficiency. 31 func List(client *golangsdk.ServiceClient, opts ListOpts) ([]BackupPolicy, error) { 32 url, err := golangsdk.NewURLBuilder().WithEndpoints("policies").WithQueryParams(&opts).Build() 33 if err != nil { 34 return nil, err 35 } 36 37 // GET https://{endpoint}/v1/{project_id}/policies 38 pages, err := pagination.NewPager(client, client.ServiceURL(url.String()), 39 func(r pagination.PageResult) pagination.Page { 40 return BackupPolicyPage{pagination.LinkedPageBase{PageResult: r}} 41 }).AllPages() 42 if err != nil { 43 return nil, err 44 } 45 46 policies, err := ExtractBackupPolicies(pages) 47 return policies, err 48 } 49 50 // BackupPolicyPage is the page returned by a pager when traversing over a 51 // collection of backup policies. 52 type BackupPolicyPage struct { 53 pagination.LinkedPageBase 54 } 55 56 // NextPageURL is invoked when a paginated collection of backup policies has reached 57 // the end of a page and the pager seeks to traverse over a new one. In order 58 // to do this, it needs to construct the next page's URL. 59 func (r BackupPolicyPage) NextPageURL() (string, error) { 60 var res []golangsdk.Link 61 err := extract.IntoSlicePtr(r.BodyReader(), &res, "policies_links") 62 if err != nil { 63 return "", err 64 } 65 return golangsdk.ExtractNextURL(res) 66 } 67 68 // IsEmpty checks whether a BackupPolicyPage struct is empty. 69 func (r BackupPolicyPage) IsEmpty() (bool, error) { 70 is, err := ExtractBackupPolicies(r) 71 return len(is) == 0, err 72 } 73 74 // ExtractBackupPolicies accepts a Page struct, specifically a BackupPolicyPage struct, 75 // and extracts the elements into a slice of Policy structs. In other words, 76 // a generic collection is mapped into a relevant slice. 77 func ExtractBackupPolicies(r pagination.Page) ([]BackupPolicy, error) { 78 var res []BackupPolicy 79 err := extract.IntoSlicePtr(r.(BackupPolicyPage).BodyReader(), &res, "policies") 80 return res, err 81 }