summaryrefslogtreecommitdiffstats
path: root/ansible_collections/sensu/sensu_go/plugins/module_utils/bonsai.py
blob: b7962cf85fcee235f87e1550af85bc0a4cd8158d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, XLAB Steampunk <steampunk@xlab.si>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

from . import errors, http


def get(path):
    url = "https://bonsai.sensu.io/api/v1/assets/{0}".format(path)
    resp = http.request("GET", url)

    if resp.status != 200:
        raise errors.BonsaiError(
            "Server returned status {0}".format(resp.status),
        )
    if resp.json is None:
        raise errors.BonsaiError("Server returned invalid JSON document")

    return resp.json


def get_available_asset_versions(namespace, name):
    asset_data = get("{0}/{1}".format(namespace, name))
    try:
        return set(v["version"] for v in asset_data["versions"])
    except (TypeError, KeyError):
        raise errors.BonsaiError(
            "Cannot extract versions from {0}".format(asset_data),
        )


def get_asset_version_builds(namespace, name, version):
    asset = get("{0}/{1}/{2}/release_asset_builds".format(
        namespace, name, version,
    ))
    if "spec" not in asset or "builds" not in asset["spec"]:
        raise errors.BonsaiError("Invalid build spec: {0}".format(asset))
    return asset


def get_asset_parameters(name, version):
    try:
        namespace, asset_name = name.split("/")
    except ValueError:
        raise errors.BonsaiError(
            "Bonsai asset names should be formatted as <namespace>/<name>.",
        )

    available_versions = get_available_asset_versions(namespace, asset_name)
    if version not in available_versions:
        raise errors.BonsaiError(
            "Version {0} is not available. Choose from: {1}.".format(
                version, ", ".join(available_versions),
            ),
        )

    asset_builds = get_asset_version_builds(namespace, asset_name, version)

    return dict(
        labels=asset_builds.get("metadata", {}).get("labels"),
        annotations=asset_builds.get("metadata", {}).get("annotations"),
        builds=asset_builds["spec"]["builds"],
    )