---
policy: /ce/ca/azure/virtual-network/virtual-network-gateway-without-connection
logic: /ce/ca/azure/virtual-network/virtual-network-gateway-without-connection/prod.logic.yaml
executionTime: 2025-05-10T12:04:36.910084775Z
rows:
  - id: test_1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10__Azure_Virtual_Network_Gateway_Connects__r.has(COMPLIANT) ||
        CA10__Azure_Virtual_Network_Gateway_Connects1__r.has(COMPLIANT)
      actual: CA10__Azure_Virtual_Network_Gateway_Connects__r.has(COMPLIANT) || CA10__Azure_Virtual_Network_Gateway_Connects1__r.has(COMPLIANT)
    runtimeError: {}
  - id: test_2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/virtual-network/virtual-network-gateway-without-connection/policy.yaml
    md5Hash: 475FFC40CB12455DBB9C54CB79C3D79C
    content: "---\nnames:\n  full: \"Azure Virtual Network Gateway has no connections\"\
      \n  contextual: \"Virtual Network Gateway has no connections\"\ndescription:\
      \ >\n  Consider deleting Network Gateways that do not have any active connections\
      \ \n  configured. Unused gateways incur unnecessary costs.\ncategories:\n  -\
      \ \"COST\"\ntype: \"COMPLIANCE_POLICY\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction\"\
      \n"
  - path: /ce/ca/azure/virtual-network/virtual-network-gateway-without-connection/prod.logic.yaml
    md5Hash: 470B704718B9BBD1FEDC64510BCB37EA
    content: |-
      ---
      inputType: "CA10__CaAzureVirtualNetworkGateway__c"
      testData:
        - file: test-data.json
      conditions:
        - status: "COMPLIANT"
          currentStateMessage: "Azure Virtual Network Gateway has at least one connection."
          check:
            OR:
              args:
                - RELATED_LIST_HAS:
                    status: "COMPLIANT"
                    relationshipName: "CA10__Azure_Virtual_Network_Gateway_Connects__r"
                - RELATED_LIST_HAS:
                    status: "COMPLIANT"
                    relationshipName: "CA10__Azure_Virtual_Network_Gateway_Connects1__r"
      otherwise:
        status: "INCOMPLIANT"
        currentStateMessage: "Azure Virtual Network Gateway has no connections."
        remediationMessage: "Consider deleting Virtual Network Gateway."
      relatedLists:
        - relationshipName: "CA10__Azure_Virtual_Network_Gateway_Connects__r"
          conditions: []
          otherwise:
            status: "COMPLIANT"
            currentStateMessage: "Connection exists."
        - relationshipName: "CA10__Azure_Virtual_Network_Gateway_Connects1__r"
          conditions: []
          otherwise:
            status: "COMPLIANT"
            currentStateMessage: "Connection exists."
  - path: /ce/ca/azure/virtual-network/virtual-network-gateway-without-connection/test-data.json
    md5Hash: FE2EF7CDFA7CC3A6C363BC642C9E908B
    content: |
      [
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "199",
            "conditionText": "CA10__Azure_Virtual_Network_Gateway_Connects__r.has(COMPLIANT) || CA10__Azure_Virtual_Network_Gateway_Connects1__r.has(COMPLIANT)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-10T23:53:50Z" },
          "Id": "test_1",
          "CA10__disappearanceTime__c": null,
          "CA10__Azure_Virtual_Network_Gateway_Connects__r": [
            {
              "Id": "test1_1",
              "CA10__disappearanceTime__c": null,
              "CA10__virtualNetworkGateway__c": "test_1"
            }
          ],
          "CA10__Azure_Virtual_Network_Gateway_Connects1__r": []
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-10T23:53:50Z" },
          "Id": "test_2",
          "CA10__disappearanceTime__c": null,
          "CA10__Azure_Virtual_Network_Gateway_Connects__r": [],
          "CA10__Azure_Virtual_Network_Gateway_Connects1__r": []
        }
      ]
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" : "test_1",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "CA10__Azure_Virtual_Network_Gateway_Connects__r.has(COMPLIANT) || CA10__Azure_Virtual_Network_Gateway_Connects1__r.has(COMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test_2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureVirtualNetworkGateway__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-04-10T23:53:50Z")
    },
    "Id" : "test_1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-10T23:53:50Z")
    },
    "Id" : "test_2"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureVirtualNetworkGatewayConnect__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__virtualNetworkGateway__c STRING,
      CA10__virtualNetworkGateway2__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-04-10T23:53:50Z")
    },
    "Id" : "test1_1",
    "CA10__virtualNetworkGateway__c" : "test_1"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureVirtualNetworkGateway__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__Azure_Virtual_Network_Gateway_Connects__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__virtualNetworkGateway__c STRING,
              result STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
          >>,
          CA10__Azure_Virtual_Network_Gateway_Connects1__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__virtualNetworkGateway2__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 references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Azure [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};
      }
      var count_CA10__Azure_Virtual_Network_Gateway_Connects__r_COMPLIANT2 = 0;
      if (obj.CA10__Azure_Virtual_Network_Gateway_Connects__r != null) {
          for (var i4 = 0; i4 < obj.CA10__Azure_Virtual_Network_Gateway_Connects__r.length; i4++) {
              if (obj.CA10__Azure_Virtual_Network_Gateway_Connects__r[i4].result.status == 'COMPLIANT') {
                  count_CA10__Azure_Virtual_Network_Gateway_Connects__r_COMPLIANT2 += 1;
              }
          }
      }
      var count_CA10__Azure_Virtual_Network_Gateway_Connects1__r_COMPLIANT3 = 0;
      if (obj.CA10__Azure_Virtual_Network_Gateway_Connects1__r != null) {
          for (var i5 = 0; i5 < obj.CA10__Azure_Virtual_Network_Gateway_Connects1__r.length; i5++) {
              if (obj.CA10__Azure_Virtual_Network_Gateway_Connects1__r[i5].result.status == 'COMPLIANT') {
                  count_CA10__Azure_Virtual_Network_Gateway_Connects1__r_COMPLIANT3 += 1;
              }
          }
      }
      // condition[1], conditionIndex:[100..199]
      references1.push('Related list [CA10__Azure_Virtual_Network_Gateway_Connects__r] ' + (count_CA10__Azure_Virtual_Network_Gateway_Connects__r_COMPLIANT2 > 0 ? 'has' : 'does not have') + ' objects in COMPLIANT status');
      references1.push('Related list [CA10__Azure_Virtual_Network_Gateway_Connects1__r] ' + (count_CA10__Azure_Virtual_Network_Gateway_Connects1__r_COMPLIANT3 > 0 ? 'has' : 'does not have') + ' objects in COMPLIANT status');
      if (count_CA10__Azure_Virtual_Network_Gateway_Connects__r_COMPLIANT2 > 0 || count_CA10__Azure_Virtual_Network_Gateway_Connects1__r_COMPLIANT3 > 0) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "CA10__Azure_Virtual_Network_Gateway_Connects__r.has(COMPLIANT) || CA10__Azure_Virtual_Network_Gateway_Connects1__r.has(COMPLIANT)", currentStateMessage: "Azure Virtual Network Gateway has at least one connection.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "Azure Virtual Network Gateway has no connections.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting Virtual Network Gateway.", runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__Azure_Virtual_Network_Gateway_Connects__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__virtualNetworkGateway__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 references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Azure [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};
      }
      return {status: 'COMPLIANT', conditionIndex: 100, conditionText: "otherwise", currentStateMessage: "Connection exists.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__Azure_Virtual_Network_Gateway_Connects1__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__virtualNetworkGateway2__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 references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Azure [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};
      }
      return {status: 'COMPLIANT', conditionIndex: 100, conditionText: "otherwise", currentStateMessage: "Connection exists.", 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,
          `CA10__Azure_Virtual_Network_Gateway_Connects__r`.arr AS CA10__Azure_Virtual_Network_Gateway_Connects__r,
          `CA10__Azure_Virtual_Network_Gateway_Connects1__r`.arr AS CA10__Azure_Virtual_Network_Gateway_Connects1__r,
          process_CA10__CaAzureVirtualNetworkGateway__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  `CA10__Azure_Virtual_Network_Gateway_Connects__r`.arr AS CA10__Azure_Virtual_Network_Gateway_Connects__r,
                  `CA10__Azure_Virtual_Network_Gateway_Connects1__r`.arr AS CA10__Azure_Virtual_Network_Gateway_Connects1__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureVirtualNetworkGateway__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10__virtualNetworkGateway__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__virtualNetworkGateway__c AS CA10__virtualNetworkGateway__c,
                      process_CA10__Azure_Virtual_Network_Gateway_Connects__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__virtualNetworkGateway__c AS CA10__virtualNetworkGateway__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaAzureVirtualNetworkGatewayConnect__c()) AS sObject
          GROUP BY sObject.CA10__virtualNetworkGateway__c
      ) AS `CA10__Azure_Virtual_Network_Gateway_Connects__r`
          ON sObject.Id = `CA10__Azure_Virtual_Network_Gateway_Connects__r`.CA10__virtualNetworkGateway__c
      LEFT JOIN (
          SELECT
              sObject.CA10__virtualNetworkGateway2__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__virtualNetworkGateway2__c AS CA10__virtualNetworkGateway2__c,
                      process_CA10__Azure_Virtual_Network_Gateway_Connects1__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__virtualNetworkGateway2__c AS CA10__virtualNetworkGateway2__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaAzureVirtualNetworkGatewayConnect__c()) AS sObject
          GROUP BY sObject.CA10__virtualNetworkGateway2__c
      ) AS `CA10__Azure_Virtual_Network_Gateway_Connects1__r`
          ON sObject.Id = `CA10__Azure_Virtual_Network_Gateway_Connects1__r`.CA10__virtualNetworkGateway2__c
  ) sObject ON sObject.Id = expectedResult.Id;
