Skip to content

Commit

Permalink
feat: optimized location setting
Browse files Browse the repository at this point in the history
  • Loading branch information
1101-1 committed Dec 9, 2024
1 parent f8c6370 commit cdf7329
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
27 changes: 14 additions & 13 deletions plugins/gcp/fix_plugin_gcp/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,38 +213,31 @@ def add_region_to_node(self, node: GcpResourceType, source: Optional[Json] = Non
def set_zone_or_region(location_name: str) -> bool:
return set_zone(location_name) or set_region(location_name)

def set_zone(location_name: str) -> bool:
if zone := self.zone_by_name.get(location_name):
def set_zone(zone_name: str) -> bool:
if zone := self.zone_by_name.get(zone_name):
node._zone = zone
node._region = self.region_by_zone_name.get(zone.id)
self.add_edge(zone, node=node)
return True
else:
log.debug(
"Zone property '%s' found in the source but no corresponding region object is available to associate with the node.",
location_name,
zone_name,
)
return False

def set_region(location_name: str) -> bool:
if region := self.region_by_name.get(location_name):
def set_region(region_name: str) -> bool:
if region := self.region_by_name.get(region_name):
node._region = region
self.add_edge(node, node=region, reverse=True)
return True
else:
log.debug(
"Region property '%s' found in the source but no corresponding region object is available to associate with the node.",
location_name,
region_name,
)
return False

parts = node.id.split("/", maxsplit=4)
if len(parts) > 3 and parts[0] == "projects":
if parts[2] in ["locations", "zones", "regions"]:
location_name = parts[3].lower()
if set_zone_or_region(location_name):
return

if source is not None:
if ZoneProp in source:
zone_name = source[ZoneProp].lower().rsplit("/", 1)[-1]
Expand All @@ -260,10 +253,18 @@ def set_region(location_name: str) -> bool:
region_name = source[RegionProp].lower().rsplit("/", 1)[-1]
if set_region(region_name):
return
# location property can be a zone or region
if LocationProp in source:
location_name = source[LocationProp].lower().rsplit("/", 1)[-1]
if set_zone_or_region(location_name):
return

parts = node.id.split("/", maxsplit=4)
if len(parts) > 3 and parts[0] == "projects":
if parts[2] in ["locations", "zones", "regions"]:
location_name = parts[3].lower()
if set_zone_or_region(location_name):
return

# Fallback to GraphBuilder region, i.e. regional collection
if self.region is not None:
Expand Down
4 changes: 2 additions & 2 deletions plugins/gcp/fix_plugin_gcp/resources/scc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
GcpProject,
GcpExpectedErrorCodes,
)
from fixlib.baseresources import SEVERITY_MAPPING, Finding, Severity, PhantomBaseResource
from fixlib.baseresources import SEVERITY_MAPPING, Finding, Severity
from fixlib.json_bender import Bender, S, Bend
from fixlib.types import Json

Expand Down Expand Up @@ -122,7 +122,7 @@ class GcpFindingResource:


@define(eq=False, slots=False)
class GcpSccFinding(GcpResource, PhantomBaseResource):
class GcpSccFinding(GcpResource):
kind: ClassVar[str] = "gcp_scc_finding"
_model_export: ClassVar[bool] = False
api_spec: ClassVar[GcpApiSpec] = GcpApiSpec(
Expand Down

0 comments on commit cdf7329

Please sign in to comment.