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

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