def update_node(serviceTag, body): # noqa: E501 """Updates a pet in the store with form data # noqa: E501 :param serviceTag: serviceTag of node that needs to be updated :type serviceTag: str :param body: Node object that needs to be updated :type body: dict | bytes :rtype: Node """ if connexion.request.is_json: body = Node.from_dict(connexion.request.get_json()) # noqa: E501 attributes = [ a for a in dir(body) if not a.startswith('__') and not a.startswith('_') and not a == "swagger_types" and not a == "attribute_map" and not callable(getattr(body, a)) ] i = 0 updates = "" for attribute in attributes: cam_attr = conv_to_cam(attribute) attr_value = getattr(body, attribute) if i == 0: updates = f"{cam_attr} = '{attr_value}'" else: updates = f"{updates}, {cam_attr} = '{attr_value}'" i += 1 try: with sqlite3.connect(db_name) as con: con.row_factory = dict_factory cur = con.cursor() cur.execute(f"UPDATE nodes SET {updates}" f"WHERE serviceTag = '{serviceTag}'") con.commit() select = cur.execute("SELECT * FROM nodes WHERE " f"serviceTag='{serviceTag}'") result = select.fetchone() except: return {"error": "Invalid Field"}, 405 if not result: return {"error": "Node not created"}, 500 return result
def add_node(body): # noqa: E501 """Add a new node # noqa: E501 :param body: Node object that needs to be added :type body: dict | bytes :rtype: Node """ if connexion.request.is_json: body = Node.from_dict(connexion.request.get_json()) # noqa: E501 try: with sqlite3.connect(db_name) as con: con.row_factory = dict_factory cur = con.cursor() pre_select = cur.execute("SELECT * FROM nodes WHERE " f"serviceTag='{body.service_tag}'") pre_result = pre_select.fetchone() if pre_result: return {"error": "Node not created, already exists"}, 500 cur.execute(f"INSERT INTO nodes VALUES ('{body.fqdn}'," f"'{body.service_tag}'," f"'{body.host_name}'," f"'{body.status}'," f"'{body.v_center}'," f"'{body.cluster_name}'," f"'{body.vendor}'," f"'{body.idrac_ip}'," f"'{body.host_ip}')") con.commit() select = cur.execute("SELECT * FROM nodes WHERE " f"serviceTag='{body.service_tag}'") result = select.fetchone() except: return {"error": "Invalid Field"}, 405 if not result: return {"error": "Node not created"}, 500 return result
def get_virtualcluster_node_by_name(virtualclustername, nodename): # noqa: E501 """get_virtualcluster_node_by_name Returns the specified node info of the specified virtualcluster # noqa: E501 :param virtualclustername: :type virtualclustername: str :param nodename: :type nodename: str :rtype: Node """ nodes = virtualclusters.find({"name": "%s" % virtualclustername}, {'_id': False})[0]["nodes"] found = False ret = {} for node in nodes: nodeobj = Node.from_dict(node) if nodeobj.name == nodename: found = True ret = nodeobj print (ret) break return ret