def validate_params(self): # state=present: name is required # state=absent: name or service_id required params = self.get_module().params name = params.get('name', None) service_id = params.get(self.get_config().PARAM_SERVICE_ID, None) state = params['state'] if state == 'present' and not name: raise SolaceParamsValidationError( 'name', name, "required for state='present'") if state == 'absent' and not name and not service_id: raise SolaceParamsValidationError( f"name, {self.get_config().PARAM_SERVICE_ID}", name, "at least one is required for state='absent'")
def validate_params(self): params = self.get_module().params name = params.get('name', None) if self.get_config().is_solace_cloud() and name != 'default': raise SolaceParamsValidationError( 'name', name, "ldap profile name for Solace Cloud must be 'default'.") invalid_chars = '-' if SolaceUtils.stringContainsAnyChars(name, invalid_chars): raise SolaceParamsValidationError( 'name', name, f"contains 1 or more invalid chars from set: '{invalid_chars}'" )
def get_remoteFormattedHostInventory( self, solace_cloud_service_facts: SolaceCloudBrokerFacts, search_dict: dict, remote_host_inventory: dict, remote_host_inventory_hostname: str, host_entry: str, api_token: str = None, meta: dict = None): remote_host_inventory_params = solace_cloud_service_facts.get_field( remote_host_inventory, remote_host_inventory_hostname) if not remote_host_inventory_params: raise SolaceParamsValidationError( "remote_host_inventory_hostname", remote_host_inventory_hostname, f"not found in remote_host_inventory: {remote_host_inventory}") _field, broker_inventory = self.get_formattedHostInventory( solace_cloud_service_facts, search_dict, host_entry, api_token, meta) del broker_inventory['all']['hosts'][host_entry]['ansible_connection'] remote_inventory = { 'all': { 'hosts': { host_entry: remote_host_inventory_params } } } inventory = SolaceUtils.merge_dicts_recursive(remote_inventory, broker_inventory) return 'remoteFormattedHostInventory', inventory
def validate_params(self): params = self.get_module().params wait_timeout_seconds = params['wait_timeout_seconds'] if wait_timeout_seconds <= 0: raise SolaceParamsValidationError('wait_timeout_seconds', wait_timeout_seconds, "must be >= 0")
def validate_params(self): params = self.get_module().params client_cert_authority_name = params['client_cert_authority_name'] if '-' in client_cert_authority_name: raise SolaceParamsValidationError('client_cert_authority_name', client_cert_authority_name, "must not contain '-'")
def validate_param_get_functions(self, valid_get_funcs: list, param_get_funcs: list) -> bool: if param_get_funcs and len(param_get_funcs) > 0: for get_func in param_get_funcs: exists = (True if get_func in valid_get_funcs else False) if not exists: raise SolaceParamsValidationError( "unknown get_function", get_func, f"valid get functions are: {valid_get_funcs}") return True return False
def create_func(self, key, name, settings=None): if not settings: raise SolaceParamsValidationError( 'settings', settings, "required for creating a service") data = { 'adminState': 'start', 'partitionId': 'default', 'name': name } data.update(settings) return self.solace_cloud_api.create_service(self.get_config(), self.get_module().params['wait_timeout_minutes'], data)
def filter(self, settings: dict, query_params: dict) -> dict: if not query_params: return settings where_list = [] if ("where" in query_params and query_params['where'] and len(query_params['where']) > 0): where_list = query_params['where'] is_match = True for where in where_list: # OP: == where_elements = where.split('==') if len(where_elements) != 2: raise SolaceParamsValidationError( 'query_params.where', where, "cannot parse where clause - must be in format '{key}=={pattern}' (other ops are not supported)" ) sempv2_key = where_elements[0] pattern = where_elements[1] solace_cloud_key = self.MAPPINGS.get(sempv2_key, None) if not solace_cloud_key: raise SolaceParamsValidationError( 'query_params.where', where, f"unknown key for solace cloud '{sempv2_key}' - check with Solace Cloud API settings" ) # pattern match solace_cloud_value = settings.get(solace_cloud_key, None) if not solace_cloud_value: raise SolaceInternalError( f"solace-cloud-key={solace_cloud_key} not found in solace cloud settings - likely a key map issue" ) # create regex regex = pattern.replace("*", ".+") this_match = re.search(regex, solace_cloud_value) is_match = (is_match and this_match) if not is_match: break if is_match: return settings return None
def validate_params(self): params = self.get_module().params from_dict = params['from_dict'] if not isinstance(from_dict, dict): raise SolaceParamsValidationError("from_dict", type(from_dict), "invalid type, must be a dict") has_get_funcs = False if params.get('get_formattedHostInventory', None): has_get_funcs = True if params.get('get_remoteFormattedHostInventory', None): has_get_funcs = True if not has_get_funcs: raise SolaceError("no get functions found - specify at least one") return
def do_task(self): self.validate_params() params = self.get_module().params search_dict = params['from_dict'] solaceCloudServiceFacts = SolaceCloudBrokerFacts( self.get_module()._name, search_dict, vpn=None) facts = {} usr_msg = None try: field, value = self.get_serviceState(solaceCloudServiceFacts, search_dict) facts[field] = value if value != 'completed': raise SolaceParamsValidationError("service.creationState", value, "is not 'completed'") param_get_formattedHostInventory = params[ 'get_formattedHostInventory'] if param_get_formattedHostInventory: field, value = self.get_formattedHostInventory( solaceCloudServiceFacts, search_dict, param_get_formattedHostInventory['host_entry'], param_get_formattedHostInventory['api_token'], param_get_formattedHostInventory['meta']) facts[field] = value param_get_remoteFormattedHostInventory = params[ 'get_remoteFormattedHostInventory'] if param_get_remoteFormattedHostInventory: field, value = self.get_remoteFormattedHostInventory( solaceCloudServiceFacts, search_dict, param_get_remoteFormattedHostInventory[ 'remote_host_inventory'], param_get_remoteFormattedHostInventory[ 'remote_host_inventory_hostname'], param_get_remoteFormattedHostInventory['host_entry'], param_get_remoteFormattedHostInventory['api_token'], param_get_remoteFormattedHostInventory['meta']) facts[field] = value except Exception as e: self.logExceptionAsDebug(type(e), e) ex_msg_list = [f"Error: {str(e)}"] usr_msg = ["Cannot get requested facts from 'from_dict'" ] + ex_msg_list self.update_result(dict(rc=1, changed=self.changed)) self.update_result(dict(facts=facts)) return usr_msg, self.get_result()
def validate_params(self): params = self.get_module().params hostvars = params['hostvars'] hostvars_inventory_hostname = params['hostvars_inventory_hostname'] param_get_functions = params['get_functions'] # check hostvars if hostvars_inventory_hostname not in hostvars: raise SolaceParamsValidationError( "hostvars, hostvars_inventory_hostname", hostvars_inventory_hostname, f"cannot find host={hostvars_inventory_hostname} in hostvars - cross check spelling with inventory file" ) if 'ansible_facts' not in hostvars[hostvars_inventory_hostname]: raise SolaceParamsValidationError( f"hostvars[{hostvars_inventory_hostname}]", hostvars[hostvars_inventory_hostname], "cannot find 'ansible_facts'") if 'solace' not in hostvars[hostvars_inventory_hostname][ 'ansible_facts']: raise SolaceParamsValidationError( f"hostvars[{hostvars_inventory_hostname}]['ansible_facts']", hostvars[hostvars_inventory_hostname]['ansible_facts'], "cannot find 'solace'") # get funcs has_get_funcs = self.validate_param_get_functions( self.GET_FUNCTIONS, param_get_functions) if not has_get_funcs: raise SolaceParamsValidationError("get_functions", param_get_functions, "empty. specify at least one") # check vpn exists search_dict = hostvars[hostvars_inventory_hostname]['ansible_facts'][ 'solace'] vpns = self.get_vpns(search_dict) vpn_name = params['msg_vpn'] if not vpn_name and len(vpns) == 1: vpn_name = vpns[0] # check for wrong vpn if vpn_name and vpn_name not in vpns: raise SolaceParamsValidationError( "msg_vpn", params['msg_vpn'], f"vpn does not exist - select one of {vpns}") self.vpn_name = vpn_name get_functions = params['get_functions'] if get_functions and len(get_functions) > 0: for get_func in get_functions: if get_func in self.REQUIRES_VPN and not self.vpn_name: raise SolaceParamsValidationError( "msg_vpn", params['msg_vpn'], f"required for get_function={get_func}. vpns found: {vpns}" )
def validate_params(self): params = self.get_module().params name = params['name'] if '-' in name: raise SolaceParamsValidationError( 'name', name, "must not contain '-'")
def validate_params(self): name = self.get_module().params['name'] if len(name) > 32: raise SolaceParamsValidationError( 'name', name, f"length={len(name)}, must not be longer than 32 chars")
def validate_key(self, key): if SolaceUtils.doesStringContainAnyWhitespaces(key): raise SolaceParamsValidationError( 'name', key, "must not contain any whitespace")
def validate_params(self): topic = self.get_module().params['name'] if SolaceUtils.doesStringContainAnyWhitespaces(topic): raise SolaceParamsValidationError( 'topic', topic, "must not contain any whitespace") return super().validate_params()