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