def run_rule()

in gcpdiag/lint/gce/warn_2022_010_resource_availability.py [0:0]


def run_rule(context: models.Context, report: lint.LintReportRuleInterface):
  project = crm.get_project(context.project_id)
  # skip entire rule if gce api is disabled
  if not apis.is_enabled(context.project_id, 'compute'):
    report.add_skipped(project, 'compute api is disabled')
    return

  # skip entire rule if logging disabled
  if not apis.is_enabled(context.project_id, 'logging'):
    report.add_skipped(project, 'logging api is disabled')
    return

  # To hold affected zones
  stockout_zones = set()
  if logs_by_project.get(context.project_id) and \
     logs_by_project[context.project_id].entries:
    for entry in logs_by_project[context.project_id].entries:

      msg = get_path(entry, ('protoPayload', 'status', 'message'), default='')
      method = get_path(entry, ('protoPayload', 'methodName'), default='')

      if (entry['severity'] == 'ERROR' and METHOD_NAME_MATCH in method) and \
         (STOCKOUT_MESSAGE in msg) or (INSUFFICIENT_RESOURCES in msg) or \
             (RESOURCE_EXHAUSTED in msg):
        zone = get_path(entry, ('resource', 'labels', 'zone'), default='')
        if zone:
          stockout_zones.add(zone)

    if stockout_zones:
      report.add_failed(project, \
          f'Resource exhaustion in zones: {stockout_zones}')
    else:
      report.add_ok(project)