def __create_helper__(self, force=False, interactive=False): """Helper for creating new resources. Arguments: force: Boolean interactive: Boolean """ try: query_result = self.__find_existing_resources__() self.__find_existing_match__(query_result) except (ResourceAlreadyExistsError, ResourceHasMultipleExactMatchesError) as e: if not force and not interactive: # Rethrow error to user if we're not force creating things raise e elif interactive: msg = 'A resource with the name "{0}" already exists. ' +\ 'Do you want to create a new one?' if click.confirm(msg.format(self.__get__('name'))): return True else: raise ResourceAlreadyExistsError(self.__get__('name')) # Otherwise this is perfectly fine, create the resource! except ResourceMatchNotFoundError: pass return True
def __find_existing_match__(self, query_result): """Attempt to find a matching dashboard given a Sfx API result. Arguments: query_result: the API response from SignalFx for this Dashboard. Returns: None. Raises: ResourceMatchNotFoundError: if a single exact match couldn't be found in the SignalFx API. ResourceAlreadyExistsError: if a single exact match is found in the SignalFx API. ResourceHasMultipleExactMatchesError: if multiple exact matches were found in the SignalFx API. """ results = self.__filter_matches__(query_result) if results: raise ResourceAlreadyExistsError(self.__get__('name')) raise ResourceMatchNotFoundError(self.__get__('name'))