Skip to content

Commit 0894888

Browse files
using generic handler rather than creating new files.
1 parent e3dc0e4 commit 0894888

File tree

8 files changed

+75
-203
lines changed

8 files changed

+75
-203
lines changed

components/site-workflows/sensors/sensor-ironic-oslo-inspecting-event.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ spec:
7676
steps:
7777
- - name: oslo-events
7878
templateRef:
79-
name: update-nautobot-on-openstack-oslo-event
79+
name: openstack-oslo-event
8080
template: main
8181
arguments:
8282
parameters:

docs/operator-guide/openstack-ironic-nautobot-device-interfaces-sync.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ Server Enrollment → Redfish Inspection → Oslo Event Bus → Argo Events Sens
7070

7171
5. **Workflow Trigger**
7272
- Sensor creates an Argo Workflow named `update-nautobot-*`
73-
- Workflow uses template `update-nautobot-on-openstack-oslo-event`
73+
- Workflow uses template `openstack-oslo-event`
7474
- Event data is passed as workflow parameter
7575

7676
6. **Data Processing**
77-
- Workflow executes `update-nautobot-on-openstack-oslo-event` script
77+
- Workflow executes `openstack-oslo-event` script
7878
- Script fetches full inventory data from Ironic API using node UUID
7979
- Inventory data is parsed and transformed into Nautobot format
8080

@@ -99,11 +99,11 @@ The sensor configuration defines:
9999

100100
### Workflow Template
101101

102-
**File:** `workflows/argo-events/workflowtemplates/update-nautobot-on-openstack-oslo-event.yaml`
102+
**File:** `workflows/argo-events/workflowtemplates/openstack-oslo-event.yaml`
103103

104104
The workflow template:
105105

106-
- Runs the `update-nautobot-on-openstack-oslo-event` command
106+
- Runs the `openstack-oslo-event` command
107107
- Mounts Nautobot token and OpenStack credentials
108108
- Passes event JSON as input file
109109
- Uses service account with appropriate permissions
@@ -271,7 +271,7 @@ openstack baremetal node show <node-uuid> -f json > node.json
271271

272272
# Submit workflow manually
273273
argo -n argo-events submit \
274-
--from workflowtemplate/update-nautobot-on-openstack-oslo-event \
274+
--from workflowtemplate/openstack-oslo-event \
275275
-p event-json "$(cat node.json)"
276276
```
277277

python/understack-workflows/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ sync-network-segment-range = "understack_workflows.main.sync_ucvni_group_range:m
4040
openstack-oslo-event = "understack_workflows.main.openstack_oslo_event:main"
4141
netapp-create-svm = "understack_workflows.main.netapp_create_svm:main"
4242
netapp-configure-interfaces = "understack_workflows.main.netapp_configure_net:main"
43-
update-nautobot-on-openstack-oslo-event = "understack_workflows.main.update_nautobot:main"
4443

4544
[dependency-groups]
4645
test = [

python/understack-workflows/understack_workflows/main/openstack_oslo_event.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from understack_workflows.oslo_event import keystone_project
1919
from understack_workflows.oslo_event import neutron_network
2020
from understack_workflows.oslo_event import neutron_subnet
21+
from understack_workflows.oslo_event import update_nautobot
2122

2223
logger = setup_logger(__name__)
2324

@@ -62,12 +63,16 @@ class NoEventHandlerError(Exception):
6263
# Type alias for event handler functions
6364
EventHandler = Callable[[Connection, NautobotApi, dict[str, Any]], int]
6465

65-
# add the event_type here and the function that should be called
66-
_event_handlers: dict[str, EventHandler] = {
66+
# add the event_type here and the function(s) that should be called
67+
# each event type can have a single handler or a list of handlers
68+
_event_handlers: dict[str, EventHandler | list[EventHandler]] = {
6769
"baremetal.port.create.end": ironic_port.handle_port_create_update,
6870
"baremetal.port.update.end": ironic_port.handle_port_create_update,
6971
"baremetal.port.delete.end": ironic_port.handle_port_delete,
70-
"baremetal.node.provision_set.end": ironic_node.handle_provision_end,
72+
"baremetal.node.provision_set.end": [
73+
ironic_node.handle_provision_end,
74+
update_nautobot.handle_provision_end,
75+
],
7176
"identity.project.created": keystone_project.handle_project_created,
7277
"identity.project.updated": keystone_project.handle_project_updated,
7378
"identity.project.deleted": keystone_project.handle_project_deleted,
@@ -179,14 +184,20 @@ def main() -> int:
179184

180185
logger.info("Processing event type: %s", event_type)
181186

182-
# look up the event handler
183-
event_handler = _event_handlers.get(event_type)
184-
if event_handler is None:
187+
# look up the event handler(s)
188+
event_handlers = _event_handlers.get(event_type)
189+
if event_handlers is None:
185190
logger.error("No event handler for event type: %s", event_type)
186191
logger.debug("Available event handlers: %s", list(_event_handlers.keys()))
187192
sys.exit(_EXIT_NO_EVENT_HANDLER)
188193

189-
logger.debug("Found event handler for event type: %s", event_type)
194+
# normalize to list for consistent processing
195+
if not isinstance(event_handlers, list):
196+
event_handlers = [event_handlers]
197+
198+
logger.debug(
199+
"Found %d handler(s) for event type: %s", len(event_handlers), event_type
200+
)
190201

191202
# get a connection to OpenStack and to Nautobot
192203
try:
@@ -195,17 +206,21 @@ def main() -> int:
195206
logger.exception("Client initialization failed")
196207
sys.exit(_EXIT_CLIENT_ERROR)
197208

198-
# execute the event handler
199-
logger.info("Executing event handler for event type: %s", event_type)
200-
try:
201-
ret = event_handler(conn, nautobot, event)
202-
except Exception:
203-
logger.exception("Event handler failed")
204-
sys.exit(_EXIT_HANDLER_ERROR)
205-
206-
logger.info("Event handler completed successfully with return code: %s", ret)
207-
208-
# exit if the event handler provided a return code or just with success
209-
if isinstance(ret, int):
210-
return ret
211-
return _EXIT_SUCCESS
209+
# execute all event handlers
210+
last_ret = _EXIT_SUCCESS
211+
for idx, event_handler in enumerate(event_handlers, 1):
212+
handler_name = event_handler.__name__
213+
logger.info(
214+
"Executing handler %d/%d: %s", idx, len(event_handlers), handler_name
215+
)
216+
try:
217+
ret = event_handler(conn, nautobot, event)
218+
if isinstance(ret, int):
219+
last_ret = ret
220+
logger.info("Handler %s completed with return code: %s", handler_name, ret)
221+
except Exception:
222+
logger.exception("Handler %s failed", handler_name)
223+
sys.exit(_EXIT_HANDLER_ERROR)
224+
225+
logger.info("All handlers completed successfully")
226+
return last_ret

python/understack-workflows/understack_workflows/main/update_nautobot.py

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from openstack.connection import Connection
2+
from pynautobot.core.api import Api as Nautobot
3+
4+
from understack_workflows import nautobot_device
5+
from understack_workflows.bmc_chassis_info import ChassisInfo
6+
from understack_workflows.helpers import setup_logger
7+
from understack_workflows.ironic.client import IronicClient
8+
from understack_workflows.ironic.inventory import get_device_info
9+
from understack_workflows.openstack.client import get_ironic_client
10+
11+
logger = setup_logger(__name__)
12+
13+
14+
def handle_provision_end(conn: Connection, nautobot: Nautobot, event_data: dict) -> int:
15+
"""Handle Ironic node provisioning END event."""
16+
payload = event_data.get("payload", {})
17+
payload_data = payload.get("ironic_object.data")
18+
19+
if not payload_data:
20+
raise ValueError("Missing 'ironic_object.data' in event payload")
21+
22+
cloud_name = getattr(conn.config, "name", None) if hasattr(conn, "config") else None
23+
ironic_raw_client = get_ironic_client(cloud=cloud_name)
24+
ironic_client = IronicClient(ironic_raw_client)
25+
26+
node_inventory = ironic_client.get_node_inventory(
27+
node_ident=str(payload_data["uuid"])
28+
)
29+
device_info: ChassisInfo = get_device_info(node_inventory)
30+
nb_device = nautobot_device.find_or_create(device_info, nautobot)
31+
32+
logger.info("Updated Nautobot device: %s", nb_device)
33+
return 0

workflows/argo-events/kustomization.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ resources:
2424
- sensors/alertmanager-webhook-sensor.yaml
2525
- eventsources/alertmanager-webhook-eventsource.yaml
2626
- workflowtemplates/alert-automation-neutron-agent-down.yaml
27-
- workflowtemplates/update-nautobot-on-openstack-oslo-event.yaml
2827

2928
helmCharts:
3029
- name: nautobot-token

workflows/argo-events/workflowtemplates/update-nautobot-on-openstack-oslo-event.yaml

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)