blob: 7de272f8842709f107e71aac1c6f5b0f804dfa88 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
3import (
4 "github.com/racker/perigee"
Jon Perrittd1d6a742014-09-17 01:10:59 -05005 "github.com/rackspace/gophercloud"
Jon Perritte03b35c2014-09-17 18:15:34 -05006 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perrittd1d6a742014-09-17 01:10:59 -05007 "github.com/rackspace/gophercloud/pagination"
Jon Perrittee6074f2014-04-30 18:42:32 -05008)
9
Jon Perrittd1d6a742014-09-17 01:10:59 -050010type VolumeOpts struct {
Jon Perritte03b35c2014-09-17 18:15:34 -050011 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 Perrittee6074f2014-04-30 18:42:32 -050018}
Jon Perritte77b9b22014-05-01 13:11:12 -050019
Jon Perritte03b35c2014-09-17 18:15:34 -050020func Create(client *gophercloud.ServiceClient, opts VolumeOpts) (*Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050021
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 Perritt94963ad2014-05-05 12:14:39 -050032 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050033
34 type request struct {
35 Volume volume `json:"volume"`
Jon Perritt94963ad2014-05-05 12:14:39 -050036 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050037
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 Perritte03b35c2014-09-17 18:15:34 -050057 _, err := perigee.Request("POST", volumesURL(client), perigee.Options{
58 MoreHeaders: client.Provider.AuthenticatedHeaders(),
59 OkCodes: []int{200, 201},
Jon Perrittd1d6a742014-09-17 01:10:59 -050060 ReqBody: &reqBody,
61 Results: &respBody,
Jon Perritt94963ad2014-05-05 12:14:39 -050062 })
Jon Perrittd1d6a742014-09-17 01:10:59 -050063 if err != nil {
64 return nil, err
65 }
66
67 return &respBody.Volume, nil
68}
69
70func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perrittd1d6a742014-09-17 01:10:59 -050071
72 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Jon Perritte03b35c2014-09-17 18:15:34 -050073 return ListResult{pagination.SinglePageBase(r)}
Jon Perrittd1d6a742014-09-17 01:10:59 -050074 }
75
Jon Perritte03b35c2014-09-17 18:15:34 -050076 return pagination.NewPager(client, volumesURL(client), createPage)
Jon Perritt94963ad2014-05-05 12:14:39 -050077}
78
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050079func 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 Perritt70dd47d2014-05-01 13:51:53 -050084 })
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050085 return gr, err
Jon Perritt70dd47d2014-05-01 13:51:53 -050086}
87
Jon Perritt3db65cd2014-09-17 18:32:43 -050088func Delete(client *gophercloud.ServiceClient, id string) error {
89 _, err := perigee.Request("DELETE", volumeURL(client, id), perigee.Options{
90 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritte77b9b22014-05-01 13:11:12 -050091 })
92 return err
93}