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