github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/vbs/v2/backups/requests.go (about)

     1  package backups
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  // ListOpts allows the filtering and sorting of paginated collections through
    11  // the API. Filtering is achieved by passing in struct field values that map to
    12  // the backup attributes you want to see returned.
    13  type ListOpts struct {
    14  	Id         string
    15  	SnapshotId string
    16  	Name       string `q:"name"`
    17  	Status     string `q:"status"`
    18  	Limit      int    `q:"limit"`
    19  	Offset     int    `q:"offset"`
    20  	VolumeId   string `q:"volume_id"`
    21  }
    22  
    23  // List returns collection of
    24  // Backup. It accepts a ListOpts struct, which allows you to filter and sort
    25  // the returned collection for greater efficiency.
    26  //
    27  // Default policy settings return only those Backup that are owned by the
    28  // tenant who submits the request, unless an admin user submits the request.
    29  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Backup, error) {
    30  	q, err := golangsdk.BuildQueryString(&opts)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	u := listURL(c) + q.String()
    35  	pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    36  		return BackupPage{pagination.LinkedPageBase{PageResult: r}}
    37  	}).AllPages()
    38  
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	allBackups, err := ExtractBackups(pages)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return FilterBackups(allBackups, opts)
    49  }
    50  
    51  func FilterBackups(backup []Backup, opts ListOpts) ([]Backup, error) {
    52  
    53  	var refinedBackup []Backup
    54  	var matched bool
    55  	m := map[string]interface{}{}
    56  
    57  	if opts.Id != "" {
    58  		m["Id"] = opts.Id
    59  	}
    60  	if opts.SnapshotId != "" {
    61  		m["SnapshotId"] = opts.SnapshotId
    62  	}
    63  
    64  	if len(m) > 0 && len(backup) > 0 {
    65  		for _, backup := range backup {
    66  			matched = true
    67  
    68  			for key, value := range m {
    69  				if sVal := getStructField(&backup, key); !(sVal == value) {
    70  					matched = false
    71  				}
    72  			}
    73  
    74  			if matched {
    75  				refinedBackup = append(refinedBackup, backup)
    76  			}
    77  		}
    78  	} else {
    79  		refinedBackup = backup
    80  	}
    81  	return refinedBackup, nil
    82  }
    83  
    84  func getStructField(v *Backup, field string) string {
    85  	r := reflect.ValueOf(v)
    86  	f := reflect.Indirect(r).FieldByName(field)
    87  	return string(f.String())
    88  }
    89  
    90  // CreateOptsBuilder allows extensions to add additional parameters to the
    91  // Create request.
    92  type CreateOptsBuilder interface {
    93  	ToBackupCreateMap() (map[string]interface{}, error)
    94  }
    95  
    96  // CreateOpts contains all the values needed to create a new backup.
    97  type CreateOpts struct {
    98  	//ID of the disk to be backed up
    99  	VolumeId string `json:"volume_id" required:"true"`
   100  	//Snapshot ID of the disk to be backed up
   101  	SnapshotId string `json:"snapshot_id,omitempty" `
   102  	//Backup name, which cannot start with autobk
   103  	Name string `json:"name" required:"true"`
   104  	//Backup description
   105  	Description string `json:"description,omitempty"`
   106  	//List of tags to be configured for the backup resources
   107  	Tags []Tag `json:"tags,omitempty"`
   108  }
   109  
   110  type Tag struct {
   111  	//Tag key
   112  	Key string `json:"key" required:"true"`
   113  	//Tag value
   114  	Value string `json:"value" required:"true"`
   115  }
   116  
   117  // ToBackupCreateMap builds a create request body from CreateOpts.
   118  func (opts CreateOpts) ToBackupCreateMap() (map[string]interface{}, error) {
   119  	return golangsdk.BuildRequestBody(opts, "backup")
   120  }
   121  
   122  // Create will create a new Backup based on the values in CreateOpts. To extract
   123  // the Backup object from the response, call the ExtractJobResponse method on the
   124  // JobResult.
   125  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
   126  	b, err := opts.ToBackupCreateMap()
   127  	if err != nil {
   128  		r.Err = err
   129  		return
   130  	}
   131  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   132  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
   133  	return
   134  }
   135  
   136  // RestoreOptsBuilder allows extensions to add additional parameters to the
   137  // Create request.
   138  type RestoreOptsBuilder interface {
   139  	ToRestoreCreateMap() (map[string]interface{}, error)
   140  }
   141  
   142  // BackupRestoreOpts contains all the values needed to create a new backup.
   143  type BackupRestoreOpts struct {
   144  	//ID of the disk to be backed up
   145  	VolumeId string `json:"volume_id" required:"true"`
   146  }
   147  
   148  // ToRestoreCreateMap builds a create request body from BackupRestoreOpts.
   149  func (opts BackupRestoreOpts) ToRestoreCreateMap() (map[string]interface{}, error) {
   150  	return golangsdk.BuildRequestBody(opts, "restore")
   151  }
   152  
   153  // CreateBackupRestore will create a new Restore based on the values in BackupRestoreOpts. To extract
   154  // the BackupRestoreInfo object from the response, call the ExtractBackupRestore method on the
   155  // CreateResult.
   156  func CreateBackupRestore(c *golangsdk.ServiceClient, id string, opts RestoreOptsBuilder) (r CreateResult) {
   157  	b, err := opts.ToRestoreCreateMap()
   158  	if err != nil {
   159  		r.Err = err
   160  		return
   161  	}
   162  	_, r.Err = c.Post(restoreURL(c, id), b, &r.Body, nil)
   163  	return
   164  }
   165  
   166  // Get retrieves a particular backup based on its unique ID. To extract
   167  //// the Backup object from the response, call the Extract method on the
   168  //// GetResult.
   169  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   170  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   171  	return
   172  }
   173  
   174  // Delete will permanently delete a particular backup based on its unique ID.
   175  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   176  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   177  	return
   178  }