diff --git a/odins_spear/exceptions.py b/odins_spear/exceptions.py index 6c8df1a..c3057a3 100644 --- a/odins_spear/exceptions.py +++ b/odins_spear/exceptions.py @@ -113,8 +113,14 @@ class OSInvalidPasswordType(OSError): """ def __str__(self) -> str: - return f"Invalid or unsupported password, please review supported passwords." - + return f"Invalid or unsupported password, please review supported passwords." + +class OSExtensionNotFound(OSError): + """ Raised when a searched extension is not found + """ + + def __str__(self) -> str: + return f"Cannot locate extension. Please alter search criteria" class OSServiceNotAssigned(OSError): """ Raised a service needed is not assigned to a Broadworks entity. @@ -123,7 +129,6 @@ class OSServiceNotAssigned(OSError): def __str__(self) -> str: return f"Service not assigend to target Broadworks entity. Please check services assigned." - class OSFileNotFound(OSError): """ Raised when a file can not be found. """ diff --git a/odins_spear/scripter.py b/odins_spear/scripter.py index f783bcc..4b9b9f7 100644 --- a/odins_spear/scripter.py +++ b/odins_spear/scripter.py @@ -167,9 +167,17 @@ def remove_numbers(self, service_provider_id: str, group_id: str, start_of_range return scripts.remove_numbers.main(self.api, service_provider_id, group_id, start_of_range_number, end_of_range_number) - def locate_free_extension(self, service_provider_id: str, group_id: str, field: list): + def locate_free_extension(self, service_provider_id: str, group_id: str, extension_range: list): + """Locates the lowest value free extension given the provided range of extension numbers. - return scripts.locate_free_extension(self.api, service_provider_id, group_id, field) + Note: The extension range is passed as a list like such [100, 1000] + + Args: + service_provider_id (str): Service Provider/ Enterprise ID where Group is located which hosts needed free extensions + group_id (str): Group ID where target extensions are located. + extension_range (list): List value representing the search range for free extensions + """ + return scripts.locate_free_extension.main(self.api, service_provider_id, group_id, extension_range) def service_provider_trunking_capacity(self, service_provider_id: str): """Returns a JSON breakdown of the Trunking Call Capacity of a Service Provider/ Enterprise (SP/ENT). diff --git a/odins_spear/scripts/locate_free_extension.py b/odins_spear/scripts/locate_free_extension.py index 349011f..a285f83 100644 --- a/odins_spear/scripts/locate_free_extension.py +++ b/odins_spear/scripts/locate_free_extension.py @@ -1,3 +1,4 @@ +from exceptions import OSExtensionNotFound def retrieve_extensions( api, @@ -7,97 +8,49 @@ def retrieve_extensions( extensions = [] - dataset = api.get.users( - service_provider_id, - group_id - ) - - for data in dataset: - if not data['extension']: - continue - ### - - extensions.append(data['extension']) - - dataset = api.get.group_hunt_groups( - service_provider_id, - group_id + dataset = ( + api.get.users( service_provider_id, group_id ) + + api.get.group_hunt_groups( service_provider_id,group_id ) + + api.get.group_call_centers( service_provider_id, group_id ) + + api.get.auto_attendants( service_provider_id, group_id ) ) for data in dataset: if not data['extension']: continue - ### - extensions.append(data['extension']) + extensions.append(int(data['extension'])) - dataset = api.get.group_call_centers( - service_provider_id, - group_id - ) - - for data in dataset: - if not data['extension']: - continue - ### - - extensions.append(data['extension']) - - dataset = api.get.auto_attendants( - service_provider_id, - group_id - ) - - for data in dataset: - if not data['extension']: - continue - - extensions.append(data['extension']) - ### - - if not extensions: - return - - return extensions -### + return extensions if extensions else None def main( api, - service_provider_id, - group_id, - field: list + service_provider_id: str, + group_id: str, + extension_range: list ): + '''Retrieves The Lowest Free Extension Available In The Designated Group Passed. ''' - Retrieves The Lowest Free Extension Available In The Designated Group Passed. - field is passed as such [100, 1000] - ''' - ### Retrieve List Of Occupied Extensions Within The Group + if extension_range[0] > extension_range[1]: + initial_range = extension_range[1] + + extension_range[1] = extension_range[0] + extension_range[0] = initial_range + + # Retrieve List Of Occupied Extensions Within The Group extensions = retrieve_extensions( api, service_provider_id, group_id, ) - ### Cull Out Of Range Extensions - for extension in extensions: - - if field[0] < extension and field[1] > extension: - continue - - extensions.remove(extension) - ### - - ### Locate Initial Free Extension - extension_set = set(extensions) - - for extension in range(field[0] + 1, field[1]): - if extension not in extension_set: - return extension - #### + for extension in range(extension_range[0], extension_range[1] + 1): + if extension not in extensions: + return {'extension': extension} - print("Unable To Locate A Free Extension Within The Range") - return + if not extension: + raise OSExtensionNotFound