---
policy: /ce/ca/google/compute-engine/commitment-expired-in-last-30-days
logic: /ce/ca/google/compute-engine/commitment-expired-in-last-30-days/prod.logic.yaml
executionTime: 2025-10-01T12:49:57.499738735Z
generationMs: 53
executionMs: 670
rows:
  - id: test1
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10G1__disappearanceTime__c)
      actual: isDisappeared(CA10G1__disappearanceTime__c)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10G1__status__c') != 'EXPIRED'
      actual: extract('CA10G1__status__c') != 'EXPIRED'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10G1__endTimestamp__c').withinLastDays(30)
      actual: extract('CA10G1__endTimestamp__c').withinLastDays(30)
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/compute-engine/commitment-expired-in-last-30-days/policy.yaml
    md5Hash: 7DC62973FB02549BEF6386706C3C63B5
    content: |-
      ---
      names:
        full: "Google GCE Commitment expired in the last 30 days"
        contextual: "Commitment expired in the last 30 days"
      description: "This policy identifies Google GCE Commitments that have expired\
        \ within the last 30 days to alert teams of potential unplanned increases in cloud spending and verify that the expirations were intentional."
      categories:
        - "COST"
      type: "BEST_PRACTICE"
      frameworkMappings:
        - "/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction"
  - path: /ce/ca/google/compute-engine/commitment-expired-in-last-30-days/prod.logic.yaml
    md5Hash: AE0411BD1B7DD386E6C0813995E86881
    content: "---\ninputType: \"CA10G1__CaGoogleGceCommitment__c\"\ntestData:\n  -\
      \ file: test-data.json\nimportExtracts:\n  - file: /types/CA10G1__CaGoogleGceCommitment__c/object.extracts.yaml\n\
      conditions:\n  - status: \"INAPPLICABLE\"\n    currentStateMessage: \"The commitment\
      \ is not expired.\"\n    check:\n      NOT_EQUAL:\n        left:\n         \
      \ EXTRACT: \"CA10G1__status__c\"\n        right: \n          TEXT: \"EXPIRED\"\
      \n  - status: \"INCOMPLIANT\"\n    currentStateMessage: \"The commitment has\
      \ expired within the last 30 days.\"\n    remediationMessage: \"Review the expired\
      \ commitment to determine if a new one should be purchased to maintain cost\
      \ savings.\"\n    check:\n      IS_WITHIN_LAST_DAYS:\n        offsetDays: 30\n\
      \        arg:\n          EXTRACT: \"CA10G1__endTimestamp__c\"\notherwise:\n\
      \  status: \"COMPLIANT\"\n  currentStateMessage: \"The commitment expired more\
      \ than 30 days ago.\""
  - path: /ce/ca/google/compute-engine/commitment-expired-in-last-30-days/test-data.json
    md5Hash: ACEC75A6A6699FF51629CC950A5D8C19
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10G1__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test1",
              "CA10G1__disappearanceTime__c": "2025-09-21T23:29:57Z",
              "CA10G1__status__c": "ACTIVE",
              "CA10G1__endTimestamp__c": "2025-10-12T07:00:00Z"
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10G1__status__c') != 'EXPIRED'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test2",
              "CA10G1__disappearanceTime__c": null,
              "CA10G1__status__c": "ACTIVE",
              "CA10G1__endTimestamp__c": "2025-10-12T07:00:00Z"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10G1__endTimestamp__c').withinLastDays(30)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test3",
              "CA10G1__disappearanceTime__c": null,
              "CA10G1__status__c": "EXPIRED",
              "CA10G1__endTimestamp__c": "2025-09-20T23:29:57Z"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test4",
              "CA10G1__disappearanceTime__c": null,
              "CA10G1__status__c": "EXPIRED",
              "CA10G1__endTimestamp__c": "2025-06-19T07:00:00Z"
          }
      ]
  - path: /types/CA10G1__CaGoogleGceCommitment__c/object.extracts.yaml
    md5Hash: 73094F2229D28C8F9B6A97CDA40D5630
    content: "---\nextracts:\n# Values: NOT_YET_ACTIVE | ACTIVE | EXPIRED. Not nullable.\
      \ Can't have no access, retrieved via compute.regionCommitments.aggregatedList\n\
      \  - name: \"CA10G1__status__c\"\n    value: \n      FIELD:\n        path: \"\
      CA10G1__status__c\"\n# Not nullable. Can't have no access, retrieved via compute.regionCommitments.aggregatedList\n\
      \  - name: \"CA10G1__endTimestamp__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10G1__endTimestamp__c\"\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" : "test1",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10G1__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10G1__status__c') != 'EXPIRED'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10G1__endTimestamp__c').withinLastDays(30)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10G1__CaGoogleGceCommitment__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10G1__disappearanceTime__c TIMESTAMP,
      CA10G1__status__c STRING,
      CA10G1__endTimestamp__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test1",
    "CA10G1__disappearanceTime__c" : new Date("2025-09-21T23:29:57Z"),
    "CA10G1__status__c" : "ACTIVE",
    "CA10G1__endTimestamp__c" : new Date("2025-10-12T07:00:00Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test2",
    "CA10G1__status__c" : "ACTIVE",
    "CA10G1__endTimestamp__c" : new Date("2025-10-12T07:00:00Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test3",
    "CA10G1__status__c" : "EXPIRED",
    "CA10G1__endTimestamp__c" : new Date("2025-09-20T23:29:57Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test4",
    "CA10G1__status__c" : "EXPIRED",
    "CA10G1__endTimestamp__c" : new Date("2025-06-19T07:00:00Z")
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10G1__CaGoogleGceCommitment__c(
      obj STRUCT<
          Id STRING,
          CA10G1__disappearanceTime__c TIMESTAMP,
          CA10G1__status__c STRING,
          CA10G1__endTimestamp__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 todayMinus30Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-30 * 86400000);
      var todayPlus1Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (1 * 86400000);
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Google [CA10G1__disappearanceTime__c]: ' + obj.CA10G1__disappearanceTime__c);
      if (obj.CA10G1__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10G1__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.CA10G1__status__c; } return this.out; };
      references1.push('Status [obj.CA10G1__status__c]: ' + obj.CA10G1__status__c);
      if (TextLib.notEqual(extract3.call(extract3), 'EXPIRED')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10G1__status__c') != 'EXPIRED'", currentStateMessage: "The commitment is not expired.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10G1__endTimestamp__c; } return this.out; };
      references1.push('End Timestamp [obj.CA10G1__endTimestamp__c]: ' + obj.CA10G1__endTimestamp__c);
      if (extract6.call(extract6) != null && extract6.call(extract6).getTime() >= todayMinus30Day && extract6.call(extract6).getTime() < todayPlus1Day) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10G1__endTimestamp__c').withinLastDays(30)", currentStateMessage: "The commitment has expired within the last 30 days.", currentStateReferences: references1.join('\n'), remediation: "Review the expired commitment to determine if a new one should be purchased to maintain cost savings.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "The commitment expired more than 30 days ago.", 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.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
          sObject.CA10G1__status__c AS CA10G1__status__c,
          sObject.CA10G1__endTimestamp__c AS CA10G1__endTimestamp__c,
          process_CA10G1__CaGoogleGceCommitment__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
                  sObject.CA10G1__status__c AS CA10G1__status__c,
                  sObject.CA10G1__endTimestamp__c AS CA10G1__endTimestamp__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10G1__CaGoogleGceCommitment__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
