github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v1/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. 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 golangsdk.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 *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    35  	b, err := opts.ToSnapshotCreateMap()
    36  	if err != nil {
    37  		r.Err = err
    38  		return
    39  	}
    40  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    41  		OkCodes: []int{200, 201},
    42  	})
    43  	return
    44  }
    45  
    46  // Delete will delete the existing Snapshot with the provided ID.
    47  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    48  	_, r.Err = client.Delete(deleteURL(client, id), nil)
    49  	return
    50  }
    51  
    52  // Get retrieves the Snapshot with the provided ID. To extract the Snapshot
    53  // object from the response, call the Extract method on the GetResult.
    54  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    55  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    56  	return
    57  }
    58  
    59  // ListOptsBuilder allows extensions to add additional parameters to the List
    60  // request.
    61  type ListOptsBuilder interface {
    62  	ToSnapshotListQuery() (string, error)
    63  }
    64  
    65  // ListOpts hold options for listing Snapshots. It is passed to the
    66  // snapshots.List function.
    67  type ListOpts struct {
    68  	Name     string `q:"display_name"`
    69  	Status   string `q:"status"`
    70  	VolumeID string `q:"volume_id"`
    71  }
    72  
    73  // ToSnapshotListQuery formats a ListOpts into a query string.
    74  func (opts ListOpts) ToSnapshotListQuery() (string, error) {
    75  	q, err := golangsdk.BuildQueryString(opts)
    76  	return q.String(), err
    77  }
    78  
    79  // List returns Snapshots optionally limited by the conditions provided in
    80  // ListOpts.
    81  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    82  	url := listURL(client)
    83  	if opts != nil {
    84  		query, err := opts.ToSnapshotListQuery()
    85  		if err != nil {
    86  			return pagination.Pager{Err: err}
    87  		}
    88  		url += query
    89  	}
    90  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    91  		return SnapshotPage{pagination.SinglePageBase(r)}
    92  	})
    93  }
    94  
    95  // UpdateMetadataOptsBuilder allows extensions to add additional parameters to
    96  // the Update request.
    97  type UpdateMetadataOptsBuilder interface {
    98  	ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
    99  }
   100  
   101  // UpdateMetadataOpts contain options for updating an existing Snapshot. This
   102  // object is passed to the snapshots.Update function. For more information
   103  // about the parameters, see the Snapshot object.
   104  type UpdateMetadataOpts struct {
   105  	Metadata map[string]interface{} `json:"metadata,omitempty"`
   106  }
   107  
   108  // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
   109  // an UpdateMetadataOpts.
   110  func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
   111  	return golangsdk.BuildRequestBody(opts, "")
   112  }
   113  
   114  // UpdateMetadata will update the Snapshot with provided information. To
   115  // extract the updated Snapshot from the response, call the ExtractMetadata
   116  // method on the UpdateMetadataResult.
   117  func UpdateMetadata(client *golangsdk.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
   118  	b, err := opts.ToSnapshotUpdateMetadataMap()
   119  	if err != nil {
   120  		r.Err = err
   121  		return
   122  	}
   123  	_, r.Err = client.Put(updateMetadataURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   124  		OkCodes: []int{200},
   125  	})
   126  	return
   127  }
   128  
   129  // IDFromName is a convienience function that returns a snapshot's ID given its name.
   130  func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) {
   131  	count := 0
   132  	id := ""
   133  
   134  	listOpts := ListOpts{
   135  		Name: name,
   136  	}
   137  
   138  	pages, err := List(client, listOpts).AllPages()
   139  	if err != nil {
   140  		return "", err
   141  	}
   142  
   143  	all, err := ExtractSnapshots(pages)
   144  	if err != nil {
   145  		return "", err
   146  	}
   147  
   148  	for _, s := range all {
   149  		if s.Name == name {
   150  			count++
   151  			id = s.ID
   152  		}
   153  	}
   154  
   155  	switch count {
   156  	case 0:
   157  		return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "snapshot"}
   158  	case 1:
   159  		return id, nil
   160  	default:
   161  		return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"}
   162  	}
   163  }