Example #1
0
def host_collection(hosts: Iterable[watolib.CREHost]) -> Response:
    _hosts = {
        'id':
        'host',
        'domainType':
        'host_config',
        'value': [
            constructors.collection_item(
                domain_type='host_config',
                obj={
                    'title': host.name(),
                    'id': host.id()
                },
            ) for host in hosts
        ],
        'links': [
            constructors.link_rel('self',
                                  constructors.collection_href('host_config'))
        ],
    }
    return constructors.serve_json(_hosts)
Example #2
0
def get_bi_packs(params):
    """Show all BI Packs"""

    bi_packs = get_cached_bi_packs()
    bi_packs.load_config()
    packs = [
        constructors.collection_item(
            domain_type='bi_pack',
            obj={
                'id': pack.id,
                'title': pack.title,
            },
        ) for pack in bi_packs.packs.values()
    ]

    collection_object = constructors.collection_object(
        domain_type='bi_pack',
        value=packs,
        links=[constructors.link_rel('self', constructors.collection_href('bi_pack'))],
    )
    return constructors.serve_json(collection_object)
Example #3
0
def list_passwords(params):
    """Show all passwords"""
    user.need_permission("wato.passwords")
    password_collection = {
        "id":
        "password",
        "domainType":
        "password",
        "value": [
            constructors.collection_item(
                domain_type="password",
                title=details["title"],
                identifier=password_id,
            ) for password_id, details in load_passwords().items()
        ],
        "links": [
            constructors.link_rel("self",
                                  constructors.collection_href("password"))
        ],
    }
    return constructors.serve_json(password_collection)
Example #4
0
def _serialize_folder(folder: CREFolder, show_hosts):
    links = []

    if not folder.is_root():
        links.append(
            constructors.link_rel(
                rel="cmk/move",
                href=constructors.object_action_href(
                    "folder_config",
                    folder_slug(folder),
                    action_name="move",
                ),
                method="post",
                title="Move the folder",
            ))

    rv = constructors.domain_object(
        domain_type="folder_config",
        identifier=folder_slug(folder),
        title=folder.title(),
        extensions={
            "path": "/" + folder.path(),
            "attributes": folder.attributes().copy(),
        },
        links=links,
    )
    if show_hosts:
        rv["members"]["hosts"] = constructors.collection_property(
            name="hosts",
            base=constructors.object_href("folder_config",
                                          folder_slug(folder)),
            value=[
                constructors.collection_item(
                    domain_type="host_config",
                    identifier=host.id(),
                    title=host.name(),
                ) for host in folder.hosts().values()
            ],
        )
    return rv
Example #5
0
def list_passwords(params):
    """Show all passwords"""
    password_collection = {
        "id":
        "password",
        "domainType":
        "password",
        "value": [
            constructors.collection_item(
                domain_type="password",
                obj={
                    "title": details["title"],
                    "id": password_id,
                },
            ) for password_id, details in load_passwords().items()
        ],
        "links": [
            constructors.link_rel("self",
                                  constructors.collection_href("password"))
        ],
    }
    return constructors.serve_json(password_collection)
Example #6
0
def list_users(params):
    """Show all users"""
    user_collection = {
        "id":
        "user",
        "domainType":
        "user_config",
        "value": [
            constructors.collection_item(
                domain_type="user_config",
                obj={
                    "title": attrs["alias"],
                    "id": user_id
                },
            ) for user_id, attrs in userdb.load_users(False).items()
        ],
        "links": [
            constructors.link_rel("self",
                                  constructors.collection_href("user_config"))
        ],
    }
    return constructors.serve_json(user_collection)
Example #7
0
def get_bi_packs(params):
    """Show all BI packs"""

    bi_packs = get_cached_bi_packs()
    bi_packs.load_config()
    packs = [
        constructors.collection_item(
            domain_type="bi_pack",
            identifier=pack.id,
            title=pack.title,
        ) for pack in bi_packs.packs.values()
    ]

    collection_object = constructors.collection_object(
        domain_type="bi_pack",
        value=packs,
        links=[
            constructors.link_rel("self",
                                  constructors.collection_href("bi_pack"))
        ],
    )
    return constructors.serve_json(collection_object)
Example #8
0
def list_users(params):
    """Show all users"""
    user_collection = {
        'id':
        'user',
        'domainType':
        'user_config',
        'value': [
            constructors.collection_item(
                domain_type='user_config',
                obj={
                    'title': attrs['alias'],
                    'id': user_id
                },
            ) for user_id, attrs in userdb.load_users(False).items()
        ],
        'links': [
            constructors.link_rel('self',
                                  constructors.collection_href('user_config'))
        ],
    }
    return constructors.serve_json(user_collection)
Example #9
0
def list_folders(_params):
    """List folders"""
    folders = [
        constructors.collection_item(
            domain_type='folder_config',
            obj={
                'title': folder.title(),
                'id': folder.id()
            },
        ) for folder in watolib.Folder.root_folder().subfolders()
    ]

    collection_object = constructors.collection_object(
        domain_type='folder_config',
        value=folders,
        links=[
            constructors.link_rel(
                'self', constructors.collection_href('folder_config'))
        ],
    )

    return constructors.serve_json(collection_object)
Example #10
0
def list_time_periods(params):
    """Show all time periods"""
    time_periods = []
    for time_period_id, time_period_details in load_timeperiods().items():
        alias = time_period_details['alias']
        if not isinstance(alias, str):  # check for mypy
            continue
        time_periods.append(
            constructors.collection_item(
                domain_type='time_period',
                obj={
                    'title': alias,
                    'id': time_period_id
                },
            ))
    time_period_collection = {
        'id': "timeperiod",
        'domainType': 'time_period',
        'value': time_periods,
        'links': [constructors.link_rel('self', constructors.collection_href('time_period'))],
    }
    return constructors.serve_json(time_period_collection)
Example #11
0
def list_host_tag_groups(params):
    """Show all host tag groups"""
    tag_config = load_tag_config()
    tag_config += BuiltinTagConfig()
    tag_groups_collection = {
        "id":
        "host_tag",
        "domainType":
        "host_tag_group",
        "value": [
            constructors.collection_item(
                domain_type="host_tag_group",
                title=tag_group_obj.title,
                identifier=tag_group_obj.id,
            ) for tag_group_obj in tag_config.get_tag_groups()
        ],
        "links": [
            constructors.link_rel(
                "self", constructors.collection_href("host_tag_group"))
        ],
    }
    return constructors.serve_json(tag_groups_collection)
Example #12
0
def list_host_tag_groups(params):
    """Show all host tags"""
    tag_config = load_tag_config()
    tag_groups_collection = {
        'id':
        'host_tag',
        'domainType':
        'host_tag_group',
        'value': [
            constructors.collection_item(
                domain_type='host_tag_group',
                obj={
                    'title': tag_group_obj.title,
                    'id': tag_group_obj.id
                },
            ) for tag_group_obj in tag_config.get_tag_groups()
        ],
        'links': [
            constructors.link_rel(
                'self', constructors.collection_href('host_tag_group'))
        ]
    }
    return constructors.serve_json(tag_groups_collection)
Example #13
0
def _folders_collection(
    folders: List[CREFolder],
    show_hosts: bool,
):
    folders_ = []
    for folder in folders:
        members = {}
        if show_hosts:
            members["hosts"] = constructors.object_collection(
                name="hosts",
                domain_type="folder_config",
                entries=[
                    constructors.collection_item(
                        "host_config",
                        title=host_name,
                        identifier=host_name,
                    )
                    for host_name in folder.hosts()
                ],
                base="",
            )
        folders_.append(
            constructors.domain_object(
                domain_type="folder_config",
                identifier=folder_slug(folder),
                title=folder.title(),
                extensions={
                    "path": "/" + folder.path(),
                    "attributes": folder.attributes().copy(),
                },
                members=members,
            )
        )
    return constructors.collection_object(
        domain_type="folder_config",
        value=folders_,
    )