Ejemplo n.º 1
0
def config(fmt: str = "html") -> Union[Response, RespTuple]:
    if fmt == "json":
        if http_request.method == "GET":
            return (
                json.dumps(state.get_raw_configs()),
                200,
                {
                    "Content-Type": "application/json"
                },
            )
        else:
            assert http_request.method == "POST"
            state.set_configs(
                json.loads(http_request.data),
                user=http_request.headers.get("x-forwarded-email"),
            )
            return (
                json.dumps(state.get_raw_configs()),
                200,
                {
                    "Content-Type": "application/json"
                },
            )

    else:
        return application.send_static_file("config.html")
Ejemplo n.º 2
0
def config(fmt='html'):
    if fmt == 'json':
        if request.method == 'GET':
            return (json.dumps(state.get_raw_configs()), 200, {'Content-Type': 'application/json'})
        elif request.method == 'POST':
            state.set_configs(json.loads(request.data), user=request.headers.get('x-forwarded-email'))
            return (json.dumps(state.get_raw_configs()), 200, {'Content-Type': 'application/json'})
    else:
        return application.send_static_file('config.html')
Ejemplo n.º 3
0
def get(*, key: str, format: str) -> None:
    "Get a single key."

    try:
        rv = state.get_raw_configs()[key]
    except KeyError:
        raise click.ClickException(f"Key {key!r} not found.")
    click.echo(FORMATS[format]({key: rv}))
Ejemplo n.º 4
0
def delete(*, key: str) -> None:
    "Delete a single key."

    try:
        rv = state.get_raw_configs()[key]
    except KeyError:
        raise click.ClickException(f"Key {key!r} not found.")

    click.echo(human_fmt({key: rv}))
    click.confirm(f"\nAre you sure you want to delete this?", abort=True)

    state.delete_config(key, user=get_user())
Ejemplo n.º 5
0
def config(fmt: str = "html") -> Union[Response, RespTuple]:
    if fmt == "json":
        if http_request.method == "GET":
            return (
                json.dumps(state.get_raw_configs()),
                200,
                {"Content-Type": "application/json"},
            )
        else:
            assert http_request.method == "POST"
            try:
                state.set_configs(
                    json.loads(http_request.data),
                    user=http_request.headers.get("x-forwarded-email"),
                )
                return (
                    json.dumps(state.get_raw_configs()),
                    200,
                    {"Content-Type": "application/json"},
                )
            except MismatchedTypeException as exc:
                return (
                    json.dumps(
                        {
                            "error": {
                                "type": "client_error",
                                "message": "Existing value and New value have different types. Use option force to override check",
                                "key": str(exc.key),
                                "original value type": str(exc.original_type),
                                "new_value_type": str(exc.new_type),
                            }
                        },
                        indent=4,
                    ),
                    400,
                    {"Content-Type": "application/json"},
                )
    else:
        return application.send_static_file("config.html")
Ejemplo n.º 6
0
def get_all(*, format: str) -> None:
    "Dump all runtime configuration."

    rv = state.get_raw_configs()
    click.echo(FORMATS[format](rv))
Ejemplo n.º 7
0
def configs() -> Response:
    if request.method == "POST":
        data = json.loads(request.data)
        try:
            key, value, desc = data["key"], data["value"], data["description"]

            assert isinstance(key, str), "Invalid key"
            assert isinstance(value, str), "Invalid value"
            assert key != "", "Key cannot be empty string"

        except (KeyError, AssertionError) as exc:
            return Response(
                json.dumps({"error": f"Invalid config: {str(exc)}"}),
                400,
                {"Content-Type": "application/json"},
            )

        existing_config = state.get_uncached_config(key)
        if existing_config is not None:
            return Response(
                json.dumps({"error": f"Config with key {key} exists"}),
                400,
                {"Content-Type": "application/json"},
            )

        user = request.headers.get(USER_HEADER_KEY)

        state.set_config(key, value, user=user)
        state.set_config_description(key, desc, user=user)

        evaluated_value = state.get_uncached_config(key)
        assert evaluated_value is not None
        evaluated_type = get_config_type_from_value(evaluated_value)

        config = {
            "key": key,
            "value": str(evaluated_value),
            "description": state.get_config_description(key),
            "type": evaluated_type,
        }

        notification_client.notify(
            RuntimeConfigAction.ADDED,
            {
                "option": key,
                "old": None,
                "new": evaluated_value
            },
            user,
        )

        return Response(json.dumps(config), 200,
                        {"Content-Type": "application/json"})

    else:
        descriptions = state.get_all_config_descriptions()

        raw_configs: Sequence[Tuple[str,
                                    Any]] = state.get_raw_configs().items()

        sorted_configs = sorted(raw_configs, key=lambda c: c[0])

        config_data = [{
            "key":
            k,
            "value":
            str(v) if v is not None else None,
            "description":
            str(descriptions.get(k)) if k in descriptions else None,
            "type":
            get_config_type_from_value(v),
        } for (k, v) in sorted_configs]

        return Response(
            json.dumps(config_data),
            200,
            {"Content-Type": "application/json"},
        )