-
Notifications
You must be signed in to change notification settings - Fork 22
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
GCS/GCP Subscription ID Management #921
Merged
derek-globus
merged 9 commits into
globus:main
from
derek-globus:gcs-subscription-id-sc-28587
Jan 4, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b72b85
globus gcs endpoint set-subscription-id
derek-globus d80b0df
Scriv
derek-globus e47278c
Update changelog.d/20231215_140223_derek_gcs_subscription_id_sc_28587.md
derek-globus 31e92fa
Update changelog.d/20231215_140223_derek_gcs_subscription_id_sc_28587.md
derek-globus e81ba1f
PR comments:
derek-globus c8bd24d
Wording Update
derek-globus a0454e4
Dedicated globus gcp set-subscription-id implementation
derek-globus 0c9a247
Update src/globus_cli/commands/gcp/set_subscription_id.py
derek-globus dc66ed8
Minor wording update
derek-globus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
changelog.d/20231215_140223_derek_gcs_subscription_id_sc_28587.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
### Enhancements | ||
|
||
* `[sc-28587] <https://app.shortcut.com/globus/story/28587>`_ | ||
Added a new command `globus gcs endpoint set-subscription-id` which allows | ||
subscription managers and endpoint admins to modify the subscription ID for a | ||
GCS endpoint. | ||
|
||
* `[sc-28587] <https://app.shortcut.com/globus/story/28587>`_ | ||
Added a new command `globus gcp set-subscription-id` which allows subscription | ||
managers and collection admins to modify the subscription ID for a GCP collection. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
import uuid | ||
|
||
import click | ||
|
||
from globus_cli.constants import EXPLICIT_NULL, ExplicitNullType | ||
from globus_cli.endpointish import Endpointish, EntityType | ||
from globus_cli.login_manager import LoginManager | ||
from globus_cli.parsing import command, endpoint_id_arg | ||
from globus_cli.termio import TextMode, display | ||
|
||
|
||
class GCPSubscriptionIdType(click.ParamType): | ||
def get_type_annotation(self, _: click.Parameter) -> type: | ||
return t.cast(type, uuid.UUID | ExplicitNullType) | ||
|
||
def convert( | ||
self, value: str, param: click.Parameter | None, ctx: click.Context | None | ||
) -> t.Any: | ||
if ctx and ctx.resilient_parsing: | ||
return None | ||
|
||
if value.lower() == "null": | ||
return EXPLICIT_NULL | ||
|
||
try: | ||
return uuid.UUID(value) | ||
except ValueError: | ||
msg = ( | ||
f"{value} is invalid. Expected either a UUID or the special value " | ||
'"null"' | ||
) | ||
self.fail(msg, param, ctx) | ||
|
||
|
||
@command("set-subscription-id", short_help="Update a GCP endpoint's subscription") | ||
@endpoint_id_arg | ||
@click.argument("SUBSCRIPTION_ID", type=GCPSubscriptionIdType()) | ||
@LoginManager.requires_login("transfer") | ||
def set_endpoint_subscription_id( | ||
login_manager: LoginManager, | ||
*, | ||
endpoint_id: uuid.UUID, | ||
subscription_id: uuid.UUID | ExplicitNullType, | ||
) -> None: | ||
""" | ||
Update a GCP endpoint's subscription. | ||
|
||
This operation does not require you to be an admin of the endpoint. It is useful in | ||
cases where you are a subscription manager applying a subscription to an endpoint | ||
administered by someone else. | ||
|
||
SUBSCRIPTION_ID must be one of: (1) A valid subscription ID (UUID) or (2) the value | ||
"null" (clears the endpoint's subscription). | ||
""" | ||
transfer_client = login_manager.get_transfer_client() | ||
epish = Endpointish(endpoint_id, transfer_client=transfer_client) | ||
epish.assert_entity_type(expect_types=EntityType.GCP_MAPPED) | ||
|
||
res = transfer_client.put( | ||
f"/endpoint/{endpoint_id}/subscription", | ||
data={"subscription_id": ExplicitNullType.nullify(subscription_id)}, | ||
) | ||
|
||
display(res, text_mode=TextMode.text_raw, response_key="message") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from globus_cli.parsing import group | ||
|
||
|
||
@group( | ||
"endpoint", | ||
lazy_subcommands={ | ||
"set-subscription-id": (".set_subscription_id", "set_subscription_id_command"), | ||
}, | ||
) | ||
def endpoint_command() -> None: | ||
"""Manage Globus Connect Server (GCS) endpoints""" |
72 changes: 72 additions & 0 deletions
72
src/globus_cli/commands/gcs/endpoint/set_subscription_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
import uuid | ||
|
||
import click | ||
|
||
from globus_cli.constants import EXPLICIT_NULL, ExplicitNullType | ||
from globus_cli.login_manager import LoginManager | ||
from globus_cli.parsing import command, endpoint_id_arg | ||
from globus_cli.termio import TextMode, display | ||
|
||
|
||
class GCSSubscriptionIdType(click.ParamType): | ||
def get_type_annotation(self, _: click.Parameter) -> type: | ||
return t.cast(type, uuid.UUID | t.Literal["DEFAULT"] | ExplicitNullType) | ||
|
||
def convert( | ||
self, value: str, param: click.Parameter | None, ctx: click.Context | None | ||
) -> t.Any: | ||
if ctx and ctx.resilient_parsing: | ||
return None | ||
|
||
if value.lower() == "null": | ||
return EXPLICIT_NULL | ||
elif value.lower() == "default": | ||
return "DEFAULT" | ||
try: | ||
return uuid.UUID(value) | ||
except ValueError: | ||
msg = ( | ||
f"{value} is invalid. Expected either a UUID or the special " | ||
'values "DEFAULT" or "null"' | ||
) | ||
self.fail(msg, param, ctx) | ||
|
||
|
||
@command("set-subscription-id", short_help="Update an endpoint's subscription") | ||
derek-globus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@endpoint_id_arg | ||
@click.argument("SUBSCRIPTION_ID", type=GCSSubscriptionIdType()) | ||
@LoginManager.requires_login("transfer") | ||
def set_subscription_id_command( | ||
login_manager: LoginManager, | ||
*, | ||
endpoint_id: uuid.UUID, | ||
subscription_id: uuid.UUID | t.Literal["DEFAULT"] | ExplicitNullType, | ||
) -> None: | ||
""" | ||
Update an endpoint's subscription. | ||
|
||
SUBSCRIPTION_ID must be one of: (1) A valid subscription ID (UUID), (2) the value | ||
"DEFAULT" (requires that you manage exactly one subscription & assigns the endpoint | ||
to that subscription), or (3) the value "null" (clears the endpoint's subscription). | ||
|
||
Setting a subscription requires that you are a subscription manager for the | ||
subscription being assigned. | ||
|
||
Removing a subscription requires that you are either (1) a subscription manager for | ||
the current assigned subscription group or (2) an admin of the endpoint. | ||
""" | ||
gcs_client = login_manager.get_gcs_client(endpoint_id=endpoint_id) | ||
|
||
subscription_id_val = None if subscription_id is EXPLICIT_NULL else subscription_id | ||
res = gcs_client.put( | ||
"/endpoint/subscription_id", | ||
data={ | ||
"DATA_TYPE": "endpoint_subscription#1.0.0", | ||
"subscription_id": subscription_id_val, | ||
}, | ||
) | ||
|
||
display(res, text_mode=TextMode.text_raw, response_key="message") |
Empty file.
Empty file.
derek-globus marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import uuid | ||
|
||
import pytest | ||
import responses | ||
from globus_sdk._testing import load_response_set | ||
|
||
|
||
@pytest.mark.parametrize("subscription_id", (str(uuid.uuid4()), "DEFAULT", "null")) | ||
def test_gcs_endpoint_set_subscription_id(subscription_id, run_line, add_gcs_login): | ||
meta = load_response_set("cli.collection_operations").metadata | ||
endpoint_id = meta["endpoint_id"] | ||
gcs_hostname = meta["gcs_hostname"] | ||
add_gcs_login(endpoint_id) | ||
|
||
responses.put( | ||
f"https://{gcs_hostname}/api/endpoint/subscription_id", | ||
json={ | ||
"DATA_TYPE": "result#1.0.0", | ||
"code": "success", | ||
"detail": "success", | ||
"has_next_page": False, | ||
"http_response_code": 200, | ||
"message": f"Updated Endpoint {endpoint_id}", | ||
}, | ||
) | ||
|
||
result = run_line( | ||
f"globus gcs endpoint set-subscription-id {endpoint_id} {subscription_id}" | ||
) | ||
|
||
assert f"Updated Endpoint {endpoint_id}" in result.stdout | ||
|
||
|
||
def test_gcs_endpoint_set_subscription_id__when_not_subscription_manager( | ||
kurtmckee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
run_line, add_gcs_login | ||
): | ||
meta = load_response_set("cli.collection_operations").metadata | ||
endpoint_id = meta["endpoint_id"] | ||
gcs_hostname = meta["gcs_hostname"] | ||
add_gcs_login(endpoint_id) | ||
|
||
error_message = ( | ||
"Unable to use DEFAULT subscription. Your identity does not manage any" | ||
"subscriptions" | ||
) | ||
responses.put( | ||
f"https://{gcs_hostname}/api/endpoint/subscription_id", | ||
status=400, | ||
json={ | ||
"DATA_TYPE": "result#1.0.0", | ||
"code": "bad_request", | ||
"detail": "bad_request", | ||
"has_next_page": False, | ||
"http_response_code": 400, | ||
"message": error_message, | ||
}, | ||
) | ||
|
||
result = run_line( | ||
f"globus gcs endpoint set-subscription-id {endpoint_id} DEFAULT", | ||
assert_exit_code=1, | ||
) | ||
|
||
assert error_message in result.stderr |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not super-closely related to this PR, but just because I think it's cool, I wanted to mention that under the latest
click-type-test
, you can writeconvert(...) -> uuid.UUID | ExplicitNullType
and omitget_type_annotation
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With #923 merged, you could now apply this change here (after a rebase).