---
policy: /ce/ca/aws/ec2/instance-legacy-virtualization-type
logic: /ce/ca/aws/ec2/instance-legacy-virtualization-type/prod.logic.yaml
executionTime: 2025-09-06T12:03:15.597684449Z
generationMs: 107
executionMs: 924
rows:
  - id: test1
    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: test2
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__stateName__c') != 'running'
      actual: extract('CA10__stateName__c') != 'running'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__virtualizationType__c') == 'paravirtual'
      actual: extract('CA10__virtualizationType__c') == 'paravirtual'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/ec2/instance-legacy-virtualization-type/policy.yaml
    md5Hash: F94C6168D7DCBAA20EA34D24FB972048
    content: "---\nnames:\n  full: AWS EC2 Instance uses paravirtual Virtualization\
      \ Type\n  contextual: Instance uses paravirtual Virtualization Type\ndescription:\
      \ >\n  Ensure that Amazon EC2 instances are using Hardware Virtual Machine (HVM)\
      \ \n  virtualization instead of the legacy paravirtual (PV) type to take advantage\
      \ \n  of enhanced performance and access to the latest AWS features and instance\
      \ types.\ntype: \"COMPLIANCE_POLICY\"\ncategories:\n  - \"RELIABILITY\"\n  -\
      \ \"PERFORMANCE\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/resource-reliability/infrastructure-modernization\"\
      \n  - \"/frameworks/cloudaware/resource-performance/workload-efficiency\"\n\
      \  - \"/frameworks/aws-fsbp-v1.0.0/ec2/24\""
  - path: /ce/ca/aws/ec2/instance-legacy-virtualization-type/prod.logic.yaml
    md5Hash: ACF510F82C8318E13FC734B279D677C5
    content: |-
      inputType: "CA10__CaAwsInstance__c"
      testData:
        - file: 'test-data.json'
      importExtracts:
        - file: "/types/CA10__CaAwsInstance__c/object.extracts.yaml"
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The instance is not in a running state."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__stateName__c"
              right:
                TEXT: "running"
        - status: "INCOMPLIANT"
          currentStateMessage: "The instance is using the legacy paravirtual (PV) virtualization type."
          remediationMessage: "Migrate the instance to a Hardware Virtual Machine (HVM) AMI."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__virtualizationType__c"
              right:
                TEXT: "paravirtual"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The instance is using the recommended HVM virtualization type."
  - path: /ce/ca/aws/ec2/instance-legacy-virtualization-type/test-data.json
    md5Hash: BB78817A42313A6B2C8E7E11B2F11929
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-01T17:22:51Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2025-09-01T15:42:54Z",
              "CA10__stateName__c": "terminated",
              "CA10__virtualizationType__c": "hvm"
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__stateName__c') != 'running'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-01T17:22:51Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__stateName__c": "stopped",
              "CA10__virtualizationType__c": "hvm"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-01T17:22:51Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__stateName__c": "running",
              "CA10__virtualizationType__c": "hvm"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__virtualizationType__c') == 'paravirtual'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-01T17:22:51Z"
              },
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__stateName__c": "running",
              "CA10__virtualizationType__c": "paravirtual"
          }
      ]
  - path: /types/CA10__CaAwsInstance__c/object.extracts.yaml
    md5Hash: 8F1515239F0680B895FAE3E90763836D
    content: "---\nextracts:\n# Values pending, applied. Not nullable. Can't have\
      \ no access, retrieved via ec2:DescribeInstances\n  - name: \"CA10__metadataState__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__metadataState__c\"\n    \
      \    undeterminedIf:\n          isEmpty: \"Corrupted data. EC2 Instances Metadata\
      \ State cannot be empty.\"\n# Values: optional, required. Not nullable. Can't\
      \ have no access, retrieved via ec2:DescribeInstances\n  - name: \"CA10__metadataHttpTokens__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__metadataHttpTokens__c\"\n\
      \        undeterminedIf:\n          isEmpty: \"Corrupted data. EC2 Instances\
      \ Metadata Http Token cannot be empty.\"\n  - name: CA10__iamInstanceProfileArn__c\n\
      \    value:\n      FIELD: \n        path: CA10__iamInstanceProfileArn__c\n#\
      \ Nullable. Can't have no access, retrieved via ec2:DescribeInstances\n  - name:\
      \ \"CA10__publicIpAddress__c\"\n    value: \n      FIELD:\n        path: \"\
      CA10__publicIpAddress__c\"\n# Values: enabled, disabled. Not nullable. Can't\
      \ have no access, retrieved via ec2:DescribeInstances\n  - name: \"CA10__monitoringState__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__monitoringState__c\"\n  \
      \      undeterminedIf:\n          isEmpty: \"Corrupted data. EC2 Instances Detailed\
      \ Monitoring State cannot be empty.\"\n# Values: pending | running | shutting-down\
      \ | terminated | stopping | stopped. Not nullable.\n  - name: \"CA10__stateName__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__stateName__c\"\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: \"CA10__stateName__c\"\n   \
      \         currentStateMessage: \"Unable to determine EC2 Instances State Name.\
      \ Possible permission issue with ec2:DescribeInstanceStatus\"\n# Number of Days.\
      \ Nullable. Can't have no access, retrieved via ec2:DescribeInstances\n  - name:\
      \ \"CA10__currentRunningTime__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__currentRunningTime__c\"      \n# Nullable.\n  - name: \"CA10__averageCpuTwoWeeks__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__averageCpuTwoWeeks__c\" \
      \     \n# Nullable.\n  - name: \"CA10__maxCpuTwoWeeks__c\"\n    value: \n  \
      \    FIELD:\n        path: \"CA10__maxCpuTwoWeeks__c\"      \n# Nullable.\n\
      \  - name: \"CA10__sumTwoWeekNetworkOutMb__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__sumTwoWeekNetworkOutMb__c\"      \n# Nullable.\n  - name:\
      \ \"CA10__sumTwoWeekNetworkInMb__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__sumTwoWeekNetworkInMb__c\"      \n# Values: paravirtual | hvm. Not\
      \ nullable. Can't have no access, retrieved via ec2:DescribeInstances\n  - name:\
      \ \"CA10__virtualizationType__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__virtualizationType__c\"\n        undeterminedIf:\n          isEmpty:\
      \ \"Corrupted data. EC2 Instances Virtualization Type cannot 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" : "test1",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__stateName__c') != 'running'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__virtualizationType__c') == 'paravirtual'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__stateName__c STRING,
      CA10__virtualizationType__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-09-01T17:22:51Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2025-09-01T15:42:54Z"),
    "CA10__stateName__c" : "terminated",
    "CA10__virtualizationType__c" : "hvm"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-01T17:22:51Z")
    },
    "Id" : "test2",
    "CA10__stateName__c" : "stopped",
    "CA10__virtualizationType__c" : "hvm"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-01T17:22:51Z")
    },
    "Id" : "test3",
    "CA10__stateName__c" : "running",
    "CA10__virtualizationType__c" : "hvm"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-01T17:22:51Z")
    },
    "Id" : "test4",
    "CA10__stateName__c" : "running",
    "CA10__virtualizationType__c" : "paravirtual"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__stateName__c STRING,
          CA10__virtualizationType__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 fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__stateName__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__stateName__c.delegatedTo(CA10__stateName__c).isEmpty()", currentStateMessage: "Unable to determine EC2 Instances State Name. Possible permission issue with ec2:DescribeInstanceStatus", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__stateName__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('State Name [obj.CA10__stateName__c]: ' + obj.CA10__stateName__c);
      try {
          if (TextLib.notEqual(extract3.call(extract3), 'running')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__stateName__c') != 'running'", currentStateMessage: "The instance is not in a running state.", 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 (TextLib.isEmpty(obj.CA10__virtualizationType__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "CA10__virtualizationType__c.isEmpty()", currentStateMessage: "Corrupted data. EC2 Instances Virtualization Type cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__virtualizationType__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      references1.push('Virtualization Type [obj.CA10__virtualizationType__c]: ' + obj.CA10__virtualizationType__c);
      try {
          if (TextLib.equal(extract6.call(extract6), 'paravirtual')) {
              return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__virtualizationType__c') == 'paravirtual'", currentStateMessage: "The instance is using the legacy paravirtual (PV) virtualization type.", currentStateReferences: references1.join('\n'), remediation: "Migrate the instance to a Hardware Virtual Machine (HVM) AMI.", 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 instance is using the recommended HVM virtualization type.", 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__stateName__c AS CA10__stateName__c,
          sObject.CA10__virtualizationType__c AS CA10__virtualizationType__c,
          process_CA10__CaAwsInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__stateName__c AS CA10__stateName__c,
                  sObject.CA10__virtualizationType__c AS CA10__virtualizationType__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsInstance__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
