def get_block_volume_list(access_path=None, host=None): """ access_path: access path id or name. int -> id, str -> name if not specified, return all block volumes. """ retval = -1 volume_list = [] cmd = utils.XMS_CLI_HEADER + "-f json block-volume list" print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0 or ret[0] is None or len(ret[0]) == 0: print "[Error] Failed to get block volume info. Error message: [{err}]".format( err=ret[1]) else: try: volume_info = json.loads(ret[0]) volumes = volume_info['block_volumes'] for v in volumes: if v['access_path'] and (access_path is None \ or isinstance(access_path, str) and v['access_path']['name'] == access_path \ or isinstance(access_path, int) and v['access_path']['id'] == access_path): volume_list.append( BlockVolume.BlockVolume(v['id'], v['name'], v['client_group_num'], v['pool']['id'], v['pool']['name'], v['access_path']['id'], v['access_path']['name'])) retval = 0 except Exception as e: print "[Error] The volume info is invalid. Error message: " + e.message return retval, volume_list
def get_target_list(access_path=None, host=None): """ access_path: access path id or name. int -> id, str -> name if not specified, return all targets. """ retval = -1 target_list = [] cmd = utils.XMS_CLI_HEADER + "-f json target list" ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0 or ret[0] is None or len(ret[0]) == 0: print "[Error] Failed to get target info. Error message: [{err}]".format( err=ret[1]) else: try: target_info = json.loads(ret[0]) targets = target_info['targets'] for t in targets: if access_path is None \ or isinstance(access_path, str) and t['access_path']['name'] == access_path \ or isinstance(access_path, int) and t['access_path']['id'] == access_path: target_list.append( Target(t['id'], t['host']['id'], t['iqn'], t['access_path']['id'], t['status'], t['port'])) retval = 0 except Exception as e: print "[Error] The target info is invalid." return retval, target_list
def create_access_path(name, aptype, description=None, chap=False, tname=None, tsecret=None, host=None): retval = -1 if aptype not in ("iSCSI", "Local", "FC"): print "[Error] The access path type is not valid." else: cmd = utils.XMS_CLI_HEADER + "access-path create --type {aptype} ".format( aptype=aptype) if description: cmd += "--description {des} ".format(des=description) if chap and tname and tsecret: cmd += "--chap {chap} --tname {tname} --tsecret {tsecret} ".format( chap=str(chap), tname=tname, tsecret=tsecret) cmd += str(name) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to create access path. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def remove_block_volume(mg_id, block_volumes, host=None): """ This method is used to remove block volume from a mapping group. :Param mg_id: **Mandatory**. Mapping group id. :Param block_volumes: **Mandatory**. Block volumes ids attached to this mapping group, seperated by comma: 1,2,3. """ retval = -1 try: retval, block_volumes = _handle_block_volumes(block_volumes, host) if retval != 0: return retval except Exception as e: print "[Error] The block volumes are not valid." else: cmd = utils.XMS_CLI_HEADER + "mapping-group remove block-volume {mgid} {bvs}".format( mgid=mg_id, bvs=block_volumes) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: retval = -1 print "[Error] Failed to remove mapping group block volumes. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def create_client_group(name, cgtype, codes, description=None, host=None): """ :Param name: client group name :Param cgtype: iSCSI or FC :Param codes: for iSCSI, the codes are IP or IQN; for FC, they are WWN. """ retval = -1 try: if isinstance(codes, list): codes = ",".join(codes) except Exception as e: print "[Error] The client group codes is not valid." else: if cgtype not in ("iSCSI", "FC"): print "[Error] The client group type is not valid." else: if description: cmd = utils.XMS_CLI_HEADER + "client-group create --type {cgtype} --codes {codes} --description {des} {name}".format( name=name, codes=codes, des=description, cgtype=cgtype) else: cmd = utils.XMS_CLI_HEADER + "client-group create --type {cgtype} --codes {codes} {name}".format( name=name, codes=codes, cgtype=cgtype) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to create client group. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def get_access_path_id(name, host=None): retval = -1 cmd = utils.XMS_CLI_HEADER + "-f '{{range .}}{{println .id}}{{end}}' access-path list -q name.raw:" + str( name) # cmd = utils.XMS_CLI_HEADER + "-f json access-path list" ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0 or ret[0] is None or len(ret[0]) == 0: print "[Error] Failed to get access path info. Error message: [{err}]".format( err=ret[1]) # else: # try: # access_path_info = json.loads(ret[0]) # paths = access_path_info['access_paths'] # for p in paths: # if p['name'] == str(name): # return p['id'] # except Exception as e: # print "[Error] The access path info is invalid." else: try: return int(ret[0]) except Exception as e: print "[Error] The access path info is invalid. Error message: [{err}]".format( err=ret[1]) return retval
def get_block_volume_list(client_group, host="localhost"): retval = -1 volume_list = [] if type(client_group) in (str, int): if isinstance(client_group, str): client_group = get_client_group_id(client_group, host) if client_group != -1: token = utils.get_access_token(host) if token != -1: url = utils.XMS_REST_BASE_URL.format(ip=host) + "block-volumes/?token={token}\&client_group_id={cgid}" curl_header = utils.XMS_CURL_GET_HEADER cmd = curl_header + url.format(token=token, cgid=client_group) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to get client group volumes info. Error message: [{err}]".format(err=ret[1]) else: try: volume_info = json.loads(ret[0]) volumes = volume_info['block_volumes'] for v in volumes: if v['access_path']: volume_list.append(BlockVolume.BlockVolume(v['id'], v['name'], v['client_group_num'], v['pool']['id'], v['pool']['name'], v['access_path']['id'], v['access_path']['name'])) else: volume_list.append(BlockVolume.BlockVolume(v['id'], v['name'], v['client_group_num'], v['pool']['id'], v['pool']['name'], None, None)) retval = 0 except Exception as e: print "[Error] The volumes info is invalid. Error message: " + e.message return retval, volume_list
def delete_mapping_group(mg_id, host=None): retval = -1 cmd = utils.XMS_CLI_HEADER + "mapping-group delete {mgid}".format( mgid=mg_id) ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to delete mapping group. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def _get_mapping_groups(host=None): retval = -1 cmd = utils.XMS_CLI_HEADER + "-f json mapping-group list" ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to get mapping groups information. Error message: [{err}]".format( err=ret[1]) else: try: return 0, json.loads(ret[0]) except Exception as e: print "[Error] The mapping group info is invalid. Error message: " + e.message return retval, None
def delete_target(target_id, host=None): retval = -1 if not isinstance(target_id, int): print "[Error] The target id is invalid." else: cmd = utils.XMS_CLI_HEADER + "target delete {target}".format( target=target_id) ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to delete target. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def delete_block_snapshot(snapshot, host=None): retval = 0 if not isinstance(snapshot, int): snapshot = get_block_snapshot_id(snapshot, host) if snapshot == -1: print "[Error] The block snapshot name/id is invalid." return -1 cmd = utils.XMS_CLI_HEADER + "block-snapshot delete {snapid}".format(snapid=snapshot) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to delete block snapshot. Error message: [{err}]".format(err=ret[1]) retval = -1 return retval
def delete_client_group(client_group, host=None): retval = -1 if not isinstance(client_group, int): client_group = get_client_group_id(client_group, host) if client_group == -1: print "[Error] The client group name/id is invalid." else: cmd = utils.XMS_CLI_HEADER + "client-group delete {cgid}".format(cgid=client_group) ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to delete client group. Error message: [{err}]".format(err=ret[1]) else: retval = 0 return retval
def delete_access_path(access_path, host=None): retval = -1 if not isinstance(access_path, int): access_path = get_access_path_id(access_path, host) if access_path == -1: print "[Error] The access path name/id is invalid." else: cmd = utils.XMS_CLI_HEADER + "access-path delete {apid}".format( apid=access_path) ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to delete access path. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def get_client_group_id(name, host=None): retval = -1 cmd = utils.XMS_CLI_HEADER + "-f json client-group list" print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to get client group info. Error message: [{err}]".format(err=ret[1]) else: try: client_group_info = json.loads(ret[0]) groups = client_group_info['client_groups'] for g in groups: if g['name'] == name: return g['id'] except Exception as e: print "[Error] The client group info is invalid." return retval
def get_block_snapshot_id(snapshot_name, host=None): retval = -1 cmd = utils.XMS_CLI_HEADER + "-f json block-snapshot list" print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to get snapshot info. Error message: [{err}]".format(err=ret[1]) else: try: snapshotinfo = json.loads(ret[0]) snapshot_list = snapshotinfo["block_snapshots"] for s in snapshot_list: if s["name"] == snapshot_name: return s["id"] except Exception as e: print "[Error] the error message is: " + e.message return retval
def create_block_snapshot(snapshot_name, block_volume, description=None, host=None): retval = 0 if not isinstance(block_volume, int): block_volume = BlockVolume.get_block_volume_id(block_volume, host) if block_volume == -1: print "[Error] The block volume id is invalid." return -1 if description is None: cmd = utils.XMS_CLI_HEADER + "block-snapshot create --block-volume {volume} {snap}".format(volume=block_volume, snap=snapshot_name) else: cmd = utils.XMS_CLI_HEADER + "block-snapshot create --block-volume {volume} --description {des} {snap}".format(volume=block_volume, snap=snapshot_name, des=description) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to create block snapshot. Error message: [{err}]".format(err=ret[1]) retval = -1 return retval
def delete_block_volume(volume, host=None): retval = -1 if type(volume) in (str, int): if isinstance(volume, str): volume = get_block_volume_id(volume, host) print "-----" + str(volume) if volume == -1: print "[Error] The volume %s is not existed" % volume else: cmd = utils.XMS_CLI_HEADER + "block-volume delete {id}".format( id=volume) ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to delete block volume " + str( volume) + ". Error message: [{err}]".format(err=ret[1]) else: retval = 0 return retval
def get_block_volume_id(volume_name, host=None): """ return the block volume id according to the volume name. """ cmd = utils.XMS_CLI_HEADER + "-f json block-volume list --name {name}".format( name=volume_name) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0 or isinstance(ret[0], dict): print "[Error] Failed to get block volume info. Error message: [{err}]".format( err=ret[1]) return -1 try: volume_info = json.loads(ret[0]) return volume_info["block_volumes"][0]["id"] except Exception as e: print "[Error] error message is: " + e.message return -1
def get_client_group_ids(host=None): retval = -1 cgid_list = [] cmd = utils.XMS_CLI_HEADER + "-f json client-group list" print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to get client group info. Error message: [{err}]".format(err=ret[1]) else: try: client_group_info = json.loads(ret[0]) groups = client_group_info['client_groups'] for g in groups: cgid_list.append(g['id']) retval = 0 except Exception as e: print "[Error] The client group info is invalid. Error message: " + e.message return retval, cgid_list
def create_target(access_path, node, host=None): retval = -1 if not isinstance(access_path, int): access_path = get_access_path_id(access_path, host) if not isinstance(node, int): node = Host.get_host_id(node, host) if access_path == -1 or node == -1: print "[Error] The access path or host name/id is invalid." else: cmd = utils.XMS_CLI_HEADER + "target create --access-path {apid} --host {host_id}".format( apid=access_path, host_id=node) ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to create target. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def get_block_volume_ids(host=None): retval = -1 volume_list = [] cmd = utils.XMS_CLI_HEADER + "-f json block-volume list" ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to get block volume info. Error message: [{err}]".format( err=ret[1]) else: try: volumes_info = json.loads(ret[0]) volumes = volumes_info['block_volumes'] for v in volumes: volume_list.append(v['id']) retval = 0 except Exception as e: print "[Error] The block volume info is invalid. Error message: " + e.message return retval, volume_list
def get_pool_id(pool_name, host=None): """ return the pool id according to the pool name. """ cmd = utils.XMS_CLI_HEADER + "-f json pool list" print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0 or isinstance(ret[0], dict): print "[Error] Failed to get pool info. Error message: [{err}]".format( err=ret[1]) return -1 try: pool_info = json.loads(ret[0]) pools = pool_info["pools"] for p in pools: if pool_name == p["name"]: return p["id"] except Exception as e: print "[Error] error message is: " + e.message return -1
def get_host_id(host_name, host=None): """ return the block volume id according to the volume name. """ retval = -1 cmd = utils.XMS_CLI_HEADER + "-f json host list" print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0 or isinstance(ret[0], dict): print "[Error] Failed to get host info. Error message: [{err}]".format( err=ret[1]) return retval try: host_info = json.loads(ret[0]) hosts = host_info["hosts"] for h in hosts: if host_name == h["name"]: return h["id"] except Exception as e: print "[Error] error message is: " + e.message return retval
def get_access_path_list(host=None): retval = -1 paths = [] cmd = utils.XMS_CLI_HEADER + "-f json access-path list" ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to list access paths. Error message: [{err}]".format( err=ret[1]) else: try: ap_info = json.loads(ret[0]) aps = ap_info['access_paths'] for t in aps: paths.append( AccessPath(t['id'], t['name'], t['type'], t['status'], t['block_volume_num'], t['client_group_num'], t['description'])) retval = 0 except Exception as e: print "[Error] The access path info is invalid." return retval, paths
def get_pool_ids(host=None): """ return the pool ids """ cmd = utils.XMS_CLI_HEADER + "-f json pool list" print cmd pool_ids = [] ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0 or isinstance(ret[0], dict): print "[Error] Failed to get pool info. Error message: [{err}]".format( err=ret[1]) return -1, pool_ids try: pool_info = json.loads(ret[0]) pools = pool_info["pools"] for p in pools: pool_ids.append(p["id"]) except Exception as e: print "[Error] error message is: " + e.message return -1, pool_ids return 0, pool_ids
def create_mapping_group(access_path, block_volumes, client_group, host=None): """ This method is used to create mapping group. :Param access_path: **Mandatory**. Expecting value is access path name or id. :Param block_volumes: **Mandatory**. Block volumes ids attached to this mapping group, seperated by comma: 1,2,3. :Param client_group: **Mandatory**. Client group id or name attached to this mapping group. :Param host: **Optional**. If host is None, it will run in the local. :returns: status 0 success, -1 failed. """ retval = -1 if not isinstance(access_path, int): access_path = AccessPath.get_access_path_id(access_path, host) if not isinstance(client_group, int): client_group = ClientGroup.get_client_group_id(client_group, host) if access_path == -1 or client_group == -1: print "[Error] The access path or client group name/id is invalid." else: try: retval, block_volumes = _handle_block_volumes(block_volumes, host) if retval != 0: return retval except Exception as e: print "[Error] The block volumes are not valid." else: cmd = utils.XMS_CLI_HEADER + "mapping-group create --access-path {ap} --block-volumes {bvs} --client-group {cg}".format( ap=access_path, bvs=block_volumes, cg=client_group) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: retval = -1 print "[Error] Failed to create mapping group. Error message: [{err}]".format( err=ret[1]) else: retval = 0 return retval
def create_block_volume(volume_name, pool, size, description='', format=128, performance_priority=0, qos_enabled=False, max_total_iops=0, max_total_bw=0, burst_total_iops=0, burst_total_iobw=0, host=None): """ volume_name: volume name pool: pool id or name size: volume size, like 100M, 100G, or 100000. If the unit is ignored, the unit is byte. """ retval = 0 if not isinstance(pool, int): pool = Pool.get_pool_id(pool, host) if pool == -1: print "[Error] The pool id is invalid." return -1 cmd = utils.XMS_CLI_HEADER + "-f json block-volume create -p {poolid} -s {volsize} -f {fmt} --pp {pp} {volname}".format( poolid=pool, volsize=size, fmt=format, pp=performance_priority, volname=volume_name) print cmd ret = utils.execute_cmd_in_host(cmd, host) if ret[2] != 0: print "[Error] Failed to create block volume " + str( volume_name) + ". Error message: [{err}]".format(err=ret[1]) retval = -1 return retval