Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 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 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 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. |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 17 | type CreateOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 18 | Size int `json:"size" required:"true"` |
| 19 | Availability string `json:"availability,omitempty"` |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 20 | Description string `json:"display_description,omitempty"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 21 | Metadata map[string]string `json:"metadata,omitempty"` |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 22 | Name string `json:"display_name,omitempty"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 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"` |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 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) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 32 | return gophercloud.BuildRequestBody(opts, "volume") |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 33 | } |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 34 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 35 | // Create will create a new Volume based on the values in CreateOpts. To extract |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 36 | // the Volume object from the response, call the Extract method on the |
| 37 | // CreateResult. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 38 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 39 | b, err := opts.ToVolumeCreateMap() |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 40 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 41 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 42 | return |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 43 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 44 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 45 | OkCodes: []int{200, 201}, |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 46 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 47 | return |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 50 | // Delete will delete the existing Volume with the provided ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 51 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 52 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 53 | return |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 54 | } |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 55 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 56 | // Get retrieves the Volume with the provided ID. To extract the Volume object |
| 57 | // from the response, call the Extract method on the GetResult. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 58 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 59 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 60 | return |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 63 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 64 | // request. |
| 65 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 66 | ToVolumeListQuery() (string, error) |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 69 | // ListOpts holds options for listing Volumes. It is passed to the volumes.List |
| 70 | // function. |
| 71 | type ListOpts struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 72 | // admin-only option. Set it to true to see all tenant volumes. |
| 73 | AllTenants bool `q:"all_tenants"` |
| 74 | // List only volumes that contain Metadata. |
| 75 | Metadata map[string]string `q:"metadata"` |
| 76 | // List only volumes that have Name as the display name. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 77 | Name string `q:"display_name"` |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 78 | // List only volumes that have a status of Status. |
| 79 | Status string `q:"status"` |
| 80 | } |
| 81 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 82 | // ToVolumeListQuery formats a ListOpts into a query string. |
| 83 | func (opts ListOpts) ToVolumeListQuery() (string, error) { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 84 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 85 | return q.String(), err |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 88 | // List returns Volumes optionally limited by the conditions provided in ListOpts. |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 89 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 90 | url := listURL(client) |
| 91 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 92 | query, err := opts.ToVolumeListQuery() |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 93 | if err != nil { |
| 94 | return pagination.Pager{Err: err} |
| 95 | } |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 96 | url += query |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 97 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 98 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 99 | return VolumePage{pagination.SinglePageBase(r)} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 100 | }) |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 104 | // Update request. |
| 105 | type UpdateOptsBuilder interface { |
| 106 | ToVolumeUpdateMap() (map[string]interface{}, error) |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // UpdateOpts contain options for updating an existing Volume. This object is passed |
| 110 | // to the volumes.Update function. For more information about the parameters, see |
| 111 | // the Volume object. |
| 112 | type UpdateOpts struct { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 113 | Name string `json:"display_name,omitempty"` |
| 114 | Description string `json:"display_description,omitempty"` |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 115 | Metadata map[string]string `json:"metadata,omitempty"` |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // ToVolumeUpdateMap assembles a request body based on the contents of an |
| 119 | // UpdateOpts. |
| 120 | func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 121 | return gophercloud.BuildRequestBody(opts, "volume") |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Update will update the Volume with provided information. To extract the updated |
| 125 | // Volume from the response, call the Extract method on the UpdateResult. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 126 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 127 | b, err := opts.ToVolumeUpdateMap() |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 128 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 129 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 130 | return |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 131 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 132 | _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 133 | OkCodes: []int{200}, |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 134 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 135 | return |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 136 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 137 | |
| 138 | // IDFromName is a convienience function that returns a server's ID given its name. |
| 139 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 140 | count := 0 |
| 141 | id := "" |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 142 | pages, err := List(client, nil).AllPages() |
| 143 | if err != nil { |
| 144 | return "", err |
| 145 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 146 | |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 147 | all, err := ExtractVolumes(pages) |
| 148 | if err != nil { |
| 149 | return "", err |
| 150 | } |
| 151 | |
| 152 | for _, s := range all { |
| 153 | if s.Name == name { |
| 154 | count++ |
| 155 | id = s.ID |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | switch count { |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 160 | case 0: |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 161 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"} |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 162 | case 1: |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 163 | return id, nil |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 164 | default: |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 165 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"} |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 166 | } |
| 167 | } |