blob: a51eebcc45aeef3b3f2cb6115c270a2d376143fc [file] [log] [blame]
feiskyda546142015-09-17 12:28:23 +08001package volumes
2
3import (
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02004 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
feiskyda546142015-09-17 12:28:23 +08006)
7
8// CreateOptsBuilder allows extensions to add additional parameters to the
9// Create request.
10type CreateOptsBuilder interface {
11 ToVolumeCreateMap() (map[string]interface{}, error)
12}
13
14// CreateOpts contains options for creating a Volume. This object is passed to
15// the volumes.Create function. For more information about these parameters,
16// see the Volume object.
17type CreateOpts struct {
jrperritt9b7b9e62016-07-11 22:30:50 -050018 // The size of the volume, in GB
19 Size int `json:"size" required:"true"`
20 // The availability zone
21 AvailabilityZone string `json:"availability_zone,omitempty"`
22 // ConsistencyGroupID is the ID of a consistency group
23 ConsistencyGroupID string `json:"consistencygroup_id,omitempty"`
24 // The volume description
25 Description string `json:"description,omitempty"`
26 // One or more metadata key and value pairs to associate with the volume
27 Metadata map[string]string `json:"metadata,omitempty"`
28 // The volume name
29 Name string `json:"name,omitempty"`
30 // the ID of the existing volume snapshot
31 SnapshotID string `json:"snapshot_id,omitempty"`
32 // SourceReplica is a UUID of an existing volume to replicate with
33 SourceReplica string `json:"source_replica,omitempty"`
34 // the ID of the existing volume
35 SourceVolID string `json:"source_volid,omitempty"`
feiskycf0c7fe2015-11-05 22:06:17 +080036 // The ID of the image from which you want to create the volume.
37 // Required to create a bootable volume.
jrperritt9b7b9e62016-07-11 22:30:50 -050038 ImageID string `json:"imageRef,omitempty"`
39 // The associated volume type
40 VolumeType string `json:"volume_type,omitempty"`
feiskyda546142015-09-17 12:28:23 +080041}
42
43// ToVolumeCreateMap assembles a request body based on the contents of a
44// CreateOpts.
45func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
jrperritt9b7b9e62016-07-11 22:30:50 -050046 return gophercloud.BuildRequestBody(opts, "volume")
feiskyda546142015-09-17 12:28:23 +080047}
48
49// Create will create a new Volume based on the values in CreateOpts. To extract
50// the Volume object from the response, call the Extract method on the
51// CreateResult.
jrperritt9b7b9e62016-07-11 22:30:50 -050052func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
53 b, err := opts.ToVolumeCreateMap()
feiskyda546142015-09-17 12:28:23 +080054 if err != nil {
jrperritt9b7b9e62016-07-11 22:30:50 -050055 r.Err = err
56 return
feiskyda546142015-09-17 12:28:23 +080057 }
jrperritt9b7b9e62016-07-11 22:30:50 -050058 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
feiskycf0c7fe2015-11-05 22:06:17 +080059 OkCodes: []int{202},
feiskyda546142015-09-17 12:28:23 +080060 })
jrperritt9b7b9e62016-07-11 22:30:50 -050061 return
feiskyda546142015-09-17 12:28:23 +080062}
63
64// Delete will delete the existing Volume with the provided ID.
jrperritt9b7b9e62016-07-11 22:30:50 -050065func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
66 _, r.Err = client.Delete(deleteURL(client, id), nil)
67 return
feiskyda546142015-09-17 12:28:23 +080068}
69
70// Get retrieves the Volume with the provided ID. To extract the Volume object
71// from the response, call the Extract method on the GetResult.
jrperritt9b7b9e62016-07-11 22:30:50 -050072func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
73 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
74 return
feiskyda546142015-09-17 12:28:23 +080075}
76
77// ListOptsBuilder allows extensions to add additional parameters to the List
78// request.
79type ListOptsBuilder interface {
80 ToVolumeListQuery() (string, error)
81}
82
83// ListOpts holds options for listing Volumes. It is passed to the volumes.List
84// function.
85type ListOpts struct {
86 // admin-only option. Set it to true to see all tenant volumes.
87 AllTenants bool `q:"all_tenants"`
88 // List only volumes that contain Metadata.
89 Metadata map[string]string `q:"metadata"`
90 // List only volumes that have Name as the display name.
91 Name string `q:"name"`
92 // List only volumes that have a status of Status.
93 Status string `q:"status"`
Ildar Svetlov5e650162018-04-13 16:16:57 +040094 // The ID of the last-seen item.
95 Marker string `q:"marker"`
feiskyda546142015-09-17 12:28:23 +080096}
97
98// ToVolumeListQuery formats a ListOpts into a query string.
99func (opts ListOpts) ToVolumeListQuery() (string, error) {
100 q, err := gophercloud.BuildQueryString(opts)
jrperritt9b7b9e62016-07-11 22:30:50 -0500101 return q.String(), err
feiskyda546142015-09-17 12:28:23 +0800102}
103
104// List returns Volumes optionally limited by the conditions provided in ListOpts.
105func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
106 url := listURL(client)
107 if opts != nil {
108 query, err := opts.ToVolumeListQuery()
109 if err != nil {
110 return pagination.Pager{Err: err}
111 }
112 url += query
113 }
feiskyda546142015-09-17 12:28:23 +0800114
jrperritt9b7b9e62016-07-11 22:30:50 -0500115 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
116 return VolumePage{pagination.SinglePageBase(r)}
117 })
feiskyda546142015-09-17 12:28:23 +0800118}
119
120// UpdateOptsBuilder allows extensions to add additional parameters to the
121// Update request.
122type UpdateOptsBuilder interface {
123 ToVolumeUpdateMap() (map[string]interface{}, error)
124}
125
126// UpdateOpts contain options for updating an existing Volume. This object is passed
127// to the volumes.Update function. For more information about the parameters, see
128// the Volume object.
129type UpdateOpts struct {
jrperritt9b7b9e62016-07-11 22:30:50 -0500130 Name string `json:"name,omitempty"`
131 Description string `json:"description,omitempty"`
132 Metadata map[string]string `json:"metadata,omitempty"`
feiskyda546142015-09-17 12:28:23 +0800133}
134
135// ToVolumeUpdateMap assembles a request body based on the contents of an
136// UpdateOpts.
137func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
jrperritt9b7b9e62016-07-11 22:30:50 -0500138 return gophercloud.BuildRequestBody(opts, "volume")
feiskyda546142015-09-17 12:28:23 +0800139}
140
141// Update will update the Volume with provided information. To extract the updated
142// Volume from the response, call the Extract method on the UpdateResult.
jrperritt9b7b9e62016-07-11 22:30:50 -0500143func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
144 b, err := opts.ToVolumeUpdateMap()
feiskyda546142015-09-17 12:28:23 +0800145 if err != nil {
jrperritt9b7b9e62016-07-11 22:30:50 -0500146 r.Err = err
147 return
feiskyda546142015-09-17 12:28:23 +0800148 }
jrperritt9b7b9e62016-07-11 22:30:50 -0500149 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
feiskyda546142015-09-17 12:28:23 +0800150 OkCodes: []int{200},
151 })
jrperritt9b7b9e62016-07-11 22:30:50 -0500152 return
feiskyda546142015-09-17 12:28:23 +0800153}
154
155// IDFromName is a convienience function that returns a server's ID given its name.
156func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
jrperritt9b7b9e62016-07-11 22:30:50 -0500157 count := 0
158 id := ""
159 pages, err := List(client, nil).AllPages()
160 if err != nil {
161 return "", err
feiskyda546142015-09-17 12:28:23 +0800162 }
feiskyda546142015-09-17 12:28:23 +0800163
jrperritt9b7b9e62016-07-11 22:30:50 -0500164 all, err := ExtractVolumes(pages)
165 if err != nil {
166 return "", err
167 }
feiskyda546142015-09-17 12:28:23 +0800168
jrperritt9b7b9e62016-07-11 22:30:50 -0500169 for _, s := range all {
170 if s.Name == name {
171 count++
172 id = s.ID
173 }
174 }
175
176 switch count {
feiskyda546142015-09-17 12:28:23 +0800177 case 0:
jrperritt9b7b9e62016-07-11 22:30:50 -0500178 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"}
feiskyda546142015-09-17 12:28:23 +0800179 case 1:
jrperritt9b7b9e62016-07-11 22:30:50 -0500180 return id, nil
feiskyda546142015-09-17 12:28:23 +0800181 default:
jrperritt9b7b9e62016-07-11 22:30:50 -0500182 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"}
feiskyda546142015-09-17 12:28:23 +0800183 }
184}