---
policy: /ce/ca/google/compute-engine/commitment-expires-in-60-days
logic: /ce/ca/google/compute-engine/commitment-expires-in-60-days/prod.logic.yaml
executionTime: 2025-10-01T12:49:58.362918491Z
generationMs: 56
executionMs: 772
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') != 'ACTIVE'
      actual: extract('CA10G1__status__c') != 'ACTIVE'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10G1__endTimestamp__c').withinNextDays(60)
      actual: extract('CA10G1__endTimestamp__c').withinNextDays(60)
    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-expires-in-60-days/policy.yaml
    md5Hash: 5A4EE8C413613BFC1556B9122E156E9E
    content: |-
      ---
      names:
        full: "Google GCE Commitment expires in the next 60 days"
        contextual: "Commitment expires in the next 60 days"
      description: "This policy identifies Google GCE Commitments that are set to expire\
        \ within the next 60 days to help with proactive renewal and cost management."
      categories:
        - "COST"
      type: "COMPLIANCE_POLICY"
      frameworkMappings:
        - "/frameworks/cloudaware/cost-efficiency-and-optimization/resource-optimization"
  - path: /ce/ca/google/compute-engine/commitment-expires-in-60-days/prod.logic.yaml
    md5Hash: 37EE24DE0D82D2CE8748C2DD30FCDCEF
    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 active.\"\n    check:\n      NOT_EQUAL:\n        left:\n          EXTRACT:\
      \ \"CA10G1__status__c\"\n        right: \n          TEXT: \"ACTIVE\"\n  - status:\
      \ \"INCOMPLIANT\"\n    currentStateMessage: \"The commitment expires in 60 days\
      \ or less.\"\n    remediationMessage: \"Consider renewing the expiring commitment\
      \ to maintain cost savings.\"\n    check:\n      IS_WITHIN_NEXT_DAYS:\n    \
      \    offsetDays: 60\n        arg:\n          EXTRACT: CA10G1__endTimestamp__c\n\
      otherwise:\n  status: \"COMPLIANT\"\n  currentStateMessage: \"The commitment\
      \ is not expiring within the next 60 days.\"\n"
  - path: /ce/ca/google/compute-engine/commitment-expires-in-60-days/test-data.json
    md5Hash: 40A1DAF465373261CA88C44492587B5B
    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') != 'ACTIVE'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test2",
              "CA10G1__disappearanceTime__c": null,
              "CA10G1__status__c": "EXPIRED",
              "CA10G1__endTimestamp__c": "2025-06-19T07:00:00Z"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10G1__endTimestamp__c').withinNextDays(60)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test3",
              "CA10G1__disappearanceTime__c": null,
              "CA10G1__status__c": "ACTIVE",
              "CA10G1__endTimestamp__c": "2025-10-12T07:00:00Z"
          },
          {
              "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": "ACTIVE",
              "CA10G1__endTimestamp__c": "2025-11-24T08: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') != 'ACTIVE'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10G1__endTimestamp__c').withinNextDays(60)",
      "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" : "EXPIRED",
    "CA10G1__endTimestamp__c" : new Date("2025-06-19T07:00:00Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test3",
    "CA10G1__status__c" : "ACTIVE",
    "CA10G1__endTimestamp__c" : new Date("2025-10-12T07:00:00Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test4",
    "CA10G1__status__c" : "ACTIVE",
    "CA10G1__endTimestamp__c" : new Date("2025-11-24T08: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 today = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime();
      var todayPlus61Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (61 * 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), 'ACTIVE')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10G1__status__c') != 'ACTIVE'", currentStateMessage: "The commitment is not active.", 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() >= today && extract6.call(extract6).getTime() < todayPlus61Day) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10G1__endTimestamp__c').withinNextDays(60)", currentStateMessage: "The commitment expires in 60 days or less.", currentStateReferences: references1.join('\n'), remediation: "Consider renewing the expiring commitment to maintain cost savings.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "The commitment is not expiring within the next 60 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.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;
