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