Example #1
0
def list_rules(param):
    """List rules"""
    all_sets = watolib.AllRulesets()
    all_sets.load()
    ruleset_name = param["ruleset_name"]

    try:
        ruleset = all_sets.get(ruleset_name.replace("-", ":"))
    except KeyError:
        return problem(
            status=400,
            title="Unknown ruleset.",
            detail=f"The ruleset of name {ruleset_name!r} is not known.",
        )

    result = []
    for folder, index, rule in ruleset.get_rules():
        result.append(_serialize_rule(folder, index, rule))

    return serve_json(
        constructors.collection_object(
            domain_type="rule",
            value=result,
            extensions={
                "found_rules": len(result),
            },
        )
    )
Example #2
0
def _list_services(param):
    live = sites.live()

    columns = verify_columns(Services, param['columns'])
    q = Query(columns)

    host_name = param.get('host_name')
    if host_name is not None:
        q = q.filter(Services.host_name == host_name)

    query_expr = param.get('query')
    if query_expr:
        q = q.filter(query_expr)

    result = q.iterate(live)

    return constructors.serve_json(
        constructors.collection_object(
            domain_type='service',
            value=[
                constructors.domain_object(
                    domain_type='service',
                    title=f"{entry['description']} on {entry['host_name']}",
                    identifier=entry['description'],
                    editable=False,
                    deletable=False,
                    extensions=entry,
                ) for entry in result
            ],
        ))
Example #3
0
def list_hosts(param):
    """Show hosts of specific condition"""
    live = sites.live()
    sites_to_query = param['sites']
    if sites_to_query:
        live.only_sites = sites_to_query

    columns = verify_columns(Hosts, param['columns'])
    q = Query(columns)

    # TODO: add sites parameter
    query_expr = param.get('query')
    if query_expr:
        q = q.filter(query_expr)

    result = q.iterate(live)

    return constructors.serve_json(
        constructors.collection_object(
            domain_type='host',
            value=[
                constructors.domain_object(
                    domain_type='host',
                    title=f"{entry['name']}",
                    identifier=entry['name'],
                    editable=False,
                    deletable=False,
                    extensions=entry,
                ) for entry in result
            ],
        ))
Example #4
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,
                        "id": host
                    }) for host 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_,
    )
Example #5
0
def list_hosts(param):
    """Show hosts of specific condition"""
    live = sites.live()
    sites_to_query = param["sites"]
    if sites_to_query:
        live.only_sites = sites_to_query

    q = Query(param["columns"])

    query_expr = param.get("query")
    if query_expr:
        q = q.filter(query_expr)

    result = q.iterate(live)

    return constructors.serve_json(
        constructors.collection_object(
            domain_type="host",
            value=[
                constructors.domain_object(
                    domain_type="host",
                    title=f"{entry['name']}",
                    identifier=entry["name"],
                    editable=False,
                    deletable=False,
                    extensions=entry,
                )
                for entry in result
            ],
        )
    )
Example #6
0
def list_folders(_params):
    return constructors.serve_json({
        'id': 'folders',
        'value': [
            constructors.collection_object('folders', 'folder', folder)
            for folder in watolib.Folder.root_folder().subfolders()
        ],
        'links': [constructors.link_rel('self', '/collections/folder')]
    })
Example #7
0
def _serialize_downtimes(downtimes):
    entries = []
    for downtime in downtimes:
        entries.append(_serialize_single_downtime(downtime))

    return constructors.collection_object(
        "downtime",
        value=entries,
    )
Example #8
0
def list_users(params):
    """Show all users"""
    users = []
    for user_id, attrs in userdb.load_users(False).items():
        user_attributes = _internal_to_api_format(attrs)
        users.append(
            serialize_user(user_id, complement_customer(user_attributes)))

    return constructors.serve_json(
        constructors.collection_object(domain_type="user_config", value=users))
Example #9
0
def list_groups(params):
    """List service-groups"""
    return constructors.serve_json({
        'id': 'folders',
        'value': [
            constructors.collection_object('service_group', 'service_group', group)
            for group in load_service_group_information().values()
        ],
        'links': [constructors.link_rel('self', '/collections/service_group')]
    })
Example #10
0
def list_group(params):
    return constructors.serve_json({
        'id':
        'folders',
        'value': [
            constructors.collection_object('contact_group', 'contact_group',
                                           group)
            for group in load_contact_group_information().values()
        ],
        'links': [constructors.link_rel('self', '/collections/contact_group')]
    })
Example #11
0
def _folders_collection(folders):
    collection_object = constructors.collection_object(
        domain_type='folder_config',
        value=[
            constructors.collection_item(
                domain_type='folder_config',
                obj={
                    'title': folder.title(),
                    'id': folder.id()
                },
            ) for folder in folders
        ],
        links=[constructors.link_rel('self', constructors.collection_href('folder_config'))],
    )
    return collection_object
def list_activations(params):
    """Show all currently running activations"""
    manager = watolib.ActivateChangesManager()
    activations = []
    for activation_id, change in manager.activations():
        activations.append(
            constructors.collection_item(
                domain_type='activation_run',
                obj={
                    'id': activation_id,
                    'title': change['_comment'],
                },
            ))

    return constructors.collection_object(domain_type='activation_run', value=activations)
Example #13
0
def list_activations(params):
    """Show all currently running activations"""
    manager = watolib.ActivateChangesManager()
    activations = []
    for activation_id, change in manager.activations():
        activations.append(
            constructors.collection_item(
                domain_type="activation_run",
                identifier=activation_id,
                title=change["_comment"],
            ))

    return constructors.serve_json(
        constructors.collection_object(domain_type="activation_run",
                                       value=activations))
Example #14
0
def serialize_group_list(
    domain_type: GroupName,
    collection: Sequence[Dict[str, Any]],
) -> constructors.CollectionObject:
    return constructors.collection_object(
        domain_type=domain_type,
        value=[
            constructors.collection_item(
                domain_type=domain_type,
                title=group["alias"],
                identifier=group["id"],
            )
            for group in collection
        ],
        links=[constructors.link_rel("self", constructors.collection_href(domain_type))],
    )
Example #15
0
def serialize_group_list(
    domain_type: GroupName,
    collection: Sequence[Dict[str, Any]],
) -> constructors.CollectionObject:
    return constructors.collection_object(
        domain_type=domain_type,
        value=[
            constructors.collection_item(
                domain_type=domain_type,
                obj={
                    'title': group['alias'],
                    'id': group['id'],
                },
            ) for group in collection
        ],
        links=[constructors.link_rel('self', constructors.collection_href(domain_type))],
    )
Example #16
0
def _folders_collection(
    folders: List[CREFolder],
    show_hosts: bool,
):
    folders_ = []
    for folder in folders:
        if show_hosts:
            folders_.append(
                constructors.domain_object(
                    domain_type='folder_config',
                    identifier=folder.id(),
                    title=folder.title(),
                    extensions={
                        'attributes': folder.attributes().copy(),
                    },
                    members={
                        'hosts':
                        constructors.object_collection(
                            name='hosts',
                            domain_type='folder_config',
                            entries=[
                                constructors.collection_item(
                                    "host_config", {
                                        "title": host,
                                        "id": host
                                    }) for host in folder.hosts()
                            ],
                            base="",
                        )
                    },
                ))
        folders_.append(
            constructors.collection_item(
                domain_type='folder_config',
                obj={
                    'title': folder.title(),
                    'id': folder.id()
                },
            ))
    return constructors.collection_object(
        domain_type='folder_config',
        value=folders_,
    )
Example #17
0
def list_rules(param):
    """List rules"""
    all_sets = watolib.AllRulesets()
    all_sets.load()
    ruleset = all_sets.get(param["ruleset_name"].replace("-", ":"))

    result = []
    for folder, index, rule in ruleset.get_rules():
        result.append(_serialize_rule(folder, index, rule))

    return serve_json(
        constructors.collection_object(
            domain_type="rule",
            value=result,
            extensions={
                "found_rules": len(result),
            },
        )
    )
Example #18
0
def _list_services(param):
    live = sites.live()

    q = Query(param["columns"])

    host_name = param.get("host_name")
    if host_name is not None:
        q = q.filter(Services.host_name == host_name)

    query_expr = param.get("query")
    if query_expr:
        q = q.filter(query_expr)

    result = q.iterate(live)

    return constructors.serve_json(
        constructors.collection_object(
            domain_type="service",
            value=[
                constructors.domain_object(
                    domain_type="service",
                    title=f"{entry['description']} on {entry['host_name']}",
                    identifier=f"{entry['host_name']}:{entry['description']}",
                    editable=False,
                    deletable=False,
                    extensions=entry,
                    self_link=constructors.link_rel(
                        rel="cmk/show",
                        href=constructors.object_action_href(
                            "host",
                            entry["host_name"],
                            "show_service",
                            query_params=[("service_description", entry["description"])],
                        ),
                        method="get",
                        title=f"Show the service {entry['description']}",
                    ),
                )
                for entry in result
            ],
        )
    )
Example #19
0
def get_bi_packs(params):
    """Show all BI packs"""
    user.need_permission("wato.bi_rules")
    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 #20
0
def _list_services(param):
    live = sites.live()

    q = Query(param['columns'])

    host_name = param.get('host_name')
    if host_name is not None:
        q = q.filter(Services.host_name == host_name)

    query_expr = param.get('query')
    if query_expr:
        q = q.filter(query_expr)

    result = q.iterate(live)

    return constructors.serve_json(
        constructors.collection_object(
            domain_type='service',
            value=[
                constructors.domain_object(
                    domain_type='service',
                    title=f"{entry['description']} on {entry['host_name']}",
                    identifier=f"{entry['host_name']}:{entry['description']}",
                    editable=False,
                    deletable=False,
                    extensions=entry,
                    self_link=constructors.link_rel(
                        rel='cmk/show',
                        href=constructors.object_action_href(
                            'host',
                            entry['host_name'],
                            'show_service',
                            query_params=[('service_description',
                                           entry['description'])],
                        ),
                        method='get',
                        title=f"Show the service {entry['description']}",
                    ),
                ) for entry in result
            ],
        ))
Example #21
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 #22
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 #23
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 #24
0
        options = dict(params)
        if "folder" in options:
            del options["folder"]
        return options

    if search_options := _get_search_options(param):
        all_sets = SearchedRulesets(all_sets, search_options)

    ruleset_collection: List[DomainObject] = []
    for ruleset in all_sets.get_rulesets().values():
        ruleset_collection.append(_serialize_ruleset(ruleset))

    # We don't do grouping like in the GUI. This would not add any value here.
    return serve_json(
        constructors.collection_object(
            domain_type="ruleset",
            value=ruleset_collection,
        ))


@Endpoint(
    constructors.object_href(domain_type="ruleset", obj_id="{ruleset_name}"),
    "cmk/show",
    method="get",
    etag="output",
    path_params=[RULESET_NAME],
    response_schema=RulesetObject,
    permissions_required=PERMISSIONS,
)
def show_ruleset(param):
    """Show a ruleset"""
    ruleset_name = param["ruleset_name"]