blob: 1aff4942ae5a7c5035d141794f67ba2e60048eb5 [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
Eugene Yakubovichb3a4f332016-10-13 11:01:06 -070053// BeginDetach will mark the volume as detaching
54func BeginDetaching(client *gophercloud.ServiceClient, id string) (r BeginDetachingResult) {
55 b := map[string]interface{}{"os-begin_detaching": make(map[string]interface{})}
56 _, r.Err = client.Post(beginDetachingURL(client, id), b, nil, &gophercloud.RequestOpts{
57 OkCodes: []int{202},
58 })
59 return
60}
61
jrperritt9b7b9e62016-07-11 22:30:50 -050062// DetachOptsBuilder allows extensions to add additional parameters to the
63// Detach request.
64type DetachOptsBuilder interface {
65 ToVolumeDetachMap() (map[string]interface{}, error)
66}
feiskycf0c7fe2015-11-05 22:06:17 +080067
jrperritt9b7b9e62016-07-11 22:30:50 -050068type DetachOpts struct {
69 AttachmentID string `json:"attachment_id,omitempty"`
70}
feiskycf0c7fe2015-11-05 22:06:17 +080071
jrperritt9b7b9e62016-07-11 22:30:50 -050072// ToVolumeDetachMap assembles a request body based on the contents of a
73// DetachOpts.
74func (opts DetachOpts) ToVolumeDetachMap() (map[string]interface{}, error) {
75 return gophercloud.BuildRequestBody(opts, "os-detach")
76}
77
78// Detach will detach a volume based on volume id.
79func Detach(client *gophercloud.ServiceClient, id string, opts DetachOptsBuilder) (r DetachResult) {
80 b, err := opts.ToVolumeDetachMap()
81 if err != nil {
82 r.Err = err
83 return
84 }
85 _, r.Err = client.Post(detachURL(client, id), b, nil, &gophercloud.RequestOpts{
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080086 OkCodes: []int{202},
feiskycf0c7fe2015-11-05 22:06:17 +080087 })
jrperritt9b7b9e62016-07-11 22:30:50 -050088 return
feiskycf0c7fe2015-11-05 22:06:17 +080089}
90
Pengfei Ni8bfbfb02016-04-29 16:04:12 +080091// Reserve will reserve a volume based on volume id.
jrperritt9b7b9e62016-07-11 22:30:50 -050092func Reserve(client *gophercloud.ServiceClient, id string) (r ReserveResult) {
93 b := map[string]interface{}{"os-reserve": make(map[string]interface{})}
94 _, r.Err = client.Post(reserveURL(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
Pengfei Ni8bfbfb02016-04-29 16:04:12 +0800100// Unreserve will unreserve a volume based on volume id.
jrperritt9b7b9e62016-07-11 22:30:50 -0500101func Unreserve(client *gophercloud.ServiceClient, id string) (r UnreserveResult) {
102 b := map[string]interface{}{"os-unreserve": make(map[string]interface{})}
103 _, r.Err = client.Post(unreserveURL(client, id), b, nil, &gophercloud.RequestOpts{
feiskycf0c7fe2015-11-05 22:06:17 +0800104 OkCodes: []int{200, 201, 202},
105 })
jrperritt9b7b9e62016-07-11 22:30:50 -0500106 return
feiskycf0c7fe2015-11-05 22:06:17 +0800107}
108
jrperritt9b7b9e62016-07-11 22:30:50 -0500109// InitializeConnectionOptsBuilder allows extensions to add additional parameters to the
Pengfei Ni8bfbfb02016-04-29 16:04:12 +0800110// InitializeConnection request.
jrperritt9b7b9e62016-07-11 22:30:50 -0500111type InitializeConnectionOptsBuilder interface {
112 ToVolumeInitializeConnectionMap() (map[string]interface{}, error)
feiskycf0c7fe2015-11-05 22:06:17 +0800113}
114
jrperritt9b7b9e62016-07-11 22:30:50 -0500115// InitializeConnectionOpts hosts options for InitializeConnection.
116type InitializeConnectionOpts struct {
117 IP string `json:"ip,omitempty"`
118 Host string `json:"host,omitempty"`
119 Initiator string `json:"initiator,omitempty"`
120 Wwpns []string `json:"wwpns,omitempty"`
121 Wwnns string `json:"wwnns,omitempty"`
122 Multipath *bool `json:"multipath,omitempty"`
123 Platform string `json:"platform,omitempty"`
124 OSType string `json:"os_type,omitempty"`
feiskycf0c7fe2015-11-05 22:06:17 +0800125}
126
jrperritt9b7b9e62016-07-11 22:30:50 -0500127// ToVolumeInitializeConnectionMap assembles a request body based on the contents of a
128// InitializeConnectionOpts.
129func (opts InitializeConnectionOpts) ToVolumeInitializeConnectionMap() (map[string]interface{}, error) {
130 b, err := gophercloud.BuildRequestBody(opts, "connector")
131 return map[string]interface{}{"os-initialize_connection": b}, err
feiskycf0c7fe2015-11-05 22:06:17 +0800132}
133
Pengfei Ni8bfbfb02016-04-29 16:04:12 +0800134// InitializeConnection initializes iscsi connection.
jrperritt9b7b9e62016-07-11 22:30:50 -0500135func InitializeConnection(client *gophercloud.ServiceClient, id string, opts InitializeConnectionOptsBuilder) (r InitializeConnectionResult) {
136 b, err := opts.ToVolumeInitializeConnectionMap()
feiskycf0c7fe2015-11-05 22:06:17 +0800137 if err != nil {
jrperritt9b7b9e62016-07-11 22:30:50 -0500138 r.Err = err
139 return
feiskycf0c7fe2015-11-05 22:06:17 +0800140 }
jrperritt9b7b9e62016-07-11 22:30:50 -0500141 _, r.Err = client.Post(initializeConnectionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
feiskycf0c7fe2015-11-05 22:06:17 +0800142 OkCodes: []int{200, 201, 202},
143 })
jrperritt9b7b9e62016-07-11 22:30:50 -0500144 return
145}
feiskycf0c7fe2015-11-05 22:06:17 +0800146
jrperritt9b7b9e62016-07-11 22:30:50 -0500147// TerminateConnectionOptsBuilder allows extensions to add additional parameters to the
148// TerminateConnection request.
149type TerminateConnectionOptsBuilder interface {
150 ToVolumeTerminateConnectionMap() (map[string]interface{}, error)
151}
152
153// TerminateConnectionOpts hosts options for TerminateConnection.
154type TerminateConnectionOpts struct {
155 IP string `json:"ip,omitempty"`
156 Host string `json:"host,omitempty"`
157 Initiator string `json:"initiator,omitempty"`
158 Wwpns []string `json:"wwpns,omitempty"`
159 Wwnns string `json:"wwnns,omitempty"`
160 Multipath *bool `json:"multipath,omitempty"`
161 Platform string `json:"platform,omitempty"`
162 OSType string `json:"os_type,omitempty"`
163}
164
165// ToVolumeTerminateConnectionMap assembles a request body based on the contents of a
166// TerminateConnectionOpts.
167func (opts TerminateConnectionOpts) ToVolumeTerminateConnectionMap() (map[string]interface{}, error) {
168 b, err := gophercloud.BuildRequestBody(opts, "connector")
169 return map[string]interface{}{"os-terminate_connection": b}, err
feiskycf0c7fe2015-11-05 22:06:17 +0800170}
171
Pengfei Ni8bfbfb02016-04-29 16:04:12 +0800172// TerminateConnection terminates iscsi connection.
jrperritt9b7b9e62016-07-11 22:30:50 -0500173func TerminateConnection(client *gophercloud.ServiceClient, id string, opts TerminateConnectionOptsBuilder) (r TerminateConnectionResult) {
174 b, err := opts.ToVolumeTerminateConnectionMap()
feiskycf0c7fe2015-11-05 22:06:17 +0800175 if err != nil {
jrperritt9b7b9e62016-07-11 22:30:50 -0500176 r.Err = err
177 return
feiskycf0c7fe2015-11-05 22:06:17 +0800178 }
jrperritt9b7b9e62016-07-11 22:30:50 -0500179 _, r.Err = client.Post(teminateConnectionURL(client, id), b, nil, &gophercloud.RequestOpts{
Jamie Hannaford531e0cc2016-05-13 13:03:39 +0200180 OkCodes: []int{202},
feiskycf0c7fe2015-11-05 22:06:17 +0800181 })
jrperritt9b7b9e62016-07-11 22:30:50 -0500182 return
feiskycf0c7fe2015-11-05 22:06:17 +0800183}
Mario Luana4d49302016-09-02 11:37:24 -0400184
185// ExtendSizeOptsBuilder allows extensions to add additional parameters to the
186// ExtendSize request.
187type ExtendSizeOptsBuilder interface {
188 ToVolumeExtendSizeMap() (map[string]interface{}, error)
189}
190
191// ExtendSizeOpts contain options for extending the size of an existing Volume. This object is passed
192// to the volumes.ExtendSize function.
193type ExtendSizeOpts struct {
194 // NewSize is the new size of the volume, in GB
195 NewSize int `json:"new_size" required:"true"`
196}
197
198// ToVolumeExtendSizeMap assembles a request body based on the contents of an
199// ExtendSizeOpts.
200func (opts ExtendSizeOpts) ToVolumeExtendSizeMap() (map[string]interface{}, error) {
201 return gophercloud.BuildRequestBody(opts, "os-extend")
202}
203
204// ExtendSize will extend the size of the volume based on the provided information.
205// This operation does not return a response body.
206func ExtendSize(client *gophercloud.ServiceClient, id string, opts ExtendSizeOptsBuilder) (r ExtendSizeResult) {
207 b, err := opts.ToVolumeExtendSizeMap()
208 if err != nil {
209 r.Err = err
210 return
211 }
212 _, r.Err = client.Post(extendSizeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
213 OkCodes: []int{202},
214 })
215 return
216}