---
policy: /ce/ca/azure/app-service/plan-without-apps
logic: /ce/ca/azure/app-service/plan-without-apps/prod.logic.yaml
executionTime: 2025-05-10T12:02:37.326759342Z
rows:
  - id: test1
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__numberOfSites__c') == number(0.0)
      actual: extract('CA10__numberOfSites__c') == number(0.0)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__numberOfSites__c').isEmpty()
      actual: extract('CA10__numberOfSites__c').isEmpty()
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/app-service/plan-without-apps/policy.yaml
    md5Hash: B5E3D0CADC5068FE264A0B4C7A23D49D
    content: |
      ---
      names:
        full: "Azure App Service Plan has no Apps assigned"
        contextual: "App Service Plan has no Apps assigned"
      description: "Ensure that Azure App Service Plan has at least one attached Azure App.\
        \ Consider deleting App Service Plans that are empty and have no applications,\
        \ sites or jobs associated with them."
      categories:
        - "COST"
      type: "COMPLIANCE_POLICY"
      frameworkMappings:
        - "/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction"
  - path: /ce/ca/azure/app-service/plan-without-apps/prod.logic.yaml
    md5Hash: 5AA9DF3EE264F86F47A1F6365CAE5BFB
    content: |-
      ---
      inputType: "CA10__CaAzureAppServicePlan__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAzureAppServicePlan__c/object.extracts.yaml
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "Azure App Service Plan has no Apps assigned."
          remediationMessage: "Consider deleting the empty App Service Plan."
          check:
            IS_EMPTY:
              arg:
                EXTRACT: "CA10__numberOfSites__c"
        - status: "INCOMPLIANT"
          currentStateMessage: "Azure App Service Plan has no Apps assigned."
          remediationMessage: "Consider deleting the empty App Service Plan."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__numberOfSites__c"
              right:
                NUMBER: 0.0
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "Azure App Service Plan has Apps assigned."
  - path: /types/CA10__CaAzureAppServicePlan__c/object.extracts.yaml
    md5Hash: 874C85D079713FFDDFD45E8EC52BC0A5
    content: "---\nextracts:\n# Nullable.\n  - name: \"CA10__numberOfSites__c\"\n\
      \    value: \n      FIELD:\n        path: \"CA10__numberOfSites__c\"\n"
  - path: /ce/ca/azure/app-service/plan-without-apps/test-data.json
    md5Hash: 2CD0BC829C66590E4352D747382FD188
    content: |
      [
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "extract('CA10__numberOfSites__c') == number(0.0)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-03-27T10:40:33Z" },
          "Id": "test1",
          "CA10__disappearanceTime__c": null,
          "CA10__numberOfSites__c": 0
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__numberOfSites__c').isEmpty()",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-03-27T10:40:33Z" },
          "Id": "test2",
          "CA10__disappearanceTime__c": null,
          "CA10__numberOfSites__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-03-27T10:40:33Z" },
          "Id": "test3",
          "CA10__disappearanceTime__c": null,
          "CA10__numberOfSites__c": 1
        }
      ]
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" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__numberOfSites__c') == number(0.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__numberOfSites__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureAppServicePlan__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__numberOfSites__c FLOAT64,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-03-27T10:40:33Z")
    },
    "Id" : "test1",
    "CA10__numberOfSites__c" : 0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-03-27T10:40:33Z")
    },
    "Id" : "test2",
    "CA10__numberOfSites__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-03-27T10:40:33Z")
    },
    "Id" : "test3",
    "CA10__numberOfSites__c" : 1
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureAppServicePlan__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__numberOfSites__c FLOAT64
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var IsEmptyLib = new function () {
          this.simpleIsEmpty = function(arg) {
              return arg == null;
          };
          this.simpleIsNotEmpty = function(arg) {
              return arg != null;
          };
      }();
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Azure [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__numberOfSites__c; } return this.out; };
      references1.push('Number Of Sites [obj.CA10__numberOfSites__c]: ' + obj.CA10__numberOfSites__c);
      if (IsEmptyLib.simpleIsEmpty(extract3.call(extract3))) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__numberOfSites__c').isEmpty()", currentStateMessage: "Azure App Service Plan has no Apps assigned.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting the empty App Service Plan.", runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__numberOfSites__c; } return this.out; };
      if (extract6.call(extract6) == 0.0) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__numberOfSites__c') == number(0.0)", currentStateMessage: "Azure App Service Plan has no Apps assigned.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting the empty App Service Plan.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "Azure App Service Plan has Apps assigned.", 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__numberOfSites__c AS CA10__numberOfSites__c,
          process_CA10__CaAzureAppServicePlan__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__numberOfSites__c AS CA10__numberOfSites__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureAppServicePlan__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
