Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions openstack/blockstorage/v1/volumes/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,170 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error)
return "", fmt.Errorf("Found %d volumes matching %s", volumeCount, name)
}
}

type AttachOptsBuilder interface {
ToVolumeAttachMap() (map[string]interface{}, error)
}

type AttachOpts struct {
MountPoint string
InstanceUUID string
HostName string
Mode string
}

func (opts AttachOpts) ToVolumeAttachMap() (map[string]interface{}, error) {
v := make(map[string]interface{})

if opts.MountPoint != "" {
v["mountpoint"] = opts.MountPoint
}
if opts.Mode != "" {
v["mode"] = opts.Mode
}
if opts.InstanceUUID != "" {
v["instance_uuid"] = opts.InstanceUUID
}
if opts.HostName != "" {
v["host_name"] = opts.HostName
}

return map[string]interface{}{"os-attach": v}, nil
}

func Attach(client *gophercloud.ServiceClient, id string, opts AttachOptsBuilder) AttachResult {
var res AttachResult

reqBody, err := opts.ToVolumeAttachMap()
if err != nil {
res.Err = err
return res
}

_, res.Err = client.Post(attachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201, 202},
})

return res
}

func Detach(client *gophercloud.ServiceClient, id string) DetachResult {
var res DetachResult

v := make(map[string]interface{})
reqBody := map[string]interface{}{"os-detach": v}

_, res.Err = client.Post(detachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201, 202},
})

return res
}

func Reserve(client *gophercloud.ServiceClient, id string) ReserveResult {
var res ReserveResult

v := make(map[string]interface{})
reqBody := map[string]interface{}{"os-reserve": v}

_, res.Err = client.Post(reserveURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201, 202},
})

return res
}

func Unreserve(client *gophercloud.ServiceClient, id string) UnreserveResult {
var res UnreserveResult

v := make(map[string]interface{})
reqBody := map[string]interface{}{"os-unreserve": v}

_, res.Err = client.Post(unreserveURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201, 202},
})

return res
}

type ConnectorOptsBuilder interface {
ToConnectorMap() (map[string]interface{}, error)
}

type ConnectorOpts struct {
IP string
Host string
Initiator string
Wwpns string
Wwnns string
Multipath bool
Platform string
OSType string
}

func (opts ConnectorOpts) ToConnectorMap() (map[string]interface{}, error) {
v := make(map[string]interface{})

if opts.IP != "" {
v["ip"] = opts.IP
}
if opts.Host != "" {
v["host"] = opts.Host
}
if opts.Initiator != "" {
v["initiator"] = opts.Initiator
}
if opts.Wwpns != "" {
v["wwpns"] = opts.Wwpns
}
if opts.Wwnns != "" {
v["wwnns"] = opts.Wwnns
}

v["multipath"] = opts.Multipath

if opts.Platform != "" {
v["platform"] = opts.Platform
}
if opts.OSType != "" {
v["os_type"] = opts.OSType
}

return map[string]interface{}{"connector": v}, nil
}

func InitializeConnection(client *gophercloud.ServiceClient, id string, opts ConnectorOpts) InitializeConnectionResult {
var res InitializeConnectionResult

connctorMap, err := opts.ToConnectorMap()
if err != nil {
res.Err = err
return res
}

reqBody := map[string]interface{}{"os-initialize_connection": connctorMap}

_, res.Err = client.Post(attachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201, 202},
})

return res
}

func TerminateConnection(client *gophercloud.ServiceClient, id string, opts ConnectorOpts) TerminateConnectionResult {
var res TerminateConnectionResult

connctorMap, err := opts.ToConnectorMap()
if err != nil {
res.Err = err
return res
}

reqBody := map[string]interface{}{"os-terminate_connection": connctorMap}

_, res.Err = client.Post(attachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201, 202},
})

return res
}
30 changes: 30 additions & 0 deletions openstack/blockstorage/v1/volumes/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ type UpdateResult struct {
commonResult
}

// AttachResult contains the response body and error from a Get request.
type AttachResult struct {
commonResult
}

// DetachResult contains the response body and error from a Get request.
type DetachResult struct {
commonResult
}

// ReserveResult contains the response body and error from a Get request.
type ReserveResult struct {
commonResult
}

// UnreserveResult contains the response body and error from a Get request.
type UnreserveResult struct {
commonResult
}

// InitializeConnectionResult contains the response body and error from a Get request.
type InitializeConnectionResult struct {
commonResult
}

// TerminateConnectionResult contains the response body and error from a Get request.
type TerminateConnectionResult struct {
commonResult
}

type commonResult struct {
gophercloud.Result
}
Expand Down
24 changes: 24 additions & 0 deletions openstack/blockstorage/v1/volumes/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ func getURL(c *gophercloud.ServiceClient, id string) string {
func updateURL(c *gophercloud.ServiceClient, id string) string {
return deleteURL(c, id)
}

func attachURL(c *gophercloud.ServiceClient, id string) string {
return c.ServiceURL("volumes", id, "action")
}

func detachURL(c *gophercloud.ServiceClient, id string) string {
return attachURL(c, id)
}

func reserveURL(c *gophercloud.ServiceClient, id string) string {
return attachURL(c, id)
}

func unreserveURL(c *gophercloud.ServiceClient, id string) string {
return attachURL(c, id)
}

func initializeConnectionURL(c *gophercloud.ServiceClient, id string) string {
return attachURL(c, id)
}

func teminateConnectionURL(c *gophercloud.ServiceClient, id string) string {
return attachURL(c, id)
}