Skip to content

Commit f6ff536

Browse files
authored
Merge pull request #15 from CloudBoltSoftware/supportArrayOfPropertesInPsets/CMP-2659
fix to support list of properties in propertysets[CMP-2659]
2 parents f428db5 + 7dc2300 commit f6ff536

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

onefuse/admin.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -740,19 +740,25 @@ def get_create_properties(self, template_properties: dict):
740740
"""
741741
Parse a dict to find any properties prepended with
742742
OneFuse_CreateProperties_. If found, extract the key:value out of the
743-
property and return them as a dict.
743+
property and return them as a dict. Supports both single objects and arrays of objects.
744744
745745
Ex:
746746
{
747747
"root_prop":"root_value"
748748
"OneFuse_CreateProperties_Test": {
749-
"key": "name_app",
749+
"key": "name_app1",
750750
"value": "apache"
751-
}
751+
},
752+
"OneFuse_CreateProperties_Apps": [
753+
{"key": "name_app2", "value": "nginx"},
754+
{"key": "name_app3", "value": "mysql"}
755+
]
752756
}
753757
The above JSON when passed in to this function would return:
754758
{
755-
"name_app": "apache"
759+
"name_app1": "apache",
760+
"name_app2": "nginx",
761+
"name_app3": "mysql"
756762
}
757763
758764
Parameters
@@ -763,15 +769,28 @@ def get_create_properties(self, template_properties: dict):
763769
create_properties = {}
764770
pattern = re.compile('OneFuse_CreateProperties_')
765771
for key in template_properties.keys():
772+
# Match the key against a defined pattern
766773
result = pattern.match(key)
767774
if result is not None:
768-
self.logger.debug(f'Starting JSON parse of key: {key}, '
769-
f'value: {template_properties[key]}')
775+
self.logger.debug(f'Starting parse of key: {key}, '
776+
f'value: {template_properties[key]}')
770777
value_obj = template_properties[key]
771778
self.logger.debug(f'Create Props Object: {value_obj}')
772-
if type(value_obj) == str:
779+
780+
# If the value_obj is a string, parse JSON.
781+
if isinstance(value_obj, str):
773782
value_obj = json.loads(value_obj)
774-
if value_obj["key"] and value_obj["value"]:
783+
784+
# If the value_obj is a list (array of key/value pairs).
785+
if isinstance(value_obj, list):
786+
for item in value_obj:
787+
# If the item is a dictionary containing both 'key' and 'value'
788+
if isinstance(item, dict) and "key" in item and "value" in item:
789+
# Add the 'key' and 'value' from the item to the create_properties dictionary
790+
create_properties[item["key"]] = item["value"]
791+
# If the value_obj is a single key/value pair containing both 'key' and 'value'
792+
elif isinstance(value_obj, dict) and "key" in value_obj and "value" in value_obj:
793+
# Add the 'key' and 'value' from the dictionary to the create_properties dictionary
775794
create_properties[value_obj["key"]] = value_obj["value"]
776795
return create_properties
777796

0 commit comments

Comments
 (0)