github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/backups/requests.go (about)

     1  package backups
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // CreateOptsBuilder allows extensions to add additional parameters to the
     9  // Create request.
    10  type CreateOptsBuilder interface {
    11  	ToBackupCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts contains options for creating a Backup. This object is passed to
    15  // the backups.Create function. For more information about these parameters,
    16  // see the Backup object.
    17  type CreateOpts struct {
    18  	// VolumeID is the ID of the volume to create the backup from.
    19  	VolumeID string `json:"volume_id" required:"true"`
    20  
    21  	// Force will force the creation of a backup regardless of the
    22  	//volume's status.
    23  	Force bool `json:"force,omitempty"`
    24  
    25  	// Name is the name of the backup.
    26  	Name string `json:"name,omitempty"`
    27  
    28  	// Description is the description of the backup.
    29  	Description string `json:"description,omitempty"`
    30  
    31  	// Metadata is metadata for the backup.
    32  	// Requires microversion 3.43 or later.
    33  	Metadata map[string]string `json:"metadata,omitempty"`
    34  
    35  	// Container is a container to store the backup.
    36  	Container string `json:"container,omitempty"`
    37  
    38  	// Incremental is whether the backup should be incremental or not.
    39  	Incremental bool `json:"incremental,omitempty"`
    40  
    41  	// SnapshotID is the ID of a snapshot to backup.
    42  	SnapshotID string `json:"snapshot_id,omitempty"`
    43  
    44  	// AvailabilityZone is an availability zone to locate the volume or snapshot.
    45  	// Requires microversion 3.51 or later.
    46  	AvailabilityZone string `json:"availability_zone,omitempty"`
    47  }
    48  
    49  // ToBackupCreateMap assembles a request body based on the contents of a
    50  // CreateOpts.
    51  func (opts CreateOpts) ToBackupCreateMap() (map[string]interface{}, error) {
    52  	return gophercloud.BuildRequestBody(opts, "backup")
    53  }
    54  
    55  // Create will create a new Backup based on the values in CreateOpts. To
    56  // extract the Backup object from the response, call the Extract method on the
    57  // CreateResult.
    58  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    59  	b, err := opts.ToBackupCreateMap()
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    65  		OkCodes: []int{202},
    66  	})
    67  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    68  	return
    69  }
    70  
    71  // Delete will delete the existing Backup with the provided ID.
    72  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    73  	resp, err := client.Delete(deleteURL(client, id), nil)
    74  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    75  	return
    76  }
    77  
    78  // Get retrieves the Backup with the provided ID. To extract the Backup
    79  // object from the response, call the Extract method on the GetResult.
    80  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
    81  	resp, err := client.Get(getURL(client, id), &r.Body, nil)
    82  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    83  	return
    84  }
    85  
    86  // ListOptsBuilder allows extensions to add additional parameters to the List
    87  // request.
    88  type ListOptsBuilder interface {
    89  	ToBackupListQuery() (string, error)
    90  }
    91  
    92  type ListOpts struct {
    93  	// AllTenants will retrieve backups of all tenants/projects.
    94  	AllTenants bool `q:"all_tenants"`
    95  
    96  	// Name will filter by the specified backup name.
    97  	// This does not work in later microversions.
    98  	Name string `q:"name"`
    99  
   100  	// Status will filter by the specified status.
   101  	// This does not work in later microversions.
   102  	Status string `q:"status"`
   103  
   104  	// TenantID will filter by a specific tenant/project ID.
   105  	// Setting AllTenants is required to use this.
   106  	TenantID string `q:"project_id"`
   107  
   108  	// VolumeID will filter by a specified volume ID.
   109  	// This does not work in later microversions.
   110  	VolumeID string `q:"volume_id"`
   111  
   112  	// Comma-separated list of sort keys and optional sort directions in the
   113  	// form of <key>[:<direction>].
   114  	Sort string `q:"sort"`
   115  
   116  	// Requests a page size of items.
   117  	Limit int `q:"limit"`
   118  
   119  	// Used in conjunction with limit to return a slice of items.
   120  	Offset int `q:"offset"`
   121  
   122  	// The ID of the last-seen item.
   123  	Marker string `q:"marker"`
   124  }
   125  
   126  // ToBackupListQuery formats a ListOpts into a query string.
   127  func (opts ListOpts) ToBackupListQuery() (string, error) {
   128  	q, err := gophercloud.BuildQueryString(opts)
   129  	return q.String(), err
   130  }
   131  
   132  // List returns Backups optionally limited by the conditions provided in
   133  // ListOpts.
   134  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   135  	url := listURL(client)
   136  	if opts != nil {
   137  		query, err := opts.ToBackupListQuery()
   138  		if err != nil {
   139  			return pagination.Pager{Err: err}
   140  		}
   141  		url += query
   142  	}
   143  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   144  		return BackupPage{pagination.LinkedPageBase{PageResult: r}}
   145  	})
   146  }
   147  
   148  // ListDetailOptsBuilder allows extensions to add additional parameters to the ListDetail
   149  // request.
   150  type ListDetailOptsBuilder interface {
   151  	ToBackupListDetailQuery() (string, error)
   152  }
   153  
   154  type ListDetailOpts struct {
   155  	// AllTenants will retrieve backups of all tenants/projects.
   156  	AllTenants bool `q:"all_tenants"`
   157  
   158  	// Comma-separated list of sort keys and optional sort directions in the
   159  	// form of <key>[:<direction>].
   160  	Sort string `q:"sort"`
   161  
   162  	// Requests a page size of items.
   163  	Limit int `q:"limit"`
   164  
   165  	// Used in conjunction with limit to return a slice of items.
   166  	Offset int `q:"offset"`
   167  
   168  	// The ID of the last-seen item.
   169  	Marker string `q:"marker"`
   170  
   171  	// True to include `count` in the API response, supported from version 3.45
   172  	WithCount bool `q:"with_count"`
   173  }
   174  
   175  // ToBackupListDetailQuery formats a ListDetailOpts into a query string.
   176  func (opts ListDetailOpts) ToBackupListDetailQuery() (string, error) {
   177  	q, err := gophercloud.BuildQueryString(opts)
   178  	return q.String(), err
   179  }
   180  
   181  // ListDetail returns more detailed information about Backups optionally
   182  // limited by the conditions provided in ListDetailOpts.
   183  func ListDetail(client *gophercloud.ServiceClient, opts ListDetailOptsBuilder) pagination.Pager {
   184  	url := listDetailURL(client)
   185  	if opts != nil {
   186  		query, err := opts.ToBackupListDetailQuery()
   187  		if err != nil {
   188  			return pagination.Pager{Err: err}
   189  		}
   190  		url += query
   191  	}
   192  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   193  		return BackupPage{pagination.LinkedPageBase{PageResult: r}}
   194  	})
   195  }
   196  
   197  // UpdateOptsBuilder allows extensions to add additional parameters to
   198  // the Update request.
   199  type UpdateOptsBuilder interface {
   200  	ToBackupUpdateMap() (map[string]interface{}, error)
   201  }
   202  
   203  // UpdateOpts contain options for updating an existing Backup.
   204  type UpdateOpts struct {
   205  	// Name is the name of the backup.
   206  	Name *string `json:"name,omitempty"`
   207  
   208  	// Description is the description of the backup.
   209  	Description *string `json:"description,omitempty"`
   210  
   211  	// Metadata is metadata for the backup.
   212  	// Requires microversion 3.43 or later.
   213  	Metadata map[string]string `json:"metadata,omitempty"`
   214  }
   215  
   216  // ToBackupUpdateMap assembles a request body based on the contents of
   217  // an UpdateOpts.
   218  func (opts UpdateOpts) ToBackupUpdateMap() (map[string]interface{}, error) {
   219  	return gophercloud.BuildRequestBody(opts, "")
   220  }
   221  
   222  // Update will update the Backup with provided information. To extract
   223  // the updated Backup from the response, call the Extract method on the
   224  // UpdateResult.
   225  // Requires microversion 3.9 or later.
   226  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   227  	b, err := opts.ToBackupUpdateMap()
   228  	if err != nil {
   229  		r.Err = err
   230  		return
   231  	}
   232  	resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   233  		OkCodes: []int{200},
   234  	})
   235  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   236  	return
   237  }
   238  
   239  // RestoreOpts contains options for restoring a Backup. This object is passed to
   240  // the backups.RestoreFromBackup function.
   241  type RestoreOpts struct {
   242  	// VolumeID is the ID of the existing volume to restore the backup to.
   243  	VolumeID string `json:"volume_id,omitempty"`
   244  
   245  	// Name is the name of the new volume to restore the backup to.
   246  	Name string `json:"name,omitempty"`
   247  }
   248  
   249  // ToRestoreMap assembles a request body based on the contents of a
   250  // RestoreOpts.
   251  func (opts RestoreOpts) ToRestoreMap() (map[string]interface{}, error) {
   252  	return gophercloud.BuildRequestBody(opts, "restore")
   253  }
   254  
   255  // RestoreFromBackup will restore a Backup to a volume based on the values in
   256  // RestoreOpts. To extract the Restore object from the response, call the
   257  // Extract method on the RestoreResult.
   258  func RestoreFromBackup(client *gophercloud.ServiceClient, id string, opts RestoreOpts) (r RestoreResult) {
   259  	b, err := opts.ToRestoreMap()
   260  	if err != nil {
   261  		r.Err = err
   262  		return
   263  	}
   264  	resp, err := client.Post(restoreURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   265  		OkCodes: []int{202},
   266  	})
   267  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   268  	return
   269  }
   270  
   271  // Export will export a Backup information. To extract the Backup export record
   272  // object from the response, call the Extract method on the ExportResult.
   273  func Export(client *gophercloud.ServiceClient, id string) (r ExportResult) {
   274  	resp, err := client.Get(exportURL(client, id), &r.Body, nil)
   275  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   276  	return
   277  }
   278  
   279  // ImportOpts contains options for importing a Backup. This object is passed to
   280  // the backups.ImportBackup function.
   281  type ImportOpts BackupRecord
   282  
   283  // ToBackupImportMap assembles a request body based on the contents of a
   284  // ImportOpts.
   285  func (opts ImportOpts) ToBackupImportMap() (map[string]interface{}, error) {
   286  	return gophercloud.BuildRequestBody(opts, "backup-record")
   287  }
   288  
   289  // Import will import a Backup data to a backup based on the values in
   290  // ImportOpts. To extract the Backup object from the response, call the
   291  // Extract method on the ImportResult.
   292  func Import(client *gophercloud.ServiceClient, opts ImportOpts) (r ImportResult) {
   293  	b, err := opts.ToBackupImportMap()
   294  	if err != nil {
   295  		r.Err = err
   296  		return
   297  	}
   298  	resp, err := client.Post(importURL(client), b, &r.Body, &gophercloud.RequestOpts{
   299  		OkCodes: []int{201},
   300  	})
   301  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   302  	return
   303  }