def match_any(full, patterns): matched = list( filter(lambda x: any(fnmatch.fnmatchcase(x, p) for p in patterns), full)) unmatched = list( filter(lambda x: not any(fnmatch.fnmatchcase(x, p) for p in patterns), full)) unmatched_patterns = list( filter(lambda p: not any(fnmatch.fnmatchcase(x, p) for x in full), patterns)) return matched, unmatched, unmatched_patterns
def match_ports(ports_list, port_ids_conf): """Filters the port in `ports_list` with the port id in `port_ids_conf`. A tuple of (`sp_ports_map`, `unmanaged_port_ids`) is returned, in which `sp_ports_map` is a dict whose key is SPA or SPB, value is the matched port id set, `unmanaged_port_ids` is the un-matched port id set. """ patterns = (set('*') if port_ids_conf is None else set( item.strip() for item in port_ids_conf if item.strip())) if not patterns: patterns = set('*') sp_ports_map = {} unmanaged_port_ids = set() for port in ports_list: port_id = port.get_id() for pattern in patterns: if fnmatch.fnmatchcase(port_id, pattern): sp_id = port.parent_storage_processor.get_id() ports_set = sp_ports_map.setdefault(sp_id, set()) ports_set.add(port_id) break else: unmanaged_port_ids.add(port_id) return sp_ports_map, unmanaged_port_ids
def get_rsrc_restricted_actions(self, resource_name): """Returns a set of restricted actions. For a given resource we get the set of restricted actions. Actions are set in this format via `resources`:: { "restricted_actions": [update, replace] } A restricted_actions value is either `update`, `replace` or a list of those values. Resources support wildcard matching. The asterisk sign matches everything. """ ress = self._registry['resources'] restricted_actions = set() for name_pattern, resource in ress.items(): if fnmatch.fnmatchcase(resource_name, name_pattern): if 'restricted_actions' in resource: actions = resource['restricted_actions'] if isinstance(actions, str): restricted_actions.add(actions) elif isinstance(actions, collections.Sequence): restricted_actions |= set(actions) return restricted_actions
def get_rsrc_restricted_actions(self, resource_name): """Returns a set of restricted actions. For a given resource we get the set of restricted actions. Actions are set in this format via `resources`: { "restricted_actions": [update, replace] } A restricted_actions value is either `update`, `replace` or a list of those values. Resources support wildcard matching. The asterisk sign matches everything. """ ress = self._registry['resources'] restricted_actions = set() for name_pattern, resource in six.iteritems(ress): if fnmatch.fnmatchcase(resource_name, name_pattern): if 'restricted_actions' in resource: actions = resource['restricted_actions'] if isinstance(actions, six.string_types): restricted_actions.add(actions) elif isinstance(actions, collections.Sequence): restricted_actions |= set(actions) return restricted_actions
def clear_wildcard_hooks(hc, stack_id, stack_patterns, hook_type, resource_pattern): if stack_patterns: for resource in hc.resources.list(stack_id): res_name = resource.resource_name if fnmatch.fnmatchcase(res_name, stack_patterns[0]): nested_stack = hc.resources.get(stack_id=stack_id, resource_name=res_name) clear_wildcard_hooks(hc, nested_stack.physical_resource_id, stack_patterns[1:], hook_type, resource_pattern) else: for resource in hc.resources.list(stack_id): res_name = resource.resource_name if fnmatch.fnmatchcase(res_name, resource_pattern): clear_hook(hc, stack_id, res_name, hook_type)
def match_ports(ports_list, port_ids_conf): """Filters the port in `ports_list` with the port id in `port_ids_conf`. A tuple of (`sp_ports_map`, `unmanaged_port_ids`) is returned, in which `sp_ports_map` is a dict whose key is SPA or SPB, value is the matched port id set, `unmanaged_port_ids` is the un-matched port id set. """ patterns = (set('*') if port_ids_conf is None else set(item.strip() for item in port_ids_conf if item.strip())) if not patterns: patterns = set('*') sp_ports_map = {} unmanaged_port_ids = set() for port in ports_list: port_id = port.get_id() for pattern in patterns: if fnmatch.fnmatchcase(port_id, pattern): sp_id = port.parent_storage_processor.get_id() ports_set = sp_ports_map.setdefault(sp_id, set()) ports_set.add(port_id) break else: unmanaged_port_ids.add(port_id) return sp_ports_map, unmanaged_port_ids
def clear_wildcard_hooks(hc, stack_id, stack_patterns, hook_type, resource_pattern): if stack_patterns: for resource in hc.resources.list(stack_id): res_name = resource.resource_name if fnmatch.fnmatchcase(res_name, stack_patterns[0]): nested_stack = hc.resources.get( stack_id=stack_id, resource_name=res_name) clear_wildcard_hooks( hc, nested_stack.physical_resource_id, stack_patterns[1:], hook_type, resource_pattern) else: for resource in hc.resources.list(stack_id): res_name = resource.resource_name if fnmatch.fnmatchcase(res_name, resource_pattern): clear_hook(hc, stack_id, res_name, hook_type)
def remove_resources_except(self, resource_name): ress = self._registry['resources'] new_resources = {} for name, res in six.iteritems(ress): if fnmatch.fnmatchcase(resource_name, name): new_resources.update(res) if resource_name in ress: new_resources.update(ress[resource_name]) self._registry['resources'] = new_resources
def remove_resources_except(self, resource_name): ress = self._registry['resources'] new_resources = {} for name, res in ress.items(): if fnmatch.fnmatchcase(resource_name, name): new_resources.update(res) if resource_name in ress: new_resources.update(ress[resource_name]) self._registry['resources'] = new_resources
def do_match(full, matcher_list): matched = set() full = set([item.strip() for item in full]) if matcher_list is None: # default to all matcher_list = set('*') else: matcher_list = set([item.strip() for item in matcher_list]) for item in full: for matcher in matcher_list: if fnmatch.fnmatchcase(item, matcher): matched.add(item) return matched, full - matched
def _get_managed_storage_pools(self, pools): matched_pools = set() if pools: # Get the real pools from the backend storage status, backend_pools = self._get_context('StoragePool').get_all() if status != constants.STATUS_OK: message = (_("Failed to get storage pool information. " "Reason: %s") % backend_pools) LOG.error(message) raise exception.EMCVnxXMLAPIError(err=message) real_pools = set([item for item in backend_pools]) conf_pools = set([item.strip() for item in pools.split(",")]) for pool in real_pools: for matcher in conf_pools: if fnmatch.fnmatchcase(pool, matcher): matched_pools.add(pool) nonexistent_pools = real_pools.difference(matched_pools) if not matched_pools: msg = (_("All the specified storage pools to be managed " "do not exist. Please check your configuration " "emc_nas_pool_names in manila.conf. " "The available pools in the backend are %s") % ",".join(real_pools)) raise exception.InvalidParameterValue(err=msg) if nonexistent_pools: LOG.warning( _LW("The following specified storage pools " "do not exist: %(unexist)s. " "This host will only manage the storage " "pools: %(exist)s"), { 'unexist': ",".join(nonexistent_pools), 'exist': ",".join(matched_pools) }) else: LOG.debug("Storage pools: %s will be managed.", ",".join(matched_pools)) else: LOG.debug("No storage pool is specified, so all pools " "in storage system will be managed.") return matched_pools
def _get_managed_storage_pools(self, pools): matched_pools = set() if pools: # Get the real pools from the backend storage status, backend_pools = self._get_context('StoragePool').get_all() if status != constants.STATUS_OK: message = (_("Failed to get storage pool information. " "Reason: %s") % backend_pools) LOG.error(message) raise exception.EMCVnxXMLAPIError(err=message) real_pools = set([item for item in backend_pools]) conf_pools = set([item.strip() for item in pools.split(",")]) for pool in real_pools: for matcher in conf_pools: if fnmatch.fnmatchcase(pool, matcher): matched_pools.add(pool) nonexistent_pools = real_pools.difference(matched_pools) if not matched_pools: msg = (_("All the specified storage pools to be managed " "do not exist. Please check your configuration " "emc_nas_pool_names in manila.conf. " "The available pools in the backend are %s") % ",".join(real_pools)) raise exception.InvalidParameterValue(err=msg) if nonexistent_pools: LOG.warning(_LW("The following specified storage pools " "do not exist: %(unexist)s. " "This host will only manage the storage " "pools: %(exist)s"), {'unexist': ",".join(nonexistent_pools), 'exist': ",".join(matched_pools)}) else: LOG.debug("Storage pools: %s will be managed.", ",".join(matched_pools)) else: LOG.debug("No storage pool is specified, so all pools " "in storage system will be managed.") return matched_pools
def do_match_any(full, matcher_list): """Finds items that match any of the matchers. :param full: Full item list :param matcher_list: The list of matchers. Each matcher supports Unix shell-style wildcards :return: The matched items set and the unmatched items set """ matched = set() not_matched = set() full = set([item.strip() for item in full]) matcher_list = set([item.strip() for item in matcher_list]) for matcher in matcher_list: for item in full: if fnmatch.fnmatchcase(item, matcher): matched.add(item) not_matched = full - matched return matched, not_matched
def matches_hook(self, resource_name, hook): """Return whether a resource have a hook set in the environment. For a given resource and a hook type, we check to see if the passed group of resources has the right hook associated with the name. Hooks are set in this format via `resources`: { "res_name": { "hooks": [pre-create, pre-update] }, "*_suffix": { "hooks": pre-create }, "prefix_*": { "hooks": pre-update } } A hook value is either `pre-create`, `pre-update` or a list of those values. Resources support wildcard matching. The asterisk sign matches everything. """ ress = self._registry['resources'] for name_pattern, resource in six.iteritems(ress): if fnmatch.fnmatchcase(resource_name, name_pattern): if 'hooks' in resource: hooks = resource['hooks'] if isinstance(hooks, six.string_types): if hook == hooks: return True elif isinstance(hooks, collections.Sequence): if hook in hooks: return True return False
def matches_hook(self, resource_name, hook): """Return whether a resource have a hook set in the environment. For a given resource and a hook type, we check to see if the passed group of resources has the right hook associated with the name. Hooks are set in this format via `resources`:: { "res_name": { "hooks": [pre-create, pre-update] }, "*_suffix": { "hooks": pre-create }, "prefix_*": { "hooks": pre-update } } A hook value is either `pre-create`, `pre-update` or a list of those values. Resources support wildcard matching. The asterisk sign matches everything. """ ress = self._registry['resources'] for name_pattern, resource in ress.items(): if fnmatch.fnmatchcase(resource_name, name_pattern): if 'hooks' in resource: hooks = resource['hooks'] if isinstance(hooks, str): if hook == hooks: return True elif isinstance(hooks, collections.Sequence): if hook in hooks: return True return False