def _handle_resource()

in blueprints/cloud-operations/network-quota-monitoring/src/plugins/discover-cai.py [0:0]


def _handle_resource(resources, asset_type, data):
  'Parses and returns a single resource. Calls resource-level handler.'
  # general attributes shared by all resource types
  attrs = data['data']
  # we use the asset type as the discovery name sometimes does not match
  # e.g. assetType = GlobalAddress but discoveryName = Address
  resource_name = NAMES[asset_type]
  resource = {
      'id':
          attrs.get('id'),
      'name':
          attrs['name'],
      # Some resources (ex: Filestore) don't have a self_link, using parent + name in that case
      'self_link':
          f'{data["parent"]}/{attrs["name"]}'
          if not 'selfLink' in attrs else _self_link(attrs['selfLink']),
      'assetType':
          asset_type
  }
  # derive parent type and id and skip if parent is not within scope
  parent_data = _get_parent(data['parent'], resources)
  if not parent_data:
    LOGGER.debug(f'{resource["self_link"]} outside perimeter')
    LOGGER.debug([
        resources['organization'], resources['folders'],
        resources['projects:number']
    ])
    return
  resource.update(parent_data)
  # gets and calls the resource-level handler for type specific attributes
  func = globals().get(f'_handle_{resource_name}')
  if not callable(func):
    raise SystemExit(f'specialized function missing for {resource_name}')
  extra_attrs = func(resource, attrs)
  if not extra_attrs:
    return
  resource.update(extra_attrs)
  return Resource(resource_name, resource['self_link'], resource)