github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/csbs/v1/backup/List.go (about)

     1  package backup
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     6  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     7  )
     8  
     9  // ListOpts allows the filtering and sorting of paginated collections through
    10  // the API. Filtering is achieved by passing in struct field values that map to
    11  // the attributes you want to see returned. Marker and Limit are used for pagination.
    12  type ListOpts struct {
    13  	// Query based on field status is supported.
    14  	// Value range: waiting_protect, protecting, available, waiting_restore, restoring, error, waiting_delete, deleting, and deleted
    15  	Status string `q:"status"`
    16  	// Number of resources displayed per page. The value must be a positive integer. The value defaults to 1000.
    17  	Limit string `q:"limit"`
    18  	// ID of the last record displayed on the previous page
    19  	Marker string `q:"marker"`
    20  	// A group of properties separated by commas (,) and sorting directions. The value format is <key1>[:<direction>],
    21  	// <key2>[:<direction>], where the value of direction is asc (in ascending order) or desc (in descending order).
    22  	// If the parameter direction is not specified, the default sorting direction is desc.
    23  	// The value of sort contains a maximum of 255 characters. Enumeration values of the key are as follows:
    24  	// created_at, updated_at, name, status, protected_at, and id.
    25  	Sort string `q:"sort"`
    26  	// Whether to query the backup of all tenants. Only administrators can query the backup of all tenants.
    27  	AllTenants string `q:"all_tenants"`
    28  	// Fuzzy search based on field name is supported.
    29  	Name string `q:"name"`
    30  	// Filtering based on the backup AZ is supported.
    31  	Az string `q:"az"`
    32  	// Filtering based on the backup object ID is supported.
    33  	ResourceId string `q:"resource_id"`
    34  	// Fuzzy search based on the backup object name is supported.
    35  	ResourceName string `q:"resource_name"`
    36  	// Filtering based on the backup start time is supported.
    37  	// For example: 2017-04-18T01:21:52.701973
    38  	StartTime string `q:"start_time"`
    39  	// Filtering based on the backup end time is supported.
    40  	// For example: 2017-04-18T01:21:52.701973
    41  	EndTime string `q:"end_time"`
    42  	// Supports filtering by image type, for example, backup.
    43  	ImageType string `q:"image_type"`
    44  	// Filtering based on policy_id is supported.
    45  	PolicyId string `q:"policy_id"`
    46  	// Offset value, which is a positive integer.
    47  	Offset string `q:"offset"`
    48  	// Filtering based on checkpoint_id is supported.
    49  	CheckpointId string `q:"checkpoint_id"`
    50  	// Type of the backup object. For example, OS::Nova::Server
    51  	ResourceType string `q:"resource_type"`
    52  	// IP address of the server.
    53  	VmIp string `q:"ip"`
    54  }
    55  
    56  // List returns collection of
    57  // backups. It accepts a ListOpts struct, which allows you to filter and sort
    58  // the returned collection for greater efficiency.
    59  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Backup, error) {
    60  	url, err := golangsdk.NewURLBuilder().WithEndpoints("checkpoint_items").WithQueryParams(&opts).Build()
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	// GET https://{endpoint}/v1/{project_id}/checkpoint_items
    66  	pages, err := pagination.NewPager(c, c.ServiceURL(url.String()),
    67  		func(r pagination.PageResult) pagination.Page {
    68  			return СsbsBackupPage{pagination.LinkedPageBase{PageResult: r}}
    69  		}).AllPages()
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	allBackups, err := ExtractBackups(pages)
    75  	return allBackups, err
    76  }
    77  
    78  // СsbsBackupPage is the page returned by a pager when traversing over a
    79  // collection of backups.
    80  type СsbsBackupPage struct {
    81  	pagination.LinkedPageBase
    82  }
    83  
    84  // NextPageURL is invoked when a paginated collection of backups has reached
    85  // the end of a page and the pager seeks to traverse over a new one. In order
    86  // to do this, it needs to construct the next page's URL.
    87  func (r СsbsBackupPage) NextPageURL() (string, error) {
    88  	var res []golangsdk.Link
    89  	err := extract.IntoSlicePtr(r.BodyReader(), &res, "checkpoint_items_links")
    90  	if err != nil {
    91  		return "", err
    92  	}
    93  	return golangsdk.ExtractNextURL(res)
    94  }
    95  
    96  // IsEmpty checks whether a СsbsBackupPage struct is empty.
    97  func (r СsbsBackupPage) IsEmpty() (bool, error) {
    98  	is, err := ExtractBackups(r)
    99  	return len(is) == 0, err
   100  }
   101  
   102  // ExtractBackups accepts a Page struct, specifically a СsbsBackupPage struct,
   103  // and extracts the elements into a slice of Backup structs. In other words,
   104  // a generic collection is mapped into a relevant slice.
   105  func ExtractBackups(r pagination.Page) ([]Backup, error) {
   106  	var res []Backup
   107  	err := extract.IntoSlicePtr(r.(СsbsBackupPage).BodyReader(), &res, "checkpoint_items")
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return res, nil
   112  }