Skip to content

Commit 9789ab1

Browse files
authored
Add support for Search's Index Update API (#1310)
2 parents 1158090 + 64fffef commit 9789ab1

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Added
2+
-----
3+
4+
- Added ``SearchClient.update_index`` as a method for modifying index names and
5+
descriptions. (:pr:`NUMBER`)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import uuid
2+
3+
from globus_sdk._testing.models import RegisteredResponse, ResponseSet
4+
5+
INDEX_ID = str(uuid.uuid4())
6+
7+
_default_display_name = "Awesome Index of Awesomeness"
8+
_default_description = "An index so awesome that it simply cannot be described"
9+
10+
RESPONSES = ResponseSet(
11+
default=RegisteredResponse(
12+
service="search",
13+
method="PATCH",
14+
path=f"/v1/index/{INDEX_ID}",
15+
json={
16+
"@datatype": "GSearchIndex",
17+
"@version": "2017-09-01",
18+
"creation_date": "2021-04-05 15:05:18",
19+
"display_name": _default_display_name,
20+
"description": _default_description,
21+
"id": INDEX_ID,
22+
"is_trial": True,
23+
"subscription_id": None,
24+
"max_size_in_mb": 1,
25+
"num_entries": 0,
26+
"num_subjects": 0,
27+
"size_in_mb": 0,
28+
"status": "open",
29+
},
30+
metadata={"index_id": INDEX_ID, "display_name": _default_display_name},
31+
),
32+
forbidden=RegisteredResponse(
33+
service="search",
34+
method="PATCH",
35+
path=f"/v1/index/{INDEX_ID}",
36+
status=403,
37+
json={
38+
"@datatype": "GError",
39+
"@version": "2017-09-01",
40+
"status": 403,
41+
"code": "Forbidden.Generic",
42+
"message": "index_update request denied by service",
43+
"request_id": "0e73b6a61e53468684f86c7993336a72",
44+
"error_data": {
45+
"cause": (
46+
"You do not have the proper roles "
47+
"to perform the index_update operation."
48+
),
49+
"recommended_resolution": (
50+
"Ensure you are making a call authenticated with "
51+
"a valid Search token and that you have been granted "
52+
"the required roles for this operation"
53+
),
54+
},
55+
},
56+
metadata={"index_id": INDEX_ID},
57+
),
58+
)

src/globus_sdk/services/search/client.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from globus_sdk import client, paging, response, utils
88
from globus_sdk.exc.warnings import warn_deprecated
99
from globus_sdk.scopes import Scope, SearchScopes
10+
from globus_sdk.utils import MISSING, MissingType
1011

1112
from .data import SearchQuery, SearchScrollQuery
1213
from .errors import SearchAPIError
@@ -81,6 +82,52 @@ def create_index(
8182
"/v1/index", data={"display_name": display_name, "description": description}
8283
)
8384

85+
def update_index(
86+
self,
87+
index_id: uuid.UUID | str,
88+
*,
89+
display_name: str | MissingType = MISSING,
90+
description: str | MissingType = MISSING,
91+
) -> response.GlobusHTTPResponse:
92+
"""
93+
Update index metadata.
94+
95+
:param index_id: the ID of the index
96+
:param display_name: the name of the index
97+
:param description: a description of the index
98+
99+
.. tab-set::
100+
101+
.. tab-item:: Example Usage
102+
103+
.. code-block:: python
104+
105+
sc = globus_sdk.SearchClient(...)
106+
MY_INDEX_ID = ...
107+
r = sc.update_index(
108+
MY_INDEX_ID,
109+
display_name="My Awesome Index",
110+
description="Very awesome searchable data",
111+
)
112+
print(f"index ID: {r['id']}")
113+
114+
.. tab-item:: Example Response Data
115+
116+
.. expandtestfixture:: search.create_index
117+
118+
.. tab-item:: API Info
119+
120+
``PATCH /v1/index/<index_id>``
121+
122+
.. extdoclink:: Index Update
123+
:ref: search/reference/index_update/
124+
"""
125+
log.debug(f"SearchClient.update_index({index_id!r}, ...)")
126+
return self.patch(
127+
f"/v1/index/{index_id}",
128+
data={"display_name": display_name, "description": description},
129+
)
130+
84131
def delete_index(self, index_id: uuid.UUID | str) -> response.GlobusHTTPResponse:
85132
"""
86133
Mark an index for deletion.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
3+
import globus_sdk
4+
from globus_sdk._testing import load_response
5+
6+
7+
def test_update_index(client):
8+
meta = load_response(client.update_index).metadata
9+
10+
res = client.update_index(meta["index_id"], display_name="foo")
11+
assert res.http_status == 200
12+
assert res["display_name"] == meta["display_name"]
13+
14+
15+
def test_update_index_forbidden_error(client):
16+
meta = load_response(client.update_index, case="forbidden").metadata
17+
18+
with pytest.raises(globus_sdk.SearchAPIError) as excinfo:
19+
client.update_index(meta["index_id"])
20+
21+
err = excinfo.value
22+
23+
assert err.http_status == 403
24+
assert err.code == "Forbidden.Generic"

0 commit comments

Comments
 (0)