Skip to content

Commit 6bde2e3

Browse files
Add a notebook for example user management platform API endpoint calls (yugabyte#24770)
* Add a notebook for example user management endpoint calls * Minor corrections to comments * Additions to summary regarding LDAP and OIDC users
1 parent 7574ecd commit 6bde2e3

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# User Management\n",
8+
"This notebook will demonstrate the platform APIs for local user management. Note that RBAC uses a different workflow.\n",
9+
"\n",
10+
"Additionally note that YBA can also be configured to authenticate users using LDAP [https://docs.yugabyte.com/preview/yugabyte-platform/administer-yugabyte-platform/ldap-authentication/] or OIDC [https://docs.yugabyte.com/preview/yugabyte-platform/administer-yugabyte-platform/oidc-authentication/]. The user management APIs in this notebook are not applicable in those cases. "
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"## Setup\n",
18+
"\n",
19+
"Set the `YB_PLATFORM_URL` and `YB_API_KEY` environment variables in a .env file before running this notebook.\n",
20+
"\n",
21+
"Note: \"verify=False\" is used to disable SSL verification for the purposes of this demonstration, but you should use appropriate certificates in a production environment.\n"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"import os\n",
31+
"import requests\n",
32+
"from pprint import pprint\n",
33+
"from dotenv import load_dotenv\n",
34+
"\n",
35+
"load_dotenv()\n",
36+
"\n",
37+
"yba_url = os.getenv(\"YBA_PLATFORM_URL\")\n",
38+
"yba_api_key = os.getenv(\"YBA_API_KEY\")\n",
39+
"\n",
40+
"headers = {\n",
41+
" 'Content-Type': \"application/json\",\n",
42+
" 'X-AUTH-YW-API-TOKEN': f\"{yba_api_key}\"\n",
43+
"}\n",
44+
"\n",
45+
"session = requests.Session()\n",
46+
"session.verify = False\n",
47+
"session.headers = headers\n",
48+
"\n",
49+
"pprint(yba_url)\n",
50+
"pprint(yba_api_key)\n",
51+
"pprint(headers)"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"## Get the customer ID\n",
59+
"\n",
60+
"The customer ID is unique to the YBA platform and is required for the following Platform API calls. "
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"route = f\"{yba_url}/api/v1/session_info\"\n",
70+
"response = session.get(url=route).json()\n",
71+
"customer_uuid = response[\"customerUUID\"]\n",
72+
"\n",
73+
"print(customer_uuid)"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"## List current users\n",
81+
"\n",
82+
"List users that can login to the YBA platform.\n",
83+
"\n",
84+
"ref: https://api-docs.yugabyte.com/docs/yugabyte-platform/18e7f5bab7963-list-all-users"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"route = f\"{yba_url}/api/v1/customers/{customer_uuid}/users\"\n",
94+
"response = session.get(url=route).json()\n",
95+
"pprint(response)"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"## Get details for a specific user\n",
103+
"\n",
104+
"Choose a user from the above list and get their details. Place the user's UUID in the `user_uuid` variable below.\n",
105+
"\n",
106+
"ref: https://api-docs.yugabyte.com/docs/yugabyte-platform/c11938226a50a-get-a-user-s-details"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"user_uuid=\"asdfasdf-asdf-asdf-asdf-asdfasdfasdf\" # Replace with your user UUID\n",
116+
"\n",
117+
"route = f\"{yba_url}/api/v1/customers/{customer_uuid}/users/{user_uuid}\"\n",
118+
"response = session.get(url=route).json()\n",
119+
"pprint(response)"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"## Create a user\n",
127+
"\n",
128+
"Create a new user of the YBA platform.\n",
129+
"\n",
130+
"ref: https://api-docs.yugabyte.com/docs/yugabyte-platform/b37e8d3b835f1-create-a-user"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": null,
136+
"metadata": {},
137+
"outputs": [],
138+
"source": [
139+
"email = \"name@email.tld\" # Replace with user's email\n",
140+
"password = \"Yo4rPassw*rd\" # Replace with user's password\n",
141+
"confirm_password = \"Yo4rPassw*rd\" # Replace with user's password\n",
142+
"role = \"ReadOnly\" # Replace with user's role (ConnectOnly, ReadOnly, BackupAdmin, Admin)\n",
143+
"timezone = \"America/New_York\" # Replace with user's timezone\n",
144+
"\n",
145+
"route = f\"{yba_url}/api/v1/customers/{customer_uuid}/users\"\n",
146+
"payload = {\n",
147+
" \"email\": email,\n",
148+
" \"password\": password,\n",
149+
" \"confirmPassword\": confirm_password,\n",
150+
" \"role\": role,\n",
151+
" \"timezone\": timezone\n",
152+
"}\n",
153+
"\n",
154+
"# avoids no CSRF token error by emptying the cookie jar\n",
155+
"session.cookies = requests.cookies.RequestsCookieJar()\n",
156+
"\n",
157+
"response = session.post(url=route, json=payload).json()\n",
158+
"pprint(response)"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"## Delete a user\n",
166+
"\n",
167+
"Delete a user from the YBA platform.\n",
168+
"\n",
169+
"ref: https://api-docs.yugabyte.com/docs/yugabyte-platform/9fbb314f9b10f-delete-a-user"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {},
176+
"outputs": [],
177+
"source": [
178+
"user_uuid=\"asdfasdf-asdf-asdf-asdf-asdfasdfasdf\" # Replace with your user UUID\n",
179+
"\n",
180+
"route = f\"{yba_url}/api/v1/customers/{customer_uuid}/users/{user_uuid}\"\n",
181+
"response = session.delete(url=route).json()\n",
182+
"pprint(response)"
183+
]
184+
}
185+
],
186+
"metadata": {
187+
"kernelspec": {
188+
"display_name": "Python 3",
189+
"language": "python",
190+
"name": "python3"
191+
},
192+
"language_info": {
193+
"codemirror_mode": {
194+
"name": "ipython",
195+
"version": 3
196+
},
197+
"file_extension": ".py",
198+
"mimetype": "text/x-python",
199+
"name": "python",
200+
"nbconvert_exporter": "python",
201+
"pygments_lexer": "ipython3",
202+
"version": "3.10.9"
203+
}
204+
},
205+
"nbformat": 4,
206+
"nbformat_minor": 2
207+
}

0 commit comments

Comments
 (0)