---
policy: /ce/ca/google/sql/instance-idle
logic: /ce/ca/google/sql/instance-idle/prod.logic.yaml
executionTime: 2025-10-01T12:50:56.831650714Z
generationMs: 57
executionMs: 836
rows:
  - id: '001'
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10__disappearanceTime__c)
      actual: isDisappeared(CA10__disappearanceTime__c)
    runtimeError: {}
  - id: '002'
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__state__c') != 'RUNNABLE'
      actual: extract('CA10__state__c') != 'RUNNABLE'
    runtimeError: {}
  - id: '003'
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: not(extract('CA10__createTime__c').beyondLastDays(30))
      actual: not(extract('CA10__createTime__c').beyondLastDays(30))
    runtimeError: {}
  - id: '004'
    match: true
    status:
      expected: UNDETERMINED
      actual: UNDETERMINED
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10__cpuUtilizationAvg30Day__c').isEmpty() || extract('CA10__cloudSqlConnectionsAvg30Day__c').isEmpty()
      actual: extract('CA10__cpuUtilizationAvg30Day__c').isEmpty() || extract('CA10__cloudSqlConnectionsAvg30Day__c').isEmpty()
    runtimeError: {}
  - id: '005'
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 499
      actual: 499
    conditionText:
      expected: extract('CA10__cpuUtilizationAvg30Day__c') < number(5.0) && extract('CA10__cloudSqlConnectionsAvg30Day__c')
        <= number(0.0)
      actual: extract('CA10__cpuUtilizationAvg30Day__c') < number(5.0) && extract('CA10__cloudSqlConnectionsAvg30Day__c')
        <= number(0.0)
    runtimeError: {}
  - id: '006'
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 500
      actual: 500
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/sql/instance-idle/policy.yaml
    md5Hash: 2FFB42BBE1BE8497C0ADA236AFDD2B9E
    content: |-
      ---
      names:
        full: Google Cloud SQL Instance is idle
        contextual: Instance is idle
      description: >
        Identify idle Google Cloud SQL Instances to optimize costs by stopping,
        terminating, or downscaling instances with low utilization. Instances are
        considered idle if their average CPU utilization is below 5% and they have an
        average of zero connections over a 30-day period.
      type: "COMPLIANCE_POLICY"
      categories:
        - "COST"
        - "PERFORMANCE"
      frameworkMappings:
        - "/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction"
        - "/frameworks/cloudaware/resource-performance/workload-efficiency"
  - path: /ce/ca/google/sql/instance-idle/prod.logic.yaml
    md5Hash: 926E5ABFDCE1F224DD8DD5BEF312325B
    content: |
      ---
      inputType: "CA10__CaGoogleSqlInstance__c"
      importExtracts:
        - file: "/types/CA10__CaGoogleSqlInstance__c/object.extracts.yaml"
      testData:
        - file: 'test-data.json'
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The Instance is not running."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__state__c"
              right:
                TEXT: "RUNNABLE"
        - status: "INAPPLICABLE"
          currentStateMessage: "The instance has been created less than 30 days ago."
          check:
            NOT:
              arg:
                IS_BEYOND_LAST_DAYS:
                  offsetDays: 30
                  arg:
                    EXTRACT: "CA10__createTime__c"
        - status: "UNDETERMINED"
          currentStateMessage: "There is not enough data in the CMDB to identify if the instance is idle."
          check:
            OR:
              args:
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__cpuUtilizationAvg30Day__c"
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__cloudSqlConnectionsAvg30Day__c"
        - status: "INCOMPLIANT"
          currentStateMessage: "The instance is idle with Average CPU Utilization < 5% and\
            \ zero Average Connections."
          remediationMessage: "Consider stopping, terminating, or rightsizing the instance."
          check:
            AND:
              args:
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__cpuUtilizationAvg30Day__c
                    right:
                      NUMBER: 5.0
                - LESS_THAN_EQUAL:
                    left:
                      EXTRACT: CA10__cloudSqlConnectionsAvg30Day__c
                    right:
                      NUMBER: 0.0
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The instance is not idle."
  - path: /ce/ca/google/sql/instance-idle/test-data.json
    md5Hash: 1CABC87D1CB36B45588BAFD984DCC500
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-22T23:38:09Z"
              },
              "Id": "001",
              "CA10__disappearanceTime__c": "2025-09-22T23:38:09Z",
              "CA10__state__c": "RUNNABLE",
              "CA10__createTime__c": "2025-09-12T17:07:12Z",
              "CA10__cpuUtilizationAvg30Day__c": 1,
              "CA10__cloudSqlConnectionsAvg30Day__c": 0
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__state__c') != 'RUNNABLE'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-22T23:38:09Z"
              },
              "Id": "002",
              "CA10__disappearanceTime__c": null,
              "CA10__state__c": "FAILED",
              "CA10__createTime__c": "2025-08-18T18:26:19Z",
              "CA10__cpuUtilizationAvg30Day__c": 1,
              "CA10__cloudSqlConnectionsAvg30Day__c": 0
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "299",
                  "conditionText": "not(extract('CA10__createTime__c').beyondLastDays(30))",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-22T23:38:09Z"
              },
              "Id": "003",
              "CA10__disappearanceTime__c": null,
              "CA10__state__c": "RUNNABLE",
              "CA10__createTime__c": "2025-09-18T18:26:19Z",
              "CA10__cpuUtilizationAvg30Day__c": 1,
              "CA10__cloudSqlConnectionsAvg30Day__c": 0
          },
          {
              "expectedResult": {
                  "status": "UNDETERMINED",
                  "conditionIndex": "399",
                  "conditionText": "extract('CA10__cpuUtilizationAvg30Day__c').isEmpty() || extract('CA10__cloudSqlConnectionsAvg30Day__c').isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-22T23:38:09Z"
              },
              "Id": "004",
              "CA10__disappearanceTime__c": null,
              "CA10__state__c": "RUNNABLE",
              "CA10__createTime__c": "2025-08-18T18:26:19Z",
              "CA10__cpuUtilizationAvg30Day__c": null,
              "CA10__cloudSqlConnectionsAvg30Day__c": null
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "499",
                  "conditionText": "extract('CA10__cpuUtilizationAvg30Day__c') < number(5.0) && extract('CA10__cloudSqlConnectionsAvg30Day__c') <= number(0.0)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-22T23:38:09Z"
              },
              "Id": "005",
              "CA10__disappearanceTime__c": null,
              "CA10__state__c": "RUNNABLE",
              "CA10__createTime__c": "2025-08-18T18:26:19Z",
              "CA10__cpuUtilizationAvg30Day__c": 1,
              "CA10__cloudSqlConnectionsAvg30Day__c": 0
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "500",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-22T23:38:09Z"
              },
              "Id": "006",
              "CA10__disappearanceTime__c": null,
              "CA10__state__c": "RUNNABLE",
              "CA10__createTime__c": "2025-03-24T14:34:15Z",
              "CA10__cpuUtilizationAvg30Day__c": 5,
              "CA10__cloudSqlConnectionsAvg30Day__c": 1
          }
      ]
  - path: /types/CA10__CaGoogleSqlInstance__c/object.extracts.yaml
    md5Hash: 8DD86EFB2FFA06BD1E9BC0452FF2EC8A
    content: "---\nextracts:\n  - name: \"CA10__databaseVersion__c\"\n    value: \n\
      \      FIELD:\n        path: \"CA10__databaseVersion__c\"\n        undeterminedIf:\n\
      \          isEmpty:  \"Database version should not be empty\"\n  - name: \"\
      CA10__ipv4Enabled__c\"\n    value: \n      FIELD:\n        path: \"CA10__ipv4Enabled__c\"\
      \n        undeterminedIf:\n          isEmpty:  \"Ipv4Enabled should not be empty\"\
      \n  - name: \"caJsonFrom__databaseFlagsJson__c\"\n    value: \n      JSON_FROM:\n\
      \        arg:\n          FIELD:\n            path: CA10__databaseFlagsJson__c\n\
      \            returnType: BYTES\n        undeterminedIf:\n          isInvalid:\
      \ \"Database Flags JSON is invalid.\"\n          isEmpty: \"Database Flags JSON\
      \ is empty.\"\n  - name: \"caJsonText__databaseFlagsSkipShowDatabaseFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'skip_show_database'].value[] |\
      \ [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"caJsonText__databaseFlagsLocalInfileFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'local_infile'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n  - name: \"caJsonText__databaseFlagsLogErrorVerbosityFlagValue__c\"\n   \
      \ value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression:  \"to_string([?name == 'log_error_verbosity'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"caJsonText__databaseFlagsLogConnectionsFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_connections'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n  - name: \"caJsonText__databaseFlagsLogDisconnectionsFlagValue__c\"\n   \
      \ value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression:  \"to_string([?name == 'log_disconnections'].value[] |\
      \ [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"caJsonText__databaseFlagsLogStatementFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_statement'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n  - name: \"caJsonText__databaseFlagsLogMinMessagesFlagValue__c\"\n    value:\
      \ \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_min_messages'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n  - name: \"caJsonText__databaseFlagsLogMinErrorStatementFlagValue__c\"\n\
      \    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_min_error_statement'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"caJsonText__databaseFlagsLogMinDurationStatementFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_min_duration_statement'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"caJsonText__databaseFlagsCloudsqlEnablePgauditFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'cloudsql.enable_pgaudit'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"caJsonText__databaseFlagsExternalScriptsEnabledFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'external scripts enabled'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"caJsonText__databaseFlagsCrossDbOwnershipChainingFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'cross db ownership chaining'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caJsonText__databaseFlagsUserConnectionsFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'user connections'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caJsonText__databaseFlagsUserOptionsFlagValue__c\"\n    value:\
      \ \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'user options'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n  - name: \"caJsonText__databaseFlagsRemoteAccessFlagValue__c\"\n    value:\
      \ \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'remote access'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n  - name: \"caJsonText__databaseFlags3625FlagValue__c\"\n    value: \n   \
      \   JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == '3625'].value[] | [0])\"\n     \
      \   undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n  - name: \"caJsonText__databaseFlagsContainedDatabaseAuthenticationFlagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'contained database authentication'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n  - name: \"CA10__authorizedNetworksJson__c\"\n    value: \n\
      \      FIELD:\n        path: \"CA10__authorizedNetworksJson__c\"\n        returnType:\
      \ BYTES\n  - name: \"caJsonFrom__authorizedNetworksJson__c\"\n    value: \n\
      \      JSON_FROM:\n        arg: \n          EXTRACT: \"CA10__authorizedNetworksJson__c\"\
      \n        undeterminedIf:\n          isInvalid: \"Authorized Networks JSON is\
      \ invalid.\"  \n  - name: \"caJsonText__authorizedNetworksJsonValue__c\"\n \
      \   value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__authorizedNetworksJson__c\"\
      \n        expression: \"to_string([].value)\"\n        undeterminedIf:\n   \
      \       evaluationError: \"The JSON query was failed.\"\n          resultTypeMismatch:\
      \ \"The JSON query did not return a text.\"\n  - name: \"CA10__instanceType__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__instanceType__c\"\n     \
      \   undeterminedIf:\n          isEmpty: \"Instance type should not be empty\"\
      \n  - name: \"CA10__backendType__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__backendType__c\"\n        undeterminedIf:\n          isEmpty: \"Backend\
      \ type should not be empty\"\n  - name: \"CA10__backupEnabled__c\"\n    value:\
      \ \n      FIELD:\n        path: \"CA10__backupEnabled__c\"\n        undeterminedIf:\n\
      \          isEmpty: \"Backup enabled should not be empty\"\n  - name: \"CA10__backupStartTime__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__backupStartTime__c\"\n  -\
      \ name: \"CA10__sslMode__c\"\n    value: \n      FIELD:\n        path: \"CA10__sslMode__c\"\
      \n  - name: \"CA10__createTime__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__createTime__c\"\n  - name: \"CA10__state__c\"\n    value: \n     \
      \ FIELD:\n        path: \"CA10__state__c\"\n  - name: \"CA10__cpuUtilizationAvg30Day__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__cpuUtilizationAvg30Day__c\"\
      \n  - name: \"CA10__memoryUtilizationAvg30Day__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__memoryUtilizationAvg30Day__c\"\n  - name: \"CA10__diskReadIoOpsRate30Day__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__diskReadIoOpsRate30Day__c\"\
      \n  - name: \"CA10__diskWriteIoOpsRate30Day__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__diskWriteIoOpsRate30Day__c\"\n  - name: \"CA10__cloudSqlConnectionsAvg30Day__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__cloudSqlConnectionsAvg30Day__c\"\
      \n# Text\n  - name: \"CA10__regionName__c\"\n    value: \n      FIELD:\n   \
      \     path: \"CA10__regionName__c\"\n"
script: |-
  CREATE TEMP FUNCTION mock_ExpectedResult()
  RETURNS ARRAY<STRUCT<
          Id STRING,
          expectedResult STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, runtimeError STRING>
      >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "Id" : "001",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "002",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__state__c') != 'RUNNABLE'",
      "runtimeError" : null
    }
  }, {
    "Id" : "003",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "299",
      "conditionText" : "not(extract('CA10__createTime__c').beyondLastDays(30))",
      "runtimeError" : null
    }
  }, {
    "Id" : "004",
    "expectedResult" : {
      "status" : "UNDETERMINED",
      "conditionIndex" : "399",
      "conditionText" : "extract('CA10__cpuUtilizationAvg30Day__c').isEmpty() || extract('CA10__cloudSqlConnectionsAvg30Day__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "005",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "499",
      "conditionText" : "extract('CA10__cpuUtilizationAvg30Day__c') < number(5.0) && extract('CA10__cloudSqlConnectionsAvg30Day__c') <= number(0.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "006",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "500",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleSqlInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__state__c STRING,
      CA10__createTime__c TIMESTAMP,
      CA10__cpuUtilizationAvg30Day__c FLOAT64,
      CA10__cloudSqlConnectionsAvg30Day__c FLOAT64,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-09-22T23:38:09Z")
    },
    "Id" : "001",
    "CA10__disappearanceTime__c" : new Date("2025-09-22T23:38:09Z"),
    "CA10__state__c" : "RUNNABLE",
    "CA10__createTime__c" : new Date("2025-09-12T17:07:12Z"),
    "CA10__cpuUtilizationAvg30Day__c" : 1,
    "CA10__cloudSqlConnectionsAvg30Day__c" : 0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-22T23:38:09Z")
    },
    "Id" : "002",
    "CA10__state__c" : "FAILED",
    "CA10__createTime__c" : new Date("2025-08-18T18:26:19Z"),
    "CA10__cpuUtilizationAvg30Day__c" : 1,
    "CA10__cloudSqlConnectionsAvg30Day__c" : 0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-22T23:38:09Z")
    },
    "Id" : "003",
    "CA10__state__c" : "RUNNABLE",
    "CA10__createTime__c" : new Date("2025-09-18T18:26:19Z"),
    "CA10__cpuUtilizationAvg30Day__c" : 1,
    "CA10__cloudSqlConnectionsAvg30Day__c" : 0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-22T23:38:09Z")
    },
    "Id" : "004",
    "CA10__state__c" : "RUNNABLE",
    "CA10__createTime__c" : new Date("2025-08-18T18:26:19Z"),
    "CA10__cpuUtilizationAvg30Day__c" : null,
    "CA10__cloudSqlConnectionsAvg30Day__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-22T23:38:09Z")
    },
    "Id" : "005",
    "CA10__state__c" : "RUNNABLE",
    "CA10__createTime__c" : new Date("2025-08-18T18:26:19Z"),
    "CA10__cpuUtilizationAvg30Day__c" : 1,
    "CA10__cloudSqlConnectionsAvg30Day__c" : 0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-22T23:38:09Z")
    },
    "Id" : "006",
    "CA10__state__c" : "RUNNABLE",
    "CA10__createTime__c" : new Date("2025-03-24T14:34:15Z"),
    "CA10__cpuUtilizationAvg30Day__c" : 5,
    "CA10__cloudSqlConnectionsAvg30Day__c" : 1
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleSqlInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__state__c STRING,
          CA10__createTime__c TIMESTAMP,
          CA10__cpuUtilizationAvg30Day__c FLOAT64,
          CA10__cloudSqlConnectionsAvg30Day__c FLOAT64
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var TextLib = new function () {
          this.normalize = function(arg) {
              return arg == null ? '' : arg.replace(/\s+/g, ' ').trim().toLowerCase();
          };
          this.isEmpty = function(arg) {
              return this.normalize(arg) == '';
          };
          this.isNotEmpty = function(arg) {
              return this.normalize(arg) != '';
          };
          this.equal = function(left, right) {
              return this.normalize(left) == this.normalize(right);
          };
          this.notEqual = function(left, right) {
              return this.normalize(left) != this.normalize(right);
          };
          this.startsWith = function(arg, substring) {
              return this.normalize(arg).startsWith(this.normalize(substring));
          };
          this.endsWith = function(arg, substring) {
              return this.normalize(arg).endsWith(this.normalize(substring));
          };
          this.contains = function(arg, substring) {
              return this.normalize(arg).includes(this.normalize(substring));
          };
          this.containsAll = function(arg, substrings) {
              if (substrings == null || substrings.length === 0) return false;
              let normalizedArg = this.normalize(arg);
              return substrings.every(sub => normalizedArg.includes(this.normalize(sub)));
          };
          this.containsAny = function(arg, substrings) {
              if (substrings == null || substrings.length === 0) return false;
              let normalizedArg = this.normalize(arg);
              return substrings.some(sub => normalizedArg.includes(this.normalize(sub)));
          };
      }();
      var todayMinus30Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-30 * 86400000);
      var IsEmptyLib = new function () {
          this.simpleIsEmpty = function(arg) {
              return arg == null;
          };
          this.simpleIsNotEmpty = function(arg) {
              return arg != null;
          };
      }();
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Google [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c);
      if (obj.CA10__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[1], conditionIndex:[100..199]
      function extract3() { if (!this.out) { this.out = obj.CA10__state__c; } return this.out; };
      references1.push('State [obj.CA10__state__c]: ' + obj.CA10__state__c);
      if (TextLib.notEqual(extract3.call(extract3), 'RUNNABLE')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__state__c') != 'RUNNABLE'", currentStateMessage: "The Instance is not running.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__createTime__c; } return this.out; };
      references1.push('Create Time [obj.CA10__createTime__c]: ' + obj.CA10__createTime__c);
      if (!(extract6.call(extract6) != null && extract6.call(extract6).getTime() < todayMinus30Day)) {
          return {status: 'INAPPLICABLE', conditionIndex: 299, conditionText: "not(extract('CA10__createTime__c').beyondLastDays(30))", currentStateMessage: "The instance has been created less than 30 days ago.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      function extract10() { if (!this.out) { this.out = obj.CA10__cpuUtilizationAvg30Day__c; } return this.out; };
      function extract13() { if (!this.out) { this.out = obj.CA10__cloudSqlConnectionsAvg30Day__c; } return this.out; };
      references1.push('Metrics: CPU Utilization, 30-Day [obj.CA10__cpuUtilizationAvg30Day__c]: ' + obj.CA10__cpuUtilizationAvg30Day__c);
      references1.push('Metrics: Cloud SQL Connections, 30-Day [obj.CA10__cloudSqlConnectionsAvg30Day__c]: ' + obj.CA10__cloudSqlConnectionsAvg30Day__c);
      if (IsEmptyLib.simpleIsEmpty(extract10.call(extract10)) || IsEmptyLib.simpleIsEmpty(extract13.call(extract13))) {
          return {status: 'UNDETERMINED', conditionIndex: 399, conditionText: "extract('CA10__cpuUtilizationAvg30Day__c').isEmpty() || extract('CA10__cloudSqlConnectionsAvg30Day__c').isEmpty()", currentStateMessage: "There is not enough data in the CMDB to identify if the instance is idle.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[4], conditionIndex:[400..499]
      function extract17() { if (!this.out) { this.out = obj.CA10__cpuUtilizationAvg30Day__c; } return this.out; };
      function extract21() { if (!this.out) { this.out = obj.CA10__cloudSqlConnectionsAvg30Day__c; } return this.out; };
      if ((extract17.call(extract17) != null && 5.0 != null && extract17.call(extract17) < 5.0) && (extract21.call(extract21) != null && 0.0 != null && extract21.call(extract21) <= 0.0)) {
          return {status: 'INCOMPLIANT', conditionIndex: 499, conditionText: "extract('CA10__cpuUtilizationAvg30Day__c') < number(5.0) && extract('CA10__cloudSqlConnectionsAvg30Day__c') <= number(0.0)", currentStateMessage: "The instance is idle with Average CPU Utilization < 5% and zero Average Connections.", currentStateReferences: references1.join('\n'), remediation: "Consider stopping, terminating, or rightsizing the instance.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 500, conditionText: "otherwise", currentStateMessage: "The instance is not idle.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;


  SELECT
    expectedResult.Id as Id,
    IF (
      IFNULL(expectedResult.expectedResult.status, '') = IFNULL(sObject.result.status, '')
      AND IFNULL(expectedResult.expectedResult.conditionIndex, -1) = IFNULL(sObject.result.conditionIndex, -1)
      AND IFNULL(expectedResult.expectedResult.conditionText, '') = IFNULL(sObject.result.conditionText, '')
      AND IFNULL(expectedResult.expectedResult.runtimeError, '') = IFNULL(sObject.result.runtimeError, ''),
      "MATCH",
      "FAIL"
    ) as match,
    expectedResult.expectedResult.status as expectedStatus, sObject.result.status as actualStatus,
    expectedResult.expectedResult.conditionIndex as expectedConditionIndex, sObject.result.conditionIndex as actualConditionIndex,
    expectedResult.expectedResult.conditionText as expectedConditionText, sObject.result.conditionText as actualConditionText,
    expectedResult.expectedResult.runtimeError as expectedRuntimeError, sObject.result.runtimeError as actualRuntimeError
  FROM UNNEST(mock_ExpectedResult()) expectedResult
  LEFT JOIN (
      SELECT
          sObject.Id AS Id,
          sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__state__c AS CA10__state__c,
          sObject.CA10__createTime__c AS CA10__createTime__c,
          sObject.CA10__cpuUtilizationAvg30Day__c AS CA10__cpuUtilizationAvg30Day__c,
          sObject.CA10__cloudSqlConnectionsAvg30Day__c AS CA10__cloudSqlConnectionsAvg30Day__c,
          process_CA10__CaGoogleSqlInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__state__c AS CA10__state__c,
                  sObject.CA10__createTime__c AS CA10__createTime__c,
                  sObject.CA10__cpuUtilizationAvg30Day__c AS CA10__cpuUtilizationAvg30Day__c,
                  sObject.CA10__cloudSqlConnectionsAvg30Day__c AS CA10__cloudSqlConnectionsAvg30Day__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleSqlInstance__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
