Skip to content

Commit

Permalink
fix: proper error handling of AzurePublicIpv4Addresses (#4288)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxue Wang <[email protected]>
  • Loading branch information
JoySnow authored Nov 28, 2024
1 parent 0cebbc4 commit ce06652
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions insights/parsers/azure_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ def parse_content(self, content):

try:
plan = json.loads(' '.join(content))
for pair in plan["loadbalancer"]["publicIpAddresses"]:
self.append(pair["frontendIpAddress"])
except:
raise ParseException("Unable to parse JSON")

for pair in plan.get("loadbalancer", {}).get("publicIpAddresses", {}):
if pair.get("frontendIpAddress"):
self.append(pair["frontendIpAddress"])

if len(self) == 0:
raise SkipComponent("No 'frontendIpAddress'.")
26 changes: 26 additions & 0 deletions insights/tests/parsers/test_azure_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@
curl: (28) connect() timed out!
""".strip()
AZURE_LB_ERR4 = "}}}"
AZURE_LB_ERR5 = """
{ "error": "No load balancer metadata is found. Please check if your VM is using any non-basic SKU load balancer and retry later." }
""".strip()
AZURE_LB_ERR6 = """
{
"loadbalancer": {
"publicIpAddresses": [
{
"frontendIpAddress": "",
"privateIpAddress": "10.0.0.4"
},
{
"privateIpAddress": "10.0.0.4"
}
],
"inboundRules": [],
"outboundRules": []
}
}
""".strip()


# Test AzureInstanceID
Expand Down Expand Up @@ -213,5 +233,11 @@ def test_azure_public_ipv4():
with pytest.raises(ParseException):
AzurePublicIpv4Addresses(context_wrap(AZURE_LB_ERR4))

with pytest.raises(SkipComponent):
AzurePublicIpv4Addresses(context_wrap(AZURE_LB_ERR5))

with pytest.raises(SkipComponent):
AzurePublicIpv4Addresses(context_wrap(AZURE_LB_ERR6))

azure = AzurePublicIpv4Addresses(context_wrap(AZURE_LB_1))
assert azure[0] == "137.116.118.209"

0 comments on commit ce06652

Please sign in to comment.