Skip to content

Commit

Permalink
Merge pull request #1078 from rjmello/compute-client-function-methods
Browse files Browse the repository at this point in the history
Add ComputeClient methods to manage functions
  • Loading branch information
sirosen authored Oct 14, 2024
2 parents 350eea0 + a5263cc commit 38a6fbc
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Added
~~~~~

- Added ``ComputeClient.register_function()`` and ``ComputeClient.delete_function()``
methods. (:pr:`NUMBER`)

- Added ``ComputeFunctionDocument`` and ``ComputeFunctionMetadata`` data classes for
use with the ``ComputeClient.register_function()`` method. (:pr:`NUMBER`)
11 changes: 11 additions & 0 deletions docs/services/compute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ Globus Compute
.. listknownscopes:: globus_sdk.scopes.ComputeScopes
:base_name: ComputeClient.scopes

Helper Objects
--------------

.. autoclass:: ComputeFunctionDocument
:members:
:show-inheritance:

.. autoclass:: ComputeFunctionMetadata
:members:
:show-inheritance:

Client Errors
-------------

Expand Down
6 changes: 6 additions & 0 deletions src/globus_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def _force_eager_imports() -> None:
"services.compute": {
"ComputeClient",
"ComputeAPIError",
"ComputeFunctionDocument",
"ComputeFunctionMetadata",
},
"services.gcs": {
"CollectionDocument",
Expand Down Expand Up @@ -198,6 +200,8 @@ def _force_eager_imports() -> None:
from .services.auth import DependentScopeSpec
from .services.compute import ComputeClient
from .services.compute import ComputeAPIError
from .services.compute import ComputeFunctionDocument
from .services.compute import ComputeFunctionMetadata
from .services.gcs import CollectionDocument
from .services.gcs import GCSAPIError
from .services.gcs import GCSClient
Expand Down Expand Up @@ -316,6 +320,8 @@ def __getattr__(name: str) -> t.Any:
"CollectionPolicies",
"ComputeAPIError",
"ComputeClient",
"ComputeFunctionDocument",
"ComputeFunctionMetadata",
"ConfidentialAppAuthClient",
"ConnectorTable",
"DeleteData",
Expand Down
2 changes: 2 additions & 0 deletions src/globus_sdk/_generate_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def __getattr__(name: str) -> t.Any:
(
"ComputeClient",
"ComputeAPIError",
"ComputeFunctionDocument",
"ComputeFunctionMetadata",
),
),
(
Expand Down
13 changes: 13 additions & 0 deletions src/globus_sdk/_testing/data/compute/delete_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from globus_sdk._testing.models import RegisteredResponse, ResponseSet

from ._common import FUNCTION_ID

RESPONSES = ResponseSet(
metadata={"function_id": FUNCTION_ID},
default=RegisteredResponse(
service="compute",
path=f"/v2/functions/{FUNCTION_ID}",
method="DELETE",
json={"result": 302},
),
)
1 change: 1 addition & 0 deletions src/globus_sdk/_testing/data/compute/get_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
default=RegisteredResponse(
service="compute",
path=f"/v2/functions/{FUNCTION_ID}",
method="GET",
json=FUNCTION_DOC,
),
)
17 changes: 17 additions & 0 deletions src/globus_sdk/_testing/data/compute/register_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from globus_sdk._testing.models import RegisteredResponse, ResponseSet

from ._common import FUNCTION_CODE, FUNCTION_ID, FUNCTION_NAME

RESPONSES = ResponseSet(
metadata={
"function_id": FUNCTION_ID,
"function_name": FUNCTION_NAME,
"function_code": FUNCTION_CODE,
},
default=RegisteredResponse(
service="compute",
path="/v2/functions",
method="POST",
json={"function_uuid": FUNCTION_ID},
),
)
3 changes: 3 additions & 0 deletions src/globus_sdk/services/compute/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from .client import ComputeClient
from .data import ComputeFunctionDocument, ComputeFunctionMetadata
from .errors import ComputeAPIError

__all__ = (
"ComputeAPIError",
"ComputeClient",
"ComputeFunctionDocument",
"ComputeFunctionMetadata",
)
35 changes: 35 additions & 0 deletions src/globus_sdk/services/compute/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

import logging
import typing as t

from globus_sdk import GlobusHTTPResponse, client
from globus_sdk._types import UUIDLike
from globus_sdk.scopes import ComputeScopes, Scope

from .data import ComputeFunctionDocument
from .errors import ComputeAPIError

log = logging.getLogger(__name__)
Expand All @@ -23,6 +25,24 @@ class ComputeClient(client.BaseClient):
scopes = ComputeScopes
default_scope_requirements = [Scope(ComputeScopes.all)]

def register_function(
self,
function_data: ComputeFunctionDocument | dict[str, t.Any],
) -> GlobusHTTPResponse:
"""Register a new function.
:param function_data: A function registration document.
.. tab-set::
.. tab-item:: API Info
.. extdoclink:: Register Function
:service: compute
:ref: Functions/operation/register_function_v2_functions_post
""" # noqa: E501
return self.post("/v2/functions", data=function_data)

def get_function(self, function_id: UUIDLike) -> GlobusHTTPResponse:
"""Get information about a registered function.
Expand All @@ -37,3 +57,18 @@ def get_function(self, function_id: UUIDLike) -> GlobusHTTPResponse:
:ref: Functions/operation/get_function_v2_functions__function_uuid__get
""" # noqa: E501
return self.get(f"/v2/functions/{function_id}")

def delete_function(self, function_id: UUIDLike) -> GlobusHTTPResponse:
"""Delete a registered function.
:param function_id: The ID of the function.
.. tab-set::
.. tab-item:: API Info
.. extdoclink:: Delete Function
:service: compute
:ref: Functions/operation/delete_function_v2_functions__function_uuid__delete
""" # noqa: E501
return self.delete(f"/v2/functions/{function_id}")
53 changes: 53 additions & 0 deletions src/globus_sdk/services/compute/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from __future__ import annotations

from globus_sdk import utils
from globus_sdk._types import UUIDLike
from globus_sdk.utils import MISSING, MissingType


class ComputeFunctionMetadata(utils.PayloadWrapper):
"""A wrapper for function metadata.
:param python_version: The Python version used to serialize the function.
:param sdk_version: The Globus Compute SDK version used to serialize the function.
"""

def __init__(
self,
*,
python_version: str | MissingType = MISSING,
sdk_version: str | MissingType = MISSING,
):
super().__init__()
self["python_version"] = python_version
self["sdk_version"] = sdk_version


class ComputeFunctionDocument(utils.PayloadWrapper):
"""A function registration document.
:param function_name: The name of the function.
:param function_code: The serialized function source code.
:param description: The description of the function.
:param metadata: The metadata of the function.
:param group: Restrict function access to members of this Globus group.
:param public: Indicates whether the function is public.
"""

def __init__(
self,
*,
function_name: str,
function_code: str,
description: str | MissingType = MISSING,
metadata: ComputeFunctionMetadata | MissingType = MISSING,
group: UUIDLike | MissingType = MISSING,
public: bool = False,
):
super().__init__()
self["function_name"] = function_name
self["function_code"] = function_code
self["description"] = description
self["metadata"] = metadata
self["group"] = group
self["public"] = public
9 changes: 9 additions & 0 deletions tests/functional/services/compute/test_delete_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import globus_sdk
from globus_sdk._testing import load_response


def test_delete_function(compute_client: globus_sdk.ComputeClient):
meta = load_response(compute_client.delete_function).metadata
res = compute_client.delete_function(function_id=meta["function_id"])
assert res.http_status == 200
assert res.data == {"result": 302}
13 changes: 13 additions & 0 deletions tests/functional/services/compute/test_register_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import globus_sdk
from globus_sdk._testing import load_response
from globus_sdk.services.compute import ComputeFunctionDocument


def test_register_function(compute_client: globus_sdk.ComputeClient):
meta = load_response(compute_client.register_function).metadata
function_doc = ComputeFunctionDocument(
function_name=meta["function_name"], function_code=meta["function_code"]
)
res = compute_client.register_function(function_doc)
assert res.http_status == 200
assert res.data["function_uuid"] == meta["function_id"]

0 comments on commit 38a6fbc

Please sign in to comment.