github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v2/backups/requests.go (about)

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