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 | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame^] | 20 | func Create(client *gophercloud.ServiceClient, opts CreateOpts) (*Volume, error) { |
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 | |
| 51 | type response struct { |
| 52 | Volume Volume `json:"volume"` |
| 53 | } |
| 54 | |
| 55 | var respBody response |
| 56 | |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 57 | _, err := perigee.Request("POST", volumesURL(client), perigee.Options{ |
| 58 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 59 | OkCodes: []int{200, 201}, |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 60 | ReqBody: &reqBody, |
| 61 | Results: &respBody, |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 62 | }) |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | |
| 67 | return &respBody.Volume, nil |
| 68 | } |
| 69 | |
| 70 | func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 71 | |
| 72 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 73 | return ListResult{pagination.SinglePageBase(r)} |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 76 | return pagination.NewPager(client, volumesURL(client), createPage) |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 79 | func Get(client *gophercloud.ServiceClient, id string) (GetResult, error) { |
| 80 | var gr GetResult |
| 81 | _, err := perigee.Request("GET", volumeURL(client, id), perigee.Options{ |
| 82 | Results: &gr, |
| 83 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 84 | }) |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 85 | return gr, err |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame^] | 88 | type UpdateOpts struct { |
| 89 | Name string |
| 90 | Description string |
| 91 | Metadata map[string]string |
| 92 | } |
| 93 | |
| 94 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Volume, error) { |
| 95 | type update struct { |
| 96 | Description *string `json:"display_description,omitempty"` |
| 97 | Metadata map[string]string `json:"metadata,omitempty"` |
| 98 | Name *string `json:"display_name,omitempty"` |
| 99 | } |
| 100 | |
| 101 | type request struct { |
| 102 | Volume update `json:"volume"` |
| 103 | } |
| 104 | |
| 105 | reqBody := request{ |
| 106 | Volume: update{}, |
| 107 | } |
| 108 | |
| 109 | reqBody.Volume.Description = utils.MaybeString(opts.Description) |
| 110 | reqBody.Volume.Name = utils.MaybeString(opts.Name) |
| 111 | |
| 112 | type response struct { |
| 113 | Volume Volume `json:"volume"` |
| 114 | } |
| 115 | |
| 116 | var respBody response |
| 117 | |
| 118 | _, err := perigee.Request("PUT", volumeURL(client, id), perigee.Options{ |
| 119 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 120 | OkCodes: []int{200}, |
| 121 | ReqBody: &reqBody, |
| 122 | Results: &respBody, |
| 123 | }) |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | |
| 128 | return &respBody.Volume, nil |
| 129 | |
| 130 | } |
| 131 | |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 132 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 133 | _, err := perigee.Request("DELETE", volumeURL(client, id), perigee.Options{ |
| 134 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 135 | }) |
| 136 | return err |
| 137 | } |