---
policy: /ce/ca/azure/virtual-machine/managed-disk-snapshot-90-days-old
logic: /ce/ca/azure/virtual-machine/managed-disk-snapshot-90-days-old/prod.logic.yaml
executionTime: 2025-05-10T12:04:08.450042426Z
rows:
  - id: test1
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__timeCreated__c').beyondLastDays(90)
      actual: extract('CA10__timeCreated__c').beyondLastDays(90)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/virtual-machine/managed-disk-snapshot-90-days-old/policy.yaml
    md5Hash: 58877510C47C8CAA7A453662B68358BD
    content: "---\nnames:\n  full: \"Azure Managed Disk Snapshot is 90 days old or\
      \ more\"\n  contextual: \"Managed Disk Snapshot is 90 days old or more\"\ndescription:\
      \ \"Consider deleting or archiving Azure Managed Disk Snapshots that \n  are\
      \ 90 days old or more.\"\ncategories:\n  - \"COST\"\ntype: \"COMPLIANCE_POLICY\"\
      \nframeworkMappings:\n  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction\"\
      \n"
  - path: /ce/ca/azure/virtual-machine/managed-disk-snapshot-90-days-old/prod.logic.yaml
    md5Hash: 959BA221A759484C96375F9E235F1AE8
    content: |-
      ---
      inputType: "CA10__CaAzureSnapshot__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAzureSnapshot__c/object.extracts.yaml
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "The Managed Disk Snapshot is 90 days old or more."
          remediationMessage: "Consider deleting or archiving the snapshot."
          check:
            IS_BEYOND_LAST_DAYS:
              offsetDays: 90
              arg:
                EXTRACT: "CA10__timeCreated__c"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The Managed Disk Snapshot is less than 90 days old."
  - path: /ce/ca/azure/virtual-machine/managed-disk-snapshot-90-days-old/test-data.json
    md5Hash: 2A20C1A4FDBD57D2E4A62CCBF54DA5F0
    content: |-
      [
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__timeCreated__c').beyondLastDays(90)",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-27T10:40:33Z"
          },
          "Id": "test1",
          "CA10__disappearanceTime__c": null,
          "CA10__timeCreated__c": "2020-01-03T14:40:22Z"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-27T10:40:33Z"
          },
          "Id": "test2",
          "CA10__disappearanceTime__c": null,
          "CA10__timeCreated__c": "2025-03-03T14:13:44Z"
        }
      ]
  - path: /types/CA10__CaAzureSnapshot__c/object.extracts.yaml
    md5Hash: 877368E2D05FE2543FB8900327D662EC
    content: "---\nextracts:\n# Date/time\n  - name: \"CA10__timeCreated__c\"\n  \
      \  value: \n      FIELD:\n        path: \"CA10__timeCreated__c\"\n# The snapshots\
      \ sku name. Can be Standard_LRS, Premium_LRS, or Standard_ZRS.\n  - name: \"\
      CA10__diskSkuName__c\"\n    value: \n      FIELD:\n        path: \"CA10__diskSkuName__c\"\
      \n        undeterminedIf:\n          isEmpty: \"Corrupted data. SKU name cannot\
      \ 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" : "test1",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__timeCreated__c').beyondLastDays(90)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureSnapshot__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__timeCreated__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-03-27T10:40:33Z")
    },
    "Id" : "test1",
    "CA10__timeCreated__c" : new Date("2020-01-03T14:40:22Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-03-27T10:40:33Z")
    },
    "Id" : "test2",
    "CA10__timeCreated__c" : new Date("2025-03-03T14:13:44Z")
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureSnapshot__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__timeCreated__c TIMESTAMP
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var todayMinus90Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-90 * 86400000);
      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__timeCreated__c; } return this.out; };
      references1.push('Time Created [obj.CA10__timeCreated__c]: ' + obj.CA10__timeCreated__c);
      if (extract3.call(extract3) != null && extract3.call(extract3).getTime() < todayMinus90Day) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__timeCreated__c').beyondLastDays(90)", currentStateMessage: "The Managed Disk Snapshot is 90 days old or more.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting or archiving the snapshot.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "The Managed Disk Snapshot is less than 90 days old.", 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__timeCreated__c AS CA10__timeCreated__c,
          process_CA10__CaAzureSnapshot__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__timeCreated__c AS CA10__timeCreated__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureSnapshot__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
