feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 1 | package volumeactions |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud" |
| 5 | ) |
| 6 | |
| 7 | type AttachOptsBuilder interface { |
| 8 | ToVolumeAttachMap() (map[string]interface{}, error) |
| 9 | } |
| 10 | |
| 11 | type AttachOpts struct { |
| 12 | // The mountpoint of this volume |
| 13 | MountPoint string |
| 14 | // The nova instance ID, can't set simultaneously with HostName |
| 15 | InstanceUUID string |
| 16 | // The hostname of baremetal host, can't set simultaneously with InstanceUUID |
| 17 | HostName string |
| 18 | // Mount mode of this volume |
| 19 | Mode string |
| 20 | } |
| 21 | |
| 22 | func (opts AttachOpts) ToVolumeAttachMap() (map[string]interface{}, error) { |
| 23 | v := make(map[string]interface{}) |
| 24 | |
| 25 | if opts.MountPoint != "" { |
| 26 | v["mountpoint"] = opts.MountPoint |
| 27 | } |
| 28 | if opts.Mode != "" { |
| 29 | v["mode"] = opts.Mode |
| 30 | } |
| 31 | if opts.InstanceUUID != "" { |
| 32 | v["instance_uuid"] = opts.InstanceUUID |
| 33 | } |
| 34 | if opts.HostName != "" { |
| 35 | v["host_name"] = opts.HostName |
| 36 | } |
| 37 | |
| 38 | return map[string]interface{}{"os-attach": v}, nil |
| 39 | } |
| 40 | |
| 41 | func Attach(client *gophercloud.ServiceClient, id string, opts AttachOptsBuilder) AttachResult { |
| 42 | var res AttachResult |
| 43 | |
| 44 | reqBody, err := opts.ToVolumeAttachMap() |
| 45 | if err != nil { |
| 46 | res.Err = err |
| 47 | return res |
| 48 | } |
| 49 | |
| 50 | _, res.Err = client.Post(attachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 51 | OkCodes: []int{200, 201, 202}, |
| 52 | }) |
| 53 | |
| 54 | return res |
| 55 | } |
| 56 | |
| 57 | func Detach(client *gophercloud.ServiceClient, id string) DetachResult { |
| 58 | var res DetachResult |
| 59 | |
| 60 | v := make(map[string]interface{}) |
| 61 | reqBody := map[string]interface{}{"os-detach": v} |
| 62 | |
| 63 | _, res.Err = client.Post(detachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 64 | OkCodes: []int{200, 201, 202}, |
| 65 | }) |
| 66 | |
| 67 | return res |
| 68 | } |
| 69 | |
| 70 | func Reserve(client *gophercloud.ServiceClient, id string) ReserveResult { |
| 71 | var res ReserveResult |
| 72 | |
| 73 | v := make(map[string]interface{}) |
| 74 | reqBody := map[string]interface{}{"os-reserve": v} |
| 75 | |
| 76 | _, res.Err = client.Post(reserveURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 77 | OkCodes: []int{200, 201, 202}, |
| 78 | }) |
| 79 | |
| 80 | return res |
| 81 | } |
| 82 | |
| 83 | func Unreserve(client *gophercloud.ServiceClient, id string) UnreserveResult { |
| 84 | var res UnreserveResult |
| 85 | |
| 86 | v := make(map[string]interface{}) |
| 87 | reqBody := map[string]interface{}{"os-unreserve": v} |
| 88 | |
| 89 | _, res.Err = client.Post(unreserveURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 90 | OkCodes: []int{200, 201, 202}, |
| 91 | }) |
| 92 | |
| 93 | return res |
| 94 | } |
| 95 | |
| 96 | type ConnectorOptsBuilder interface { |
| 97 | ToConnectorMap() (map[string]interface{}, error) |
| 98 | } |
| 99 | |
| 100 | type ConnectorOpts struct { |
| 101 | IP string |
| 102 | Host string |
| 103 | Initiator string |
| 104 | Wwpns string |
| 105 | Wwnns string |
| 106 | Multipath bool |
| 107 | Platform string |
| 108 | OSType string |
| 109 | } |
| 110 | |
| 111 | func (opts ConnectorOpts) ToConnectorMap() (map[string]interface{}, error) { |
| 112 | v := make(map[string]interface{}) |
| 113 | |
| 114 | if opts.IP != "" { |
| 115 | v["ip"] = opts.IP |
| 116 | } |
| 117 | if opts.Host != "" { |
| 118 | v["host"] = opts.Host |
| 119 | } |
| 120 | if opts.Initiator != "" { |
| 121 | v["initiator"] = opts.Initiator |
| 122 | } |
| 123 | if opts.Wwpns != "" { |
| 124 | v["wwpns"] = opts.Wwpns |
| 125 | } |
| 126 | if opts.Wwnns != "" { |
| 127 | v["wwnns"] = opts.Wwnns |
| 128 | } |
| 129 | |
| 130 | v["multipath"] = opts.Multipath |
| 131 | |
| 132 | if opts.Platform != "" { |
| 133 | v["platform"] = opts.Platform |
| 134 | } |
| 135 | if opts.OSType != "" { |
| 136 | v["os_type"] = opts.OSType |
| 137 | } |
| 138 | |
| 139 | return map[string]interface{}{"connector": v}, nil |
| 140 | } |
| 141 | |
| 142 | func InitializeConnection(client *gophercloud.ServiceClient, id string, opts *ConnectorOpts) InitializeConnectionResult { |
| 143 | var res InitializeConnectionResult |
| 144 | |
| 145 | connctorMap, err := opts.ToConnectorMap() |
| 146 | if err != nil { |
| 147 | res.Err = err |
| 148 | return res |
| 149 | } |
| 150 | |
| 151 | reqBody := map[string]interface{}{"os-initialize_connection": connctorMap} |
| 152 | |
| 153 | _, res.Err = client.Post(initializeConnectionURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 154 | OkCodes: []int{200, 201, 202}, |
| 155 | }) |
| 156 | |
| 157 | return res |
| 158 | } |
| 159 | |
| 160 | func TerminateConnection(client *gophercloud.ServiceClient, id string, opts *ConnectorOpts) TerminateConnectionResult { |
| 161 | var res TerminateConnectionResult |
| 162 | |
| 163 | connctorMap, err := opts.ToConnectorMap() |
| 164 | if err != nil { |
| 165 | res.Err = err |
| 166 | return res |
| 167 | } |
| 168 | |
| 169 | reqBody := map[string]interface{}{"os-terminate_connection": connctorMap} |
| 170 | |
| 171 | _, res.Err = client.Post(teminateConnectionURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 172 | OkCodes: []int{200, 201, 202}, |
| 173 | }) |
| 174 | |
| 175 | return res |
| 176 | } |