github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/csbs/v1/backup/requests.go (about)

     1  package backup
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type ResourceTag struct {
     9  	Key   string `json:"key"`
    10  	Value string `json:"value"`
    11  }
    12  
    13  // ListOpts allows the filtering and sorting of paginated collections through
    14  // the API. Filtering is achieved by passing in struct field values that map to
    15  // the attributes you want to see returned. Marker and Limit are used for pagination.
    16  type ListOpts struct {
    17  	Status       string `q:"status"`
    18  	Limit        string `q:"limit"`
    19  	Marker       string `q:"marker"`
    20  	Sort         string `q:"sort"`
    21  	AllTenants   string `q:"all_tenants"`
    22  	Name         string `q:"name"`
    23  	ResourceId   string `q:"resource_id"`
    24  	ResourceName string `q:"resource_name"`
    25  	PolicyId     string `q:"policy_id"`
    26  	VmIp         string `q:"ip"`
    27  	CheckpointId string `q:"checkpoint_id"`
    28  	ID           string
    29  	ResourceType string `q:"resource_type"`
    30  }
    31  
    32  // List returns collection of
    33  // backups. It accepts a ListOpts struct, which allows you to filter and sort
    34  // the returned collection for greater efficiency.
    35  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Backup, error) {
    36  	q, err := golangsdk.BuildQueryString(&opts)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	u := listURL(c) + q.String()
    41  	pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    42  		return BackupPage{pagination.LinkedPageBase{PageResult: r}}
    43  	}).AllPages()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	allBackups, err := ExtractBackups(pages)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	if opts.ID != "" {
    54  		return FilterBackupsById(allBackups, opts.ID)
    55  	}
    56  
    57  	return allBackups, nil
    58  
    59  }
    60  
    61  func FilterBackupsById(backups []Backup, filterId string) ([]Backup, error) {
    62  
    63  	var refinedBackups []Backup
    64  
    65  	for _, backup := range backups {
    66  
    67  		if filterId == backup.Id {
    68  			refinedBackups = append(refinedBackups, backup)
    69  		}
    70  	}
    71  
    72  	return refinedBackups, nil
    73  }
    74  
    75  // CreateOptsBuilder allows extensions to add additional parameters to the
    76  // Create request.
    77  type CreateOptsBuilder interface {
    78  	ToBackupCreateMap() (map[string]interface{}, error)
    79  }
    80  
    81  // CreateOpts contains the options for create a Backup. This object is
    82  // passed to backup.Create().
    83  type CreateOpts struct {
    84  	BackupName   string        `json:"backup_name,omitempty"`
    85  	Description  string        `json:"description,omitempty"`
    86  	ResourceType string        `json:"resource_type,omitempty"`
    87  	Tags         []ResourceTag `json:"tags,omitempty"`
    88  	ExtraInfo    interface{}   `json:"extra_info,omitempty"`
    89  }
    90  
    91  // ToBackupCreateMap assembles a request body based on the contents of a
    92  // CreateOpts.
    93  func (opts CreateOpts) ToBackupCreateMap() (map[string]interface{}, error) {
    94  	return golangsdk.BuildRequestBody(opts, "protect")
    95  }
    96  
    97  // Create will create a new backup based on the values in CreateOpts. To extract
    98  // the checkpoint object from the response, call the Extract method on the
    99  // CreateResult.
   100  func Create(client *golangsdk.ServiceClient, resourceId string, opts CreateOptsBuilder) (r CreateResult) {
   101  	b, err := opts.ToBackupCreateMap()
   102  	if err != nil {
   103  		r.Err = err
   104  		return
   105  	}
   106  	_, r.Err = client.Post(rootURL(client, resourceId), b, &r.Body, &golangsdk.RequestOpts{
   107  		OkCodes: []int{200},
   108  	})
   109  	return
   110  }
   111  
   112  // ResourceBackupCapabilityOptsBuilder allows extensions to add additional parameters to the
   113  // QueryResourceBackupCapability request.
   114  type ResourceBackupCapabilityOptsBuilder interface {
   115  	ToQueryResourceCreateMap() (map[string]interface{}, error)
   116  }
   117  
   118  // ResourceBackupCapOpts contains the options for querying whether resources can be backed up. This object is
   119  // passed to backup.QueryResourceBackupCapability().
   120  type ResourceBackupCapOpts struct {
   121  	CheckProtectable []ResourceCapQueryParams `json:"check_protectable" required:"true"`
   122  }
   123  
   124  type ResourceCapQueryParams struct {
   125  	ResourceId   string `json:"resource_id" required:"true"`
   126  	ResourceType string `json:"resource_type" required:"true"`
   127  }
   128  
   129  // ToQueryResourceCreateMap assembles a request body based on the contents of a
   130  // ResourceBackupCapOpts.
   131  func (opts ResourceBackupCapOpts) ToQueryResourceCreateMap() (map[string]interface{}, error) {
   132  	return golangsdk.BuildRequestBody(opts, "")
   133  }
   134  
   135  // QueryResourceBackupCapability will query whether resources can be backed up based on the values in ResourceBackupCapOpts. To extract
   136  // the ResourceCap object from the response, call the ExtractQueryResponse method on the
   137  // QueryResult.
   138  func QueryResourceBackupCapability(client *golangsdk.ServiceClient, opts ResourceBackupCapabilityOptsBuilder) (r QueryResult) {
   139  	b, err := opts.ToQueryResourceCreateMap()
   140  	if err != nil {
   141  		r.Err = err
   142  
   143  		return
   144  	}
   145  	_, r.Err = client.Post(resourceURL(client), b, &r.Body, &golangsdk.RequestOpts{
   146  		OkCodes: []int{200},
   147  	})
   148  	return
   149  }
   150  
   151  // Get will get a single backup with specific ID. To extract the Backup object from the response,
   152  // call the ExtractBackup method on the GetResult.
   153  func Get(client *golangsdk.ServiceClient, backupId string) (r GetResult) {
   154  	_, r.Err = client.Get(getURL(client, backupId), &r.Body, nil)
   155  
   156  	return
   157  
   158  }
   159  
   160  // Delete will delete an existing backup.
   161  func Delete(client *golangsdk.ServiceClient, checkpoint_id string) (r DeleteResult) {
   162  	_, r.Err = client.Delete(deleteURL(client, checkpoint_id), &golangsdk.RequestOpts{
   163  		OkCodes:      []int{200},
   164  		JSONResponse: nil,
   165  	})
   166  	return
   167  }