Skip to content

Commit 600c034

Browse files
committed
Fix collection function
1 parent 4176d8b commit 600c034

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

functions/log-event/main.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from falconfoundry import FoundryFunction, FoundryRequest, FoundryResponse, FoundryAPIError
2-
from falconpy import CustomStorage
2+
from falconpy import APIHarnessV2
3+
from logging import Logger
4+
from typing import Dict
35
import time
4-
6+
import os
7+
import uuid
58

69
func = FoundryFunction.instance()
710

811

912
@func.handler(method='POST', path='/log-event')
10-
def on_post(request: FoundryRequest) -> FoundryResponse:
13+
def on_post(request: FoundryRequest, config: Dict[str, object] | None, logger: Logger) -> FoundryResponse:
1114
# Validate request
1215
if 'event_data' not in request.body:
1316
return FoundryResponse(
@@ -20,19 +23,34 @@ def on_post(request: FoundryRequest) -> FoundryResponse:
2023
try:
2124
# Store data in a collection
2225
# This assumes you've already created a collection named "event_logs"
26+
event_id = str(uuid.uuid4())
2327
json = {
28+
"event_id": event_id,
2429
"data": event_data,
2530
"timestamp": int(time.time())
2631
}
2732

28-
falcon = CustomStorage()
33+
# Allow setting APP_ID as an env variable for local testing
34+
headers = {}
35+
if os.environ.get("APP_ID"):
36+
headers = {
37+
"X-CS-APP-ID": os.environ.get("APP_ID")
38+
}
39+
40+
api_client = APIHarnessV2()
41+
collection_name = "event_logs"
42+
43+
response = api_client.command("PutObject",
44+
body=json,
45+
collection_name=collection_name,
46+
object_key=event_id,
47+
headers=headers
48+
)
2949

30-
response = falcon.PutObject(body=json,
31-
collection_name="event_logs",
32-
object_key="event_id"
33-
)
50+
# Log the raw response for troubleshooting
51+
logger.info(f"Collections API response: {response}")
3452

35-
if response["status_code"] != 201:
53+
if response["status_code"] != 200:
3654
error_message = response.get('error', {}).get('message', 'Unknown error')
3755
return FoundryResponse(
3856
code=response["status_code"],
@@ -42,18 +60,18 @@ def on_post(request: FoundryRequest) -> FoundryResponse:
4260
)]
4361
)
4462

45-
# Query the collection to retrieve recent events
46-
one_hour_ago = int(time.time()) - 3600
47-
query_response = falcon.search(filter=f"timestamp > {one_hour_ago}",
48-
collection_name="event_logs",
49-
limit=5
50-
)
63+
# Query the collection to retrieve the event by id
64+
query_response = api_client.command("SearchObjects",
65+
filter=f"event_id:'{event_id}'",
66+
collection_name=collection_name,
67+
limit=5,
68+
headers=headers
69+
)
5170

5271
return FoundryResponse(
5372
body={
5473
"stored": True,
55-
"record_id": response["id"],
56-
"recent_events": query_response.get("resources", [])
74+
"metadata": query_response.get("body").get("resources", [])
5775
},
5876
code=200
5977
)

functions/log-event/request_schema.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"properties": {
4-
"event_data": {
4+
"event_id": {
55
"type": "string"
6+
},
7+
"event_data": {
8+
"type": "object"
69
}
710
},
811
"required": [

functions/log-event/response_schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"stored": {
55
"type": "boolean"
66
},
7-
"record_id": {
8-
"type": "string"
9-
},
10-
"recent_events": {
7+
"metadata": {
118
"type": "array"
129
}
1310
},

workflows/Test_log-event_function.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ actions:
88
log_event_510f8f4e:
99
id: functions.log-event.log-event
1010
properties:
11-
event_data: some text to log
11+
event_data: '{"quote": "Tis but a scratch"}'

0 commit comments

Comments
 (0)