Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for overriding cloud-configurations per user #512

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changes for croud
Unreleased
==========

- Added support for overriding cloud-configurations per user.

1.11.1 - 2024/04/11
===================

Expand Down
14 changes: 14 additions & 0 deletions croud/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,10 @@
"--org-id", type=str, required=False,
help="Override the value for a single organization only.",
),
Argument(
"--user-id", type=str, required=False,
help="Override the value for a single user only.",
),
],
"resolver": cloud_configurations_set,
},
Expand All @@ -1676,6 +1680,11 @@
help="Optionally get the value for a certain organization. "
"Defaults to the global configuration value.",
),
Argument(
"--user-id", type=str, required=False,
help="Optionally get the value for a certain user. "
"Defaults to the global configuration value.",
),
],
"resolver": cloud_configurations_get,
},
Expand All @@ -1690,6 +1699,11 @@
help="Optionally get the values for a certain organization. "
"Defaults to the global configuration values.",
),
Argument(
"--user-id", type=str, required=False,
help="Optionally get the values for a certain user. "
"Defaults to the global configuration values.",
),
],
"resolver": cloud_configurations_list,
}
Expand Down
29 changes: 22 additions & 7 deletions croud/cloud_configurations/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,29 @@
from croud.printer import print_response


def _org_id_transform(field):
def _override_id_transform(field):
return field or ""


def cloud_configurations_set(args: Namespace) -> None:
body = {"value": args.value}
if args.org_id:
body["organization_id"] = args.org_id
elif args.user_id:
body["user_id"] = args.user_id

client = Client.from_args(args)
data, errors = client.put(f"/api/v2/configurations/{args.key}/", body=body)
print_response(
data=data,
errors=errors,
keys=["key", "value", "organization_id"],
keys=["key", "value", "organization_id", "user_id"],
success_message="Configuration updated.",
output_fmt=get_output_format(args),
transforms={"organization_id": _org_id_transform},
transforms={
"organization_id": _override_id_transform,
"user_id": _override_id_transform,
},
)


Expand All @@ -50,13 +55,18 @@ def cloud_configurations_get(args: Namespace) -> None:
params = {}
if args.org_id:
params["organization_id"] = args.org_id
elif args.user_id:
params["user_id"] = args.user_id
data, errors = client.get(f"/api/v2/configurations/{args.key}/", params=params)
print_response(
data=data,
errors=errors,
keys=["key", "value", "organization_id"],
keys=["key", "value", "organization_id", "user_id"],
output_fmt=get_output_format(args),
transforms={"organization_id": _org_id_transform},
transforms={
"organization_id": _override_id_transform,
"user_id": _override_id_transform,
},
)


Expand All @@ -65,11 +75,16 @@ def cloud_configurations_list(args: Namespace) -> None:
params = {}
if args.org_id:
params["organization_id"] = args.org_id
elif args.user_id:
params["user_id"] = args.user_id
data, errors = client.get("/api/v2/configurations/", params=params)
print_response(
data=data,
errors=errors,
keys=["key", "value", "organization_id"],
keys=["key", "value", "organization_id", "user_id"],
output_fmt=get_output_format(args),
transforms={"organization_id": _org_id_transform},
transforms={
"organization_id": _override_id_transform,
"user_id": _override_id_transform,
},
)
40 changes: 20 additions & 20 deletions docs/commands/cloud-configurations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ keys of CrateDB Cloud.
``cloud-configurations list``
=============================

Lists all configurations of CrateDB Cloud. Optionally it returns org specific values.
Lists all configurations of CrateDB Cloud. Optionally it returns org or user specific values.

.. argparse::
:module: croud.__main__
Expand All @@ -31,13 +31,13 @@ Example

sh$ croud cloud-configurations list \
--org-id f6c39580-5719-431d-a508-0cee4f9e8209 --sudo
+-------------------------------------------------+-------------+--------------------------------------+
| key | value | organization_id |
|-------------------------------------------------+-------------+--------------------------------------|
| CRATEDB_CLOUD_SETTING_ONE | 100 | |
| CRATEDB_CLOUD_SETTING_ORG_SPECIFIC | 1024 | f6c39580-5719-431d-a508-0cee4f9e8209 |
| CRATEDB_CLOUD_SETTING_THREE | 30 | |
+-------------------------------------------------+-------------+--------------------------------------+
+-------------------------------------------------+-------------+--------------------------------------+-----------+
| key | value | organization_id | user_id |
|-------------------------------------------------+-------------+--------------------------------------|-----------|
| CRATEDB_CLOUD_SETTING_ONE | 100 | | |
| CRATEDB_CLOUD_SETTING_ORG_SPECIFIC | 1024 | f6c39580-5719-431d-a508-0cee4f9e8209 | |
| CRATEDB_CLOUD_SETTING_THREE | 30 | | |
+-------------------------------------------------+-------------+--------------------------------------+-----------+

.. note::

Expand All @@ -47,7 +47,7 @@ Example
``cloud-configurations get``
============================

Get a single configuration value of CrateDB Cloud. Optionally it returns the org specific value.
Get a single configuration value of CrateDB Cloud. Optionally it returns the org or user specific value.

.. argparse::
:module: croud.__main__
Expand All @@ -62,11 +62,11 @@ Example
--key CRATEDB_CLOUD_SETTING_ORG_SPECIFIC \
--org-id f6c39580-5719-431d-a508-0cee4f9e8209 \
--sudo
+-------------------------------------------------+-------------+--------------------------------------+
| key | value | organization_id |
|-------------------------------------------------+-------------+--------------------------------------|
| CRATEDB_CLOUD_SETTING_ORG_SPECIFIC | 1024 | f6c39580-5719-431d-a508-0cee4f9e8209 |
+-------------------------------------------------+-------------+--------------------------------------+
+-------------------------------------------------+-------------+--------------------------------------+-----------+
| key | value | organization_id | user_id |
|-------------------------------------------------+-------------+--------------------------------------|-----------|
| CRATEDB_CLOUD_SETTING_ORG_SPECIFIC | 1024 | f6c39580-5719-431d-a508-0cee4f9e8209 | |
+-------------------------------------------------+-------------+--------------------------------------+-----------+

.. note::

Expand All @@ -76,7 +76,7 @@ Example
``cloud-configurations set``
============================

Set a configuration value of CrateDB Cloud either globally or for a single organization only.
Set a configuration value of CrateDB Cloud either globally or for a single organization or user only.

.. argparse::
:module: croud.__main__
Expand All @@ -92,11 +92,11 @@ Example
--value 2048 \
--org-id f6c39580-5719-431d-a508-0cee4f9e8209 \
--sudo
+-------------------------------------------------+-------------+--------------------------------------+
| key | value | organization_id |
|-------------------------------------------------+-------------+--------------------------------------|
| CRATEDB_CLOUD_SETTING_ORG_SPECIFIC | 2048 | f6c39580-5719-431d-a508-0cee4f9e8209 |
+-------------------------------------------------+-------------+--------------------------------------+
+-------------------------------------------------+-------------+--------------------------------------+-----------+
| key | value | organization_id | user_id |
|-------------------------------------------------+-------------+--------------------------------------|-----------|
| CRATEDB_CLOUD_SETTING_ORG_SPECIFIC | 2048 | f6c39580-5719-431d-a508-0cee4f9e8209 | |
+-------------------------------------------------+-------------+--------------------------------------+-----------+
==> Success: Configuration updated.

.. note::
Expand Down
60 changes: 57 additions & 3 deletions tests/commands/test_cloud_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@mock.patch.object(Client, "request", return_value=({}, None))
def test_cloud_configurations_get(mock_request):
def test_cloud_configurations_get_org_override(mock_request):
org_id = gen_uuid()
call_command(
"croud",
Expand All @@ -25,7 +25,7 @@ def test_cloud_configurations_get(mock_request):


@mock.patch.object(Client, "request", return_value=({}, None))
def test_cloud_configurations_set(mock_request):
def test_cloud_configurations_set_org_override(mock_request):
org_id = gen_uuid()
call_command(
"croud",
Expand All @@ -47,7 +47,7 @@ def test_cloud_configurations_set(mock_request):


@mock.patch.object(Client, "request", return_value=({}, None))
def test_cloud_configurations_list(mock_request):
def test_cloud_configurations_list_org_override(mock_request):
org_id = gen_uuid()
call_command("croud", "cloud-configurations", "list", "--org-id", org_id)
assert_rest(
Expand All @@ -56,3 +56,57 @@ def test_cloud_configurations_list(mock_request):
"/api/v2/configurations/",
params={"organization_id": org_id},
)


@mock.patch.object(Client, "request", return_value=({}, None))
def test_cloud_configurations_get_user_override(mock_request):
user_id = gen_uuid()
call_command(
"croud",
"cloud-configurations",
"get",
"--key",
"my_config_key",
"--user-id",
user_id,
)
assert_rest(
mock_request,
RequestMethod.GET,
"/api/v2/configurations/my_config_key/",
params={"user_id": user_id},
)


@mock.patch.object(Client, "request", return_value=({}, None))
def test_cloud_configurations_set_user_override(mock_request):
user_id = gen_uuid()
call_command(
"croud",
"cloud-configurations",
"set",
"--key",
"my_config_key",
"--value",
"new_config_value",
"--user-id",
user_id,
)
assert_rest(
mock_request,
RequestMethod.PUT,
"/api/v2/configurations/my_config_key/",
body={"value": "new_config_value", "user_id": user_id},
)


@mock.patch.object(Client, "request", return_value=({}, None))
def test_cloud_configurations_list_user_override(mock_request):
user_id = gen_uuid()
call_command("croud", "cloud-configurations", "list", "--user-id", user_id)
assert_rest(
mock_request,
RequestMethod.GET,
"/api/v2/configurations/",
params={"user_id": user_id},
)
Loading