def test_host_attributes(for_what, new): topics = { "basic": [ "alias", "site", "contactgroups", "parents", ], "address": [ "tag_address_family", "ipaddress", "ipv6address", "additional_ipv4addresses", "additional_ipv6addresses", ], "monitoring_agents": [ "tag_agent", "cmk_agent_connection", "tag_snmp_ds", "snmp_community", "tag_piggyback", ], "management_board": [ "management_protocol", "management_address", "management_snmp_community", "management_ipmi_credentials", ], "meta_data": [ "meta_data", "locked_by", "locked_attributes", ], "custom_attributes": [ "labels", ], } if for_what == "folder": topics["network_scan"] = [ "network_scan", "network_scan_result", ] if new: del topics["meta_data"] current_topics = attrs.get_sorted_host_attribute_topics(for_what, new) assert sorted(topics.keys()) == sorted(dict(current_topics).keys()) for topic_id, _title in current_topics: names = [ a.name() for a in attrs.get_sorted_host_attributes_by_topic(topic_id) ] assert names == topics.get( topic_id, []), ("Expected attributes not specified for topic %r" % topic_id)
def test_host_attribute_topics(for_what): assert attrs.get_sorted_host_attribute_topics(for_what=for_what) == [ ("basic", u"Basic settings"), ("address", u'Address'), ("data_sources", u'Data sources'), ("management_board", u'Management Board'), ('meta_data', u'Meta data'), ]
def test_host_attributes(for_what, new): topics = { "basic": [ 'alias', 'site', 'contactgroups', 'parents', ], "address": [ 'tag_address_family', 'ipaddress', 'ipv6address', 'additional_ipv4addresses', 'additional_ipv6addresses', ], "data_sources": [ 'tag_agent', 'tag_snmp_ds', 'snmp_community', 'tag_piggyback', ], "management_board": [ 'management_address', 'management_protocol', 'management_snmp_community', 'management_ipmi_credentials', ], "meta_data": [ 'meta_data', 'locked_by', 'locked_attributes', ], 'custom_attributes': [ 'labels', ], } if for_what == "folder": topics["network_scan"] = [ 'network_scan', 'network_scan_result', ] if new: del topics["meta_data"] current_topics = attrs.get_sorted_host_attribute_topics(for_what, new) assert sorted(topics.keys()) == sorted(dict(current_topics).keys()) for topic_id, _title in current_topics: names = [ a.name() for a in attrs.get_sorted_host_attributes_by_topic(topic_id) ] assert names == topics.get( topic_id, []), "Expected attributes not specified for topic %r" % topic_id
def test_host_attribute_topics_for_folders(): assert attrs.get_sorted_host_attribute_topics("folder") == [ ("basic", u"Basic settings"), ('address', u'Address'), ('data_sources', u'Data sources'), ('network_scan', u'Network Scan'), ('management_board', u'Management Board'), ('meta_data', u'Meta data'), ]
def test_host_attribute_topics(for_what): assert attrs.get_sorted_host_attribute_topics(for_what=for_what, new=False) == [ ("basic", u"Basic settings"), ("address", u'Network Address'), ("data_sources", u'Data sources'), ('custom_attributes', u'Custom attributes'), ("management_board", u'Management Board'), ('meta_data', u'Creation / Locking'), ]
def test_host_attribute_topics(for_what): assert attrs.get_sorted_host_attribute_topics(for_what=for_what, new=False) == [ ("basic", "Basic settings"), ("address", "Network address"), ("monitoring_agents", "Monitoring agents"), ("custom_attributes", "Custom attributes"), ("management_board", "Management board"), ("meta_data", "Creation / Locking"), ]
def test_host_attribute_topics_for_folders(): assert attrs.get_sorted_host_attribute_topics("folder", new=False) == [ ("basic", u"Basic settings"), ('address', u'Network Address'), ('data_sources', u'Data sources'), ('custom_attributes', u'Custom attributes'), ('network_scan', u'Network Scan'), ('management_board', u'Management Board'), ('meta_data', u'Creation / Locking'), ]
def collect_attributes( object_type: ObjectType, context: ObjectContext, ) -> List[Attr]: """Collect all attributes for a specific use case Use cases can be host or folder creation or updating. Args: object_type: Either 'host', 'folder' or 'cluster' context: Either 'create' or 'update' Returns: A list of attribute describing named-tuples. Examples: >>> attrs = collect_attributes('host', 'create') >>> assert len(attrs) > 10, len(attrs) >>> attrs = collect_attributes('host', 'update') >>> assert len(attrs) > 10, len(attrs) >>> attrs = collect_attributes('cluster', 'create') >>> assert len(attrs) > 10, len(attrs) >>> attrs = collect_attributes('cluster', 'update') >>> assert len(attrs) > 10, len(attrs) >>> attrs = collect_attributes('folder', 'create') >>> assert len(attrs) > 10, len(attrs) >>> attrs = collect_attributes('folder', 'update') >>> assert len(attrs) > 10 To check the content of the list, uncomment this one. # >>> import pprint # >>> pprint.pprint(attrs) """ something = TypeVar("something") def _ensure(optional: Optional[something]) -> something: if optional is None: raise ValueError return optional T = typing.TypeVar("T") def maybe_call(func: Optional[Callable[[], T]]) -> Optional[T]: if func is None: return None return func() # NOTE: # We want to get all the topics, so we don't miss any attributes. We filter them later. # new=True may also be new=False, it doesn't matter in this context. result = [] for topic_id, topic_title in get_sorted_host_attribute_topics("always", new=True): for attr in get_sorted_host_attributes_by_topic(topic_id): if object_type == "folder" and not attr.show_in_folder(): continue if context in ["create", "update"] and not attr.openapi_editable(): continue help_text: str = strip_tags(attr.help()) or "" # TODO: what to do with attr.depends_on_tags()? attr_entry = Attr( name=attr.name(), description=help_text, section=topic_title, mandatory=attr.is_mandatory(), field=maybe_call(getattr(attr, "openapi_field", None)), ) result.append(attr_entry) tag_config = load_tag_config() tag_config += BuiltinTagConfig() def _format(tag_id: Optional[str]) -> str: if tag_id is None: return "`null`" return f'`"{tag_id}"`' tag_group: TagGroup for tag_group in tag_config.tag_groups: description: List[str] = [] if tag_group.help: description.append(tag_group.help) if tag_group.tags: description.append("Choices:") for tag in tag_group.tags: description.append(f" * {_format(tag.id)}: {tag.title}") result.append( Attr( name=_ensure(f"tag_{tag_group.id}"), section=tag_group.topic or "No topic", mandatory=False, description="\n\n".join(description), enum=[tag.id for tag in tag_group.tags], field=None, )) return result