def EventTypeFromTypeString(source_crds, type_string): """Returns the matching event type object given a list of source crds.""" for crd in source_crds: for event_type in crd.event_types: if type_string == event_type.type: return event_type raise exceptions.EventTypeNotFound( 'Unknown event type: {}.'.format(type_string))
def EventTypeFromTypeString(source_crds, type_string, source=None): """Returns the matching event type object given a list of source crds. Return an EventType object given a type string and list of source CRDs. Optionally, can also pass a source string to further restrict matching event types across multiple sources. If multiple event type are found to match the given input, the user is prompted to pick one, or an error is raised if prompting is not available. Args: source_crds: list[SourceCustomResourceDefinition] type_string: str, matching an event type string (e.g. "google.cloud.pubsub.topic.v1.messagePublished"). source: str, optional source to further specify which event type in the case of multiple sources having event types with the same type string. """ possible_matches = [] for crd in source_crds: # Only match the specified source, if provided if source is not None and source != crd.source_kind: continue for event_type in crd.event_types: if type_string == event_type.type: possible_matches.append(event_type) # No matches if not possible_matches: raise exceptions.EventTypeNotFound( "Unknown event type: {}. If you're trying to use a custom event type, " 'add the "--custom-type" flag.'.format(type_string)) # Single match if len(possible_matches) == 1: return possible_matches[0] # Multiple matches if not console_io.CanPrompt(): raise exceptions.MultipleEventTypesFound( 'Multiple matching event types found: {}.'.format(type_string)) index = console_io.PromptChoice( [ '{} from {}'.format(et.type, et.crd.source_kind) for et in possible_matches ], message=('Multiple matching event types found. ' 'Please choose an event type:'), cancel_option=True) return possible_matches[index]