Skip to content

Commit ccfb903

Browse files
committed
lint
1 parent cbde735 commit ccfb903

File tree

1 file changed

+13
-92
lines changed
  • supporting-blog-content/serverless-ai-agent

1 file changed

+13
-92
lines changed

supporting-blog-content/serverless-ai-agent/main.py

Lines changed: 13 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,27 @@ def create_ess_project(project_name: str) -> str:
5050
env_url = os.environ.get("ES_URL")
5151
api_key = os.environ.get("API_KEY")
5252
region_id = os.environ.get("REGION", "aws-eu-west-1")
53-
53+
54+
5455
if not env_url:
5556
return "Error: ES_URL is not set in the environment."
5657
if not api_key:
5758
return "Error: API_KEY is not set in the environment."
58-
59+
60+
5961
url = f"{env_url}/api/v1/serverless/projects/elasticsearch"
6062

6163
headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"}
62-
payload = {"name": project_name,"region_id": region_id}
64+
payload = {"name": project_name, "region_id": region_id}
6365

6466

6567
try:
6668
response = requests.post(url, headers=headers, json=payload)
6769
response.raise_for_status()
6870
except requests.exceptions.RequestException as e:
6971
return f"Error during project creation: {e}"
70-
72+
73+
7174
data = response.json()
7275
project_id = data.get("id")
7376
if not project_id:
@@ -78,18 +81,17 @@ def create_ess_project(project_name: str) -> str:
7881
f"Error: Received project ID '{project_id}' does not match expected format."
7982
)
8083

81-
8284
credentials = data.get("credentials")
8385
endpoints = data.get("endpoints")
84-
86+
87+
8588
PROJECT_MAP[project_name] = {
8689
"id": project_id,
87-
"credentials": credentials,
90+
"credentials": credentials,
8891
"endpoints": endpoints,
8992
}
9093
save_projects(PROJECT_MAP)
9194

92-
9395
summary = (
9496
f"Project '{project_name}' created successfully.\n"
9597
f"Project ID: {project_id}\n"
@@ -98,7 +100,6 @@ def create_ess_project(project_name: str) -> str:
98100
return summary
99101

100102

101-
102103
def delete_ess_project(project_name: str) -> str:
103104
"""
104105
Deletes a Serverless project by using the project name.
@@ -110,20 +111,17 @@ def delete_ess_project(project_name: str) -> str:
110111
return f"Error: No project found with name '{project_name}'. Ensure the project was created previously."
111112
project_info = normalize_project_info(project_info)
112113

113-
114114
project_id = project_info.get("id")
115115
env_url = os.environ.get("ES_URL")
116116
api_key = os.environ.get("API_KEY")
117117

118-
119118
if not env_url:
120119
return "Error: ES_URL is not set in the environment."
121120
if not api_key:
122121
return "Error: API_KEY is not set in the environment."
123122

124-
125123
url = f"{env_url}/api/v1/serverless/projects/elasticsearch/{project_id}"
126-
headers = {"Authorization": f"ApiKey {api_key}","Content-Type": "application/json"}
124+
headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"}
127125

128126

129127
try:
@@ -132,13 +130,11 @@ def delete_ess_project(project_name: str) -> str:
132130
except requests.exceptions.RequestException as e:
133131
return f"Error during project deletion: {e}"
134132

135-
136133
del PROJECT_MAP[project_name]
137134
save_projects(PROJECT_MAP)
138135
return f"Project '{project_name}' (ID: {project_id}) deleted successfully."
139136

140137

141-
142138
def get_ess_project_status(project_name: str) -> str:
143139
"""
144140
Retrieves the status of a Serverless project by using its project name.
@@ -150,12 +146,10 @@ def get_ess_project_status(project_name: str) -> str:
150146
return f"Error: No project found with name '{project_name}'."
151147
project_info = normalize_project_info(project_info)
152148

153-
154149
project_id = project_info.get("id")
155150
env_url = os.environ.get("ES_URL")
156151
api_key = os.environ.get("API_KEY")
157152

158-
159153
if not env_url:
160154
return "Error: ES_URL is not set in the environment."
161155
if not api_key:
@@ -164,13 +158,13 @@ def get_ess_project_status(project_name: str) -> str:
164158
url = f"{env_url}/api/v1/serverless/projects/elasticsearch/{project_id}/status"
165159
headers = {"Authorization": f"ApiKey {api_key}"}
166160

167-
168161
try:
169162
response = requests.get(url, headers=headers)
170163
response.raise_for_status()
171164
except requests.exceptions.RequestException as e:
172165
return f"Error retrieving project status: {e}"
173-
166+
167+
174168
status_data = response.json()
175169
return json.dumps(status_data, indent=4)
176170

@@ -187,12 +181,10 @@ def get_ess_project_details(project_name: str) -> str:
187181
return f"Error: No project found with name '{project_name}'."
188182
project_info = normalize_project_info(project_info)
189183

190-
191184
project_id = project_info.get("id")
192185
env_url = os.environ.get("ES_URL")
193186
api_key = os.environ.get("API_KEY")
194187

195-
196188
if not env_url:
197189
return "Error: ES_URL is not set in the environment."
198190
if not api_key:
@@ -201,90 +193,19 @@ def get_ess_project_details(project_name: str) -> str:
201193
url = f"{env_url}/api/v1/serverless/projects/elasticsearch/{project_id}"
202194
headers = {"Authorization": f"ApiKey {api_key}"}
203195

204-
205196
try:
206197
response = requests.get(url, headers=headers)
207198
response.raise_for_status()
208199
except requests.exceptions.RequestException as e:
209200
return f"Error retrieving project details: {e}"
210201

211-
212202
details_data = response.json()
213203
if not details_data.get("credentials"):
214204
details_data["credentials"] = project_info.get("credentials")
215205
if not details_data.get("endpoints"):
216206
details_data["endpoints"] = project_info.get("endpoints")
217207
return json.dumps(details_data, indent=4)
218208

219-
220-
221-
def get_index_status_in_project(project_name: str, index_name: str) -> str:
222-
"""
223-
Retrieves the status/details of an index in a Serverless Elasticsearch project.
224-
It looks up the project info from the persisted PROJECT_MAP, then retrieves the project's
225-
details to extract the Elasticsearch endpoint from the "endpoints" field and the credentials.
226-
227-
The function constructs the index URL as endpoints["elasticsearch"] + "/" + index_name and sends a GET request
228-
using HTTP Basic authentication.
229-
230-
Returns the index details as a formatted JSON string, or an error message if retrieval fails.
231-
"""
232-
project_info = PROJECT_MAP.get(project_name)
233-
if not project_info:
234-
return f"Error: No project found with name '{project_name}'. Ensure the project was created previously."
235-
project_info = normalize_project_info(project_info)
236-
237-
238-
project_id = project_info.get("id")
239-
env_url = os.environ.get("ES_URL")
240-
api_key = os.environ.get("API_KEY")
241-
if not env_url or not api_key:
242-
return "Error: ES_URL or API_KEY is not set in the environment."
243-
244-
245-
details_url = f"{env_url}/api/v1/serverless/projects/elasticsearch/{project_id}"
246-
headers = {"Authorization": f"ApiKey {api_key}"}
247-
try:
248-
resp = requests.get(details_url, headers=headers)
249-
resp.raise_for_status()
250-
except requests.exceptions.RequestException as e:
251-
return f"Error retrieving project details: {e}"
252-
253-
254-
details_data = resp.json()
255-
credentials = details_data.get("credentials") or project_info.get("credentials")
256-
endpoints = details_data.get("endpoints") or project_info.get("endpoints")
257-
258-
259-
if (
260-
not credentials
261-
or not credentials.get("username")
262-
or not credentials.get("password")
263-
):
264-
return (
265-
"Error: Credentials not available. The project may still be initializing. "
266-
"Please wait a few minutes and try again."
267-
)
268-
269-
if not endpoints or not endpoints.get("elasticsearch"):
270-
return "Error: Elasticsearch endpoint not found in project details."
271-
272-
273-
username = credentials.get("username")
274-
password = credentials.get("password")
275-
es_endpoint = endpoints.get("elasticsearch")
276-
index_url = f"{es_endpoint}/{index_name}"
277-
278-
279-
try:
280-
index_resp = requests.get(index_url, auth=(username, password))
281-
index_resp.raise_for_status()
282-
except requests.exceptions.RequestException as e:
283-
return f"Error retrieving index status: {e}"
284-
285-
return json.dumps(index_resp.json(), indent=4)
286-
287-
288209
create_project_tool = FunctionTool.from_defaults(
289210
create_ess_project,
290211
name="create_ess_project",

0 commit comments

Comments
 (0)