github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v1/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  	Size             int               `json:"size" required:"true"`
    19  	AvailabilityZone string            `json:"availability_zone,omitempty"`
    20  	Description      string            `json:"display_description,omitempty"`
    21  	Metadata         map[string]string `json:"metadata,omitempty"`
    22  	Name             string            `json:"display_name,omitempty"`
    23  	SnapshotID       string            `json:"snapshot_id,omitempty"`
    24  	SourceVolID      string            `json:"source_volid,omitempty"`
    25  	ImageID          string            `json:"imageRef,omitempty"`
    26  	VolumeType       string            `json:"volume_type,omitempty"`
    27  }
    28  
    29  // ToVolumeCreateMap assembles a request body based on the contents of a
    30  // CreateOpts.
    31  func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
    32  	return gophercloud.BuildRequestBody(opts, "volume")
    33  }
    34  
    35  // Create will create a new Volume based on the values in CreateOpts. To extract
    36  // the Volume object from the response, call the Extract method on the
    37  // CreateResult.
    38  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    39  	b, err := opts.ToVolumeCreateMap()
    40  	if err != nil {
    41  		r.Err = err
    42  		return
    43  	}
    44  	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    45  		OkCodes: []int{200, 201, 202},
    46  	})
    47  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    48  	return
    49  }
    50  
    51  // Delete will delete the existing Volume with the provided ID.
    52  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    53  	resp, err := client.Delete(deleteURL(client, id), nil)
    54  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    55  	return
    56  }
    57  
    58  // Get retrieves the Volume with the provided ID. To extract the Volume object
    59  // from the response, call the Extract method on the GetResult.
    60  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
    61  	resp, err := client.Get(getURL(client, id), &r.Body, nil)
    62  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    63  	return
    64  }
    65  
    66  // ListOptsBuilder allows extensions to add additional parameters to the List
    67  // request.
    68  type ListOptsBuilder interface {
    69  	ToVolumeListQuery() (string, error)
    70  }
    71  
    72  // ListOpts holds options for listing Volumes. It is passed to the volumes.List
    73  // function.
    74  type ListOpts struct {
    75  	// admin-only option. Set it to true to see all tenant volumes.
    76  	AllTenants bool `q:"all_tenants"`
    77  	// List only volumes that contain Metadata.
    78  	Metadata map[string]string `q:"metadata"`
    79  	// List only volumes that have Name as the display name.
    80  	Name string `q:"display_name"`
    81  	// List only volumes that have a status of Status.
    82  	Status string `q:"status"`
    83  }
    84  
    85  // ToVolumeListQuery formats a ListOpts into a query string.
    86  func (opts ListOpts) ToVolumeListQuery() (string, error) {
    87  	q, err := gophercloud.BuildQueryString(opts)
    88  	return q.String(), err
    89  }
    90  
    91  // List returns Volumes optionally limited by the conditions provided in ListOpts.
    92  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    93  	url := listURL(client)
    94  	if opts != nil {
    95  		query, err := opts.ToVolumeListQuery()
    96  		if err != nil {
    97  			return pagination.Pager{Err: err}
    98  		}
    99  		url += query
   100  	}
   101  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   102  		return VolumePage{pagination.SinglePageBase(r)}
   103  	})
   104  }
   105  
   106  // UpdateOptsBuilder allows extensions to add additional parameters to the
   107  // Update request.
   108  type UpdateOptsBuilder interface {
   109  	ToVolumeUpdateMap() (map[string]interface{}, error)
   110  }
   111  
   112  // UpdateOpts contain options for updating an existing Volume. This object is passed
   113  // to the volumes.Update function. For more information about the parameters, see
   114  // the Volume object.
   115  type UpdateOpts struct {
   116  	Name        *string           `json:"display_name,omitempty"`
   117  	Description *string           `json:"display_description,omitempty"`
   118  	Metadata    map[string]string `json:"metadata,omitempty"`
   119  }
   120  
   121  // ToVolumeUpdateMap assembles a request body based on the contents of an
   122  // UpdateOpts.
   123  func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
   124  	return gophercloud.BuildRequestBody(opts, "volume")
   125  }
   126  
   127  // Update will update the Volume with provided information. To extract the updated
   128  // Volume from the response, call the Extract method on the UpdateResult.
   129  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   130  	b, err := opts.ToVolumeUpdateMap()
   131  	if err != nil {
   132  		r.Err = err
   133  		return
   134  	}
   135  	resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   136  		OkCodes: []int{200},
   137  	})
   138  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   139  	return
   140  }