---
policy: /ce/ca/azure/cosmos-db/idle-cosmosdb-account
logic: /ce/ca/azure/cosmos-db/idle-cosmosdb-account/prod.logic.yaml
executionTime: 2025-05-10T12:02:38.172718882Z
rows:
  - id: 1
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__capabilities__c').contains('EnableServerless')
      actual: extract('CA10__capabilities__c').contains('EnableServerless')
    runtimeError: {}
  - id: 2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__monitorTotalRequestUnitsSum30Day__c') == number(0.0)
      actual: extract('CA10__monitorTotalRequestUnitsSum30Day__c') == number(0.0)
    runtimeError: {}
  - id: 3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/cosmos-db/idle-cosmosdb-account/policy.yaml
    md5Hash: DB5D6CFED96B744E6F3EEC035BBCC5BF
    content: "---\nnames:\n  full: \"Azure Cosmos DB Account has zero Total Request\
      \ Units\"\n  contextual: \"Account has zero Total Request Units\"\ndescription:\
      \ >\n  Consider deleting Cosmos DB Account or switching to the serverless option\
      \ when \n  the account has no Total Request Units over the past 30 days.\ntype:\
      \ \"COMPLIANCE_POLICY\"\ncategories:\n  - \"COST\"\nframeworkMappings:\n  -\
      \ \"/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction\"\
      \n  "
  - path: /ce/ca/azure/cosmos-db/idle-cosmosdb-account/prod.logic.yaml
    md5Hash: 7731EB6BBF409D9C8CD589C1C318C029
    content: "---\ninputType: \"CA10__CaAzureCosmosDbAccount__c\"\ntestData:\n  -\
      \ file: \"test-data.json\"\nimportExtracts:\n  - file: /types/CA10__CaAzureCosmosDbAccount__c/object.extracts.yaml\n\
      conditions:\n  - status: \"INAPPLICABLE\"\n    currentStateMessage: \"Azure\
      \ Cosmos DB serverless account type.\"\n    check:\n      CONTAINS:\n      \
      \  arg:\n          EXTRACT: \"CA10__capabilities__c\"\n        substring:\n\
      \          TEXT: \"EnableServerless\"\n  - status: \"INCOMPLIANT\"\n    currentStateMessage:\
      \ \"The Cosmos DB Account has zero Total Request Units for the \n      past\
      \ 30 days.\"\n    remediationMessage: \"Consider deleting the Cosmos DB Account\
      \ or switching to \n      the serverless option.\"\n    check:\n      IS_EQUAL:\n\
      \        left:\n          EXTRACT: \"CA10__monitorTotalRequestUnitsSum30Day__c\"\
      \n        right:\n          NUMBER: 0.0\notherwise:\n  status: \"COMPLIANT\"\
      \n  currentStateMessage: \"The Cosmos DB Account has Total Request Units for\
      \ the past 30 days.\"\n"
  - path: /types/CA10__CaAzureCosmosDbAccount__c/object.extracts.yaml
    md5Hash: D218FD427CEE53CFCE79E554F5C4662A
    content: "---\nextracts:\n# Values: Enabled, Disabled. Not Nullable. Can't have\
      \ no access, retrieved via Microsoft.DocumentDB/databaseAccounts\n  - name:\
      \ \"CA10__virtualNetworkFilterState__c\"\n    value: \n      FIELD:\n      \
      \  path: \"CA10__virtualNetworkFilterState__c\"\n        undeterminedIf:\n \
      \         isEmpty: \"Corrupted Data. Cosmos DB Account Virtual Network Filter\
      \ cannot be empty.\"\n  - name: \"CA10__monitorTotalRequestUnitsSum30Day__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__monitorTotalRequestUnitsSum30Day__c\"\
      \n  - name: \"CA10__capabilities__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__capabilities__c\""
  - path: /ce/ca/azure/cosmos-db/idle-cosmosdb-account/test-data.json
    md5Hash: 7294C126F2B3A03C5E099BD0EDD5E413
    content: |-
      [
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__capabilities__c').contains('EnableServerless')",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-04-15T14:48:00Z"
          },
          "Id": "1",
          "CA10__disappearanceTime__c": null,
          "CA10__capabilities__c": "EnableMongo\nDisableRateLimitingResponses\nEnableServerless",
          "CA10__monitorTotalRequestUnitsSum30Day__c": 179.8095238095238
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "extract('CA10__monitorTotalRequestUnitsSum30Day__c') == number(0.0)",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-04-15T14:48:00Z"
          },
          "Id": "2",
          "CA10__disappearanceTime__c": null,
          "CA10__capabilities__c": "",
          "CA10__monitorTotalRequestUnitsSum30Day__c": 0
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-04-15T14:48:00Z"
          },
          "Id": "3",
          "CA10__disappearanceTime__c": null,
          "CA10__capabilities__c": "",
          "CA10__monitorTotalRequestUnitsSum30Day__c": 44.38095238095238
        }
      ]
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" : "1",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__capabilities__c').contains('EnableServerless')",
      "runtimeError" : null
    }
  }, {
    "Id" : "2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__monitorTotalRequestUnitsSum30Day__c') == number(0.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureCosmosDbAccount__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__capabilities__c STRING,
      CA10__monitorTotalRequestUnitsSum30Day__c FLOAT64,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "1",
    "CA10__capabilities__c" : "EnableMongo\nDisableRateLimitingResponses\nEnableServerless",
    "CA10__monitorTotalRequestUnitsSum30Day__c" : 179.8095238095238
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "2",
    "CA10__capabilities__c" : "",
    "CA10__monitorTotalRequestUnitsSum30Day__c" : 0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "3",
    "CA10__capabilities__c" : "",
    "CA10__monitorTotalRequestUnitsSum30Day__c" : 44.38095238095238
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureCosmosDbAccount__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__capabilities__c STRING,
          CA10__monitorTotalRequestUnitsSum30Day__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));
          };
      }();
      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};
      }
      // condition[1], conditionIndex:[100..199]
      function extract3() { if (!this.out) { this.out = obj.CA10__capabilities__c; } return this.out; };
      references1.push('Capabilities [obj.CA10__capabilities__c]: ' + obj.CA10__capabilities__c);
      if (TextLib.contains(extract3.call(extract3), 'EnableServerless')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__capabilities__c').contains('EnableServerless')", currentStateMessage: "Azure Cosmos DB serverless account type.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__monitorTotalRequestUnitsSum30Day__c; } return this.out; };
      references1.push('Monitor: Total Request Units, 30-Day [obj.CA10__monitorTotalRequestUnitsSum30Day__c]: ' + obj.CA10__monitorTotalRequestUnitsSum30Day__c);
      if (extract6.call(extract6) == 0.0) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__monitorTotalRequestUnitsSum30Day__c') == number(0.0)", currentStateMessage: "The Cosmos DB Account has zero Total Request Units for the past 30 days.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting the Cosmos DB Account or switching to the serverless option.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "The Cosmos DB Account has Total Request Units for the past 30 days.", 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__capabilities__c AS CA10__capabilities__c,
          sObject.CA10__monitorTotalRequestUnitsSum30Day__c AS CA10__monitorTotalRequestUnitsSum30Day__c,
          process_CA10__CaAzureCosmosDbAccount__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__capabilities__c AS CA10__capabilities__c,
                  sObject.CA10__monitorTotalRequestUnitsSum30Day__c AS CA10__monitorTotalRequestUnitsSum30Day__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureCosmosDbAccount__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
