Skip to content

Commit

Permalink
feat: Rewritten references to the AVN and AS resources
Browse files Browse the repository at this point in the history
  • Loading branch information
1101-1 committed Dec 5, 2023
1 parent f976472 commit 6975927
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 45 deletions.
2 changes: 1 addition & 1 deletion plugins/aws/resoto_plugin_aws/aws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def call_single(
paginator = client.get_paginator(py_action)
result: List[Json] = []
for page in paginator.paginate(**kwargs):
log.debug2(f"[Aws] Next page for service={aws_service} action={action}{arg_info}") # type: ignore
log.debug(f"[Aws] Next page for service={aws_service} action={action}{arg_info}") # type: ignore
next_page: Json = self.__to_json(page) # type: ignore
if result_name is None:
# the whole object is appended
Expand Down
6 changes: 3 additions & 3 deletions plugins/azure/resoto_plugin_azure/azure_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ def _call(self, spec: AzureApiSpec, **kwargs: Any) -> List[Json]:
params["api-version"] = _SERIALIZER.query("api_version", spec.version, "str") # type: ignore

# Define url map
format_map_paths = {"subscriptionId": self.subscription_id, "location": self.location, **params}
format_map_paths.update({param: kwargs.pop(param, "") for param in spec.path_parameters if param in kwargs})
path_format_params = {"subscriptionId": self.subscription_id, "location": self.location, **params}
path_format_params.update({param: kwargs.pop(param, "") for param in spec.path_parameters if param in kwargs})

# Construct url
path = spec.path.format_map(format_map_paths)
path = spec.path.format_map(path_format_params)
url = self.client._client.format_url(path) # pylint: disable=protected-access

# Construct and send request
Expand Down
54 changes: 14 additions & 40 deletions plugins/azure/resoto_plugin_azure/resource/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@
from resotolib.types import Json


def extract_virtual_network_id(subnet_id: str) -> str:
"""
Extracts {virtualNetworkName} value from a subnet ID and create virtual network ID
e.g. /subscriptions/{subscriptionId}/.../virtualNetworks/{virtualNetworkName}/subnets/{subnetName}
-> /subscriptions/{subscriptionId}/.../virtualNetworks/{virtualNetworkName}
Returns:
str: Virtual network ID.
"""
# Split the subnet ID by "/"
parts = subnet_id.split("/")
# Extract the Virtual Network ID
virtual_network_id = "/".join(parts[:-2])
return virtual_network_id


@define(eq=False, slots=False)
class AzureApplicationGatewaySku:
kind: ClassVar[str] = "azure_application_gateway_sku"
Expand Down Expand Up @@ -1002,7 +984,7 @@ class AzureApplicationGateway(AzureResource, BaseGateway):
expect_array=True,
)
reference_kinds: ClassVar[ModelReference] = {
"predecessors": {"default": ["azure_virtual_network", "azure_subnet"]},
"predecessors": {"default": ["azure_subnet"]},
"successors": {"default": ["azure_web_application_firewall_policy"]},
}
mapping: ClassVar[Dict[str, Bender]] = {
Expand Down Expand Up @@ -1122,10 +1104,6 @@ def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
builder.add_edge(
self, edge_type=EdgeType.default, reverse=True, clazz=AzureSubnet, id=subnet_id
)
vn_id = extract_virtual_network_id(subnet_id)
builder.add_edge(
self, edge_type=EdgeType.default, reverse=True, clazz=AzureVirtualNetwork, id=vn_id
)


@define(eq=False, slots=False)
Expand Down Expand Up @@ -1448,7 +1426,7 @@ class AzureFirewall(AzureResource, BaseFirewall):
expect_array=True,
)
reference_kinds: ClassVar[ModelReference] = {
"predecessors": {"default": ["azure_virtual_network"]},
"predecessors": {"default": ["azure_subnet"]},
"successors": {"default": ["azure_firewall_fqdn_tag", "azure_firewall_policy", "azure_virtual_hub"]},
}
mapping: ClassVar[Dict[str, Bender]] = {
Expand Down Expand Up @@ -1500,11 +1478,9 @@ def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
builder.add_edge(self, edge_type=EdgeType.default, clazz=AzureFirewallPolicy, id=policy_id)
if ip_confs := self.firewall_ip_configurations:
for ip_conf in ip_confs:
if subnet := ip_conf.subnet:
vn_id = extract_virtual_network_id(subnet)
builder.add_edge(
self, edge_type=EdgeType.default, reverse=True, clazz=AzureVirtualNetwork, id=vn_id
)
if subnet_id := ip_conf.subnet:
builder.add_edge(self, edge_type=EdgeType.default, reverse=True, clazz=AzureSubnet, id=subnet_id)

if rule_collections := self.application_rule_collections:
for rule_collection in rule_collections:
if rules := rule_collection.rules:
Expand Down Expand Up @@ -2452,7 +2428,7 @@ class AzureFrontendIPConfiguration(AzureSubResource):
"provisioning_state": S("properties", "provisioningState"),
"_public_ip_address_id": S("properties", "publicIPAddress", "id"),
"public_ip_prefix": S("properties", "publicIPPrefix", "id"),
"subnet": S("properties", "subnet") >> Bend(AzureSubnet.mapping),
"_subnet_id": S("properties", "subnet", "id"),
"type": S("type"),
"zones": S("zones"),
}
Expand All @@ -2469,7 +2445,7 @@ class AzureFrontendIPConfiguration(AzureSubResource):
provisioning_state: Optional[str] = field(default=None, metadata={'description': 'The current provisioning state.'}) # fmt: skip
_public_ip_address_id: Optional[str] = field(default=None, metadata={'description': 'Public IP address resource.'}) # fmt: skip
public_ip_prefix: Optional[str] = field(default=None, metadata={'description': 'Reference to another subresource.'}) # fmt: skip
subnet: Optional[AzureSubnet] = field(default=None, metadata={'description': 'Subnet in a virtual network resource.'}) # fmt: skip
_subnet_id: Optional[str] = field(default=None, metadata={'description': 'Subnet in a virtual network resource.'}) # fmt: skip
type: Optional[str] = field(default=None, metadata={"description": "Type of the resource."})
zones: Optional[List[str]] = field(default=None, metadata={'description': 'A list of availability zones denoting the IP allocated for the resource needs to come from.'}) # fmt: skip

Expand Down Expand Up @@ -2635,7 +2611,7 @@ class AzurePrivateLinkServiceIpConfiguration(AzureSubResource):
"private_ip_address_version": S("properties", "privateIPAddressVersion"),
"private_ip_allocation_method": S("properties", "privateIPAllocationMethod"),
"provisioning_state": S("properties", "provisioningState"),
"subnet": S("properties", "subnet") >> Bend(AzureSubnet.mapping),
"_subnet_id": S("properties", "subnet", "id"),
"type": S("type"),
}
etag: Optional[str] = field(default=None, metadata={'description': 'A unique read-only string that changes whenever the resource is updated.'}) # fmt: skip
Expand All @@ -2645,7 +2621,7 @@ class AzurePrivateLinkServiceIpConfiguration(AzureSubResource):
private_ip_address_version: Optional[str] = field(default=None, metadata={"description": "IP address version."})
private_ip_allocation_method: Optional[str] = field(default=None, metadata={'description': 'IP address allocation method.'}) # fmt: skip
provisioning_state: Optional[str] = field(default=None, metadata={'description': 'The current provisioning state.'}) # fmt: skip
subnet: Optional[AzureSubnet] = field(default=None, metadata={'description': 'Subnet in a virtual network resource.'}) # fmt: skip
_subnet_id: Optional[str] = field(default=None, metadata={'description': 'Subnet in a virtual network resource.'}) # fmt: skip
type: Optional[str] = field(default=None, metadata={"description": "The resource type."})


Expand Down Expand Up @@ -2848,7 +2824,7 @@ class AzureDscpConfiguration(AzureResource):
expect_array=True,
)
reference_kinds: ClassVar[ModelReference] = {
"predecessors": {"default": ["azure_virtual_network"]},
"predecessors": {"default": ["azure_subnet"]},
}
mapping: ClassVar[Dict[str, Bender]] = {
"id": S("id"),
Expand Down Expand Up @@ -2909,9 +2885,8 @@ def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
ni_id, subnet_ids = info
for subnet_id in subnet_ids:
if network_interface_id == ni_id:
vn_id = extract_virtual_network_id(subnet_id)
builder.add_edge(
self, edge_type=EdgeType.default, reverse=True, clazz=AzureVirtualNetwork, id=vn_id
self, edge_type=EdgeType.default, reverse=True, clazz=AzureSubnet, id=subnet_id
)


Expand Down Expand Up @@ -4270,7 +4245,7 @@ class AzureNetworkProfile(AzureResource):
expect_array=True,
)
reference_kinds: ClassVar[ModelReference] = {
"predecessors": {"default": ["azure_virtual_network"]},
"predecessors": {"default": ["azure_subnet"]},
"successors": {"default": ["azure_virtual_machine"]},
}
mapping: ClassVar[Dict[str, Bender]] = {
Expand Down Expand Up @@ -4315,10 +4290,9 @@ def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
for container in container_nic:
if ip_configurations := container.ip_configurations:
for ip_configuration in ip_configurations:
if subnet := ip_configuration._subnet_id:
vn_id = extract_virtual_network_id(subnet)
if subnet_id := ip_configuration._subnet_id:
builder.add_edge(
self, edge_type=EdgeType.default, reverse=True, clazz=AzureVirtualNetwork, id=vn_id
self, edge_type=EdgeType.default, reverse=True, clazz=AzureSubnet, id=subnet_id
)

if (container_ni_ids := container.container_network_interfaces) and (
Expand Down
2 changes: 1 addition & 1 deletion plugins/azure/test/collector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def test_collect(
collector = AzureSubscriptionCollector(config, Cloud(id="azure"), azure_subscription, credentials, core_feedback)
collector.collect()
assert len(collector.graph.nodes) == 471
assert len(collector.graph.edges) == 499
assert len(collector.graph.edges) == 498

0 comments on commit 6975927

Please sign in to comment.