github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v3/volumes/requests.go (about)

     1  package volumes
     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  	ToVolumeCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts contains options for creating a Volume. This object is passed to
    15  // the volumes.Create function. For more information about these parameters,
    16  // see the Volume object.
    17  type CreateOpts struct {
    18  	// The size of the volume, in GB
    19  	Size int `json:"size" required:"true"`
    20  	// The availability zone
    21  	AvailabilityZone string `json:"availability_zone,omitempty"`
    22  	// ConsistencyGroupID is the ID of a consistency group
    23  	ConsistencyGroupID string `json:"consistencygroup_id,omitempty"`
    24  	// The volume description
    25  	Description string `json:"description,omitempty"`
    26  	// One or more metadata key and value pairs to associate with the volume
    27  	Metadata map[string]string `json:"metadata,omitempty"`
    28  	// The volume name
    29  	Name string `json:"name,omitempty"`
    30  	// the ID of the existing volume snapshot
    31  	SnapshotID string `json:"snapshot_id,omitempty"`
    32  	// SourceReplica is a UUID of an existing volume to replicate with
    33  	SourceReplica string `json:"source_replica,omitempty"`
    34  	// the ID of the existing volume
    35  	SourceVolID string `json:"source_volid,omitempty"`
    36  	// The ID of the image from which you want to create the volume.
    37  	// Required to create a bootable volume.
    38  	ImageID string `json:"imageRef,omitempty"`
    39  	// The associated volume type
    40  	VolumeType string `json:"volume_type,omitempty"`
    41  }
    42  
    43  // ToVolumeCreateMap assembles a request body based on the contents of a
    44  // CreateOpts.
    45  func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
    46  	return golangsdk.BuildRequestBody(opts, "volume")
    47  }
    48  
    49  // Create will create a new Volume based on the values in CreateOpts. To extract
    50  // the Volume object from the response, call the Extract method on the
    51  // CreateResult.
    52  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    53  	b, err := opts.ToVolumeCreateMap()
    54  	if err != nil {
    55  		r.Err = err
    56  		return
    57  	}
    58  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    59  		OkCodes: []int{202},
    60  	})
    61  	return
    62  }
    63  
    64  // Delete will delete the existing Volume with the provided ID.
    65  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    66  	_, r.Err = client.Delete(deleteURL(client, id), nil)
    67  	return
    68  }
    69  
    70  // Get retrieves the Volume with the provided ID. To extract the Volume object
    71  // from the response, call the Extract method on the GetResult.
    72  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    73  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    74  	return
    75  }
    76  
    77  // ListOptsBuilder allows extensions to add additional parameters to the List
    78  // request.
    79  type ListOptsBuilder interface {
    80  	ToVolumeListQuery() (string, error)
    81  }
    82  
    83  // ListOpts holds options for listing Volumes. It is passed to the volumes.List
    84  // function.
    85  type ListOpts struct {
    86  	// AllTenants will retrieve volumes of all tenants/projects.
    87  	AllTenants bool `q:"all_tenants"`
    88  
    89  	// Metadata will filter results based on specified metadata.
    90  	Metadata map[string]string `q:"metadata"`
    91  
    92  	// Name will filter by the specified volume name.
    93  	Name string `q:"name"`
    94  
    95  	// Status will filter by the specified status.
    96  	Status string `q:"status"`
    97  
    98  	// TenantID will filter by a specific tenant/project ID.
    99  	// Setting AllTenants is required for this.
   100  	TenantID string `q:"project_id"`
   101  
   102  	// Comma-separated list of sort keys and optional sort directions in the
   103  	// form of <key>[:<direction>].
   104  	Sort string `q:"sort"`
   105  
   106  	// Requests a page size of items.
   107  	Limit int `q:"limit"`
   108  
   109  	// Used in conjunction with limit to return a slice of items.
   110  	Offset int `q:"offset"`
   111  
   112  	// The ID of the last-seen item.
   113  	Marker string `q:"marker"`
   114  }
   115  
   116  // ToVolumeListQuery formats a ListOpts into a query string.
   117  func (opts ListOpts) ToVolumeListQuery() (string, error) {
   118  	q, err := golangsdk.BuildQueryString(opts)
   119  	return q.String(), err
   120  }
   121  
   122  // List returns Volumes optionally limited by the conditions provided in ListOpts.
   123  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   124  	url := listURL(client)
   125  	if opts != nil {
   126  		query, err := opts.ToVolumeListQuery()
   127  		if err != nil {
   128  			return pagination.Pager{Err: err}
   129  		}
   130  		url += query
   131  	}
   132  
   133  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   134  		return VolumePage{pagination.LinkedPageBase{PageResult: r}}
   135  	})
   136  }
   137  
   138  // UpdateOptsBuilder allows extensions to add additional parameters to the
   139  // Update request.
   140  type UpdateOptsBuilder interface {
   141  	ToVolumeUpdateMap() (map[string]interface{}, error)
   142  }
   143  
   144  // UpdateOpts contain options for updating an existing Volume. This object is passed
   145  // to the volumes.Update function. For more information about the parameters, see
   146  // the Volume object.
   147  type UpdateOpts struct {
   148  	Name        string            `json:"name,omitempty"`
   149  	Description string            `json:"description,omitempty"`
   150  	Metadata    map[string]string `json:"metadata,omitempty"`
   151  }
   152  
   153  // ToVolumeUpdateMap assembles a request body based on the contents of an
   154  // UpdateOpts.
   155  func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
   156  	return golangsdk.BuildRequestBody(opts, "volume")
   157  }
   158  
   159  // Update will update the Volume with provided information. To extract the updated
   160  // Volume from the response, call the Extract method on the UpdateResult.
   161  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   162  	b, err := opts.ToVolumeUpdateMap()
   163  	if err != nil {
   164  		r.Err = err
   165  		return
   166  	}
   167  	_, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   168  		OkCodes: []int{200},
   169  	})
   170  	return
   171  }
   172  
   173  // IDFromName is a convienience function that returns a server's ID given its name.
   174  func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) {
   175  	count := 0
   176  	id := ""
   177  
   178  	listOpts := ListOpts{
   179  		Name: name,
   180  	}
   181  
   182  	pages, err := List(client, listOpts).AllPages()
   183  	if err != nil {
   184  		return "", err
   185  	}
   186  
   187  	all, err := ExtractVolumes(pages)
   188  	if err != nil {
   189  		return "", err
   190  	}
   191  
   192  	for _, s := range all {
   193  		if s.Name == name {
   194  			count++
   195  			id = s.ID
   196  		}
   197  	}
   198  
   199  	switch count {
   200  	case 0:
   201  		return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "volume"}
   202  	case 1:
   203  		return id, nil
   204  	default:
   205  		return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"}
   206  	}
   207  }