|
15 | 15 | from googleapiclient.errors import HttpError |
16 | 16 |
|
17 | 17 | from fix_plugin_gcp.config import GcpConfig |
18 | | -from fix_plugin_gcp.gcp_client import GcpClient, GcpApiSpec, InternalZoneProp, ZoneProp, RegionProp |
| 18 | +from fix_plugin_gcp.gcp_client import GcpClient, GcpApiSpec, LocationProp, InternalZoneProp, ZoneProp, RegionProp |
19 | 19 | from fix_plugin_gcp.utils import Credentials |
20 | 20 | from fixlib.baseresources import ( |
21 | 21 | BaseResource, |
@@ -210,60 +210,61 @@ def add_region_to_node(self, node: GcpResourceType, source: Optional[Json] = Non |
210 | 210 | self.add_edge(node, node=node._region, reverse=True) |
211 | 211 | return |
212 | 212 |
|
213 | | - parts = node.id.split("/", maxsplit=4) |
214 | | - if len(parts) > 3 and parts[0] == "projects": |
215 | | - if parts[2] in ["locations", "zones", "regions"]: |
216 | | - location_name = parts[3] |
217 | | - # Check for zone first |
218 | | - if zone := self.zone_by_name.get(location_name): |
219 | | - node._zone = zone |
220 | | - node._region = self.region_by_zone_name.get(zone.id) |
221 | | - self.add_edge(zone, node=node) |
222 | | - return |
| 213 | + def set_zone_or_region(location_name: str) -> bool: |
| 214 | + return set_zone(location_name) or set_region(location_name) |
223 | 215 |
|
224 | | - # Then check for region |
225 | | - if region := self.region_by_name.get(location_name): |
226 | | - node._region = region |
227 | | - self.add_edge(region, node=node) |
228 | | - return |
| 216 | + def set_zone(zone_name: str) -> bool: |
| 217 | + if zone := self.zone_by_name.get(zone_name): |
| 218 | + node._zone = zone |
| 219 | + node._region = self.region_by_zone_name.get(zone.id) |
| 220 | + self.add_edge(zone, node=node) |
| 221 | + return True |
| 222 | + else: |
| 223 | + log.debug( |
| 224 | + "Zone property '%s' found in the source but no corresponding region object is available to associate with the node.", |
| 225 | + zone_name, |
| 226 | + ) |
| 227 | + return False |
| 228 | + |
| 229 | + def set_region(region_name: str) -> bool: |
| 230 | + if region := self.region_by_name.get(region_name): |
| 231 | + node._region = region |
| 232 | + self.add_edge(node, node=region, reverse=True) |
| 233 | + return True |
| 234 | + else: |
| 235 | + log.debug( |
| 236 | + "Region property '%s' found in the source but no corresponding region object is available to associate with the node.", |
| 237 | + region_name, |
| 238 | + ) |
| 239 | + return False |
229 | 240 |
|
230 | 241 | if source is not None: |
231 | 242 | if ZoneProp in source: |
232 | | - zone_name = source[ZoneProp].rsplit("/", 1)[-1] |
233 | | - if zone := self.zone_by_name.get(zone_name): |
234 | | - node._zone = zone |
235 | | - node._region = self.region_by_zone_name[zone_name] |
236 | | - self.add_edge(node, node=zone, reverse=True) |
| 243 | + zone_name = source[ZoneProp].lower().rsplit("/", 1)[-1] |
| 244 | + if set_zone(zone_name): |
237 | 245 | return |
238 | | - else: |
239 | | - log.debug( |
240 | | - "Zone property '%s' found in the source but no corresponding zone object is available to associate with the node.", |
241 | | - zone_name, |
242 | | - ) |
243 | 246 |
|
244 | 247 | if InternalZoneProp in source: |
245 | | - if zone := self.zone_by_name.get(source[InternalZoneProp]): |
246 | | - node._zone = zone |
247 | | - node._region = self.region_by_zone_name[source[InternalZoneProp]] |
248 | | - self.add_edge(node, node=zone, reverse=True) |
| 248 | + zone_name = source[InternalZoneProp].lower().rsplit("/", 1)[-1] |
| 249 | + if set_zone(zone_name): |
249 | 250 | return |
250 | | - else: |
251 | | - log.debug( |
252 | | - "Internal zone property '%s' exists in the source but no corresponding zone object is available to associate with the node.", |
253 | | - source[InternalZoneProp], |
254 | | - ) |
255 | 251 |
|
256 | 252 | if RegionProp in source: |
257 | | - region_name = source[RegionProp].rsplit("/", 1)[-1] |
258 | | - if region := self.region_by_name.get(region_name): |
259 | | - node._region = region |
260 | | - self.add_edge(node, node=region, reverse=True) |
| 253 | + region_name = source[RegionProp].lower().rsplit("/", 1)[-1] |
| 254 | + if set_region(region_name): |
| 255 | + return |
| 256 | + # location property can be a zone or region |
| 257 | + if LocationProp in source: |
| 258 | + location_name = source[LocationProp].lower().rsplit("/", 1)[-1] |
| 259 | + if set_zone_or_region(location_name): |
| 260 | + return |
| 261 | + |
| 262 | + parts = node.id.split("/", maxsplit=4) |
| 263 | + if len(parts) > 3 and parts[0] == "projects": |
| 264 | + if parts[2] in ["locations", "zones", "regions"]: |
| 265 | + location_name = parts[3].lower() |
| 266 | + if set_zone_or_region(location_name): |
261 | 267 | return |
262 | | - else: |
263 | | - log.debug( |
264 | | - "Region property '%s' found in the source but no corresponding region object is available to associate with the node.", |
265 | | - region_name, |
266 | | - ) |
267 | 268 |
|
268 | 269 | # Fallback to GraphBuilder region, i.e. regional collection |
269 | 270 | if self.region is not None: |
|
0 commit comments