def modify_hardware_tag(card, DB: MySQL, EEM: EEM): DB.start_transaction() status, message = modify_card(card, __table_hardware, hardware_card_keys, DB, EEM) status, message = modify_capacities(card, __table_hardware_capacity, hardware_capacity_keys, DB, EEM) # First status code checks: # 1: Syntaxis # 2: Completeness if status: status, message = DB.commit_transaction() # Second status checks: # 1: Data integrity if status: return messenger.message200("OK") # Implicit rollback else: return messenger.message404(message) else: status, error = DB.rollback(message) return messenger.message404(error)
def erase_card(id, DB: MySQL, EEM: EEM): DB.start_transaction() if get_card(id, DB, EEM)["status"] == "eeio": DB.start_transaction() status, message = DB.delete_query_simple(__table_hardware, "id", id) else: status, message = DB.delete_query_simple(__table_net, "id", id) # First status code checks: # 1: Syntaxis # 2: Completeness if status: status, result = DB.commit_transaction() # Second status checks: # 1: Data integrity if status: return messenger.message200("OK") # Implicit rollback else: return messenger.message404(message) else: status, error = DB.rollback(messsage) return messenger.message404(error)
def get_workload(id, DB: MySQL): statement = ("SELECT * FROM %s ") % __table_workloads statement += ("WHERE ID = %s") % id workload = DB.select_query(statement) if workload and isinstance(workload, Iterable): return process_workload(DB, next(iter(workload))) else: error = "The requested ID does not exist on the server" return messenger.message404(error)
def get_card(id, DB: MySQL, EEM: EEM): ids = get_all_cards(DB, EEM) if id not in ids: return messenger.message404( "The requested card ID does not exist on the server") out = eemParser.parse(EEM.get_box_info(id)) if out["status"] == "eeio": table = __table_hardware mapping = hardware_card_keys statement = ("SELECT * FROM %s " 'WHERE hardware_id = "%s"') % (__table_hardware_capacity, id) capacities = DB.select_query(statement) else: table = __table_net mapping = net_card_keys capacities = None statement = ("SELECT * FROM %s " 'WHERE id = "%s"') % (table, id) card = DB.select_query(statement) if card: if (isinstance(card, Iterable) and not isinstance(card, str)): card = next(iter(card)) for param in range(0, len(mapping)): out[mapping[param]] = card[param] else: for param in range(0, len(mapping)): if mapping[param] != "id": out[mapping[param]] = "UNKNOWN" if capacities: insert_capactities = [] for capacity in capacities: insert_capacity = {} for param in range(0, len(hardware_capacity_keys)): if hardware_capacity_keys[param] != "hardware_id": insert_capacity[hardware_capacity_keys[param]] \ = capacity[param] insert_capactities.append(insert_capacity) out["capacity"] = insert_capactities else: out["capacity"] = "UNKNOWN" return out
def get_all_workloads(DB: MySQL): statement = ("SELECT * FROM ") + __table_workloads workloads = DB.select_query(statement) res_workloads = [] if workloads: for workload in workloads: res_workloads.append(process_workload(DB, workload)) return res_workloads else: error = "The requested ID does not exist on the server" return messenger.message404(error)
def erase_workload(id, DB: MySQL): DB.start_transaction() # A single delete is enough as child tables delete on cascade status, message = DB.delete_query_simple(__table_workloads, "id", id) # First status code checks: # 1: Syntaxis # 2: Completeness if status: status, message = DB.commit_transaction() # Second status checks: # 1: Data integrity if status: return messenger.message200("OK") # Implicit rollback else: return messenger.message404(message) else: status, error = DB.rollback(messsage) return messenger.message404(error)
def get_all_assignments(DB: MySQL): statement = ("SELECT * FROM %s ") % __table assignments = DB.select_query(statement) res_assignments = [] if assignments: for assignment in assignments: res_assignment = {} for x in range(0, len(assignment_keys)): res_assignment[assignment_keys[x]] = assignment[x] res_assignments.append(res_assignment) return res_assignments else: return messenger.message404("No assignments found")
def configure(binder): binder.bind(MySQL, to=MySQL( MySQLFactory( os.environ['MYSQL_HOST'], os.environ['MYSQL_USER'], os.environ['MYSQL_PASSWORD'], os.environ['MYSQL_DATABASE'], ), )) binder.bind(EEM, to=EEM(EEMFactory( __eemConfigFile, __eemBasePath, ), )) return binder
def create_workload(workload, DB: MySQL): DB.start_transaction() mapping = deepcopy(workload_keys) mapping_requirement = deepcopy(hardware_requirements_keys) mapping_requirement_insert = deepcopy(hardware_requirements_keys) mapping_capacity = deepcopy(capacity_requirements_keys) mapping.remove("id") # As it is assigned by the API mapping_requirement.remove("workload_id") # Not present in the petition mapping_requirement.remove("requirement_id") # Not present in the petition mapping_requirement_insert.remove( "requirement_id") # Not present in the petition mapping_capacity.remove("workload_id") # Not present in the petition mapping_capacity.remove("requirement_id") # Not present in the petition if len(workload.keys()) - 1 != len(mapping): message = "Inserted workload data is incorrect" return messenger.message404(message) # Get the assigned ID of the workload statement = ("SELECT MAX(id) FROM workloads") workload_id = DB.select_query(statement) while (isinstance(workload_id, Iterable)): workload_id = next(iter(workload_id)) if not workload_id or workload_id < 0: workload_id = 1 else: workload_id += 1 values = [workload_id] for x in range(0, len(mapping)): if mapping[x] in workload: values.append(workload[mapping[x]]) else: message = "%s not introduced in the request" % (mapping[x]) return messenger.message404(message) status1, message1 = DB.insert_query(__table_workloads, workload_keys, values) # Time to create the requirements of the workload, one entry each requirements = workload["requirements"] req_number = 0 for requirement in requirements: req_number += 1 statement = ("SELECT MAX(requirement_id) FROM hardware_requirements") req_id = DB.select_query(statement) while (isinstance(req_id, Iterable)): req_id = next(iter(req_id)) if not req_id: req_id = req_number else: req_id += req_number values = [req_id, workload_id] for x in range(0, len(mapping_requirement)): if mapping_requirement[x] in requirement: values.append(requirement[mapping_requirement[x]]) else: message = "%s not introduced in the request" % ( mapping_requirement[x]) return messenger.message404(message) status2, message2 = DB.insert_query(__table_hardware_requirements, hardware_requirements_keys, values) capacity_req = requirement["hardware_capacity_requirements"] for capacity in capacity_req: values = [workload_id, req_id] for x in range(0, len(mapping_capacity)): if mapping_capacity[x] in capacity: values.append(capacity[mapping_capacity[x]]) else: message = "%s not introduced in the request" % ( mapping_capacity[x]) return messenger.message404(message) status3, message3 = DB.insert_query(__table_capacity_requirements, capacity_requirements_keys, values) if not status1: status = False message = message1 elif not status2: status = False message = message2 elif not status3: status = False message = message3 else: status = True message = "OK" # First status code checks: # 1: Syntaxis # 2: Completeness if status: status, message = DB.commit_transaction() # Second status checks: # 1: Data integrity if status: return messenger.messageWorkload(workload_id) # Implicit rollback else: return messenger.message404(message) else: status, error = DB.rollback(message) return messenger.message404(error)
def erase_assignment(workload, DB: MySQL, EEM: EEM): DB.start_transaction() workload_id = next(iter(workload.values())) # Retrieve server EEM net card and GID workload_server_net_card, gid = get_server_card(workload_id, DB) if not workload_server_net_card: return finish_assignment( False, "Workload does not exist or is not assigned to any server", DB) downs_port = get_card(workload_server_net_card, DB, EEM)["downstream_port"] if downs_port == "All down": message = ( "Cannot erase assignment as the network card %s " "does not have any hardware card attached" % workload_server_net_card) return finish_assignment( False, message, DB) downs_port = downs_port["downstream_port_id"] assigned_cards = get_assignment_hardware(workload_id, DB) if not assigned_cards: message = ( "Cannot erase assignment as the workload does not " "have any hardware card assigned" ) return finish_assignment( False, message, DB) erase_message = "" for assigned_hardware in assigned_cards: assigned_hardware = next(iter(assigned_hardware)) values = [assigned_hardware, workload_server_net_card, workload_id] if is_feasible_to_unasign_hardware( assigned_hardware, workload_server_net_card, DB): EEM.disconect_hardware_from_server(assigned_hardware) erase_message += "VLAN erased between %s and %s" % ( assigned_hardware, workload_server_net_card) else: erase_message += ( "VLAN between %s and %s could't be erased as " "there are other assignments in progress, " "DB assignment erased" ) % (assigned_hardware, workload_server_net_card) # Always eliminate assignment in DB even if the VLAN can't be erased status1, message1 = DB.delete_query( __table, assignment_keys, values) status2, message2 = DB.delete_query_simple( __table_assigned_capacities, "workload", workload_id) if not status1: status = False message = message1 elif not status2: status = False message = message2 else: status = True if not status: return finish_assignment( False, message, DB) return finish_assignment( True, erase_message, DB)
def create_assignment(workload, DB: MySQL, EEM: EEM): DB.start_transaction() workload_id = next(iter(workload.values())) statement = ("SELECT * FROM hardware_requirements ") statement += ("WHERE workload_id = %s") % workload_id workload_requirements_hardware = DB.select_query(statement) if not workload_requirements_hardware: message = "Workload does not exist or " message += "does not have any hardware requirements" return finish_assignment(False, message, DB) # Retrieve server EEM net card and GID workload_server_net_card, gid = get_server_card(workload_id, DB) if not workload_server_net_card: return finish_assignment( False, "The workload server does not have any EEM net card attached", DB) boxes = set() for requirement in workload_requirements_hardware: available_hardware = get_available_hardware( requirement, workload_server_net_card, DB, EEM) if not available_hardware: return finish_assignment( False, "There is not any availble hardware of the type: %s" % ( requirement[2]), DB) hardware_choice = select_hardware(available_hardware) status, message = assign_capacities( hardware_choice, workload_server_net_card, workload_id, requirement[0], DB) if not status: return finish_assignment( False, message, DB) boxes.add(hardware_choice) vlan_message = "" for box in boxes: values = [box, workload_server_net_card, workload_id] status, message = DB.insert_query( __table, assignment_keys, values) if not status: return finish_assignment( False, message, DB) EEM.assign_hardware_to_server(box, gid) vlan_message += "New VLAN created between %s and %s " % ( box, workload_server_net_card) return finish_assignment( True, vlan_message, DB)