Skip to content

Commit fa74860

Browse files
committed
create/update: save project tags as output
1 parent 3e2f5be commit fa74860

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed

python/understack-workflows/tests/test_keystone_project.py

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,28 @@ def test_handle_project_created_no_svm_tag(
132132
):
133133
"""Test handling project creation without SVM tag."""
134134
mock_tags.return_value = ["tag1", "tag2"]
135+
mock_file = MagicMock()
136+
mock_open.return_value.__enter__.return_value = mock_file
135137

136138
result = handle_project_created(mock_conn, mock_nautobot, valid_event_data)
137139
assert result == 0
138140
mock_tags.assert_called_once_with(mock_conn, "test-project-123")
139-
mock_open.assert_called_with("/var/run/argo/output.svm_enabled", "w")
141+
142+
# Verify both project_tags and svm_enabled files are written
143+
expected_calls = [
144+
call("/var/run/argo/output.project_tags", "w"),
145+
call("/var/run/argo/output.svm_enabled", "w"),
146+
]
147+
mock_open.assert_has_calls(expected_calls, any_order=True)
148+
149+
# Check that JSON-encoded tags were written
150+
write_calls = mock_file.write.call_args_list
151+
json_tags_written = False
152+
for call_args in write_calls:
153+
if call_args[0][0] == '["tag1", "tag2"]':
154+
json_tags_written = True
155+
break
156+
assert json_tags_written, "Project tags should be written as JSON"
140157

141158
@patch("understack_workflows.oslo_event.keystone_project.NetAppManager")
142159
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
@@ -154,6 +171,8 @@ def test_handle_project_created_with_svm_tag(
154171
mock_tags.return_value = ["tag1", SVM_PROJECT_TAG, "tag2"]
155172
mock_netapp_manager = MagicMock()
156173
mock_netapp_class.return_value = mock_netapp_manager
174+
mock_file = MagicMock()
175+
mock_open.return_value.__enter__.return_value = mock_file
157176

158177
result = handle_project_created(mock_conn, mock_nautobot, valid_event_data)
159178

@@ -168,7 +187,24 @@ def test_handle_project_created_with_svm_tag(
168187
volume_size=VOLUME_SIZE,
169188
aggregate_name=AGGREGATE_NAME,
170189
)
171-
mock_open.assert_called()
190+
191+
# Verify project tags are written as JSON
192+
expected_calls = [
193+
call("/var/run/argo/output.project_tags", "w"),
194+
call("/var/run/argo/output.svm_enabled", "w"),
195+
call("/var/run/argo/output.svm_created", "w"),
196+
call("/var/run/argo/output.svm_name", "w"),
197+
]
198+
mock_open.assert_has_calls(expected_calls, any_order=True)
199+
200+
# Check that JSON-encoded tags were written
201+
write_calls = mock_file.write.call_args_list
202+
json_tags_written = False
203+
for call_args in write_calls:
204+
if call_args[0][0] == '["tag1", "UNDERSTACK_SVM", "tag2"]':
205+
json_tags_written = True
206+
break
207+
assert json_tags_written, "Project tags should be written as JSON"
172208

173209
@patch("understack_workflows.oslo_event.keystone_project.NetAppManager")
174210
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
@@ -292,6 +328,36 @@ def test_handle_project_created_constants_used(
292328
)
293329
mock_open.assert_called()
294330

331+
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
332+
@patch("builtins.open")
333+
def test_handle_project_created_json_tags_output(
334+
self, mock_open, mock_tags, mock_conn, mock_nautobot, valid_event_data
335+
):
336+
"""Test that project tags are written as JSON to output file."""
337+
test_tags = ["custom_tag", "another_tag", SVM_PROJECT_TAG]
338+
mock_tags.return_value = test_tags
339+
mock_file = MagicMock()
340+
mock_open.return_value.__enter__.return_value = mock_file
341+
342+
with patch(
343+
"understack_workflows.oslo_event.keystone_project.NetAppManager"
344+
) as mock_netapp_class:
345+
mock_netapp_manager = MagicMock()
346+
mock_netapp_class.return_value = mock_netapp_manager
347+
348+
result = handle_project_created(mock_conn, mock_nautobot, valid_event_data)
349+
350+
assert result == 0
351+
352+
# Verify project_tags file is written
353+
mock_open.assert_any_call("/var/run/argo/output.project_tags", "w")
354+
355+
# Check that the exact JSON representation of tags was written
356+
import json
357+
358+
expected_json = json.dumps(test_tags)
359+
mock_file.write.assert_any_call(expected_json)
360+
295361

296362
class TestHandleProjectUpdated:
297363
"""Test cases for handle_project_updated function."""
@@ -338,7 +404,10 @@ def test_handle_project_updated_svm_tag_added(
338404
):
339405
"""Test project update when SVM_UNDERSTACK tag is added."""
340406
# Project now has SVM tag
341-
mock_tags.return_value = ["tag1", SVM_PROJECT_TAG, "tag2"]
407+
test_tags = ["tag1", SVM_PROJECT_TAG, "tag2"]
408+
mock_tags.return_value = test_tags
409+
mock_file = MagicMock()
410+
mock_open.return_value.__enter__.return_value = mock_file
342411

343412
mock_netapp_manager = MagicMock()
344413
mock_netapp_manager.check_if_svm_exists.return_value = (
@@ -365,6 +434,13 @@ def test_handle_project_updated_svm_tag_added(
365434
aggregate_name=AGGREGATE_NAME,
366435
)
367436

437+
# Verify project tags are written as JSON
438+
mock_open.assert_any_call("/var/run/argo/output.project_tags", "w")
439+
import json
440+
441+
expected_json = json.dumps(test_tags)
442+
mock_file.write.assert_any_call(expected_json)
443+
368444
@patch("understack_workflows.oslo_event.keystone_project.NetAppManager")
369445
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
370446
@patch("builtins.open")
@@ -588,13 +664,46 @@ def test_handle_project_updated_output_files_written(
588664
)
589665

590666
assert result == 0
591-
# Verify output files are written
667+
# Verify output files are written including project_tags
592668
expected_calls = [
669+
call("/var/run/argo/output.project_tags", "w"),
593670
call("/var/run/argo/output.svm_enabled", "w"),
594671
call("/var/run/argo/output.svm_name", "w"),
595672
]
596673
mock_open.assert_has_calls(expected_calls, any_order=True)
597674

675+
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
676+
@patch("builtins.open")
677+
def test_handle_project_updated_json_tags_empty_list(
678+
self, mock_open, mock_tags, mock_conn, mock_nautobot, valid_update_event_data
679+
):
680+
"""Test that empty project tags are written as JSON empty list."""
681+
mock_tags.return_value = []
682+
mock_file = MagicMock()
683+
mock_open.return_value.__enter__.return_value = mock_file
684+
685+
with patch(
686+
"understack_workflows.oslo_event.keystone_project.NetAppManager"
687+
) as mock_netapp_class:
688+
mock_netapp_manager = MagicMock()
689+
mock_netapp_manager.check_if_svm_exists.return_value = False
690+
mock_netapp_class.return_value = mock_netapp_manager
691+
692+
result = handle_project_updated(
693+
mock_conn, mock_nautobot, valid_update_event_data
694+
)
695+
696+
assert result == 0
697+
698+
# Verify project_tags file is written
699+
mock_open.assert_any_call("/var/run/argo/output.project_tags", "w")
700+
701+
# Check that empty list JSON was written
702+
import json
703+
704+
expected_json = json.dumps([])
705+
mock_file.write.assert_any_call(expected_json)
706+
598707

599708
class TestHandleProjectDeleted:
600709
"""Test cases for handle_project_deleted function."""

python/understack-workflows/understack_workflows/oslo_event/keystone_project.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from dataclasses import dataclass
23

34
from openstack.connection import Connection
@@ -55,6 +56,7 @@ def handle_project_created(
5556
logger.info("Starting ONTAP SVM and Volume creation workflow.")
5657
tags = _keystone_project_tags(conn, event.project_id)
5758
logger.debug("Project %s has tags: %s", event.project_id, tags)
59+
_save_output("project_tags", json.dumps(tags))
5860

5961
project_is_svm_enabled = SVM_PROJECT_TAG in tags
6062
_save_output("svm_enabled", str(project_is_svm_enabled))
@@ -86,6 +88,7 @@ def handle_project_updated(
8688
logger.info("Starting ONTAP SVM and Volume create/update workflow.")
8789
tags = _keystone_project_tags(conn, event.project_id)
8890
logger.debug("Project %s has tags: %s", event.project_id, tags)
91+
_save_output("project_tags", json.dumps(tags))
8992

9093
project_is_svm_enabled = SVM_PROJECT_TAG in tags
9194
_save_output("svm_enabled", str(project_is_svm_enabled))

workflows/argo-events/workflowtemplates/openstack-oslo-event.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ spec:
7676
valueFrom:
7777
path: /var/run/argo/output.svm_created
7878
default: "False"
79+
- name: project_tags
80+
valueFrom:
81+
path: /var/run/argo/output.project_tags
82+
default: ""

0 commit comments

Comments
 (0)