def _get_cluster(clusters_xml, clusters): for cluster in clusters: cluster_xml_obj = JsonToXml(cluster, 'cluster', clusters_xml) cluster_xml = cluster_xml_obj.generate_xml() #add_hosts cond = res_ops.gen_query_conditions('clusterUuid', '=', \ cluster.uuid) hosts = res_ops.safely_get_resource(res_ops.HOST, cond, \ session_uuid) if hosts: hosts_xml = etree.SubElement(cluster_xml, "hosts") add_xml_items(hosts, 'host', hosts_xml, \ 'availableCpuCapacity availableMemoryCapacity \ clusterUuid hypervisorType totalCpuCapacity \ totalMemoryCapacity zoneUuid') #add ps ref cond = res_ops.gen_query_conditions('attachedClusterUuids', 'in', \ cluster.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid, fields=['name']) for ps in pss: _set_primary_strorage_ref(cluster_xml, ps.name) #add l2 ref cond = res_ops.gen_query_conditions('attachedClusterUuids', 'in', \ cluster.uuid) l2s = res_ops.safely_get_resource(res_ops.L2_NETWORK, cond, session_uuid, fields=['name']) for l2 in l2s: _set_l2_ref(cluster_xml, l2.name)
def _get_primary_storage(pss_xml, zone): cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) cond = res_ops.gen_query_conditions('type', '=', 'NFS', cond) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) return add_xml_items(pss, 'nfsPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid')
def _get_primary_storage(pss_xml, zone): cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) for ps in pss: if ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'nfsPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid') json_to_xml.generate_xml() elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'cephPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid') json_to_xml.generate_xml() elif ps.type == inventory.LOCAL_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'localPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid') json_to_xml.generate_xml() cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) cond = res_ops.gen_query_conditions('type', '=', \ 'SimulatorPrimaryStorage', cond) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) add_xml_items(pss, 'simulatorPrimaryStorage', pss_xml, \ 'mountPath type zoneUuid')
def add_virtual_router(deployConfig, session_uuid, l3_name = None, \ zone_name = None): if not xmlobject.has_element(deployConfig, 'instanceOfferings.virtualRouterOffering'): return for i in xmlobject.safe_list(deployConfig.instanceOfferings.virtualRouterOffering): if l3_name and l3_name != i.managementL3NetworkRef.text_: continue if zone_name and zone_name != i.zoneRef.text_: continue print "continue l3_name: %s; zone_name: %s" % (l3_name, zone_name) action = api_actions.CreateVirtualRouterOfferingAction() action.sessionUuid = session_uuid action.name = i.name_ action.description = i.description__ action.cpuNum = i.cpuNum_ action.cpuSpeed = i.cpuSpeed_ if i.memorySize__: action.memorySize = sizeunit.get_size(i.memorySize_) elif i.memoryCapacity_: action.memorySize = sizeunit.get_size(i.memoryCapacity_) action.isDefault = i.isDefault__ action.type = 'VirtualRouter' zinvs = res_ops.get_resource(res_ops.ZONE, session_uuid, name=i.zoneRef.text_) zinv = get_first_item_from_list(zinvs, 'zone', i.zoneRef.text_, 'virtual router offering') action.zoneUuid = zinv.uuid cond = res_ops.gen_query_conditions('zoneUuid', '=', zinv.uuid) cond1 = res_ops.gen_query_conditions('name', '=', \ i.managementL3NetworkRef.text_, cond) minvs = res_ops.query_resource(res_ops.L3_NETWORK, cond1, \ session_uuid) minv = get_first_item_from_list(minvs, 'Management L3 Network', i.managementL3NetworkRef.text_, 'virtualRouterOffering') action.managementNetworkUuid = minv.uuid if xmlobject.has_element(i, 'publicL3NetworkRef'): cond1 = res_ops.gen_query_conditions('name', '=', \ i.publicL3NetworkRef.text_, cond) pinvs = res_ops.query_resource(res_ops.L3_NETWORK, cond1, \ session_uuid) pinv = get_first_item_from_list(pinvs, 'Public L3 Network', i.publicL3NetworkRef.text_, 'virtualRouterOffering') action.publicNetworkUuid = pinv.uuid iinvs = res_ops.get_resource(res_ops.IMAGE, session_uuid, \ name=i.imageRef.text_) iinv = get_first_item_from_list(iinvs, 'Image', i.imageRef.text_, 'virtualRouterOffering') action.imageUuid = iinv.uuid thread = threading.Thread(target = _thread_for_action, args = (action, )) wait_for_thread_queue() thread.start() wait_for_thread_done()
def _get_primary_storage(pss_xml, zone): cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) for ps in pss: if ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(ps, 'nfsPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid totalPhysicalCapacity') json_to_xml.generate_xml() elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(ps, 'cephPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid totalPhysicalCapacity') json_to_xml.generate_xml() elif ps.type == inventory.LOCAL_STORAGE_TYPE: json_to_xml = JsonToXml(ps, 'localPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid totalPhysicalCapacity') json_to_xml.generate_xml() cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) cond = res_ops.gen_query_conditions('type', '=', \ 'SimulatorPrimaryStorage', cond) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) add_xml_items(pss, 'simulatorPrimaryStorage', pss_xml, \ 'mountPath type zoneUuid')
def _add_cluster(action, zone_ref, cluster, cluster_ref): evt = action.run() deploy_logger(jsonobject.dumps(evt)) cinv = evt.inventory try: if xmlobject.has_element(cluster, 'primaryStorageRef'): for pref in xmlobject.safe_list(cluster.primaryStorageRef): ps_name = generate_dup_name(generate_dup_name(pref.text_, zone_ref, 'z'), cluster_ref, 'c') pinvs = res_ops.get_resource(res_ops.PRIMARY_STORAGE, session_uuid, name=ps_name) pinv = get_first_item_from_list(pinvs, 'Primary Storage', ps_name, 'Cluster') action_ps = api_actions.AttachPrimaryStorageToClusterAction() action_ps.sessionUuid = session_uuid action_ps.clusterUuid = cinv.uuid action_ps.primaryStorageUuid = pinv.uuid evt = action_ps.run() deploy_logger(jsonobject.dumps(evt)) except: exc_info.append(sys.exc_info()) if cluster.allL2NetworkRef__ == 'true': # find all L2 network in zone and attach to cluster cond = res_ops.gen_query_conditions('zoneUuid', '=', action.zoneUuid) l2_count = res_ops.query_resource_count(res_ops.L2_NETWORK, cond, session_uuid) l2invs = res_ops.query_resource_fields(res_ops.L2_NETWORK, [{'name': 'zoneUuid', 'op': '=', 'value': action.zoneUuid}], session_uuid, ['uuid'], 0, l2_count) else: l2invs = [] if xmlobject.has_element(cluster, 'l2NetworkRef'): for l2ref in xmlobject.safe_list(cluster.l2NetworkRef): l2_name = generate_dup_name(generate_dup_name(l2ref.text_, zone_ref, 'z'), cluster_ref, 'c') cond = res_ops.gen_query_conditions('zoneUuid', '=', action.zoneUuid) cond = res_ops.gen_query_conditions('name', '=', l2_name, cond) l2inv = res_ops.query_resource_fields(res_ops.L2_NETWORK, cond, session_uuid, ['uuid']) if not l2inv: raise DeployError("Can't find l2 network [%s] in database." % l2_name) l2invs.extend(l2inv) for l2inv in l2invs: action = api_actions.AttachL2NetworkToClusterAction() action.sessionUuid = session_uuid action.clusterUuid = cinv.uuid action.l2NetworkUuid = l2inv.uuid thread = threading.Thread(target=_thread_for_action, args=(action,)) wait_for_thread_queue() thread.start()
def get_instance_offering(xml_root, session_uuid=None): cond = [] vr_inst_offerings = res_ops.safely_get_resource(res_ops.VR_OFFERING, cond, \ session_uuid) if vr_inst_offerings: vr_offering_uuids = res_ops.safely_get_resource(res_ops.VR_OFFERING, \ [], session_uuid, ['uuid']) else: return uuids = '' for vr_uuid in vr_offering_uuids: uuids += ' %s' % vr_uuid.uuid cond = res_ops.gen_query_conditions('uuid', 'not in', uuids) inst_offerings = res_ops.safely_get_resource(res_ops.INSTANCE_OFFERING, \ cond, session_uuid) if vr_inst_offerings or inst_offerings: xml_item = etree.SubElement(xml_root, "instanceOfferings") add_xml_items(inst_offerings, 'instanceOffering', xml_item) for vr_offering in vr_inst_offerings: vr_offering_xml_obj = JsonToXml( vr_offering, 'virtualRouterOffering', xml_item, 'managementNetworkUuid \ publicNetworkUuid zoneUuid imageUuid') vr_offering_xml = vr_offering_xml_obj.generate_xml() zone_uuid = vr_offering.zoneUuid cond = res_ops.gen_query_conditions('uuid', '=', zone_uuid) zone_name = res_ops.query_resource(res_ops.ZONE, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'zoneRef', zone_name) pub_l3_uuid = vr_offering.publicNetworkUuid cond = res_ops.gen_query_conditions('uuid', '=', pub_l3_uuid) l3_name = res_ops.query_resource(res_ops.L3_NETWORK, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'publicL3NetworkRef', l3_name) mgt_l3_uuid = vr_offering.managementNetworkUuid cond = res_ops.gen_query_conditions('uuid', '=', mgt_l3_uuid) l3_name = res_ops.query_resource(res_ops.L3_NETWORK, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'managementL3NetworkRef', l3_name) img_uuid = vr_offering.imageUuid cond = res_ops.gen_query_conditions('uuid', '=', img_uuid) img_name = res_ops.query_resource(res_ops.IMAGE, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'imageRef', img_name) return xml_item
def get_instance_offering(xml_root, session_uuid = None): cond = [] vr_inst_offerings = res_ops.safely_get_resource(res_ops.VR_OFFERING, cond, \ session_uuid) if vr_inst_offerings: vr_offering_uuids = res_ops.safely_get_resource(res_ops.VR_OFFERING, \ [], session_uuid, ['uuid']) else: return uuids = '' for vr_uuid in vr_offering_uuids: uuids += ' %s' % vr_uuid.uuid cond = res_ops.gen_query_conditions('uuid', 'not in', uuids) inst_offerings = res_ops.safely_get_resource(res_ops.INSTANCE_OFFERING, \ cond, session_uuid) if vr_inst_offerings or inst_offerings: xml_item = etree.SubElement(xml_root, "instanceOfferings") add_xml_items(inst_offerings, 'instanceOffering', xml_item) for vr_offering in vr_inst_offerings: vr_offering_xml_obj = JsonToXml(vr_offering, 'virtualRouterOffering', xml_item, 'managementNetworkUuid \ publicNetworkUuid zoneUuid imageUuid') vr_offering_xml = vr_offering_xml_obj.generate_xml() zone_uuid = vr_offering.zoneUuid cond = res_ops.gen_query_conditions('uuid', '=', zone_uuid) zone_name = res_ops.query_resource(res_ops.ZONE, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'zoneRef', zone_name) pub_l3_uuid = vr_offering.publicNetworkUuid cond = res_ops.gen_query_conditions('uuid', '=', pub_l3_uuid) l3_name = res_ops.query_resource(res_ops.L3_NETWORK, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'publicL3NetworkRef', l3_name) mgt_l3_uuid = vr_offering.managementNetworkUuid cond = res_ops.gen_query_conditions('uuid', '=', mgt_l3_uuid) l3_name = res_ops.query_resource(res_ops.L3_NETWORK, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'managementL3NetworkRef', l3_name) img_uuid = vr_offering.imageUuid cond = res_ops.gen_query_conditions('uuid', '=', img_uuid) img_name = res_ops.query_resource(res_ops.IMAGE, cond, \ session_uuid)[0].name _set_res_ref(vr_offering_xml, 'imageRef', img_name) return xml_item
def _add_l3(l2_xml, l2): #basic cond = res_ops.gen_query_conditions('l2NetworkUuid', '=', l2.uuid) cond = res_ops.gen_query_conditions('type', '=', 'L3BasicNetwork', cond) l3s = res_ops.safely_get_resource(res_ops.L3_NETWORK, cond, session_uuid) if not l3s: return None l3s_xml = etree.SubElement(l2_xml, "l3Networks") for l3 in l3s: l3_xml_obj = JsonToXml(l3, 'l3BasicNetwork', l3s_xml, \ 'type zoneUuid l2NetworkUuid') l3_xml = l3_xml_obj.generate_xml() #ip range cond = res_ops.gen_query_conditions('l3NetworkUuid', '=', l3.uuid) ip_ranges = res_ops.safely_get_resource(res_ops.IP_RANGE, cond, session_uuid) add_xml_items(ip_ranges, 'ipRange', l3_xml, 'l3NetworkUuid') #dns if l3.dns: for dns in l3.dns: dns_xml = etree.SubElement(l3_xml, "dns") dns_xml.text = dns #network service if l3.networkServices: nss_dict = {} for ns in l3.networkServices: if not ns.networkServiceProviderUuid in nss_dict.keys(): nss_dict[ns.networkServiceProviderUuid] \ = [ns.networkServiceType] else: nss_dict[ns.networkServiceProviderUuid].append( ns.networkServiceType) for key in nss_dict.keys(): cond = res_ops.gen_query_conditions('uuid', '=', key) ns = res_ops.safely_get_resource(\ res_ops.NETWORK_SERVICE_PROVIDER, cond, session_uuid) ns_name = ns[0].name ns_xml = etree.SubElement(l3_xml, "networkService") ns_xml.set('provider', ns_name) for value in nss_dict[key]: ns_type_xml = etree.SubElement(ns_xml, 'serviceType') ns_type_xml.text = value
def _add_l3(l2_xml, l2): #basic cond = res_ops.gen_query_conditions('l2NetworkUuid', '=', l2.uuid) cond = res_ops.gen_query_conditions('type', '=', 'L3BasicNetwork', cond) l3s = res_ops.safely_get_resource(res_ops.L3_NETWORK, cond, session_uuid) if not l3s: return None l3s_xml = etree.SubElement(l2_xml, "l3Networks") for l3 in l3s: l3_xml_obj = JsonToXml(l3, 'l3BasicNetwork', l3s_xml, \ 'type zoneUuid l2NetworkUuid') l3_xml = l3_xml_obj.generate_xml() #ip range cond = res_ops.gen_query_conditions('l3NetworkUuid', '=', l3.uuid) ip_ranges = res_ops.safely_get_resource(res_ops.IP_RANGE, cond, session_uuid) add_xml_items(ip_ranges, 'ipRange', l3_xml, 'l3NetworkUuid') #dns if l3.dns: for dns in l3.dns: dns_xml = etree.SubElement(l3_xml, "dns") dns_xml.text = dns #network service if l3.networkServices: nss_dict = {} for ns in l3.networkServices: if not ns.networkServiceProviderUuid in nss_dict.keys(): nss_dict[ns.networkServiceProviderUuid] \ = [ns.networkServiceType] else: nss_dict[ns.networkServiceProviderUuid].append(ns.networkServiceType) for key in nss_dict.keys(): cond = res_ops.gen_query_conditions('uuid', '=', key) ns = res_ops.safely_get_resource(\ res_ops.NETWORK_SERVICE_PROVIDER, cond, session_uuid) ns_name = ns[0].name ns_xml = etree.SubElement(l3_xml, "networkService") ns_xml.set('provider', ns_name) for value in nss_dict[key]: ns_type_xml = etree.SubElement(ns_xml, 'serviceType') ns_type_xml.text = value
def get_backup_storage(xml_root, session_uuid = None): cond = [] bs_storages = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid) if bs_storages: xml_item = etree.SubElement(xml_root, "backupStorages") else: return None sftp_bs_storages = res_ops.safely_get_resource(res_ops.SFTP_BACKUP_STORAGE,\ cond, session_uuid) if sftp_bs_storages: add_xml_items(sftp_bs_storages, 'sftpBackupStorage', xml_item, \ 'attachedZoneUuids availableCapacity totalCapacity') cond = res_ops.gen_query_conditions('type', '=', 'SimulatorBackupStorage') simulator_bss = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, \ cond, session_uuid) if simulator_bss: add_xml_items(simulator_bss, 'simulatorBackupStorage', xml_item, \ 'attachedZoneUuids') return xml_item
def get_backup_storage(xml_root, session_uuid=None): cond = [] bs_storages = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid) if bs_storages: xml_item = etree.SubElement(xml_root, "backupStorages") else: return None bs_storages = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE,\ cond, session_uuid) for bs in bs_storages: if bs.type == inventory.SFTP_BACKUP_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'sftpBackupStorage', xml_item, \ 'attachedZoneUuids availableCapacity totalCapacity') json_to_xml.generate_xml() elif bs.type == inventory.CEPH_BACKUP_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'cephBackupStorage', xml_item, \ 'attachedZoneUuids availableCapacity totalCapacity') json_to_xml.generate_xml() cond = res_ops.gen_query_conditions('type', '=', 'SimulatorBackupStorage') simulator_bss = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, \ cond, session_uuid) if simulator_bss: add_xml_items(simulator_bss, 'simulatorBackupStorage', xml_item, \ 'attachedZoneUuids') return xml_item
def get_backup_storage(xml_root, session_uuid=None): cond = [] bs_storages = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid) if bs_storages: xml_item = etree.SubElement(xml_root, "backupStorages") else: return None sftp_bs_storages = res_ops.safely_get_resource(res_ops.SFTP_BACKUP_STORAGE,\ cond, session_uuid) if sftp_bs_storages: add_xml_items(sftp_bs_storages, 'sftpBackupStorage', xml_item, \ 'attachedZoneUuids availableCapacity totalCapacity') cond = res_ops.gen_query_conditions('type', '=', 'SimulatorBackupStorage') simulator_bss = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, \ cond, session_uuid) if simulator_bss: add_xml_items(simulator_bss, 'simulatorBackupStorage', xml_item, \ 'attachedZoneUuids') return xml_item
def get_backup_storage(xml_root, session_uuid = None): cond = [] bs_storages = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid) if bs_storages: xml_item = etree.SubElement(xml_root, "backupStorages") else: return None bs_storages = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE,\ cond, session_uuid) for bs in bs_storages: if bs.type == inventory.SFTP_BACKUP_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'sftpBackupStorage', xml_item, \ 'attachedZoneUuids availableCapacity totalCapacity') json_to_xml.generate_xml() elif bs.type == inventory.CEPH_BACKUP_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'cephBackupStorage', xml_item, \ 'attachedZoneUuids availableCapacity totalCapacity') json_to_xml.generate_xml() cond = res_ops.gen_query_conditions('type', '=', 'SimulatorBackupStorage') simulator_bss = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, \ cond, session_uuid) if simulator_bss: add_xml_items(simulator_bss, 'simulatorBackupStorage', xml_item, \ 'attachedZoneUuids') return xml_item
def reconnect_host(host_uuid, session_uuid=None, timeout=600000): action = api_actions.ReconnectHostAction() action.uuid = host_uuid action.timeout = timeout test_util.action_logger('Reconnect Host [uuid:] %s' % host_uuid) evt = account_operations.execute_action_with_session(action, session_uuid) cur_time = time.time() while True: cond = resource_operations.gen_query_conditions('uuid', '=', host_uuid) host = resource_operations.query_resource_with_num(resource_operations.HOST, cond, limit = 1)[0] if host.status == "Connected" or host.status == "Disconnected": break time.sleep(1) if cur_time - time.time() > timeout: test_util.test_logger("reconnect_host timeout(%s)" % (timeout)) break return evt.inventory
def reconnect_host(host_uuid, session_uuid=None, timeout=120000): action = api_actions.ReconnectHostAction() action.uuid = host_uuid action.timeout = timeout test_util.action_logger('Reconnect Host [uuid:] %s' % host_uuid) evt = account_operations.execute_action_with_session(action, session_uuid) cur_time = time.time() while True: cond = resource_operations.gen_query_conditions('uuid', '=', host_uuid) host = resource_operations.query_resource_with_num(resource_operations.HOST, cond, limit = 1)[0] if host.status == "Connected" or host.status == "Disconnected": break time.sleep(1) if cur_time - time.time() > timeout: test_util.test_logger("reconnect_host timeout(%s)" % (timeout)) break return evt.inventory
def get_image(xml_root, session_uuid=None): cond = [] images = res_ops.safely_get_resource(res_ops.IMAGE, cond, session_uuid) if images: xml_item = etree.SubElement(xml_root, "images") else: return None for image in images: json_to_xml = JsonToXml(image, 'image', xml_item, 'md5Sum, size') xml_image = json_to_xml.generate_xml() bss = image.backupStorageRefs for bs in bss: bs_uuid = bs.backupStorageUuid cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid) bs_name = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid)[0].name _set_backup_strorage_ref(xml_image, bs_name) return xml_item
def get_image(xml_root, session_uuid = None): cond = [] images = res_ops.safely_get_resource(res_ops.IMAGE, cond, session_uuid) if images: xml_item = etree.SubElement(xml_root, "images") else: return None for image in images: json_to_xml = JsonToXml(image, 'image', xml_item, 'md5Sum, size') xml_image = json_to_xml.generate_xml() bss = image.backupStorageRefs for bs in bss: bs_uuid = bs.backupStorageUuid cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid) bs_name = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid)[0].name _set_backup_strorage_ref(xml_image, bs_name) return xml_item
def get_zone(xml_root, session_uuid=None): def _get_primary_storage(pss_xml, zone): cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) for ps in pss: if ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'nfsPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid') json_to_xml.generate_xml() elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'cephPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid') json_to_xml.generate_xml() elif ps.type == inventory.LOCAL_STORAGE_TYPE: json_to_xml = JsonToXml(bs, 'localPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid') json_to_xml.generate_xml() cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) cond = res_ops.gen_query_conditions('type', '=', \ 'SimulatorPrimaryStorage', cond) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) add_xml_items(pss, 'simulatorPrimaryStorage', pss_xml, \ 'mountPath type zoneUuid') def _get_cluster(clusters_xml, clusters): for cluster in clusters: cluster_xml_obj = JsonToXml(cluster, 'cluster', clusters_xml) cluster_xml = cluster_xml_obj.generate_xml() #add_hosts cond = res_ops.gen_query_conditions('clusterUuid', '=', \ cluster.uuid) hosts = res_ops.safely_get_resource(res_ops.HOST, cond, \ session_uuid) if hosts: hosts_xml = etree.SubElement(cluster_xml, "hosts") add_xml_items(hosts, 'host', hosts_xml, \ 'availableCpuCapacity availableMemoryCapacity \ clusterUuid hypervisorType totalCpuCapacity \ totalMemoryCapacity zoneUuid' ) #add ps ref cond = res_ops.gen_query_conditions('attachedClusterUuids', 'in', \ cluster.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid, fields=['name']) for ps in pss: _set_primary_strorage_ref(cluster_xml, ps.name) #add l2 ref cond = res_ops.gen_query_conditions('attachedClusterUuids', 'in', \ cluster.uuid) l2s = res_ops.safely_get_resource(res_ops.L2_NETWORK, cond, session_uuid, fields=['name']) for l2 in l2s: _set_l2_ref(cluster_xml, l2.name) def _add_l3(l2_xml, l2): #basic cond = res_ops.gen_query_conditions('l2NetworkUuid', '=', l2.uuid) cond = res_ops.gen_query_conditions('type', '=', 'L3BasicNetwork', cond) l3s = res_ops.safely_get_resource(res_ops.L3_NETWORK, cond, session_uuid) if not l3s: return None l3s_xml = etree.SubElement(l2_xml, "l3Networks") for l3 in l3s: l3_xml_obj = JsonToXml(l3, 'l3BasicNetwork', l3s_xml, \ 'type zoneUuid l2NetworkUuid') l3_xml = l3_xml_obj.generate_xml() #ip range cond = res_ops.gen_query_conditions('l3NetworkUuid', '=', l3.uuid) ip_ranges = res_ops.safely_get_resource(res_ops.IP_RANGE, cond, session_uuid) add_xml_items(ip_ranges, 'ipRange', l3_xml, 'l3NetworkUuid') #dns if l3.dns: for dns in l3.dns: dns_xml = etree.SubElement(l3_xml, "dns") dns_xml.text = dns #network service if l3.networkServices: nss_dict = {} for ns in l3.networkServices: if not ns.networkServiceProviderUuid in nss_dict.keys(): nss_dict[ns.networkServiceProviderUuid] \ = [ns.networkServiceType] else: nss_dict[ns.networkServiceProviderUuid].append( ns.networkServiceType) for key in nss_dict.keys(): cond = res_ops.gen_query_conditions('uuid', '=', key) ns = res_ops.safely_get_resource(\ res_ops.NETWORK_SERVICE_PROVIDER, cond, session_uuid) ns_name = ns[0].name ns_xml = etree.SubElement(l3_xml, "networkService") ns_xml.set('provider', ns_name) for value in nss_dict[key]: ns_type_xml = etree.SubElement(ns_xml, 'serviceType') ns_type_xml.text = value cond = [] zones = res_ops.safely_get_resource(res_ops.ZONE, cond, session_uuid) if not zones: return None zones_xml = etree.SubElement(xml_root, "zones") for zone in zones: zone_xml_obj = JsonToXml(zone, 'zone', zones_xml) zone_xml = zone_xml_obj.generate_xml() #query bs ref cond = res_ops.gen_query_conditions('attachedZoneUuids', 'in', \ zone.uuid) bss = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid, fields=['name']) for bs in bss: _set_backup_strorage_ref(zone_xml, bs.name) #query ps cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) if pss: pss_xml = etree.SubElement(zone_xml, "primaryStorages") _get_primary_storage(pss_xml, zone) #query cluster cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) clusters = res_ops.safely_get_resource(res_ops.CLUSTER, cond, \ session_uuid) if clusters: clusters_xml = etree.SubElement(zone_xml, "clusters") _get_cluster(clusters_xml, clusters) #add l2 cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) l2s = res_ops.safely_get_resource(res_ops.L2_NETWORK, cond, \ session_uuid) if l2s: l2s_xml = etree.SubElement(zone_xml, "l2Networks") #query no vlan cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) cond = res_ops.gen_query_conditions('type', '=', 'L2NoVlanNetwork') l2s_no_vlan = res_ops.safely_get_resource(res_ops.L2_NETWORK, cond, session_uuid) for l2 in l2s_no_vlan: l2_no_vlan_obj = JsonToXml(l2, 'l2NoVlanNetwork', l2s_xml, 'type zoneUuid') l2_xml = l2_no_vlan_obj.generate_xml() _add_l3(l2_xml, l2) #query vlan cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) l2s_vlan = res_ops.safely_get_resource(res_ops.L2_VLAN_NETWORK, cond, session_uuid) for l2 in l2s_vlan: l2_vlan_xml_obj = JsonToXml(l2, 'l2VlanNetwork', l2s_xml, 'type zoneUuid') l2_xml = l2_vlan_xml_obj.generate_xml() _add_l3(l2_xml, l2)
def get_zone(xml_root, session_uuid = None): def _get_primary_storage(pss_xml, zone): cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) for ps in pss: if ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(ps, 'nfsPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid totalPhysicalCapacity') json_to_xml.generate_xml() elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE: json_to_xml = JsonToXml(ps, 'cephPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid totalPhysicalCapacity') json_to_xml.generate_xml() elif ps.type == inventory.LOCAL_STORAGE_TYPE: json_to_xml = JsonToXml(ps, 'localPrimaryStorage', pss_xml, \ 'availableCapacity mountPath totalCapacity type zoneUuid totalPhysicalCapacity') json_to_xml.generate_xml() cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) cond = res_ops.gen_query_conditions('type', '=', \ 'SimulatorPrimaryStorage', cond) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) add_xml_items(pss, 'simulatorPrimaryStorage', pss_xml, \ 'mountPath type zoneUuid') def _get_cluster(clusters_xml, clusters): for cluster in clusters: cluster_xml_obj = JsonToXml(cluster, 'cluster', clusters_xml) cluster_xml = cluster_xml_obj.generate_xml() #add_hosts cond = res_ops.gen_query_conditions('clusterUuid', '=', \ cluster.uuid) hosts = res_ops.safely_get_resource(res_ops.HOST, cond, \ session_uuid) if hosts: hosts_xml = etree.SubElement(cluster_xml, "hosts") add_xml_items(hosts, 'host', hosts_xml, \ 'availableCpuCapacity availableMemoryCapacity \ clusterUuid hypervisorType totalCpuCapacity \ totalMemoryCapacity zoneUuid') #add ps ref cond = res_ops.gen_query_conditions('attachedClusterUuids', 'in', \ cluster.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid, fields=['name']) for ps in pss: _set_primary_strorage_ref(cluster_xml, ps.name) #add l2 ref cond = res_ops.gen_query_conditions('attachedClusterUuids', 'in', \ cluster.uuid) l2s = res_ops.safely_get_resource(res_ops.L2_NETWORK, cond, session_uuid, fields=['name']) for l2 in l2s: _set_l2_ref(cluster_xml, l2.name) def _add_l3(l2_xml, l2): #basic cond = res_ops.gen_query_conditions('l2NetworkUuid', '=', l2.uuid) cond = res_ops.gen_query_conditions('type', '=', 'L3BasicNetwork', cond) l3s = res_ops.safely_get_resource(res_ops.L3_NETWORK, cond, session_uuid) if not l3s: return None l3s_xml = etree.SubElement(l2_xml, "l3Networks") for l3 in l3s: l3_xml_obj = JsonToXml(l3, 'l3BasicNetwork', l3s_xml, \ 'type zoneUuid l2NetworkUuid') l3_xml = l3_xml_obj.generate_xml() #ip range cond = res_ops.gen_query_conditions('l3NetworkUuid', '=', l3.uuid) ip_ranges = res_ops.safely_get_resource(res_ops.IP_RANGE, cond, session_uuid) add_xml_items(ip_ranges, 'ipRange', l3_xml, 'l3NetworkUuid') #dns if l3.dns: for dns in l3.dns: dns_xml = etree.SubElement(l3_xml, "dns") dns_xml.text = dns #network service if l3.networkServices: nss_dict = {} for ns in l3.networkServices: if not ns.networkServiceProviderUuid in nss_dict.keys(): nss_dict[ns.networkServiceProviderUuid] \ = [ns.networkServiceType] else: nss_dict[ns.networkServiceProviderUuid].append(ns.networkServiceType) for key in nss_dict.keys(): cond = res_ops.gen_query_conditions('uuid', '=', key) ns = res_ops.safely_get_resource(\ res_ops.NETWORK_SERVICE_PROVIDER, cond, session_uuid) ns_name = ns[0].name ns_xml = etree.SubElement(l3_xml, "networkService") ns_xml.set('provider', ns_name) for value in nss_dict[key]: ns_type_xml = etree.SubElement(ns_xml, 'serviceType') ns_type_xml.text = value cond = [] zones = res_ops.safely_get_resource(res_ops.ZONE, cond, session_uuid) if not zones: return None zones_xml = etree.SubElement(xml_root, "zones") for zone in zones: zone_xml_obj = JsonToXml(zone, 'zone', zones_xml) zone_xml = zone_xml_obj.generate_xml() #query bs ref cond = res_ops.gen_query_conditions('attachedZoneUuids', 'in', \ zone.uuid) bss = res_ops.safely_get_resource(res_ops.BACKUP_STORAGE, cond, \ session_uuid, fields=['name']) for bs in bss: _set_backup_strorage_ref(zone_xml, bs.name) #query ps cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) pss = res_ops.safely_get_resource(res_ops.PRIMARY_STORAGE, cond, \ session_uuid) if pss: pss_xml = etree.SubElement(zone_xml, "primaryStorages") _get_primary_storage(pss_xml, zone) #query cluster cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) clusters = res_ops.safely_get_resource(res_ops.CLUSTER, cond, \ session_uuid) if clusters: clusters_xml = etree.SubElement(zone_xml, "clusters") _get_cluster(clusters_xml, clusters) #add l2 cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) l2s = res_ops.safely_get_resource(res_ops.L2_NETWORK, cond, \ session_uuid) if l2s: l2s_xml = etree.SubElement(zone_xml, "l2Networks") #query no vlan cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) cond = res_ops.gen_query_conditions('type', '=', 'L2NoVlanNetwork') l2s_no_vlan = res_ops.safely_get_resource(res_ops.L2_NETWORK, cond, session_uuid) for l2 in l2s_no_vlan: l2_no_vlan_obj = JsonToXml(l2, 'l2NoVlanNetwork', l2s_xml, 'type zoneUuid') l2_xml = l2_no_vlan_obj.generate_xml() _add_l3(l2_xml, l2) #query vlan cond = res_ops.gen_query_conditions('zoneUuid', '=', zone.uuid) l2s_vlan = res_ops.safely_get_resource(res_ops.L2_VLAN_NETWORK, cond, session_uuid) for l2 in l2s_vlan: l2_vlan_xml_obj = JsonToXml(l2, 'l2VlanNetwork', l2s_xml, 'type zoneUuid') l2_xml = l2_vlan_xml_obj.generate_xml() _add_l3(l2_xml, l2)