blob: d2a2c79123cce30d9f976b2d3dd5044c82a57ba3 [file] [log] [blame]
feiskycf0c7fe2015-11-05 22:06:17 +08001package volumeactions
2
3import (
jrperritt9b7b9e62016-07-11 22:30:50 -05004 "github.com/gophercloud/gophercloud"
feiskycf0c7fe2015-11-05 22:06:17 +08005)
6
Pengfei Ni8bfbfb02016-04-29 16:04:12 +08007// AttachOptsBuilder allows extensions to add additional parameters to the
8// Attach request.
feiskycf0c7fe2015-11-05 22:06:17 +08009type AttachOptsBuilder interface {
10 ToVolumeAttachMap() (map[string]interface{}, error)
11}
12
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080013// AttachMode describes the attachment mode for volumes.
14type AttachMode string
15
16// These constants determine how a volume is attached
17const (
18 ReadOnly AttachMode = "ro"
19 ReadWrite AttachMode = "rw"
20)
21
22// AttachOpts contains options for attaching a Volume.
feiskycf0c7fe2015-11-05 22:06:17 +080023type AttachOpts struct {
24 // The mountpoint of this volume
jrperritt9b7b9e62016-07-11 22:30:50 -050025 MountPoint string `json:"mountpoint,omitempty"`
feiskycf0c7fe2015-11-05 22:06:17 +080026 // The nova instance ID, can't set simultaneously with HostName
jrperritt9b7b9e62016-07-11 22:30:50 -050027 InstanceUUID string `json:"instance_uuid,omitempty"`
feiskycf0c7fe2015-11-05 22:06:17 +080028 // The hostname of baremetal host, can't set simultaneously with InstanceUUID
jrperritt9b7b9e62016-07-11 22:30:50 -050029 HostName string `json:"host_name,omitempty"`
feiskycf0c7fe2015-11-05 22:06:17 +080030 // Mount mode of this volume
jrperritt9b7b9e62016-07-11 22:30:50 -050031 Mode AttachMode `json:"mode,omitempty"`
feiskycf0c7fe2015-11-05 22:06:17 +080032}
33
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080034// ToVolumeAttachMap assembles a request body based on the contents of a
35// AttachOpts.
feiskycf0c7fe2015-11-05 22:06:17 +080036func (opts AttachOpts) ToVolumeAttachMap() (map[string]interface{}, error) {
jrperritt9b7b9e62016-07-11 22:30:50 -050037 return gophercloud.BuildRequestBody(opts, "os-attach")
feiskycf0c7fe2015-11-05 22:06:17 +080038}
39
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080040// Attach will attach a volume based on the values in AttachOpts.
jrperritt9b7b9e62016-07-11 22:30:50 -050041func Attach(client *gophercloud.ServiceClient, id string, opts AttachOptsBuilder) (r AttachResult) {
42 b, err := opts.ToVolumeAttachMap()
feiskycf0c7fe2015-11-05 22:06:17 +080043 if err != nil {
jrperritt9b7b9e62016-07-11 22:30:50 -050044 r.Err = err
45 return
feiskycf0c7fe2015-11-05 22:06:17 +080046 }
jrperritt9b7b9e62016-07-11 22:30:50 -050047 _, r.Err = client.Post(attachURL(client, id), b, nil, &gophercloud.RequestOpts{
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080048 OkCodes: []int{202},
feiskycf0c7fe2015-11-05 22:06:17 +080049 })
jrperritt9b7b9e62016-07-11 22:30:50 -050050 return
feiskycf0c7fe2015-11-05 22:06:17 +080051}
52
jrperritt9b7b9e62016-07-11 22:30:50 -050053// DetachOptsBuilder allows extensions to add additional parameters to the
54// Detach request.
55type DetachOptsBuilder interface {
56 ToVolumeDetachMap() (map[string]interface{}, error)
57}
feiskycf0c7fe2015-11-05 22:06:17 +080058
jrperritt9b7b9e62016-07-11 22:30:50 -050059type DetachOpts struct {
60 AttachmentID string `json:"attachment_id,omitempty"`
61}
feiskycf0c7fe2015-11-05 22:06:17 +080062
jrperritt9b7b9e62016-07-11 22:30:50 -050063// ToVolumeDetachMap assembles a request body based on the contents of a
64// DetachOpts.
65func (opts DetachOpts) ToVolumeDetachMap() (map[string]interface{}, error) {
66 return gophercloud.BuildRequestBody(opts, "os-detach")
67}
68
69// Detach will detach a volume based on volume id.
70func Detach(client *gophercloud.ServiceClient, id string, opts DetachOptsBuilder) (r DetachResult) {
71 b, err := opts.ToVolumeDetachMap()
72 if err != nil {
73 r.Err = err
74 return
75 }
76 _, r.Err = client.Post(detachURL(client, id), b, nil, &gophercloud.RequestOpts{
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080077 OkCodes: []int{202},
feiskycf0c7fe2015-11-05 22:06:17 +080078 })
jrperritt9b7b9e62016-07-11 22:30:50 -050079 return
feiskycf0c7fe2015-11-05 22:06:17 +080080}
81
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080082// Reserve will reserve a volume based on volume id.
jrperritt9b7b9e62016-07-11 22:30:50 -050083func Reserve(client *gophercloud.ServiceClient, id string) (r ReserveResult) {
84 b := map[string]interface{}{"os-reserve": make(map[string]interface{})}
85 _, r.Err = client.Post(reserveURL(client, id), b, nil, &gophercloud.RequestOpts{
feiskycf0c7fe2015-11-05 22:06:17 +080086 OkCodes: []int{200, 201, 202},
87 })
jrperritt9b7b9e62016-07-11 22:30:50 -050088 return
feiskycf0c7fe2015-11-05 22:06:17 +080089}
90
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080091// Unreserve will unreserve a volume based on volume id.
jrperritt9b7b9e62016-07-11 22:30:50 -050092func Unreserve(client *gophercloud.ServiceClient, id string) (r UnreserveResult) {
93 b := map[string]interface{}{"os-unreserve": make(map[string]interface{})}
94 _, r.Err = client.Post(unreserveURL(client, id), b, nil, &gophercloud.RequestOpts{
feiskycf0c7fe2015-11-05 22:06:17 +080095 OkCodes: []int{200, 201, 202},
96 })
jrperritt9b7b9e62016-07-11 22:30:50 -050097 return
feiskycf0c7fe2015-11-05 22:06:17 +080098}
99
jrperritt9b7b9e62016-07-11 22:30:50 -0500100// InitializeConnectionOptsBuilder allows extensions to add additional parameters to the
Pengfei Ni8bfbfb02016-04-29 16:04:12 +0800101// InitializeConnection request.
jrperritt9b7b9e62016-07-11 22:30:50 -0500102type InitializeConnectionOptsBuilder interface {
103 ToVolumeInitializeConnectionMap() (map[string]interface{}, error)
feiskycf0c7fe2015-11-05 22:06:17 +0800104}
105
jrperritt9b7b9e62016-07-11 22:30:50 -0500106// InitializeConnectionOpts hosts options for InitializeConnection.
107type InitializeConnectionOpts struct {
108 IP string `json:"ip,omitempty"`
109 Host string `json:"host,omitempty"`
110 Initiator string `json:"initiator,omitempty"`
111 Wwpns []string `json:"wwpns,omitempty"`
112 Wwnns string `json:"wwnns,omitempty"`
113 Multipath *bool `json:"multipath,omitempty"`
114 Platform string `json:"platform,omitempty"`
115 OSType string `json:"os_type,omitempty"`
feiskycf0c7fe2015-11-05 22:06:17 +0800116}
117
jrperritt9b7b9e62016-07-11 22:30:50 -0500118// ToVolumeInitializeConnectionMap assembles a request body based on the contents of a
119// InitializeConnectionOpts.
120func (opts InitializeConnectionOpts) ToVolumeInitializeConnectionMap() (map[string]interface{}, error) {
121 b, err := gophercloud.BuildRequestBody(opts, "connector")
122 return map[string]interface{}{"os-initialize_connection": b}, err
feiskycf0c7fe2015-11-05 22:06:17 +0800123}
124
Pengfei Ni8bfbfb02016-04-29 16:04:12 +0800125// InitializeConnection initializes iscsi connection.
jrperritt9b7b9e62016-07-11 22:30:50 -0500126func InitializeConnection(client *gophercloud.ServiceClient, id string, opts InitializeConnectionOptsBuilder) (r InitializeConnectionResult) {
127 b, err := opts.ToVolumeInitializeConnectionMap()
feiskycf0c7fe2015-11-05 22:06:17 +0800128 if err != nil {
jrperritt9b7b9e62016-07-11 22:30:50 -0500129 r.Err = err
130 return
feiskycf0c7fe2015-11-05 22:06:17 +0800131 }
jrperritt9b7b9e62016-07-11 22:30:50 -0500132 _, r.Err = client.Post(initializeConnectionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
feiskycf0c7fe2015-11-05 22:06:17 +0800133 OkCodes: []int{200, 201, 202},
134 })
jrperritt9b7b9e62016-07-11 22:30:50 -0500135 return
136}
feiskycf0c7fe2015-11-05 22:06:17 +0800137
jrperritt9b7b9e62016-07-11 22:30:50 -0500138// TerminateConnectionOptsBuilder allows extensions to add additional parameters to the
139// TerminateConnection request.
140type TerminateConnectionOptsBuilder interface {
141 ToVolumeTerminateConnectionMap() (map[string]interface{}, error)
142}
143
144// TerminateConnectionOpts hosts options for TerminateConnection.
145type TerminateConnectionOpts struct {
146 IP string `json:"ip,omitempty"`
147 Host string `json:"host,omitempty"`
148 Initiator string `json:"initiator,omitempty"`
149 Wwpns []string `json:"wwpns,omitempty"`
150 Wwnns string `json:"wwnns,omitempty"`
151 Multipath *bool `json:"multipath,omitempty"`
152 Platform string `json:"platform,omitempty"`
153 OSType string `json:"os_type,omitempty"`
154}
155
156// ToVolumeTerminateConnectionMap assembles a request body based on the contents of a
157// TerminateConnectionOpts.
158func (opts TerminateConnectionOpts) ToVolumeTerminateConnectionMap() (map[string]interface{}, error) {
159 b, err := gophercloud.BuildRequestBody(opts, "connector")
160 return map[string]interface{}{"os-terminate_connection": b}, err
feiskycf0c7fe2015-11-05 22:06:17 +0800161}
162
Pengfei Ni8bfbfb02016-04-29 16:04:12 +0800163// TerminateConnection terminates iscsi connection.
jrperritt9b7b9e62016-07-11 22:30:50 -0500164func TerminateConnection(client *gophercloud.ServiceClient, id string, opts TerminateConnectionOptsBuilder) (r TerminateConnectionResult) {
165 b, err := opts.ToVolumeTerminateConnectionMap()
feiskycf0c7fe2015-11-05 22:06:17 +0800166 if err != nil {
jrperritt9b7b9e62016-07-11 22:30:50 -0500167 r.Err = err
168 return
feiskycf0c7fe2015-11-05 22:06:17 +0800169 }
jrperritt9b7b9e62016-07-11 22:30:50 -0500170 _, r.Err = client.Post(teminateConnectionURL(client, id), b, nil, &gophercloud.RequestOpts{
Jamie Hannaford531e0cc2016-05-13 13:03:39 +0200171 OkCodes: []int{202},
feiskycf0c7fe2015-11-05 22:06:17 +0800172 })
jrperritt9b7b9e62016-07-11 22:30:50 -0500173 return
feiskycf0c7fe2015-11-05 22:06:17 +0800174}
Mario Luana4d49302016-09-02 11:37:24 -0400175
176// ExtendSizeOptsBuilder allows extensions to add additional parameters to the
177// ExtendSize request.
178type ExtendSizeOptsBuilder interface {
179 ToVolumeExtendSizeMap() (map[string]interface{}, error)
180}
181
182// ExtendSizeOpts contain options for extending the size of an existing Volume. This object is passed
183// to the volumes.ExtendSize function.
184type ExtendSizeOpts struct {
185 // NewSize is the new size of the volume, in GB
186 NewSize int `json:"new_size" required:"true"`
187}
188
189// ToVolumeExtendSizeMap assembles a request body based on the contents of an
190// ExtendSizeOpts.
191func (opts ExtendSizeOpts) ToVolumeExtendSizeMap() (map[string]interface{}, error) {
192 return gophercloud.BuildRequestBody(opts, "os-extend")
193}
194
195// ExtendSize will extend the size of the volume based on the provided information.
196// This operation does not return a response body.
197func ExtendSize(client *gophercloud.ServiceClient, id string, opts ExtendSizeOptsBuilder) (r ExtendSizeResult) {
198 b, err := opts.ToVolumeExtendSizeMap()
199 if err != nil {
200 r.Err = err
201 return
202 }
203 _, r.Err = client.Post(extendSizeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
204 OkCodes: []int{202},
205 })
206 return
207}