Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 6 | |
| 7 | "github.com/racker/perigee" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 10 | // CreateOpts contains options for creating a Volume. This object is passed to |
| 11 | // the volumes.Create function. For more information about these parameters, |
| 12 | // see the Volume object. |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 13 | type CreateOpts struct { |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 14 | Availability string // OPTIONAL |
| 15 | Description string // OPTIONAL |
| 16 | Metadata map[string]string // OPTIONAL |
| 17 | Name string // OPTIONAL |
| 18 | Size int // REQUIRED |
| 19 | SnapshotID, SourceVolID, ImageID string // REQUIRED (one of them) |
| 20 | VolumeType string // OPTIONAL |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 21 | } |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 22 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 23 | // Create will create a new Volume based on the values in CreateOpts. To extract |
| 24 | // the Volume object from the response, call the Extract method on the CreateResult. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 25 | func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult { |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 26 | |
| 27 | type volume struct { |
| 28 | Availability *string `json:"availability_zone,omitempty"` |
| 29 | Description *string `json:"display_description,omitempty"` |
| 30 | ImageID *string `json:"imageRef,omitempty"` |
| 31 | Metadata map[string]string `json:"metadata,omitempty"` |
| 32 | Name *string `json:"display_name,omitempty"` |
| 33 | Size *int `json:"size,omitempty"` |
| 34 | SnapshotID *string `json:"snapshot_id,omitempty"` |
| 35 | SourceVolID *string `json:"source_volid,omitempty"` |
| 36 | VolumeType *string `json:"volume_type,omitempty"` |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 37 | } |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 38 | |
| 39 | type request struct { |
| 40 | Volume volume `json:"volume"` |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 41 | } |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 42 | |
| 43 | reqBody := request{ |
| 44 | Volume: volume{}, |
| 45 | } |
| 46 | |
Jon Perritt | 8d26258 | 2014-10-03 11:11:46 -0500 | [diff] [blame] | 47 | reqBody.Volume.Availability = gophercloud.MaybeString(opts.Availability) |
| 48 | reqBody.Volume.Description = gophercloud.MaybeString(opts.Description) |
| 49 | reqBody.Volume.ImageID = gophercloud.MaybeString(opts.ImageID) |
| 50 | reqBody.Volume.Name = gophercloud.MaybeString(opts.Name) |
| 51 | reqBody.Volume.Size = gophercloud.MaybeInt(opts.Size) |
| 52 | reqBody.Volume.SnapshotID = gophercloud.MaybeString(opts.SnapshotID) |
| 53 | reqBody.Volume.SourceVolID = gophercloud.MaybeString(opts.SourceVolID) |
| 54 | reqBody.Volume.VolumeType = gophercloud.MaybeString(opts.VolumeType) |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 55 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 56 | var res CreateResult |
| 57 | _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{ |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 58 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 59 | ReqBody: &reqBody, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 60 | Results: &res.Resp, |
| 61 | OkCodes: []int{200, 201}, |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 62 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 63 | return res |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 66 | // Delete will delete the existing Volume with the provided ID. |
| 67 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 68 | _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{ |
| 69 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 70 | OkCodes: []int{202, 204}, |
| 71 | }) |
| 72 | return err |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 73 | } |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 74 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 75 | // Get retrieves the Volume with the provided ID. To extract the Volume object from |
| 76 | // the response, call the Extract method on the GetResult. |
Jon Perritt | 03cb46d | 2014-09-22 20:46:20 -0500 | [diff] [blame] | 77 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 78 | var res GetResult |
| 79 | _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{ |
| 80 | Results: &res.Resp, |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 81 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 82 | OkCodes: []int{200}, |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 83 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 84 | return res |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 87 | // ListOpts holds options for listing Volumes. It is passed to the volumes.List |
| 88 | // function. |
| 89 | type ListOpts struct { |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 90 | AllTenants bool `q:"all_tenants"` // admin-only option. Set it to true to see all tenant volumes. |
| 91 | Metadata map[string]string `q:"metadata"` // List only volumes that contain Metadata. |
| 92 | Name string `q:"name"` // List only volumes that have Name as the display name. |
| 93 | Status string `q:"status"` // List only volumes that have a status of Status. |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 96 | // List returns Volumes optionally limited by the conditions provided in ListOpts. |
| 97 | func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager { |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 98 | url := listURL(client) |
| 99 | if opts != nil { |
| 100 | query, err := gophercloud.BuildQueryString(opts) |
| 101 | if err != nil { |
| 102 | return pagination.Pager{Err: err} |
| 103 | } |
| 104 | url += query.String() |
| 105 | } |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 106 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
| 107 | return ListResult{pagination.SinglePageBase(r)} |
| 108 | } |
| 109 | return pagination.NewPager(client, listURL(client), createPage) |
| 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 // OPTIONAL |
| 117 | Description string // OPTIONAL |
| 118 | Metadata map[string]string // OPTIONAL |
| 119 | } |
| 120 | |
| 121 | // Update will update the Volume with provided information. To extract the updated |
| 122 | // Volume from the response, call the Extract method on the UpdateResult. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 123 | func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult { |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 124 | type update struct { |
| 125 | Description *string `json:"display_description,omitempty"` |
| 126 | Metadata map[string]string `json:"metadata,omitempty"` |
| 127 | Name *string `json:"display_name,omitempty"` |
| 128 | } |
| 129 | |
| 130 | type request struct { |
| 131 | Volume update `json:"volume"` |
| 132 | } |
| 133 | |
| 134 | reqBody := request{ |
| 135 | Volume: update{}, |
| 136 | } |
| 137 | |
Jon Perritt | 8d26258 | 2014-10-03 11:11:46 -0500 | [diff] [blame] | 138 | reqBody.Volume.Description = gophercloud.MaybeString(opts.Description) |
| 139 | reqBody.Volume.Name = gophercloud.MaybeString(opts.Name) |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 140 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 141 | var res UpdateResult |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 142 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 143 | _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{ |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 144 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 145 | OkCodes: []int{200}, |
| 146 | ReqBody: &reqBody, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 147 | Results: &res.Resp, |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 148 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 149 | return res |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 150 | } |