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

     1  package snapshots
     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  	ToSnapshotCreateMap() (map[string]any, error)
    14  }
    15  
    16  // CreateOpts contains options for creating a Snapshot. This object is passed to
    17  // the snapshots.Create function. For more information about these parameters,
    18  // see the Snapshot object.
    19  type CreateOpts struct {
    20  	VolumeID    string            `json:"volume_id" required:"true"`
    21  	Force       bool              `json:"force,omitempty"`
    22  	Name        string            `json:"name,omitempty"`
    23  	Description string            `json:"description,omitempty"`
    24  	Metadata    map[string]string `json:"metadata,omitempty"`
    25  }
    26  
    27  // ToSnapshotCreateMap assembles a request body based on the contents of a
    28  // CreateOpts.
    29  func (opts CreateOpts) ToSnapshotCreateMap() (map[string]any, error) {
    30  	return gophercloud.BuildRequestBody(opts, "snapshot")
    31  }
    32  
    33  // Create will create a new Snapshot based on the values in CreateOpts. To
    34  // extract the Snapshot object from the response, call the Extract method on the
    35  // CreateResult.
    36  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    37  	b, err := opts.ToSnapshotCreateMap()
    38  	if err != nil {
    39  		r.Err = err
    40  		return
    41  	}
    42  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    43  		OkCodes: []int{202},
    44  	})
    45  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    46  	return
    47  }
    48  
    49  // Delete will delete the existing Snapshot with the provided ID.
    50  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    51  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
    52  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    53  	return
    54  }
    55  
    56  // Get retrieves the Snapshot with the provided ID. To extract the Snapshot
    57  // object from the response, call the Extract method on the GetResult.
    58  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
    59  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
    60  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    61  	return
    62  }
    63  
    64  // ListOptsBuilder allows extensions to add additional parameters to the List
    65  // request.
    66  type ListOptsBuilder interface {
    67  	ToSnapshotListQuery() (string, error)
    68  }
    69  
    70  // ListOpts hold options for listing Snapshots. It is passed to the
    71  // snapshots.List function.
    72  type ListOpts struct {
    73  	// AllTenants will retrieve snapshots of all tenants/projects.
    74  	AllTenants bool `q:"all_tenants"`
    75  
    76  	// Name will filter by the specified snapshot name.
    77  	Name string `q:"name"`
    78  
    79  	// Status will filter by the specified status.
    80  	Status string `q:"status"`
    81  
    82  	// TenantID will filter by a specific tenant/project ID.
    83  	// Setting AllTenants is required to use this.
    84  	TenantID string `q:"project_id"`
    85  
    86  	// VolumeID will filter by a specified volume ID.
    87  	VolumeID string `q:"volume_id"`
    88  }
    89  
    90  // ToSnapshotListQuery formats a ListOpts into a query string.
    91  func (opts ListOpts) ToSnapshotListQuery() (string, error) {
    92  	q, err := gophercloud.BuildQueryString(opts)
    93  	return q.String(), err
    94  }
    95  
    96  // List returns Snapshots optionally limited by the conditions provided in
    97  // ListOpts.
    98  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    99  	url := listURL(client)
   100  	if opts != nil {
   101  		query, err := opts.ToSnapshotListQuery()
   102  		if err != nil {
   103  			return pagination.Pager{Err: err}
   104  		}
   105  		url += query
   106  	}
   107  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   108  		return SnapshotPage{pagination.SinglePageBase(r)}
   109  	})
   110  }
   111  
   112  // UpdateMetadataOptsBuilder allows extensions to add additional parameters to
   113  // the Update request.
   114  type UpdateMetadataOptsBuilder interface {
   115  	ToSnapshotUpdateMetadataMap() (map[string]any, error)
   116  }
   117  
   118  // UpdateMetadataOpts contain options for updating an existing Snapshot. This
   119  // object is passed to the snapshots.Update function. For more information
   120  // about the parameters, see the Snapshot object.
   121  type UpdateMetadataOpts struct {
   122  	Metadata map[string]any `json:"metadata,omitempty"`
   123  }
   124  
   125  // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
   126  // an UpdateMetadataOpts.
   127  func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]any, error) {
   128  	return gophercloud.BuildRequestBody(opts, "")
   129  }
   130  
   131  // UpdateMetadata will update the Snapshot with provided information. To
   132  // extract the updated Snapshot from the response, call the ExtractMetadata
   133  // method on the UpdateMetadataResult.
   134  func UpdateMetadata(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
   135  	b, err := opts.ToSnapshotUpdateMetadataMap()
   136  	if err != nil {
   137  		r.Err = err
   138  		return
   139  	}
   140  	resp, err := client.Put(ctx, updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   141  		OkCodes: []int{200},
   142  	})
   143  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   144  	return
   145  }