---
policy: /ce/ca/aws/ec2/elastic-ip-unused
logic: /ce/ca/aws/ec2/elastic-ip-unused/prod.logic.yaml
executionTime: 2025-10-01T12:44:56.968177851Z
generationMs: 57
executionMs: 639
rows:
  - id: '001'
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__associationId__c').isEmpty()
      actual: extract('CA10__associationId__c').isEmpty()
    runtimeError: {}
  - id: '002'
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/ec2/elastic-ip-unused/policy.yaml
    md5Hash: 327AB5F17E16596B740A0F0F38B667F7
    content: "---\nnames:\n  full: AWS EC2 Elastic IP is unused\n  contextual: Elastic\
      \ IP is unused\ndescription: >\n  Identify and release unused Elastic IP addresses\
      \ to avoid unnecessary charges. \n  Elastic IPs that are allocated but not associated\
      \ with a running resource incur costs.\ntype: \"COMPLIANCE_POLICY\"\ncategories:\n\
      \  - \"COST\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction\""
  - path: /ce/ca/aws/ec2/elastic-ip-unused/prod.logic.yaml
    md5Hash: B6B81FBB1AC077C1FDABF3E4A9762AB5
    content: "---\ninputType: \"CA10__CaAwsElasticIp__c\"\nimportExtracts:\n  - file:\
      \ \"/types/CA10__CaAwsElasticIp__c/object.extracts.yaml\"\ntestData:\n  - file:\
      \ 'test-data.json'\nconditions:\n  - status: INCOMPLIANT\n    currentStateMessage:\
      \ \"The Elastic IP address is not associated with any EC2 instance or network\
      \ interface.\"\n    remediationMessage: \"Consider releasing the unused Elastic\
      \ IP address.\"    \n    check:\n      IS_EMPTY:\n        arg:\n          EXTRACT:\
      \ CA10__associationId__c\notherwise:\n  status: \"COMPLIANT\"\n  currentStateMessage:\
      \ \"The Elastic Ip is used.\""
  - path: /ce/ca/aws/ec2/elastic-ip-unused/test-data.json
    md5Hash: E3FE86128D26B54B7E67B935D70AD22F
    content: |
      [
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__associationId__c').isEmpty()",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-09-01T16:12:36Z" },
          "Id": "001",
          "CA10__disappearanceTime__c": null,
          "CA10__associationId__c": ""
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-09-01T16:12:36Z" },
          "Id": "002",
          "CA10__disappearanceTime__c": null,
          "CA10__associationId__c": "associationId"
        }
      ]
  - path: /types/CA10__CaAwsElasticIp__c/object.extracts.yaml
    md5Hash: F70A967FFE6C3B6C7D48474507307B38
    content: |-
      ---
      extracts:
      # Nullable.
        - name: "CA10__associationId__c"
          value:
            FIELD:
              path: "CA10__associationId__c"
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" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__associationId__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "002",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsElasticIp__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__associationId__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-09-01T16:12:36Z")
    },
    "Id" : "001",
    "CA10__associationId__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-01T16:12:36Z")
    },
    "Id" : "002",
    "CA10__associationId__c" : "associationId"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsElasticIp__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__associationId__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));
          };
          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 references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted from AWS [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 extract3() { if (!this.out) { this.out = obj.CA10__associationId__c; } return this.out; };
      references1.push('Association ID [obj.CA10__associationId__c]: ' + obj.CA10__associationId__c);
      if (TextLib.isEmpty(extract3.call(extract3))) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__associationId__c').isEmpty()", currentStateMessage: "The Elastic IP address is not associated with any EC2 instance or network interface.", currentStateReferences: references1.join('\n'), remediation: "Consider releasing the unused Elastic IP address.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "The Elastic Ip is used.", 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__associationId__c AS CA10__associationId__c,
          process_CA10__CaAwsElasticIp__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__associationId__c AS CA10__associationId__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsElasticIp__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
