Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions plugins/gcp/fix_plugin_gcp/resources/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class GcpAddress(GcpResource, BaseIPAddress):
_kind_service: ClassVar[Optional[str]] = service_name
_metadata: ClassVar[Dict[str, Any]] = {"icon": "dns", "group": "networking"}
_reference_kinds: ClassVar[ModelReference] = {
"predecessors": {"default": ["gcp_subnetwork"]},
"predecessors": {"default": ["gcp_subnetwork", "gcp_instance"]},
"successors": {
"delete": ["gcp_subnetwork"],
},
Expand Down Expand Up @@ -159,6 +159,8 @@ class GcpAddress(GcpResource, BaseIPAddress):
def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
if self.subnetwork:
builder.dependant_node(self, reverse=True, clazz=GcpSubnetwork, link=self.subnetwork)
for user in self.users or []:
builder.add_edge(self, reverse=True, clazz=GcpInstance, link=user)


@define(eq=False, slots=False)
Expand Down Expand Up @@ -3598,7 +3600,8 @@ class GcpInstance(GcpResource, BaseInstance):
"predecessors": {
"default": ["gcp_network", "gcp_subnetwork", "gcp_machine_type"],
"delete": ["gcp_network", "gcp_subnetwork"],
}
},
"successors": {"default": ["gcp_disk"]},
}
api_spec: ClassVar[GcpApiSpec] = GcpApiSpec(
service=service_name,
Expand Down Expand Up @@ -3730,6 +3733,10 @@ def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
if self.machine_type:
self.connect_machine_type(self.machine_type, builder)

for disk in self.disks or []:
if disk.source:
builder.add_edge(self, clazz=GcpDisk, link=disk.source)

for nic in self.network_interfaces or []:
builder.dependant_node(self, reverse=True, delete_same_as_default=True, clazz=GcpNetwork, link=nic.network)
builder.dependant_node(
Expand Down
24 changes: 23 additions & 1 deletion plugins/gcp/test/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from google.auth.credentials import AnonymousCredentials

from .random_client import roundtrip, connect_resource, FixturedClient
from .random_client import roundtrip, connect_resource, FixturedClient, create_node


def test_gcp_accelerator_type(random_builder: GraphBuilder) -> None:
Expand All @@ -25,6 +25,16 @@ def test_gcp_address(random_builder: GraphBuilder) -> None:
connect_resource(random_builder, address, GcpSubnetwork, selfLink=address.subnetwork)
assert len(random_builder.edges_of(GcpSubnetwork, GcpAddress)) == 1

instance_link = f"https://www.googleapis.com/compute/v1/projects/{random_builder.project.id}/zones/us-east1-b/instances/test-instance"
address_source, address_with_user = create_node(GcpAddress, users=[instance_link])
random_builder.add_node(address_with_user, address_source)
instance_source, instance = create_node(GcpInstance, selfLink=instance_link)
random_builder.add_node(instance, instance_source)

address_with_user.connect_in_graph(random_builder, address_source)

assert len(random_builder.edges_of(GcpInstance, GcpAddress)) == 1


def test_gcp_autoscaler(random_builder: GraphBuilder) -> None:
autoscaler = roundtrip(GcpAutoscaler, random_builder)
Expand Down Expand Up @@ -150,6 +160,18 @@ def test_gcp_instance(random_builder: GraphBuilder) -> None:
roundtrip(GcpInstance, random_builder)
assert len(random_builder.nodes(clazz=GcpMachineType)) > 0

disk_link = (
f"https://www.googleapis.com/compute/v1/projects/{random_builder.project.id}/zones/us-east1-b/disks/test-disk"
)
instance_source, instance_with_disk = create_node(GcpInstance, disks=[{"source": disk_link}])
random_builder.add_node(instance_with_disk, instance_source)
disk_source, disk = create_node(GcpDisk, selfLink=disk_link)
random_builder.add_node(disk, disk_source)

instance_with_disk.connect_in_graph(random_builder, instance_source)

assert len(random_builder.edges_of(GcpInstance, GcpDisk)) == 1


def test_gcp_instance_custom_machine_type(random_builder: GraphBuilder) -> None:
CUSTOM_MACHINE_TYPE_PART = "/zones/us-east1-b/machineTypes/e2-custom-medium-1024"
Expand Down