feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 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 { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 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"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 36 | // The ID of the image from which you want to create the volume. |
| 37 | // Required to create a bootable volume. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 38 | ImageID string `json:"imageRef,omitempty"` |
| 39 | // The associated volume type |
| 40 | VolumeType string `json:"volume_type,omitempty"` |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 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) { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 46 | return gophercloud.BuildRequestBody(opts, "volume") |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 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. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 52 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 53 | b, err := opts.ToVolumeCreateMap() |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 54 | if err != nil { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 55 | r.Err = err |
| 56 | return |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 57 | } |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 58 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 59 | OkCodes: []int{202}, |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 60 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 61 | return |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Delete will delete the existing Volume with the provided ID. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 65 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
| 66 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
| 67 | return |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 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. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 72 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
| 73 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
| 74 | return |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 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 | // admin-only option. Set it to true to see all tenant volumes. |
| 87 | AllTenants bool `q:"all_tenants"` |
| 88 | // List only volumes that contain Metadata. |
| 89 | Metadata map[string]string `q:"metadata"` |
| 90 | // List only volumes that have Name as the display name. |
| 91 | Name string `q:"name"` |
| 92 | // List only volumes that have a status of Status. |
| 93 | Status string `q:"status"` |
| 94 | } |
| 95 | |
| 96 | // ToVolumeListQuery formats a ListOpts into a query string. |
| 97 | func (opts ListOpts) ToVolumeListQuery() (string, error) { |
| 98 | q, err := gophercloud.BuildQueryString(opts) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 99 | return q.String(), err |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // List returns Volumes optionally limited by the conditions provided in ListOpts. |
| 103 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 104 | url := listURL(client) |
| 105 | if opts != nil { |
| 106 | query, err := opts.ToVolumeListQuery() |
| 107 | if err != nil { |
| 108 | return pagination.Pager{Err: err} |
| 109 | } |
| 110 | url += query |
| 111 | } |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 112 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 113 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 114 | return VolumePage{pagination.SinglePageBase(r)} |
| 115 | }) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 119 | // Update request. |
| 120 | type UpdateOptsBuilder interface { |
| 121 | ToVolumeUpdateMap() (map[string]interface{}, error) |
| 122 | } |
| 123 | |
| 124 | // UpdateOpts contain options for updating an existing Volume. This object is passed |
| 125 | // to the volumes.Update function. For more information about the parameters, see |
| 126 | // the Volume object. |
| 127 | type UpdateOpts struct { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 128 | Name string `json:"name,omitempty"` |
| 129 | Description string `json:"description,omitempty"` |
| 130 | Metadata map[string]string `json:"metadata,omitempty"` |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // ToVolumeUpdateMap assembles a request body based on the contents of an |
| 134 | // UpdateOpts. |
| 135 | func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 136 | return gophercloud.BuildRequestBody(opts, "volume") |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Update will update the Volume with provided information. To extract the updated |
| 140 | // Volume from the response, call the Extract method on the UpdateResult. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 141 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
| 142 | b, err := opts.ToVolumeUpdateMap() |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 143 | if err != nil { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 144 | r.Err = err |
| 145 | return |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 146 | } |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 147 | _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 148 | OkCodes: []int{200}, |
| 149 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 150 | return |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // IDFromName is a convienience function that returns a server's ID given its name. |
| 154 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 155 | count := 0 |
| 156 | id := "" |
| 157 | pages, err := List(client, nil).AllPages() |
| 158 | if err != nil { |
| 159 | return "", err |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 160 | } |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 161 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 162 | all, err := ExtractVolumes(pages) |
| 163 | if err != nil { |
| 164 | return "", err |
| 165 | } |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 166 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 167 | for _, s := range all { |
| 168 | if s.Name == name { |
| 169 | count++ |
| 170 | id = s.ID |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | switch count { |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 175 | case 0: |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 176 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"} |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 177 | case 1: |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 178 | return id, nil |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 179 | default: |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 180 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"} |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 181 | } |
| 182 | } |