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