/usr/lib/python3/dist-packages/hplefthandclient/client.py is in python3-hplefthandclient 1.0.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | # vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2012 Hewlett Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
HPLeftHand REST Client
.. module: HPLeftHandClient
.. moduleauthor: Kurt Martin
:Author: Kurt Martin
:Description: This is the LeftHand/StoreVirtual Client that talks to the
LeftHand OS REST Service.
This client requires and works with version 11.5 of the LeftHand firmware
"""
from hplefthandclient import http
class HPLeftHandClient:
def __init__(self, api_url):
self.api_url = api_url
self.http = http.HTTPJSONRESTClient(self.api_url)
def debug_rest(self, flag):
"""
This is useful for debugging requests to LeftHand
:param flag: set to True to enable debugging
:type flag: bool
"""
self.http.set_debug_flag(flag)
def login(self, username, password):
"""
This authenticates against the LH OS REST server and creates a session.
:param username: The username
:type username: str
:param password: The password
:type password: str
:returns: None
"""
self.http.authenticate(username, password)
def logout(self):
"""
This destroys the session and logs out from the LH OS server
:returns: None
"""
self.http.unauthenticate()
def getClusters(self):
"""
Get the list of Clusters
:returns: list of Clusters
"""
response, body = self.http.get('/clusters')
return body
def getCluster(self, cluster_id):
"""
Get information about a Cluster
:param cluster_id: The id of the cluster to find
:type cluster_id: str
:returns: cluster
"""
response, body = self.http.get('/clusters/%s' % cluster_id)
return body
def getClusterByName(self, name):
"""
Get information about a cluster by name
:param name: The name of the cluster to find
:type name: str
:returns: cluster
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_CLUSTER - cluster doesn't exist
"""
response, body = self.http.get('/clusters?name=%s' % name)
return body
def getServers(self):
"""
Get the list of Servers
:returns: list of Servers
"""
response, body = self.http.get('/servers')
return body
def getServer(self, server_id):
"""
Get information about a server
:param server_id: The id of the server to find
:type server_id: str
:returns: server
:raises: :class:`~hplefthandclient.exceptions.HTTPServerError`
"""
response, body = self.http.get('/servers/%s' % server_id)
return body
def getServerByName(self, name):
"""
Get information about a server by name
:param name: The name of the server to find
:type name: str
:returns: server
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_SERVER - server doesn't exist
"""
response, body = self.http.get('/servers?name=%s' % name)
return body
def createServer(self, name, iqn, optional=None):
"""
Create a server by name
:param name: The name of the server to create
:type name: str
:param iqn: The iSCSI qualified name
:type name: str
:param optional: Dictionary of optional params
:type optional: dict
.. code-block:: python
optional = {
'description' : "some comment",
'iscsiEnabled' : True,
'chapName': "some chap name",
'chapAuthenticationRequired': False,
'chapInitiatorSecret': "initiator secret",
'chapTargetSecret': "target secret",
'iscsiLoadBalancingEnabled': True,
'controllingServerName': "server name",
'fibreChannelEnabled': False,
'inServerCluster": True
}
:returns: server
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_SERVER - server doesn't exist
"""
info = {'name': name, 'iscsiIQN': iqn}
if optional:
info = self._mergeDict(info, optional)
response, body = self.http.post('/servers', body=info)
return body
def deleteServer(self, server_id):
"""
Delete a Server
:param server_id: the server ID to delete
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_SERVER - The server does not exist
"""
response, body = self.http.delete('/servers/%s' % server_id)
return body
def getSnapshots(self):
"""
Get the list of Snapshots
:returns: list of Snapshots
"""
response, body = self.http.get('/snapshots')
return body
def getSnapshot(self, snapshot_id):
"""
Get information about a Snapshot
:returns: snapshot
:raises: :class:`~hplefthandclient.exceptions.HTTPServerError`
"""
response, body = self.http.get('/snapshots/%s' % snapshot_id)
return body
def getSnapshotByName(self, name):
"""
Get information about a snapshot by name
:param name: The name of the snapshot to find
:returns: volume
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_SNAP - shapshot doesn't exist
"""
response, body = self.http.get('/snapshots?name=%s' % name)
return body
def createSnapshot(self, name, source_volume_id, optional=None):
"""
Create a snapshot of an existing Volume
:param name: Name of the Snapshot
:type name: str
:param source_volume_id: The volume you want to snapshot
:type source_volume_id: int
:param optional: Dictionary of optional params
:type optional: dict
.. code-block:: python
optional = {
'description' : "some comment",
'inheritAccess' : false
}
"""
parameters = {'name': name}
if optional:
parameters = self._mergeDict(parameters, optional)
info = {'action': 'createSnapshot',
'parameters': parameters}
response, body = self.http.post('/volumes/%s' % source_volume_id,
body=info)
return body
def deleteSnapshot(self, snapshot_id):
"""
Delete a Snapshot
:param snapshot_id: the snapshot ID to delete
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_SNAPSHOT - The snapshot does not exist
"""
response, body = self.http.delete('/snapshots/%s' % snapshot_id)
return body
def cloneSnapshot(self, name, source_snapshot_id, optional=None):
"""
Create a clone of an existing Shapshot
:param name: Name of the Snapshot clone
:type name: str
:param source_snapshot_id: The snapshot you want to clone
:type source_snapshot_id: int
:param optional: Dictionary of optional params
:type optional: dict
.. code-block:: python
optional = {
'description' : "some comment"
}
"""
parameters = {'name': name}
if optional:
parameters = self._mergeDict(parameters, optional)
info = {'action': 'createSmartClone',
'parameters': parameters}
response, body = self.http.post('/snapshots/%s' % source_snapshot_id,
body=info)
return body
def getVolumes(self):
"""
Get the list of Volumes
:returns: list of Volumes
"""
response, body = self.http.get('/volumes')
return body
def getVolume(self, volume_id):
"""
Get information about a volume
:param volume_id: The id of the volume to find
:type volume_id: str
:returns: volume
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_VOL - volume doesn't exist
"""
response, body = self.http.get('/volumes/%s' % volume_id)
return body
def getVolumeByName(self, name):
"""
Get information about a volume by name
:param name: The name of the volume to find
:type volume_id: str
:returns: volume
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_VOL - volume doesn't exist
"""
response, body = self.http.get('/volumes?name=%s' % name)
return body
def createVolume(self, name, cluster_id, size, optional=None):
""" Create a new volume
:param name: the name of the volume
:type name: str
:param cluster_id: the cluster Id
:type cluster_id: int
:param sizeKB: size in KB for the volume
:type sizeKB: int
:param optional: dict of other optional items
:type optional: dict
.. code-block:: python
optional = {
'description': 'some comment',
'isThinProvisioned': 'true',
'autogrowSeconds': 200,
'clusterName': 'somename',
'isAdaptiveOptimizationEnabled': 'true',
'dataProtectionLevel': 2,
}
:returns: List of Volumes
:raises: :class:`~hplefthandclient.exceptions.HTTPConflict` -
EXISTENT_SV - Volume Exists already
"""
info = {'name': name, 'clusterID': cluster_id, 'size': size}
if optional:
info = self._mergeDict(info, optional)
response, body = self.http.post('/volumes', body=info)
return body
def deleteVolume(self, volume_id):
"""
Delete a volume
:param name: the name of the volume
:type name: str
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_VOL - The volume does not exist
"""
response, body = self.http.delete('/volumes/%s' % volume_id)
return body
def modifyVolume(self, volume_id, optional):
"""Modify an existing volume.
:param volume_id: The id of the volume to find
:type volume_id: str
:returns: volume
:raises: :class:`~hplefthandclient.exceptions.HTTPNotFound` -
NON_EXISTENT_VOL - volume doesn't exist
"""
info = {'volume_id': volume_id}
info = self._mergeDict(info, optional)
response, body = self.http.put('/volumes/%s' % volume_id, body=info)
return body
def cloneVolume(self, name, source_volume_id, optional=None):
"""
Create a clone of an existing Volume
:param name: Name of the Volume clone
:type name: str
:param source_volume_id: The Volume you want to clone
:type source_volume_id: int
:param optional: Dictionary of optional params
:type optional: dict
.. code-block:: python
optional = {
'description' : "some comment"
}
"""
parameters = {'name': name}
if optional:
parameters = self._mergeDict(parameters, optional)
info = {'action': 'createSmartClone',
'parameters': parameters}
response, body = self.http.post('/volumes/%s' % source_volume_id,
body=info)
return body
def addServerAccess(self, volume_id, server_id, optional=None):
"""
Assign a Volume to a Server
:param volume_id: Volume ID of the volume
:type name: int
:param server_id: Server ID of the server to add the volume to
:type source_volume_id: int
:param optional: Dictionary of optional params
:type optional: dict
.. code-block:: python
optional = {
'Transport' : 0,
'Lun' : 1,
}
"""
parameters = {'serverID': server_id,
'exclusiveAccess': True,
'readAccess': True,
'writeAccess': True}
if optional:
parameters = self._mergeDict(parameters, optional)
info = {'action': 'addServerAccess',
'parameters': parameters}
response, body = self.http.post('/volumes/%s' % volume_id,
body=info)
return body
def removeServerAccess(self, volume_id, server_id):
"""
Unassign a Volume from a Server
:param volume_id: Volume ID of the volume
:type name: int
:param server_id: Server ID of the server to remove the volume fom
:type source_volume_id: int
"""
parameters = {'serverID': server_id}
info = {'action': 'removeServerAccess',
'parameters': parameters}
response, body = self.http.post('/volumes/%s' % volume_id,
body=info)
return body
def _mergeDict(self, dict1, dict2):
"""
Safely merge 2 dictionaries together
:param dict1: The first dictionary
:type dict1: dict
:param dict2: The second dictionary
:type dict2: dict
:returns: dict
:raises Exception: dict1, dict2 is not a dictionary
"""
if type(dict1) is not dict:
raise Exception("dict1 is not a dictionary")
if type(dict2) is not dict:
raise Exception("dict2 is not a dictionary")
dict3 = dict1.copy()
dict3.update(dict2)
return dict3
|