# pvyver/policy-donotdelete architecture

> Architecture diagram of the GitHub repository pvyver/policy-donotdelete, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pvyver/policy-donotdelete
- Repository: https://github.com/pvyver/policy-donotdelete

## Overview

##### Project Type and Purpose
- Type: Infrastructure-as-Code / automation toolkit for Azure Policy.
- Purpose: Define a “Do Not Allow Deletion” policy in JSON, then automate its creation (registration) and assignment (enforcement) in Azure using PowerShell scripts.

##### File Structure
- Top-level files:
- README.md – high-level description
- createPolicy.ps1 – script to register the policy definition
- assignPolicy.ps1 – script to assign the policy to a scope
- Directory:
- policies/doNotAllowDeletionPolicy.json – the policy definition itself

##### Extract Insights from README
- The README clearly states:
- Where the policy config lives (the JSON file).
- That createPolicy.ps1 registers the policy.
- That assignPolicy.ps1 assigns the registered policy to a subscription or resource group.

##### Main Components
a. Local/Dev Environment
- Policy source directory (Git repo)
- doNotAllowDeletionPolicy.json
- createPolicy.ps1
- assignPolicy.ps1
b. Execution Context
- PowerShell (Azure PowerShell module or Azure CLI)
- Azure Active Directory credential/token cache
c. Azure Management Plane
- Azure Resource Manager (ARM) REST API
- Azure Policy Service (Definition + Assignment resources)
d. Target Scope
- Subscription or Resource Group where the policy is enforced

##### Relationships & Data Flows
- The developer runs createPolicy.ps1
→ Script reads doNotAllowDeletionPolicy.json
→ Authenticates to Azure (AAD)
→ Calls ARM API to create/register a Policy Definition
- The developer runs assignPolicy.ps1
→ Optionally references the newly created definition
→ Calls ARM API to create a Policy Assignment at a specified scope
- Azure Policy Service enforces the rule in real time on resource‐create/update operations

##### Architectural Patterns & Principles
- Declarative vs. Imperative:
- JSON file – declarative policy specification
- PowerShell – imperative orchestration
- Separation of Concerns:
- One script for definition registration
- Another for assignment/enforcement
- Idempotency: Each script should check for existing definitions/assignments before creating new ones.

##### Technologies & Frameworks
- JSON (Azure Policy schema)
- PowerShell scripting
- Azure PowerShell Module / Azure CLI underneath
- Azure Resource Manager REST endpoints

##### Final Checklist for the Engineer
- Components and flows listed above.
- Notes/callouts for idempotency checks or parameterization in scripts.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Local Development Environment
    subgraph "Local Development Environment"
        README["README.md"]:::local
        JSON["doNotAllowDeletionPolicy.json"]:::local
        CreateScript["createPolicy.ps1"]:::local
        AssignScript["assignPolicy.ps1"]:::local
    end

    %% Execution Runtime
    subgraph "Execution Context"
        PS["PowerShell (Azure Module)"]:::runtime
        AAD["Azure AD Authentication"]:::runtime
        ARMClient["ARM REST Client"]:::runtime
    end

    %% Azure Control Plane
    subgraph "Azure Control Plane"
        ARMAPI["Azure Resource Manager API"]:::azure
        PolicyService["Azure Policy Service"]:::azure
    end

    %% Azure Enforcement Plane
    subgraph "Azure Enforcement Plane"
        TargetScope["Subscription/Resource Group"]:::enforcement
    end

    %% Flows for Definition Creation
    CreateScript -->|"reads JSON"| JSON
    CreateScript -->|"invokes PS module"| PS
    PS -->|"requests token"| AAD
    CreateScript -->|"calls ARM API"| ARMClient
    ARMClient -->|"POST /policyDefinitions"| ARMAPI
    ARMAPI -->|"registers definition"| PolicyService

    %% Flows for Assignment
    AssignScript -->|"invokes PS module"| PS
    AssignScript -->|"reads definition ID"| PolicyService
    AssignScript -->|"calls ARM API"| ARMClient
    ARMClient -->|"POST /policyAssignments"| ARMAPI
    ARMAPI -->|"creates assignment"| PolicyService
    PolicyService -->|"enforces policy"| TargetScope

    %% Ancillary Reference
    README -->|"docs reference"| CreateScript
    README -->|"docs reference"| AssignScript
    README -->|"points to JSON"| JSON

    %% Click Events
    click README "https://github.com/pvyver/policy-donotdelete/blob/main/README.md"
    click JSON "https://github.com/pvyver/policy-donotdelete/blob/main/policies/doNotAllowDeletionPolicy.json"
    click CreateScript "https://github.com/pvyver/policy-donotdelete/blob/main/createPolicy.ps1"
    click AssignScript "https://github.com/pvyver/policy-donotdelete/blob/main/assignPolicy.ps1"

    %% Styles
    classDef local fill:#f9f,stroke:#333,stroke-width:1px
    classDef runtime fill:#ff9,stroke:#333,stroke-width:1px
    classDef azure fill:#9f9,stroke:#333,stroke-width:1px
    classDef enforcement fill:#9cf,stroke:#333,stroke-width:1px
```
