def change_msisdn(old_msisdn, new_msisdn): oracle = miscellaneous.OracleDB(host=Misc.ORACLE_IP_TEST) connection, cursor = oracle.connect() result = miscellaneous.Update().Users().username(oracle, connection, cursor, old_msisdn, new_msisdn) return {"status": "good", "message": result}
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 add_subscriber_oracle(attributes, config_id, msisdn, customer_id, profile_id, password): oracle = miscellaneous.OracleDB(host=Misc.ORACLE_IP_TEST) connection, cursor = oracle.connect() results = [] for name, value in attributes.items(): if name == "Framed-IP-Address" and value == "": continue result = miscellaneous.Insert().Attributes().all(oracle, connection, cursor, value, config_id, name) try: results.append(result[0]) except IndexError: return {"status": "error", "message": f"Failed to get Row before Inserting Attributes in Misc"} new_user = miscellaneous.Insert().Users().all(oracle, connection, cursor, msisdn, customer_id, config_id, profile_id, password) oracle.close(connection, cursor) return {"status": "good", "message": {"user": new_user, "results": results}}
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}