# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# E2E Test: Container Instances/ContainerInstance_LogAnalytics_Append
# Real Azure Policy: "Configure diagnostics for container group to log analytics workspace"
# Source: regolator/policyDefinitions/Container Instances/ContainerInstance_LogAnalytics_Append.json
#
# Features exercised:
#   - Append effect with details array (two fields)
#   - exists "false" operator (multiple conditions)
#   - Parameterized effect (Append/Disabled)
#   - Parameters injected into details value

aliases: test_aliases.json

policy_definition: |
  {
    "properties": {
      "displayName": "Configure diagnostics for container group to log analytics workspace",
      "policyType": "BuiltIn",
      "mode": "All",
      "parameters": {
        "effect": {
          "type": "String",
          "allowedValues": ["Append", "Disabled"],
          "defaultValue": "Append"
        },
        "workspaceId": {
          "type": "String",
          "metadata": {
            "displayName": "Workspace ID"
          }
        },
        "workspaceKey": {
          "type": "String",
          "metadata": {
            "displayName": "Workspace Key"
          }
        }
      },
      "policyRule": {
        "if": {
          "allOf": [
            {
              "field": "type",
              "equals": "Microsoft.ContainerInstance/containerGroups"
            },
            {
              "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceId",
              "exists": "false"
            },
            {
              "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceKey",
              "exists": "false"
            }
          ]
        },
        "then": {
          "effect": "[parameters('effect')]",
          "details": [
            {
              "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceId",
              "value": "[parameters('workspaceId')]"
            },
            {
              "field": "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceKey",
              "value": "[parameters('workspaceKey')]"
            }
          ]
        }
      }
    }
  }

cases:
  # =========================================================================
  # Both diagnostics fields missing → append
  # =========================================================================

  - note: append_both_missing
    resource:
      type: "Microsoft.ContainerInstance/containerGroups"
      name: "cg-no-diag"
      properties: {}
    parameters:
      workspaceId: "workspace-guid-123"
      workspaceKey: "workspace-key-abc"
    want_effect: "Append"
    want_details:
      - field: "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceId"
        value: "workspace-guid-123"
      - field: "Microsoft.ContainerInstance/containerGroups/diagnostics.logAnalytics.workspaceKey"
        value: "workspace-key-abc"

  - note: append_diagnostics_empty
    resource:
      type: "Microsoft.ContainerInstance/containerGroups"
      name: "cg-empty-diag"
      properties:
        diagnostics: {}
    parameters:
      workspaceId: "ws-id"
      workspaceKey: "ws-key"
    want_effect: "Append"

  # =========================================================================
  # One or both fields present → pass
  # =========================================================================

  - note: pass_workspace_id_present
    resource:
      type: "Microsoft.ContainerInstance/containerGroups"
      name: "cg-has-id"
      properties:
        diagnostics:
          logAnalytics:
            workspaceId: "existing-id"
    parameters:
      workspaceId: "ws-id"
      workspaceKey: "ws-key"
    want_undefined: true

  - note: pass_workspace_key_present
    resource:
      type: "Microsoft.ContainerInstance/containerGroups"
      name: "cg-has-key"
      properties:
        diagnostics:
          logAnalytics:
            workspaceKey: "existing-key"
    parameters:
      workspaceId: "ws-id"
      workspaceKey: "ws-key"
    want_undefined: true

  - note: pass_both_present
    resource:
      type: "Microsoft.ContainerInstance/containerGroups"
      name: "cg-full-diag"
      properties:
        diagnostics:
          logAnalytics:
            workspaceId: "existing-id"
            workspaceKey: "existing-key"
    parameters:
      workspaceId: "ws-id"
      workspaceKey: "ws-key"
    want_undefined: true

  # =========================================================================
  # Wrong type
  # =========================================================================

  - note: pass_wrong_type
    resource:
      type: "Microsoft.Compute/virtualMachines"
      name: "not-container"
      properties: {}
    parameters:
      workspaceId: "ws-id"
      workspaceKey: "ws-key"
    want_undefined: true
