Skip to content

Commit

Permalink
support dict/list resources type for lisa template (#3140)
Browse files Browse the repository at this point in the history
* support dict/list for resources schema

* addressed comment
  • Loading branch information
nagworld9 authored Jun 10, 2024
1 parent 49d3ce3 commit d655c29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
12 changes: 9 additions & 3 deletions tests_e2e/tests/lib/network_security_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def add_security_rule(self, security_rule: Dict[str, Any]) -> None:
self._get_network_security_group()["properties"]["securityRules"].append(security_rule)

def _get_network_security_group(self) -> Dict[str, Any]:
resources: Dict[str, Dict[str, Any]] = self._template["resources"]
resources: Any = self._template["resources"]
#
# If the NSG already exists, just return it
#
Expand All @@ -76,8 +76,14 @@ def _get_network_security_group(self) -> Dict[str, Any]:
"securityRules": []
}}
}}""")
nsg_reference = "network_security_groups"
resources[nsg_reference] = network_security_group

# resources is a dictionary in LISA's ARM template, but a list in the template for scale sets
if isinstance(resources, dict):
nsg_reference = "network_security_groups"
resources[nsg_reference] = network_security_group
else:
nsg_reference = f"[resourceId('Microsoft.Network/networkSecurityGroups', '{self._NETWORK_SECURITY_GROUP}')]"
resources.append(network_security_group)

#
# Add a dependency on the NSG to the virtual network
Expand Down
16 changes: 10 additions & 6 deletions tests_e2e/tests/lib/update_arm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,29 @@ def update(self, template: Dict[str, Any], is_lisa_template: bool) -> None:
"""

@staticmethod
def get_resource(resources: Dict[str, Dict[str, Any]], type_name: str) -> Any:
def get_resource(resources: Any, type_name: str) -> Any:
"""
Returns the first resource of the specified type in the given 'resources' list.
Returns the first resource of the specified type in the given 'resources' list/dict.
Raises KeyError if no resource of the specified type is found.
"""
for item in resources.values():
if isinstance(resources, dict):
resources = resources.values()
for item in resources:
if item["type"] == type_name:
return item
raise KeyError(f"Cannot find a resource of type {type_name} in the ARM template")

@staticmethod
def get_resource_by_name(resources: Dict[str, Dict[str, Any]], resource_name: str, type_name: str) -> Any:
def get_resource_by_name(resources: Any, resource_name: str, type_name: str) -> Any:
"""
Returns the first resource of the specified type and name in the given 'resources' list.
Returns the first resource of the specified type and name in the given 'resources' list/dict.
Raises KeyError if no resource of the specified type and name is found.
"""
for item in resources.values():
if isinstance(resources, dict):
resources = resources.values()
for item in resources:
if item["type"] == type_name and item["name"] == resource_name:
return item
raise KeyError(f"Cannot find a resource {resource_name} of type {type_name} in the ARM template")
Expand Down

0 comments on commit d655c29

Please sign in to comment.