Skip to content

Commit

Permalink
feat: Azure public IP spec (#3751)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Zapletal <[email protected]>
  • Loading branch information
lzap authored Apr 19, 2023
1 parent cc54201 commit 7fac991
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
41 changes: 41 additions & 0 deletions insights/parsers/azure_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
AzureInstancePlan - 'plan' of Azure Instance
--------------------------------------------
AzurePublicIpv4Addresses - list of public IPv4 addresses
--------------------------------------------------------
"""
import json

Expand Down Expand Up @@ -159,3 +162,41 @@ def __repr__(self):
return "<azure_plan_name: {n}, product: {pr}, publisher: {pu}, raw: {r}".format(
n=self.name, pr=self.product, pu=self.publisher, r=self.raw
)


@parser(Specs.azure_load_balancer)
class AzurePublicIpv4Addresses(CommandParser, list):
"""
Class for parsing the Azure load balancer JSON data returned by command
curl -sH "Metadata:true" --connect-timeout 5 \
"http://169.254.169.254/metadata/loadbalancer?api-version=2021-12-13&format=json"
Typical Output of this command is::
{
"loadbalancer": {
"publicIpAddresses": [
{
"frontendIpAddress": "137.116.118.209",
"privateIpAddress": "10.0.0.4"
}
],
"inboundRules": [],
"outboundRules": []
}
}
Raises:
SkipComponent: When content is empty or curl returned an error.
ParseException: On JSON parsing error.
"""
def parse_content(self, content):
validate_content(content)

try:
plan = json.loads(' '.join(content))
for pair in plan["loadbalancer"]["publicIpAddresses"]:
self.append(pair["frontendIpAddress"])
except:
raise ParseException("Unable to parse JSON")
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Specs(SpecSet):
azure_instance_id = RegistryPoint()
azure_instance_plan = RegistryPoint()
azure_instance_type = RegistryPoint()
azure_load_balancer = RegistryPoint()
bdi_read_ahead_kb = RegistryPoint(multi_output=True)
bios_uuid = RegistryPoint()
blacklisted_specs = RegistryPoint()
Expand Down
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class DefaultSpecs(Specs):
azure_instance_id = simple_command("/usr/bin/curl -s -H Metadata:true http://169.254.169.254/metadata/instance/compute/vmId?api-version=2021-12-13&format=text --connect-timeout 5", deps=[IsAzure])
azure_instance_plan = simple_command("/usr/bin/curl -s -H Metadata:true http://169.254.169.254/metadata/instance/compute/plan?api-version=2021-12-13&format=json --connect-timeout 5", deps=[IsAzure])
azure_instance_type = simple_command("/usr/bin/curl -s -H Metadata:true http://169.254.169.254/metadata/instance/compute/vmSize?api-version=2021-12-13&format=text --connect-timeout 5", deps=[IsAzure])
azure_load_balancer = simple_command("/usr/bin/curl -s -H Metadata:true http://169.254.169.254/metadata/loadbalancer?api-version=2021-12-13&format=json --connect-timeout 5", deps=[IsAzure])
bdi_read_ahead_kb = glob_file("/sys/class/bdi/*/read_ahead_kb")
bios_uuid = simple_command("/usr/sbin/dmidecode -s system-uuid")
blkid = simple_command("/sbin/blkid -c /dev/null")
Expand Down
46 changes: 45 additions & 1 deletion insights/tests/parsers/test_azure_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from insights.core.exceptions import ContentException, ParseException, SkipComponent
from insights.parsers import azure_instance
from insights.parsers.azure_instance import AzureInstanceID, AzureInstancePlan, AzureInstanceType
from insights.parsers.azure_instance import AzureInstanceID, AzureInstancePlan, AzureInstanceType, AzurePublicIpv4Addresses
from insights.tests import context_wrap

# For AzureInstanceID
Expand Down Expand Up @@ -61,6 +61,33 @@
curl: (28) connect() timed out!
""".strip()

# For AzurePublicIpv4Addresses and AzurePublicHostname
AZURE_LB_1 = """
{
"loadbalancer": {
"publicIpAddresses": [
{
"frontendIpAddress": "137.116.118.209",
"privateIpAddress": "10.0.0.4"
}
],
"inboundRules": [],
"outboundRules": []
}
}
""".strip()

AZURE_LB_ERR1 = """
curl: (7) Failed to connect to 169.254.169.254 port 80: Connection timed out
""".strip()
AZURE_LB_ERR2 = """
curl: (7) couldn't connect to host
""".strip()
AZURE_LB_ERR3 = """
curl: (28) connect() timed out!
""".strip()
AZURE_LB_ERR4 = "}}}"


# Test AzureInstanceID
def test_azure_instance_id_ab_empty():
Expand Down Expand Up @@ -171,3 +198,20 @@ def test_doc_examples():
}
failed, total = doctest.testmod(azure_instance, globs=env)
assert failed == 0


def test_azure_public_ipv4():
with pytest.raises(SkipComponent):
AzurePublicIpv4Addresses(context_wrap(AZURE_LB_ERR1))

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

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

with pytest.raises(ParseException):
AzurePublicIpv4Addresses(context_wrap(AZURE_LB_ERR4))

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

0 comments on commit 7fac991

Please sign in to comment.