---
policy: /ce/ca/azure/reservations/reservation-expires-in-60-days
logic: /ce/ca/azure/reservations/reservation-expires-in-60-days/prod.logic.yaml
executionTime: 2025-10-01T12:47:01.466697899Z
generationMs: 53
executionMs: 837
rows:
  - id: '001'
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10Z1__disappearanceTime__c)
      actual: isDisappeared(CA10Z1__disappearanceTime__c)
    runtimeError: {}
  - id: '002'
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10Z1__expiryDate__c').beyondNextDays(60)
      actual: extract('CA10Z1__expiryDate__c').beyondNextDays(60)
    runtimeError: {}
  - id: '003'
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 400
      actual: 400
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: '004'
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 400
      actual: 400
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: '005'
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10Z1__expiryDate__c').isEmpty()
      actual: extract('CA10Z1__expiryDate__c').isEmpty()
    runtimeError: {}
  - id: '006'
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10Z1__provisioningState__c') != 'Succeeded'
      actual: extract('CA10Z1__provisioningState__c') != 'Succeeded'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/reservations/reservation-expires-in-60-days/policy.yaml
    md5Hash: 47C767295779BD6699FAC69FA6519AD6
    content: |-
      ---
      names:
        full: "Azure Reservation expires in the next 60 days"
        contextual: "Reservation expires in the next 60 days"
      description: "Ensure that expiring Azure Reservations are tracked renewed or\
        \ addressed beforehand to avoid service disruptions or loss of discounts.\
        \ Proactive renewal maintains cost efficiency and ensures resources remain\
        \ covered by reservations."
      categories:
        - "COST"
      type: "COMPLIANCE_POLICY"
      frameworkMappings:
        - "/frameworks/cloudaware/cost-efficiency-and-optimization/resource-optimization"
  - path: /ce/ca/azure/reservations/reservation-expires-in-60-days/prod.logic.yaml
    md5Hash: DDA3914B6159E1345D2947E7EA4ADCF8
    content: "---\ninputType: \"CA10Z1__CaAzureReservation__c\"\ntestData:\n  - file:\
      \ test-data.json\nimportExtracts:\n  - file: /types/CA10Z1__CaAzureReservation__c/object.extracts.yaml\n\
      conditions:\n  - status: \"INAPPLICABLE\"\n    currentStateMessage: \"The Reservation\
      \ is not successfully provisioned.\"\n    check:\n      NOT_EQUAL:\n       \
      \ left:\n          EXTRACT: \"CA10Z1__provisioningState__c\"\n        right:\
      \ \n          TEXT: \"Succeeded\"\n  - status: \"INAPPLICABLE\"\n    currentStateMessage:\
      \ \"The Reservation expiry date is empty.\"\n    check:\n      IS_EMPTY:\n \
      \       arg:\n          EXTRACT: \"CA10Z1__expiryDate__c\"\n  - status: \"COMPLIANT\"\
      \n    currentStateMessage: \"The Reservation is not expiring.\"\n    check:\n\
      \      IS_BEYOND_NEXT_DAYS:\n        offsetDays: 60\n        arg:\n        \
      \  EXTRACT: \"CA10Z1__expiryDate__c\"\notherwise:\n  status: \"INCOMPLIANT\"\
      \n  currentStateMessage: \"The Reservation expires in 60 days or less.\"\n \
      \ remediationMessage: \"Consider renewing the reservation to maintain discounts\
      \ and avoid paying on-demand prices.\"\n\n"
  - path: /ce/ca/azure/reservations/reservation-expires-in-60-days/test-data.json
    md5Hash: BC71BFF06549D1AC1EB1FEBA08323799
    content: |-
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10Z1__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-07-30T19:02:37Z"
          },
          "Id": "001",
          "CA10Z1__disappearanceTime__c": "2021-03-24T00:00:00Z",
          "CA10Z1__provisioningState__c": "Succeeded",
          "CA10Z1__expiryDate__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "399",
            "conditionText": "extract('CA10Z1__expiryDate__c').beyondNextDays(60)",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-07-30T19:02:37Z"
          },
          "Id": "002",
          "CA10Z1__disappearanceTime__c": null,
          "CA10Z1__provisioningState__c": "Succeeded",
          "CA10Z1__expiryDate__c": "2025-09-30T00:00:00Z"
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "400",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-07-30T19:02:37Z"
          },
          "Id": "003",
          "CA10Z1__disappearanceTime__c": null,
          "CA10Z1__provisioningState__c": "Succeeded",
          "CA10Z1__expiryDate__c": "2025-08-11T00:00:00Z"
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "400",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-07-30T19:02:37Z"
          },
          "Id": "004",
          "CA10Z1__disappearanceTime__c": null,
          "CA10Z1__provisioningState__c": "Succeeded",
          "CA10Z1__expiryDate__c": "2021-03-24T00:00:00Z"
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "299",
            "conditionText": "extract('CA10Z1__expiryDate__c').isEmpty()",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-07-30T19:02:37Z"
          },
          "Id": "005",
          "CA10Z1__disappearanceTime__c": null,
          "CA10Z1__provisioningState__c": "Succeeded",
          "CA10Z1__expiryDate__c": null
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "extract('CA10Z1__provisioningState__c') != 'Succeeded'",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-07-30T19:02:37Z"
          },
          "Id": "006",
          "CA10Z1__disappearanceTime__c": null,
          "CA10Z1__provisioningState__c": "Expired",
          "CA10Z1__expiryDate__c": null
        }
      ]
  - path: /types/CA10Z1__CaAzureReservation__c/object.extracts.yaml
    md5Hash: BAF6C1776D15C0CD930DFF3000823EE6
    content: "---\nextracts:\n# Nullable. Date/Time.\n  - name: \"CA10Z1__expiryDate__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10Z1__expiryDate__c\"\n  - name:\
      \ \"CA10Z1__provisioningState__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10Z1__provisioningState__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" : "001",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10Z1__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "002",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "extract('CA10Z1__expiryDate__c').beyondNextDays(60)",
      "runtimeError" : null
    }
  }, {
    "Id" : "003",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "400",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "004",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "400",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "005",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10Z1__expiryDate__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "006",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10Z1__provisioningState__c') != 'Succeeded'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10Z1__CaAzureReservation__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10Z1__disappearanceTime__c TIMESTAMP,
      CA10Z1__provisioningState__c STRING,
      CA10Z1__expiryDate__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-07-30T19:02:37Z")
    },
    "Id" : "001",
    "CA10Z1__disappearanceTime__c" : new Date("2021-03-24T00:00:00Z"),
    "CA10Z1__provisioningState__c" : "Succeeded"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-30T19:02:37Z")
    },
    "Id" : "002",
    "CA10Z1__provisioningState__c" : "Succeeded",
    "CA10Z1__expiryDate__c" : new Date("2025-09-30T00:00:00Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-30T19:02:37Z")
    },
    "Id" : "003",
    "CA10Z1__provisioningState__c" : "Succeeded",
    "CA10Z1__expiryDate__c" : new Date("2025-08-11T00:00:00Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-30T19:02:37Z")
    },
    "Id" : "004",
    "CA10Z1__provisioningState__c" : "Succeeded",
    "CA10Z1__expiryDate__c" : new Date("2021-03-24T00:00:00Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-30T19:02:37Z")
    },
    "Id" : "005",
    "CA10Z1__provisioningState__c" : "Succeeded"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-30T19:02:37Z")
    },
    "Id" : "006",
    "CA10Z1__provisioningState__c" : "Expired"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10Z1__CaAzureReservation__c(
      obj STRUCT<
          Id STRING,
          CA10Z1__disappearanceTime__c TIMESTAMP,
          CA10Z1__provisioningState__c STRING,
          CA10Z1__expiryDate__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 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 Azure [CA10Z1__disappearanceTime__c]: ' + obj.CA10Z1__disappearanceTime__c);
      if (obj.CA10Z1__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10Z1__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.CA10Z1__provisioningState__c; } return this.out; };
      references1.push('Provisioning State [obj.CA10Z1__provisioningState__c]: ' + obj.CA10Z1__provisioningState__c);
      if (TextLib.notEqual(extract3.call(extract3), 'Succeeded')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10Z1__provisioningState__c') != 'Succeeded'", currentStateMessage: "The Reservation is not successfully provisioned.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10Z1__expiryDate__c; } return this.out; };
      references1.push('Expiry Date [obj.CA10Z1__expiryDate__c]: ' + obj.CA10Z1__expiryDate__c);
      if (IsEmptyLib.simpleIsEmpty(extract6.call(extract6))) {
          return {status: 'INAPPLICABLE', conditionIndex: 299, conditionText: "extract('CA10Z1__expiryDate__c').isEmpty()", currentStateMessage: "The Reservation expiry date is empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      function extract9() { if (!this.out) { this.out = obj.CA10Z1__expiryDate__c; } return this.out; };
      if (extract9.call(extract9) != null && extract9.call(extract9).getTime() >= todayPlus61Day) {
          return {status: 'COMPLIANT', conditionIndex: 399, conditionText: "extract('CA10Z1__expiryDate__c').beyondNextDays(60)", currentStateMessage: "The Reservation is not expiring.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 400, conditionText: "otherwise", currentStateMessage: "The Reservation expires in 60 days or less.", currentStateReferences: references1.join('\n'), remediation: "Consider renewing the reservation to maintain discounts and avoid paying on-demand prices.", 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.CA10Z1__disappearanceTime__c AS CA10Z1__disappearanceTime__c,
          sObject.CA10Z1__provisioningState__c AS CA10Z1__provisioningState__c,
          sObject.CA10Z1__expiryDate__c AS CA10Z1__expiryDate__c,
          process_CA10Z1__CaAzureReservation__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10Z1__disappearanceTime__c AS CA10Z1__disappearanceTime__c,
                  sObject.CA10Z1__provisioningState__c AS CA10Z1__provisioningState__c,
                  sObject.CA10Z1__expiryDate__c AS CA10Z1__expiryDate__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10Z1__CaAzureReservation__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
