From 5648a4ddd7972f42dbe5016409141e7a050feaf9 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 26 Nov 2023 21:21:45 +0400 Subject: [PATCH] fix: Fixed tests and relationships of ALB resource --- .../resoto_plugin_azure/resource/compute.py | 17 ++++++++ .../resoto_plugin_azure/resource/network.py | 40 ------------------- resotolib/resotolib/baseresources.py | 4 +- 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/plugins/azure/resoto_plugin_azure/resource/compute.py b/plugins/azure/resoto_plugin_azure/resource/compute.py index dc2e2fcd3d..af02e6648f 100644 --- a/plugins/azure/resoto_plugin_azure/resource/compute.py +++ b/plugins/azure/resoto_plugin_azure/resource/compute.py @@ -3094,6 +3094,9 @@ class AzureVirtualMachineScaleSet(AzureResource, BaseAutoScalingGroup): access_path="value", expect_array=True, ) + reference_kinds: ClassVar[ModelReference] = { + "predecessors": {"default": ["azure_load_balancer"]}, + } mapping: ClassVar[Dict[str, Bender]] = { "id": S("id"), "tags": S("tags", default={}), @@ -3152,6 +3155,20 @@ class AzureVirtualMachineScaleSet(AzureResource, BaseAutoScalingGroup): virtual_machine_profile: Optional[AzureVirtualMachineScaleSetVMProfile] = field(default=None, metadata={'description': 'Describes a virtual machine scale set virtual machine profile.'}) # fmt: skip zone_balance: Optional[bool] = field(default=None, metadata={'description': 'Whether to force strictly even virtual machine distribution cross x-zones in case there is zone outage. Zonebalance property can only be set if the zones property of the scale set contains more than one zone. If there are no zones or only one zone specified, then zonebalance property should not be set.'}) # fmt: skip + def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None: + if ( + (vm_profile := self.virtual_machine_profile) + and (net_profile := vm_profile.network_profile) + and (net_i_configs := net_profile.network_interface_configurations) + ): + for net_i_config in net_i_configs: + if ip_configs := net_i_config.ip_configurations: + for ip_config in ip_configs: + if baps := ip_config.load_balancer_backend_address_pools: + for bap in baps: + if bap_id := bap: + builder.add_edge(self, clazz=AzureLoadBalancer, id=bap_id) + @define(eq=False, slots=False) class AzureVirtualMachineSize(AzureResource, BaseInstanceType): diff --git a/plugins/azure/resoto_plugin_azure/resource/network.py b/plugins/azure/resoto_plugin_azure/resource/network.py index b3ed73e354..23f9602ca5 100644 --- a/plugins/azure/resoto_plugin_azure/resource/network.py +++ b/plugins/azure/resoto_plugin_azure/resource/network.py @@ -4096,7 +4096,6 @@ class AzureLoadBalancer(AzureResource, BaseLoadBalancer): ) reference_kinds: ClassVar[ModelReference] = { "predecessors": {"default": ["azure_virtual_network", "azure_subnet"]}, - "successors": {"default": ["azure_virtual_machine_scale_set"]}, } mapping: ClassVar[Dict[str, Bender]] = { "id": S("id"), @@ -4119,9 +4118,6 @@ class AzureLoadBalancer(AzureResource, BaseLoadBalancer): "resource_guid": S("properties", "resourceGuid"), "azure_sku": S("sku") >> Bend(AzureSku.mapping), "lb_type": S("type"), - "public_ip_address": S( - "properties", "frontendIPConfigurations", "properties", "publicIPAddress", "properties", "ipAddress" - ), "backends": S("properties", "backendAddressPools") >> ForallBend(AzureBackendAddressPool.mapping) >> ForallBend(S("virtual_network", default=[])), @@ -4140,49 +4136,13 @@ class AzureLoadBalancer(AzureResource, BaseLoadBalancer): provisioning_state: Optional[str] = field(default=None, metadata={'description': 'The current provisioning state.'}) # fmt: skip resource_guid: Optional[str] = field(default=None, metadata={'description': 'The resource GUID property of the load balancer resource.'}) # fmt: skip azure_sku: Optional[AzureSku] = field(default=None, metadata={"description": "SKU of a load balancer."}) - _vmss_and_lb_ids: Optional[List[Tuple[str, str]]] = None - - def post_process(self, graph_builder: GraphBuilder, source: Json) -> None: - def collect_vmss_ids() -> None: - resources_api_spec = AzureApiSpec( - service="compute", - version="2023-07-01", - path="/subscriptions/{subscriptionId}/providers/Microsoft.Network/virtualMachineScaleSets", - path_parameters=["subscriptionId"], - query_parameters=["api-version"], - access_path="value", - expect_array=True, - ) - - self._vmss_and_lb_ids = [ - (backend_address_pool["id"], resource["id"]) - for resource in graph_builder.client.list(resources_api_spec) - for vm_profile in [resource.get("properties", {}).get("virtualMachineProfile", {})] - if isinstance(vm_profile, dict) - for nic_config in vm_profile.get("networkProfile", {}).get("networkInterfaceConfigurations", []) - for ip_config in nic_config.get("properties", {}).get("ipConfigurations", []) - for backend_address_pool in ip_config.get("properties", {}).get("loadBalancerBackendAddressPools", []) - ] - - graph_builder.submit_work(collect_vmss_ids) def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None: - # Import placed inside the method due to circular import error resolution - from resoto_plugin_azure.resource.compute import ( # pylint: disable=import-outside-toplevel - AzureVirtualMachineScaleSet, - ) - if vns := self.backends: for vn_id in vns: builder.add_edge(self, clazz=AzureVirtualNetwork, id=vn_id) if baps := self.backend_address_pools: for bap in baps: - if bap_id := bap.id: - if vmsss_info := self._vmss_and_lb_ids: - for info in vmsss_info: - r_bap_id, vmss_id = info - if r_bap_id == bap_id: - builder.add_edge(self, clazz=AzureVirtualMachineScaleSet, id=vmss_id) if lbbas := bap.load_balancer_backend_addresses: for lbba in lbbas: if subnet_id := lbba.subnet: diff --git a/resotolib/resotolib/baseresources.py b/resotolib/resotolib/baseresources.py index b5ce4fea54..9fd0d5b6f1 100644 --- a/resotolib/resotolib/baseresources.py +++ b/resotolib/resotolib/baseresources.py @@ -910,7 +910,9 @@ class BaseNetworkQuota(BaseQuota): @define(eq=False, slots=False) class BaseFirewall(BaseResource): - kind: ClassVar[str] = "network" + kind: ClassVar[str] = "firewall" + kind_display: ClassVar[str] = "Firewall" + kind_description: ClassVar[str] = "A firewall." metadata: ClassVar[Dict[str, Any]] = {"icon": "firewall", "group": "networking"}