---
policy: /ce/ca/google/compute-engine/snapshot-90-days-old
logic: /ce/ca/google/compute-engine/snapshot-90-days-old/prod.logic.yaml
executionTime: 2025-10-01T12:50:19.849692263Z
generationMs: 68
executionMs: 2804
rows:
  - id: '001'
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10__disappearanceTime__c)
      actual: isDisappeared(CA10__disappearanceTime__c)
    runtimeError: {}
  - id: '002'
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__creationDate__c').beyondLastDays(90)
      actual: extract('CA10__creationDate__c').beyondLastDays(90)
    runtimeError: {}
  - id: '003'
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: '004'
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__status__c') != 'READY'
      actual: extract('CA10__status__c') != 'READY'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/compute-engine/snapshot-90-days-old/policy.yaml
    md5Hash: 009C4CA78CB38B15F235409FB02688CE
    content: "---\nnames:\n  full: \"Google GCE Snapshot is 90 days old or more\"\n\
      \  contextual: \"Snapshot is 90 days old or more\"\ndescription: \"Consider\
      \ deleting or archiving Google GCE Snapshots that \n  are 90 days old or more.\"\
      \ntype: \"COMPLIANCE_POLICY\"\ncategories:\n  - \"COST\"\nframeworkMappings:\n\
      \  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction\""
  - path: /ce/ca/google/compute-engine/snapshot-90-days-old/prod.logic.yaml
    md5Hash: E970143D85B3A189F0F72B9D6186C1E9
    content: |-
      ---
      inputType: "CA10__CaGoogleGceSnapshot__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaGoogleGceSnapshot__c/object.extracts.yaml
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The snapshot is not ready."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__status__c"
              right:
                TEXT: "READY"
        - status: "INCOMPLIANT"
          currentStateMessage: "The GCE Snapshot is 90 days old or more."
          remediationMessage: "Consider deleting or archiving the snapshot."
          check:
            IS_BEYOND_LAST_DAYS:
              offsetDays: 90
              arg:
                EXTRACT: "CA10__creationDate__c"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The GCE Snapshot is less than 90 days old."
  - path: /ce/ca/google/compute-engine/snapshot-90-days-old/test-data.json
    md5Hash: 4DF96E57E5F36B8F54AD005C51AC137F
    content: |
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-08-27T06:22:09Z" },
          "Id": "001",
          "CA10__disappearanceTime__c": "2025-08-17T08:34:44Z",
          "CA10__status__c": "READY",
          "CA10__creationDate__c": "2025-08-03T08:27:09Z"
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "extract('CA10__creationDate__c').beyondLastDays(90)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-08-27T06:22:09Z" },
          "Id": "002",
          "CA10__disappearanceTime__c": null,
          "CA10__status__c": "READY",
          "CA10__creationDate__c": "2023-02-08T12:36:24Z"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-08-27T06:22:09Z" },
          "Id": "003",
          "CA10__disappearanceTime__c": null,
          "CA10__status__c": "READY",
          "CA10__creationDate__c": "2025-08-13T08:27:09Z"
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__status__c') != 'READY'",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-08-27T06:22:09Z" },
          "Id": "004",
          "CA10__disappearanceTime__c": null,
          "CA10__status__c": "ERROR",
          "CA10__creationDate__c": "2023-02-08T12:36:24Z"
        }
      ]
  - path: /types/CA10__CaGoogleGceSnapshot__c/object.extracts.yaml
    md5Hash: 02A113C76DBDC4F3BA8EB7177F18AF4D
    content: "---\nextracts:\n  - name: \"CA10__creationDate__c\"\n    value: \n \
      \     FIELD:\n        path: \"CA10__creationDate__c\"\n        undeterminedIf:\n\
      \          isEmpty: \"Corrupted data. Snapshot creation date cannot not be empty.\"\
      \n  - name: \"CA10__status__c\"\n    value: \n      FIELD:\n        path: \"\
      CA10__status__c\"\n        undeterminedIf:\n          isEmpty: \"Corrupted data.\
      \ Snapshot Status cannot not be empty.\"\n"
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" : "001",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "002",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__creationDate__c').beyondLastDays(90)",
      "runtimeError" : null
    }
  }, {
    "Id" : "003",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "004",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__status__c') != 'READY'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleGceSnapshot__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__status__c STRING,
      CA10__creationDate__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-08-27T06:22:09Z")
    },
    "Id" : "001",
    "CA10__disappearanceTime__c" : new Date("2025-08-17T08:34:44Z"),
    "CA10__status__c" : "READY",
    "CA10__creationDate__c" : new Date("2025-08-03T08:27:09Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-08-27T06:22:09Z")
    },
    "Id" : "002",
    "CA10__status__c" : "READY",
    "CA10__creationDate__c" : new Date("2023-02-08T12:36:24Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-08-27T06:22:09Z")
    },
    "Id" : "003",
    "CA10__status__c" : "READY",
    "CA10__creationDate__c" : new Date("2025-08-13T08:27:09Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-08-27T06:22:09Z")
    },
    "Id" : "004",
    "CA10__status__c" : "ERROR",
    "CA10__creationDate__c" : new Date("2023-02-08T12:36:24Z")
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleGceSnapshot__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__status__c STRING,
          CA10__creationDate__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 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));
          };
          this.containsAll = function(arg, substrings) {
              if (substrings == null || substrings.length === 0) return false;
              let normalizedArg = this.normalize(arg);
              return substrings.every(sub => normalizedArg.includes(this.normalize(sub)));
          };
          this.containsAny = function(arg, substrings) {
              if (substrings == null || substrings.length === 0) return false;
              let normalizedArg = this.normalize(arg);
              return substrings.some(sub => normalizedArg.includes(this.normalize(sub)));
          };
      }();
      var IsEmptyLib = new function () {
          this.simpleIsEmpty = function(arg) {
              return arg == null;
          };
          this.simpleIsNotEmpty = function(arg) {
              return arg != null;
          };
      }();
      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 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__status__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__status__c.isEmpty()", currentStateMessage: "Corrupted data. Snapshot Status cannot not be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__status__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Status [obj.CA10__status__c]: ' + obj.CA10__status__c);
      try {
          if (TextLib.notEqual(extract3.call(extract3), 'READY')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__status__c') != 'READY'", currentStateMessage: "The snapshot is not ready.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[2], conditionIndex:[200..299]
      function fieldChecked7() {
          if (IsEmptyLib.simpleIsEmpty(obj.CA10__creationDate__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "CA10__creationDate__c.isEmpty()", currentStateMessage: "Corrupted data. Snapshot creation date cannot not be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__creationDate__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      references1.push('Creation Date [obj.CA10__creationDate__c]: ' + obj.CA10__creationDate__c);
      try {
          if (extract6.call(extract6) != null && extract6.call(extract6).getTime() < todayMinus90Day) {
              return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__creationDate__c').beyondLastDays(90)", currentStateMessage: "The GCE Snapshot is 90 days old or more.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting or archiving the snapshot.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "The GCE 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__status__c AS CA10__status__c,
          sObject.CA10__creationDate__c AS CA10__creationDate__c,
          process_CA10__CaGoogleGceSnapshot__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__status__c AS CA10__status__c,
                  sObject.CA10__creationDate__c AS CA10__creationDate__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleGceSnapshot__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
