def check_config_id(config_id): oracle = miscellaneous.OracleDB(host=Misc.ORACLE_IP_TEST) connection, cursor = oracle.connect() result = miscellaneous.Select().Attributes().config_id_equals(oracle, cursor, config_id) if result: return {"status": "error", "message": f"The provided config_id {config_id} is already in use: {result}"} else: return {"status": "good", "message": f"The provided config_id {config_id} is not used"}
def check_old_msisdn(old_msisdn): oracle = miscellaneous.OracleDB(host=Misc.ORACLE_IP_TEST) connection, cursor = oracle.connect() users = miscellaneous.Select().Users().username_equals( oracle, cursor, old_msisdn) try: user = users[0] except IndexError: return { "status": "error", "message": f"User with msisdn {old_msisdn} does not exist" } try: user.get('USER_NAME') except KeyError as key_error: return {"status": "error", "message": key_error} return {"status": "good", "message": user.get('USER_NAME')}
def main(): # input_data = {"msisdn": 375291797391, # "vrf": {"name": "VPN-BELENERGO", # "rd": 10234} # } # checking parameter passing try: input_string = sys.argv[1] except IndexError: return {"status": "error", "message": "Parameters required"} # checking if the passed parameter is correct - need json string try: input_data = json.loads(input_string) except json.decoder.JSONDecodeError as json_error: return {"status": "error", "message": str(json_error)} # Check MSISDN check_msisdn = re.findall(r"^375\d{9}$", str(input_data.get("msisdn"))) if not check_msisdn: return {"status": "error", "message": "You must enter an msisdn or " "the entered msisdn is not in a format. Correct format: 375291111111"} # Parameter check: VRF if input_data.get("vrf") is None: return {"status": "error", "message": "You must enter an VRF: 'vrf':{'name': 'VPN-BELENERGO', 'rd': 10234}"} # Check VRF Name if not isinstance(input_data.get("vrf").get("name"), str): return {"status": "error", "message": "The entered VRF Name is not str type"} msisdn = input_data.get("msisdn") vrf_name = input_data.get("vrf").get("name") oracle = miscellaneous.OracleDB(host=Misc.ORACLE_IP_TEST) connection, cursor = oracle.connect() # get user by msisdn users = miscellaneous.Select().Users().username_equals(oracle, cursor, msisdn) vrf_status = check_vrf(vrf_name) if vrf_status.get("status") is "error": return vrf_status else: # Check User Existence Check try: user = users[0] except IndexError: return {"status": "error", "message": f"User with msisdn {msisdn} does not exist"} try: config_id = user.get('CONFIG_ID') except KeyError as key_error: return {"status": "error", "message": key_error} # Check user has config_id - continue changes if config_id is (None or ""): return {"status": "error", "message": f"User with msisdn {msisdn} has no config_id in SQL-request"} users_in_config = miscellaneous.Select().Users().config_id_equals(oracle, cursor, config_id) # If there is only one user with this config_id - remove attributes, otherwise do not remove attributes # (only delete user from VPN_USERS) if len(users_in_config) == 1: attributes = miscellaneous.Select().Attributes().config_id_equals(oracle, cursor, config_id) for attribute in attributes: # if user with static ip, delete ip from netbox if attribute.get("ATT_NAME") == "Framed-IP-Address": try: ip_address = attribute.get("VALUE") delete_ip_netbox(vrf_name, ip_address) except KeyError: continue # deleting attributes from VPN_ATTRIBUTES with this config_id miscellaneous.Delete().Attributes().config_id_equals(oracle, connection, cursor, config_id) # deleting user from VPN_USERS result = miscellaneous.Delete().Users().username_equals(oracle, connection, cursor, msisdn) return {"status": "good", "message": result}