github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v3/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  	Force       bool              `json:"force,omitempty"`
    20  	Name        string            `json:"name,omitempty"`
    21  	Description string            `json:"description,omitempty"`
    22  	Metadata    map[string]string `json:"metadata,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{202},
    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  type ListOpts struct {
    66  	// AllTenants will retrieve snapshots of all tenants/projects.
    67  	AllTenants bool `q:"all_tenants"`
    68  
    69  	// Name will filter by the specified snapshot name.
    70  	Name string `q:"name"`
    71  
    72  	// Status will filter by the specified status.
    73  	Status string `q:"status"`
    74  
    75  	// TenantID will filter by a specific tenant/project ID.
    76  	// Setting AllTenants is required to use this.
    77  	TenantID string `q:"project_id"`
    78  
    79  	// VolumeID will filter by a specified volume ID.
    80  	VolumeID string `q:"volume_id"`
    81  
    82  	// Comma-separated list of sort keys and optional sort directions in the
    83  	// form of <key>[:<direction>].
    84  	Sort string `q:"sort"`
    85  
    86  	// Requests a page size of items.
    87  	Limit int `q:"limit"`
    88  
    89  	// Used in conjunction with limit to return a slice of items.
    90  	Offset int `q:"offset"`
    91  
    92  	// The ID of the last-seen item.
    93  	Marker string `q:"marker"`
    94  }
    95  
    96  // ToSnapshotListQuery formats a ListOpts into a query string.
    97  func (opts ListOpts) ToSnapshotListQuery() (string, error) {
    98  	q, err := golangsdk.BuildQueryString(opts)
    99  	return q.String(), err
   100  }
   101  
   102  // List returns Snapshots optionally limited by the conditions provided in
   103  // ListOpts.
   104  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   105  	url := listURL(client)
   106  	if opts != nil {
   107  		query, err := opts.ToSnapshotListQuery()
   108  		if err != nil {
   109  			return pagination.Pager{Err: err}
   110  		}
   111  		url += query
   112  	}
   113  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   114  		return SnapshotPage{pagination.LinkedPageBase{PageResult: r}}
   115  	})
   116  }
   117  
   118  // UpdateMetadataOptsBuilder allows extensions to add additional parameters to
   119  // the Update request.
   120  type UpdateMetadataOptsBuilder interface {
   121  	ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
   122  }
   123  
   124  // UpdateMetadataOpts contain options for updating an existing Snapshot. This
   125  // object is passed to the snapshots.Update function. For more information
   126  // about the parameters, see the Snapshot object.
   127  type UpdateMetadataOpts struct {
   128  	Metadata map[string]interface{} `json:"metadata,omitempty"`
   129  }
   130  
   131  // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
   132  // an UpdateMetadataOpts.
   133  func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
   134  	return golangsdk.BuildRequestBody(opts, "")
   135  }
   136  
   137  // UpdateMetadata will update the Snapshot with provided information. To
   138  // extract the updated Snapshot from the response, call the ExtractMetadata
   139  // method on the UpdateMetadataResult.
   140  func UpdateMetadata(client *golangsdk.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
   141  	b, err := opts.ToSnapshotUpdateMetadataMap()
   142  	if err != nil {
   143  		r.Err = err
   144  		return
   145  	}
   146  	_, r.Err = client.Put(updateMetadataURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   147  		OkCodes: []int{200},
   148  	})
   149  	return
   150  }
   151  
   152  // IDFromName is a convienience function that returns a snapshot's ID given its name.
   153  func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) {
   154  	count := 0
   155  	id := ""
   156  
   157  	listOpts := ListOpts{
   158  		Name: name,
   159  	}
   160  
   161  	pages, err := List(client, listOpts).AllPages()
   162  	if err != nil {
   163  		return "", err
   164  	}
   165  
   166  	all, err := ExtractSnapshots(pages)
   167  	if err != nil {
   168  		return "", err
   169  	}
   170  
   171  	for _, s := range all {
   172  		if s.Name == name {
   173  			count++
   174  			id = s.ID
   175  		}
   176  	}
   177  
   178  	switch count {
   179  	case 0:
   180  		return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "snapshot"}
   181  	case 1:
   182  		return id, nil
   183  	default:
   184  		return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"}
   185  	}
   186  }