github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v2/volumes/requests.go (about)

     1  package volumes
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/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 gophercloud.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 *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    53  	b, err := opts.ToVolumeCreateMap()
    54  	if err != nil {
    55  		r.Err = err
    56  		return
    57  	}
    58  	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    59  		OkCodes: []int{202},
    60  	})
    61  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    62  	return
    63  }
    64  
    65  // DeleteOptsBuilder allows extensions to add additional parameters to the
    66  // Delete request.
    67  type DeleteOptsBuilder interface {
    68  	ToVolumeDeleteQuery() (string, error)
    69  }
    70  
    71  // DeleteOpts contains options for deleting a Volume. This object is passed to
    72  // the volumes.Delete function.
    73  type DeleteOpts struct {
    74  	// Delete all snapshots of this volume as well.
    75  	Cascade bool `q:"cascade"`
    76  }
    77  
    78  // ToLoadBalancerDeleteQuery formats a DeleteOpts into a query string.
    79  func (opts DeleteOpts) ToVolumeDeleteQuery() (string, error) {
    80  	q, err := gophercloud.BuildQueryString(opts)
    81  	return q.String(), err
    82  }
    83  
    84  // Delete will delete the existing Volume with the provided ID.
    85  func Delete(client *gophercloud.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
    86  	url := deleteURL(client, id)
    87  	if opts != nil {
    88  		query, err := opts.ToVolumeDeleteQuery()
    89  		if err != nil {
    90  			r.Err = err
    91  			return
    92  		}
    93  		url += query
    94  	}
    95  	resp, err := client.Delete(url, nil)
    96  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    97  	return
    98  }
    99  
   100  // Get retrieves the Volume with the provided ID. To extract the Volume object
   101  // from the response, call the Extract method on the GetResult.
   102  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
   103  	resp, err := client.Get(getURL(client, id), &r.Body, nil)
   104  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   105  	return
   106  }
   107  
   108  // ListOptsBuilder allows extensions to add additional parameters to the List
   109  // request.
   110  type ListOptsBuilder interface {
   111  	ToVolumeListQuery() (string, error)
   112  }
   113  
   114  // ListOpts holds options for listing Volumes. It is passed to the volumes.List
   115  // function.
   116  type ListOpts struct {
   117  	// AllTenants will retrieve volumes of all tenants/projects.
   118  	AllTenants bool `q:"all_tenants"`
   119  
   120  	// Metadata will filter results based on specified metadata.
   121  	Metadata map[string]string `q:"metadata"`
   122  
   123  	// Name will filter by the specified volume name.
   124  	Name string `q:"name"`
   125  
   126  	// Status will filter by the specified status.
   127  	Status string `q:"status"`
   128  
   129  	// TenantID will filter by a specific tenant/project ID.
   130  	// Setting AllTenants is required for this.
   131  	TenantID string `q:"project_id"`
   132  
   133  	// Comma-separated list of sort keys and optional sort directions in the
   134  	// form of <key>[:<direction>].
   135  	Sort string `q:"sort"`
   136  
   137  	// Requests a page size of items.
   138  	Limit int `q:"limit"`
   139  
   140  	// Used in conjunction with limit to return a slice of items.
   141  	Offset int `q:"offset"`
   142  
   143  	// The ID of the last-seen item.
   144  	Marker string `q:"marker"`
   145  }
   146  
   147  // ToVolumeListQuery formats a ListOpts into a query string.
   148  func (opts ListOpts) ToVolumeListQuery() (string, error) {
   149  	q, err := gophercloud.BuildQueryString(opts)
   150  	return q.String(), err
   151  }
   152  
   153  // List returns Volumes optionally limited by the conditions provided in ListOpts.
   154  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   155  	url := listURL(client)
   156  	if opts != nil {
   157  		query, err := opts.ToVolumeListQuery()
   158  		if err != nil {
   159  			return pagination.Pager{Err: err}
   160  		}
   161  		url += query
   162  	}
   163  
   164  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   165  		return VolumePage{pagination.LinkedPageBase{PageResult: r}}
   166  	})
   167  }
   168  
   169  // UpdateOptsBuilder allows extensions to add additional parameters to the
   170  // Update request.
   171  type UpdateOptsBuilder interface {
   172  	ToVolumeUpdateMap() (map[string]interface{}, error)
   173  }
   174  
   175  // UpdateOpts contain options for updating an existing Volume. This object is passed
   176  // to the volumes.Update function. For more information about the parameters, see
   177  // the Volume object.
   178  type UpdateOpts struct {
   179  	Name        *string           `json:"name,omitempty"`
   180  	Description *string           `json:"description,omitempty"`
   181  	Metadata    map[string]string `json:"metadata,omitempty"`
   182  }
   183  
   184  // ToVolumeUpdateMap assembles a request body based on the contents of an
   185  // UpdateOpts.
   186  func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
   187  	return gophercloud.BuildRequestBody(opts, "volume")
   188  }
   189  
   190  // Update will update the Volume with provided information. To extract the updated
   191  // Volume from the response, call the Extract method on the UpdateResult.
   192  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   193  	b, err := opts.ToVolumeUpdateMap()
   194  	if err != nil {
   195  		r.Err = err
   196  		return
   197  	}
   198  	resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   199  		OkCodes: []int{200},
   200  	})
   201  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   202  	return
   203  }