---
policy: /ce/ca/google/sql/sql-instance-with-public-ip
logic: /ce/ca/google/sql/sql-instance-with-public-ip/prod.logic.yaml
executionTime: 2025-04-24T23:47:31.339127901Z
rows:
  - id: a8LVU1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: a8LVU20
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)
      actual: CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: a8LVU2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)
      actual: CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: a8LVU3
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__instanceType__c') != 'CLOUD_SQL_INSTANCE' || extract('CA10__backendType__c')
        != 'SECOND_GEN'
      actual: extract('CA10__instanceType__c') != 'CLOUD_SQL_INSTANCE' || extract('CA10__backendType__c')
        != 'SECOND_GEN'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/sql/sql-instance-with-public-ip/policy.yaml
    md5Hash: 557E38559C01C2B78FC51DA74E384E27
    content: "---\nnames: \n  full: \"Google Cloud SQL Instance has public IP addresses\"\
      \n  contextual: \"SQL Instance has public IP addresses\"\ndescription: \"It\
      \ is recommended to configure Second Generation Sql instance to use private\
      \ IPs instead of public IPs.\"\ntype: COMPLIANCE_POLICY\ncategories:\n  - \"\
      SECURITY\"\nframeworkMappings:\n  - /frameworks/cis-gcp-v3.0.0/06/06\n  - /frameworks/cloudaware/resource-security/public-and-anonymous-access\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/CloudSQL/sql-database-instances-with-public-ips.html\"\
      \n      name: \"Check for Cloud SQL Database Instances with Public IPs\"\n"
  - path: /ce/ca/google/sql/sql-instance-with-public-ip/prod.logic.yaml
    md5Hash: 96CBEB657B3F385608BE470FCEA5D34F
    content: "---\n\ninputType: \"CA10__CaGoogleSqlInstance__c\"\ntestData:\n  - file:\
      \ test-data.json\nimportExtracts:\n  - file: /types/CA10__CaGoogleSqlInstance__c/object.extracts.yaml\n\
      conditions:\n  - status: \"INAPPLICABLE\"\n    currentStateMessage: \"Only instance\
      \ of type 'CLOUD_SQL_INSTANCE' with backendType: 'SECOND_GEN' can be configured\
      \ to have a private IP address.\"\n    check:\n      OR:\n        args:\n  \
      \        - NOT_EQUAL:\n              left:\n                EXTRACT: \"CA10__instanceType__c\"\
      \n              right:\n                TEXT: \"CLOUD_SQL_INSTANCE\"\n     \
      \     - NOT_EQUAL:\n              left:\n                EXTRACT: \"CA10__backendType__c\"\
      \n              right:\n                TEXT: \"SECOND_GEN\"               \
      \ \n  - status: \"INCOMPLIANT\"\n    currentStateMessage: \"The SQL database\
      \ instance has public IPs.\"\n    remediationMessage: \"Remove the public IPs\
      \ and assign a private IP instead.\"\n    check:\n      RELATED_LIST_HAS:\n\
      \        status: \"INCOMPLIANT\"\n        relationshipName: \"CA10__Google_Cloud_SQL_Instance_IP_Addresses__r\"\
      \notherwise:\n  status: \"COMPLIANT\"\n  currentStateMessage: \"Cloud SQL database\
      \ instance does not have public IPs.\"\nrelatedLists:\n   - relationshipName:\
      \ \"CA10__Google_Cloud_SQL_Instance_IP_Addresses__r\"\n     importExtracts:\n\
      \       - file: /types/CA10__CaGoogleSqlInstanceIpAddress__c/object.extracts.yaml\n\
      \     conditions: \n       - status: \"INCOMPLIANT\"\n         currentStateMessage:\
      \ \"This is a public IP address.\"\n         check:\n          IS_EQUAL:\n \
      \           left:\n              EXTRACT: \"CA10__type__c\"\n            right:\n\
      \              TEXT: \"PRIMARY\"\n     otherwise:\n       status: \"COMPLIANT\"\
      \n       currentStateMessage: \"This is not a public IP address.\""
  - path: /types/CA10__CaGoogleSqlInstance__c/object.extracts.yaml
    md5Hash: CF5DA82560FA69E6A48E44B5BF123F0B
    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: \"caExtract__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\n  - name: \"caExtract_databaseFlags_skip_show_database_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_local_infile_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_log_error_verbosity_flagValue__c\"\n\
      \    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_log_connections_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_log_disconnections_flagValue__c\"\n\
      \    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_log_statement_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_log_min_messages_flagValue__c\"\n  \
      \  value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_log_min_error_statement_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_log_min_duration_statement_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n\n  - name: \"caExtract_databaseFlags_cloudsql_enable_pgaudit_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_external_scripts_enabled_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_cross_db_ownership_chaining_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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: \"caExtract_databaseFlags_user_connections_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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: \"caExtract_databaseFlags_user_options_flagValue__c\"\n    value:\
      \ \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_remote_access_flagValue__c\"\n    value:\
      \ \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_3625_flagValue__c\"\n    value: \n \
      \     JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\n  - name: \"caExtract_databaseFlags_contained_database_authentication_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__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\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: \"caJsonFrom_authorizedNetworksJson_value__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\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"
  - path: /ce/ca/google/sql/sql-instance-with-public-ip/test-data.json
    md5Hash: EB367FA4AD15720EA00664488912CD5D
    content: |
      [
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a8LVU1",
          "CA10__disappearanceTime__c": null,
          "CA10__instanceType__c": "CLOUD_SQL_INSTANCE",
          "CA10__backendType__c": "SECOND_GEN",
          "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r": [
            {
              "Id": "a8K111",
              "CA10__disappearanceTime__c": null,
              "CA10__type__c": "PRIVATE",
              "CA10__sqlInstance__c": "a8LVU1"
            }
          ]
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a8LVU20",
          "CA10__disappearanceTime__c": null,
          "CA10__instanceType__c": "CLOUD_SQL_INSTANCE",
          "CA10__backendType__c": "SECOND_GEN",
          "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r": [
            {
              "Id": "a8K2220",
              "CA10__disappearanceTime__c": null,
              "CA10__type__c": "PRIMARY",
              "CA10__sqlInstance__c": "a8LVU20"
            }
          ]
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a8LVU2",
          "CA10__disappearanceTime__c": null,
          "CA10__instanceType__c": "CLOUD_SQL_INSTANCE",
          "CA10__backendType__c": "SECOND_GEN",
          "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r": [
            {
              "Id": "a8K222",
              "CA10__disappearanceTime__c": null,
              "CA10__type__c": "PRIMARY",
              "CA10__sqlInstance__c": "a8LVU2"
            },
            {
              "Id": "a8K223",
              "CA10__disappearanceTime__c": null,
              "CA10__type__c": "PRIVATE",
              "CA10__sqlInstance__c": "a8LVU2"
            }
          ]
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__instanceType__c') != 'CLOUD_SQL_INSTANCE' || extract('CA10__backendType__c') != 'SECOND_GEN'",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a8LVU3",
          "CA10__disappearanceTime__c": null,
          "CA10__instanceType__c": "READ_REPLICA_INSTANCE",
          "CA10__backendType__c": "SECOND_GEN",
          "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r": [
            {
              "Id": "a8K333",
              "CA10__disappearanceTime__c": null,
              "CA10__type__c": "PRIVATE",
              "CA10__sqlInstance__c": "a8LVU3"
            }
          ]
        }
      ]
  - path: /types/CA10__CaGoogleSqlInstanceIpAddress__c/object.extracts.yaml
    md5Hash: 59E93B9DC62AA39B57994BA255AAB3FC
    content: "---\nextracts:\n  - name: \"CA10__type__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__type__c\"\n        undeterminedIf:\n          isEmpty:\
      \  \"Labels should not be empty\""
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" : "a8LVU1",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "a8LVU20",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "a8LVU2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "a8LVU3",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__instanceType__c') != 'CLOUD_SQL_INSTANCE' || extract('CA10__backendType__c') != 'SECOND_GEN'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleSqlInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__instanceType__c STRING,
      CA10__backendType__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8LVU1",
    "CA10__instanceType__c" : "CLOUD_SQL_INSTANCE",
    "CA10__backendType__c" : "SECOND_GEN"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8LVU20",
    "CA10__instanceType__c" : "CLOUD_SQL_INSTANCE",
    "CA10__backendType__c" : "SECOND_GEN"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8LVU2",
    "CA10__instanceType__c" : "CLOUD_SQL_INSTANCE",
    "CA10__backendType__c" : "SECOND_GEN"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8LVU3",
    "CA10__instanceType__c" : "READ_REPLICA_INSTANCE",
    "CA10__backendType__c" : "SECOND_GEN"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleSqlInstanceIpAddress__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__type__c STRING,
      CA10__sqlInstance__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8K111",
    "CA10__type__c" : "PRIVATE",
    "CA10__sqlInstance__c" : "a8LVU1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8K2220",
    "CA10__type__c" : "PRIMARY",
    "CA10__sqlInstance__c" : "a8LVU20"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8K222",
    "CA10__type__c" : "PRIMARY",
    "CA10__sqlInstance__c" : "a8LVU2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8K223",
    "CA10__type__c" : "PRIVATE",
    "CA10__sqlInstance__c" : "a8LVU2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a8K333",
    "CA10__type__c" : "PRIVATE",
    "CA10__sqlInstance__c" : "a8LVU3"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleSqlInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__instanceType__c STRING,
          CA10__backendType__c STRING,
          CA10__Google_Cloud_SQL_Instance_IP_Addresses__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__type__c STRING,
              CA10__sqlInstance__c STRING,
              result STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
          >>
      >,
      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));
          };
      }();
      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 fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__instanceType__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__instanceType__c.isEmpty()", currentStateMessage: "Instance type should not be empty", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__instanceType__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      function fieldChecked7() {
          if (TextLib.isEmpty(obj.CA10__backendType__c)) {
              throw new Error("UNDETERMINED condition:102", {cause: {status: 'UNDETERMINED', conditionIndex: 102, conditionText: "CA10__backendType__c.isEmpty()", currentStateMessage: "Backend type should not be empty", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__backendType__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      references1.push('Instance Type [obj.CA10__instanceType__c]: ' + obj.CA10__instanceType__c);
      references1.push('Backend Type [obj.CA10__backendType__c]: ' + obj.CA10__backendType__c);
      try {
          if (TextLib.notEqual(extract3.call(extract3), 'CLOUD_SQL_INSTANCE') || TextLib.notEqual(extract6.call(extract6), 'SECOND_GEN')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__instanceType__c') != 'CLOUD_SQL_INSTANCE' || extract('CA10__backendType__c') != 'SECOND_GEN'", currentStateMessage: "Only instance of type 'CLOUD_SQL_INSTANCE' with backendType: 'SECOND_GEN' can be configured to have a private IP address.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      var count_CA10__Google_Cloud_SQL_Instance_IP_Addresses__r_INCOMPLIANT8 = 0;
      if (obj.CA10__Google_Cloud_SQL_Instance_IP_Addresses__r != null) {
          for (var i9 = 0; i9 < obj.CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.length; i9++) {
              if (obj.CA10__Google_Cloud_SQL_Instance_IP_Addresses__r[i9].result.status == 'INCOMPLIANT') {
                  count_CA10__Google_Cloud_SQL_Instance_IP_Addresses__r_INCOMPLIANT8 += 1;
              }
          }
      }
      // condition[2], conditionIndex:[200..299]
      references1.push('Related list [CA10__Google_Cloud_SQL_Instance_IP_Addresses__r] ' + (count_CA10__Google_Cloud_SQL_Instance_IP_Addresses__r_INCOMPLIANT8 > 0 ? 'has' : 'does not have') + ' objects in INCOMPLIANT status');
      if (count_CA10__Google_Cloud_SQL_Instance_IP_Addresses__r_INCOMPLIANT8 > 0) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "CA10__Google_Cloud_SQL_Instance_IP_Addresses__r.has(INCOMPLIANT)", currentStateMessage: "The SQL database instance has public IPs.", currentStateReferences: references1.join('\n'), remediation: "Remove the public IPs and assign a private IP instead.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "Cloud SQL database instance does not have public IPs.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__Google_Cloud_SQL_Instance_IP_Addresses__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__type__c STRING,
          CA10__sqlInstance__c STRING
      >,
      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));
          };
      }();
      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 fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__type__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__type__c.isEmpty()", currentStateMessage: "Labels should not be empty", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__type__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Type [obj.CA10__type__c]: ' + obj.CA10__type__c);
      try {
          if (TextLib.equal(extract3.call(extract3), 'PRIMARY')) {
              return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__type__c') == 'PRIMARY'", currentStateMessage: "This is a public IP address.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "This is not a public IP address.", 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__instanceType__c AS CA10__instanceType__c,
          sObject.CA10__backendType__c AS CA10__backendType__c,
          `CA10__Google_Cloud_SQL_Instance_IP_Addresses__r`.arr AS CA10__Google_Cloud_SQL_Instance_IP_Addresses__r,
          process_CA10__CaGoogleSqlInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__instanceType__c AS CA10__instanceType__c,
                  sObject.CA10__backendType__c AS CA10__backendType__c,
                  `CA10__Google_Cloud_SQL_Instance_IP_Addresses__r`.arr AS CA10__Google_Cloud_SQL_Instance_IP_Addresses__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleSqlInstance__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10__sqlInstance__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__type__c AS CA10__type__c,
                      sObject.CA10__sqlInstance__c AS CA10__sqlInstance__c,
                      process_CA10__Google_Cloud_SQL_Instance_IP_Addresses__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__type__c AS CA10__type__c,
                              sObject.CA10__sqlInstance__c AS CA10__sqlInstance__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaGoogleSqlInstanceIpAddress__c()) AS sObject
          GROUP BY sObject.CA10__sqlInstance__c
      ) AS `CA10__Google_Cloud_SQL_Instance_IP_Addresses__r`
          ON sObject.Id = `CA10__Google_Cloud_SQL_Instance_IP_Addresses__r`.CA10__sqlInstance__c
  ) sObject ON sObject.Id = expectedResult.Id;
