Skip to content

Commit ce18ffa

Browse files
committed
Expose admin org functions, update funcs
1 parent ba768fe commit ce18ffa

File tree

6 files changed

+594
-3
lines changed

6 files changed

+594
-3
lines changed

pinecone/admin/admin.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def __init__(
116116
# Lazily initialize resources
117117
self._project = None
118118
self._api_key = None
119+
self._organization = None
119120

120121
@property
121122
def project(self):
@@ -231,3 +232,62 @@ def api_key(self):
231232
def api_keys(self):
232233
"""Alias for :func:`api_key`"""
233234
return self.api_key
235+
236+
@property
237+
def organization(self):
238+
"""A namespace for organization-related operations
239+
240+
Alias for :func:`organizations`.
241+
242+
To learn about all organization-related operations, see :func:`pinecone.admin.resources.OrganizationResource`.
243+
244+
Examples
245+
--------
246+
247+
.. code-block:: python
248+
:caption: Listing all organizations
249+
250+
from pinecone import Admin
251+
252+
# Using environment variables to pass PINECONE_CLIENT_ID and PINECONE_CLIENT_SECRET
253+
admin = Admin()
254+
255+
# List all organizations
256+
organizations_response = admin.organization.list()
257+
for org in organizations_response.data:
258+
print(org.id)
259+
print(org.name)
260+
261+
.. code-block:: python
262+
:caption: Fetching an organization
263+
264+
from pinecone import Admin
265+
266+
admin = Admin()
267+
organization = admin.organization.get(organization_id="my-organization-id")
268+
print(organization.name)
269+
print(organization.plan)
270+
271+
.. code-block:: python
272+
:caption: Updating an organization
273+
274+
from pinecone import Admin
275+
276+
admin = Admin()
277+
organization = admin.organization.update(
278+
organization_id="my-organization-id",
279+
name="updated-organization-name"
280+
)
281+
print(organization.name)
282+
283+
"""
284+
if self._organization is None:
285+
from pinecone.admin.resources import OrganizationResource
286+
287+
self._organization = OrganizationResource(self._child_api_client)
288+
return self._organization
289+
290+
@property
291+
def organizations(self):
292+
"""Alias for :func:`organization`"""
293+
return self.organization
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .project import ProjectResource
22
from .api_key import ApiKeyResource
3+
from .organization import OrganizationResource
34

4-
__all__ = ["ProjectResource", "ApiKeyResource"]
5+
__all__ = ["ProjectResource", "ApiKeyResource", "OrganizationResource"]

pinecone/admin/resources/api_key.py

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
from pinecone.openapi_support import ApiClient
33
from pinecone.core.openapi.admin.apis import APIKeysApi
44
from pinecone.utils import require_kwargs, parse_non_empty_args
5-
from pinecone.core.openapi.admin.models import CreateAPIKeyRequest
5+
from pinecone.core.openapi.admin.models import CreateAPIKeyRequest, UpdateAPIKeyRequest
66

77

88
class ApiKeyResource:
99
"""
10-
This class is used to create, delete, list, and fetch API keys.
10+
This class is used to create, delete, list, fetch, and update API keys.
1111
1212
.. note::
1313
The class should not be instantiated directly. Instead, access this classes
@@ -208,3 +208,80 @@ def create(
208208
return self._api_keys_api.create_api_key(
209209
project_id=project_id, create_api_key_request=create_api_key_request
210210
)
211+
212+
@require_kwargs
213+
def update(
214+
self, api_key_id: str, name: Optional[str] = None, roles: Optional[List[str]] = None
215+
):
216+
"""
217+
Update an API key.
218+
219+
:param api_key_id: The id of the API key to update.
220+
:type api_key_id: str
221+
:param name: A new name for the API key. The name must be 1-80 characters long.
222+
If omitted, the name will not be updated.
223+
:type name: Optional[str]
224+
:param roles: A new set of roles for the API key. Available roles include:
225+
``ProjectEditor``, ``ProjectViewer``, ``ControlPlaneEditor``,
226+
``ControlPlaneViewer``, ``DataPlaneEditor``, ``DataPlaneViewer``.
227+
Existing roles will be removed if not included. If this field is omitted,
228+
the roles will not be updated.
229+
:type roles: Optional[List[str]]
230+
:return: The updated API key.
231+
:rtype: APIKey
232+
233+
Examples
234+
--------
235+
236+
.. code-block:: python
237+
:caption: Update an API key's name
238+
:emphasize-lines: 7-10
239+
240+
from pinecone import Admin
241+
242+
# Credentials read from PINECONE_CLIENT_ID and
243+
# PINECONE_CLIENT_SECRET environment variables
244+
admin = Admin()
245+
246+
api_key = admin.api_key.update(
247+
api_key_id='my-api-key-id',
248+
name='updated-api-key-name'
249+
)
250+
print(api_key.name)
251+
252+
.. code-block:: python
253+
:caption: Update an API key's roles
254+
:emphasize-lines: 7-10
255+
256+
from pinecone import Admin
257+
258+
admin = Admin()
259+
260+
api_key = admin.api_key.update(
261+
api_key_id='my-api-key-id',
262+
roles=['ProjectViewer']
263+
)
264+
print(api_key.roles)
265+
266+
.. code-block:: python
267+
:caption: Update both name and roles
268+
:emphasize-lines: 7-12
269+
270+
from pinecone import Admin
271+
272+
admin = Admin()
273+
274+
api_key = admin.api_key.update(
275+
api_key_id='my-api-key-id',
276+
name='updated-name',
277+
roles=['ProjectEditor', 'DataPlaneEditor']
278+
)
279+
print(api_key.name)
280+
print(api_key.roles)
281+
282+
"""
283+
args = [("name", name), ("roles", roles)]
284+
update_request = UpdateAPIKeyRequest(**parse_non_empty_args(args))
285+
return self._api_keys_api.update_api_key(
286+
api_key_id=api_key_id, update_api_key_request=update_request
287+
)
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
from typing import Optional
2+
from pinecone.openapi_support import ApiClient
3+
from pinecone.core.openapi.admin.apis import OrganizationsApi
4+
from pinecone.utils import require_kwargs, parse_non_empty_args
5+
from pinecone.core.openapi.admin.models import UpdateOrganizationRequest
6+
7+
8+
class OrganizationResource:
9+
"""
10+
This class is used to list, fetch, and update organizations.
11+
12+
.. note::
13+
The class should not be instantiated directly. Instead, access this classes
14+
methods through the :class:`pinecone.Admin` class's
15+
:attr:`organization` or :attr:`organizations` attributes.
16+
17+
.. code-block:: python
18+
19+
from pinecone import Admin
20+
21+
admin = Admin()
22+
organization = admin.organization.get(organization_id="my-organization-id")
23+
"""
24+
25+
def __init__(self, api_client: ApiClient):
26+
"""
27+
Initialize the OrganizationResource.
28+
29+
.. warning::
30+
This class should not be instantiated directly. Instead, access this classes
31+
methods through the :class:`pinecone.Admin` class's
32+
:attr:`organization` or :attr:`organizations` attributes.
33+
34+
:param api_client: The API client to use.
35+
:type api_client: ApiClient
36+
"""
37+
self._organizations_api = OrganizationsApi(api_client=api_client)
38+
self._api_client = api_client
39+
40+
@require_kwargs
41+
def list(self):
42+
"""
43+
List all organizations associated with the account.
44+
45+
:return: An object with a list of organizations.
46+
:rtype: {"data": [Organization]}
47+
48+
Examples
49+
--------
50+
51+
.. code-block:: python
52+
:caption: List all organizations
53+
:emphasize-lines: 8
54+
55+
from pinecone import Admin
56+
57+
# Credentials read from PINECONE_CLIENT_ID and
58+
# PINECONE_CLIENT_SECRET environment variables
59+
admin = Admin()
60+
61+
# List all organizations
62+
organizations_response = admin.organization.list()
63+
for organization in organizations_response.data:
64+
print(organization.id)
65+
print(organization.name)
66+
print(organization.plan)
67+
print(organization.payment_status)
68+
"""
69+
return self._organizations_api.list_organizations()
70+
71+
@require_kwargs
72+
def fetch(self, organization_id: str):
73+
"""
74+
Fetch an organization by organization_id.
75+
76+
:param organization_id: The organization_id of the organization to fetch.
77+
:type organization_id: str
78+
:return: The organization.
79+
:rtype: Organization
80+
81+
Examples
82+
--------
83+
84+
.. code-block:: python
85+
:caption: Fetch an organization by organization_id
86+
:emphasize-lines: 7-9
87+
88+
from pinecone import Admin
89+
90+
# Credentials read from PINECONE_CLIENT_ID and
91+
# PINECONE_CLIENT_SECRET environment variables
92+
admin = Admin()
93+
94+
organization = admin.organization.fetch(
95+
organization_id="42ca341d-43bf-47cb-9f27-e645dbfabea6"
96+
)
97+
print(organization.id)
98+
print(organization.name)
99+
print(organization.plan)
100+
print(organization.payment_status)
101+
print(organization.created_at)
102+
print(organization.support_tier)
103+
104+
"""
105+
return self._organizations_api.fetch_organization(organization_id=organization_id)
106+
107+
@require_kwargs
108+
def get(self, organization_id: str):
109+
"""Alias for :func:`fetch`
110+
111+
Examples
112+
--------
113+
114+
.. code-block:: python
115+
:caption: Get an organization by organization_id
116+
:emphasize-lines: 7-9
117+
118+
from pinecone import Admin
119+
120+
# Credentials read from PINECONE_CLIENT_ID and
121+
# PINECONE_CLIENT_SECRET environment variables
122+
admin = Admin()
123+
124+
organization = admin.organization.get(
125+
organization_id="42ca341d-43bf-47cb-9f27-e645dbfabea6"
126+
)
127+
print(organization.id)
128+
print(organization.name)
129+
"""
130+
return self.fetch(organization_id=organization_id)
131+
132+
@require_kwargs
133+
def describe(self, organization_id: str):
134+
"""Alias for :func:`fetch`
135+
136+
Examples
137+
--------
138+
139+
.. code-block:: python
140+
:caption: Describe an organization by organization_id
141+
:emphasize-lines: 7-9
142+
143+
from pinecone import Admin
144+
145+
# Credentials read from PINECONE_CLIENT_ID and
146+
# PINECONE_CLIENT_SECRET environment variables
147+
admin = Admin()
148+
149+
organization = admin.organization.describe(
150+
organization_id="42ca341d-43bf-47cb-9f27-e645dbfabea6"
151+
)
152+
print(organization.id)
153+
print(organization.name)
154+
"""
155+
return self.fetch(organization_id=organization_id)
156+
157+
@require_kwargs
158+
def update(self, organization_id: str, name: Optional[str] = None):
159+
"""
160+
Update an organization.
161+
162+
:param organization_id: The organization_id of the organization to update.
163+
:type organization_id: str
164+
:param name: The new name for the organization. If omitted, the name will not be updated.
165+
:type name: Optional[str]
166+
:return: The updated organization.
167+
:rtype: Organization
168+
169+
Examples
170+
--------
171+
172+
.. code-block:: python
173+
:caption: Update an organization's name
174+
:emphasize-lines: 7-10
175+
176+
from pinecone import Admin
177+
178+
# Credentials read from PINECONE_CLIENT_ID and
179+
# PINECONE_CLIENT_SECRET environment variables
180+
admin = Admin()
181+
182+
organization = admin.organization.update(
183+
organization_id="42ca341d-43bf-47cb-9f27-e645dbfabea6",
184+
name="updated-organization-name"
185+
)
186+
print(organization.name)
187+
188+
"""
189+
args = [("name", name)]
190+
update_request = UpdateOrganizationRequest(**parse_non_empty_args(args))
191+
return self._organizations_api.update_organization(
192+
organization_id=organization_id, update_organization_request=update_request
193+
)

0 commit comments

Comments
 (0)