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

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