---
policy: /ce/ca/azure/virtual-machine/premium-managed-disk-snapshot
logic: /ce/ca/azure/virtual-machine/premium-managed-disk-snapshot/prod.logic.yaml
executionTime: 2025-05-10T12:04:10.015816179Z
rows:
  - id: 1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: 2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__diskSkuName__c').contains('Premium')
      actual: extract('CA10__diskSkuName__c').contains('Premium')
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/virtual-machine/premium-managed-disk-snapshot/policy.yaml
    md5Hash: 629867EBD8BDC018288D8E4CB11FF11E
    content: "---\nnames:\n  full: \"Azure Managed Disk Snapshot is stored on Premium\
      \ SSDs Managed Disk storage\"\n  contextual: \"Managed Disk Snapshot is stored\
      \ on Premium SSDs Managed Disk storage\"\ndescription: >\n  Ensure Azure Managed\
      \ Disk snapshots are stored on Standard storage instead of \n  Premium SSDs\
      \ Managed Disk storage.\ncategories:\n  - \"COST\"\ntype: \"COMPLIANCE_POLICY\"\
      \nframeworkMappings:\n  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction\"\
      \n"
  - path: /ce/ca/azure/virtual-machine/premium-managed-disk-snapshot/prod.logic.yaml
    md5Hash: 800EE2AC3E22E4A1BFEDDF1119312CC8
    content: |-
      ---
      inputType: "CA10__CaAzureSnapshot__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAzureSnapshot__c/object.extracts.yaml
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "The snapshot is stored on Premium SSDs Managed Disk storage."
          remediationMessage: "Consider changing the storage SKU to Standard storage."
          check:
            CONTAINS:
              arg:
                EXTRACT: "CA10__diskSkuName__c"
              substring:
                TEXT: "Premium"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The snapshot is stored on Standard storage."
  - 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.\""
  - path: /ce/ca/azure/virtual-machine/premium-managed-disk-snapshot/test-data.json
    md5Hash: 3AF46FA020E378D1D4194C25638ABF93
    content: |
      [
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "1",
          "CA10__disappearanceTime__c": null,
          "CA10__diskSkuName__c": "Standard_LRS"
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__diskSkuName__c').contains('Premium')",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "2",
          "CA10__disappearanceTime__c": null,
          "CA10__diskSkuName__c": "Premium_LRS"
        }
      ]
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" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__diskSkuName__c').contains('Premium')",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureSnapshot__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__diskSkuName__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "1",
    "CA10__diskSkuName__c" : "Standard_LRS"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "2",
    "CA10__diskSkuName__c" : "Premium_LRS"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureSnapshot__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__diskSkuName__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 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 fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__diskSkuName__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__diskSkuName__c.isEmpty()", currentStateMessage: "Corrupted data. SKU name cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__diskSkuName__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Disk Sku Name [obj.CA10__diskSkuName__c]: ' + obj.CA10__diskSkuName__c);
      try {
          if (TextLib.contains(extract3.call(extract3), 'Premium')) {
              return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__diskSkuName__c').contains('Premium')", currentStateMessage: "The snapshot is stored on Premium SSDs Managed Disk storage.", currentStateReferences: references1.join('\n'), remediation: "Consider changing the storage SKU to Standard storage.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "The snapshot is stored on Standard storage.", 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__diskSkuName__c AS CA10__diskSkuName__c,
          process_CA10__CaAzureSnapshot__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__diskSkuName__c AS CA10__diskSkuName__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureSnapshot__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
