Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Support for Deletion of custom ARM and Bicep template resource #861

Merged
merged 49 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
13612fc
UpdateBase
Jefajers Jan 25, 2024
25e4a7e
Update
Jefajers Jan 25, 2024
e34b4b1
Update
Jefajers Jan 25, 2024
83871d4
Update
Jefajers Jan 25, 2024
4dbaddf
PriorityFix
Jefajers Jan 25, 2024
0c6d141
Merge branch 'Azure:main' into customDeletion
Jefajers Jan 29, 2024
93ef4ad
Update
Jefajers Jan 29, 2024
ca9be1b
Update
Jefajers Jan 29, 2024
1e4fbc9
Update
Jefajers Jan 30, 2024
0e1e085
Update
Jefajers Jan 30, 2024
ade1452
Update
Jefajers Jan 31, 2024
ce40bfc
Update
Jefajers Jan 31, 2024
c95bff1
Update
Jefajers Feb 2, 2024
376d838
Update
Jefajers Feb 5, 2024
6e46d43
Merge branch 'Azure:main' into customDeletion
Jefajers Feb 9, 2024
d7fc6a8
Update
Jefajers Feb 12, 2024
a39cd00
Update
Jefajers Feb 14, 2024
cdb551e
Update
Jefajers Feb 20, 2024
74bd626
Update
Jefajers Feb 20, 2024
b631cee
Update
Jefajers Feb 22, 2024
bbadd47
Update
Jefajers Feb 22, 2024
e03dc3e
Update
Jefajers Feb 23, 2024
fa691b1
Update
Jefajers Feb 23, 2024
bc1e7ab
Update
Jefajers Feb 23, 2024
f33a738
Update
Jefajers Feb 23, 2024
ea833ad
Update
Jefajers Feb 24, 2024
a19f5df
Update
Jefajers Feb 24, 2024
4a62fb5
Update
Jefajers Feb 25, 2024
5f01ea7
Update
Jefajers Feb 27, 2024
5ca9602
Update
Jefajers Feb 27, 2024
f7a87ca
Update
Jefajers Feb 27, 2024
78c6417
Update
Jefajers Feb 27, 2024
0f885ea
Update
Jefajers Feb 27, 2024
1ceac1b
Update
Jefajers Feb 27, 2024
1ee69fa
Update
Jefajers Feb 27, 2024
34dd50d
Update
Jefajers Feb 28, 2024
10c98b4
Update
Jefajers Feb 28, 2024
dd6c50a
Update
Jefajers Feb 28, 2024
95e539c
Update
Jefajers Feb 28, 2024
2d111c4
Update
Jefajers Feb 28, 2024
d4d3093
Update
Jefajers Feb 29, 2024
a1f7829
Update
Jefajers Feb 29, 2024
735b620
Update
Jefajers Feb 29, 2024
b56ba7b
Update
Jefajers Feb 29, 2024
86d2303
Update
Jefajers Mar 1, 2024
41c12c0
Update
Jefajers Mar 1, 2024
9951e2f
Update
Jefajers Mar 4, 2024
7191fee
Update
Jefajers Mar 5, 2024
c9d34b2
Update
Jefajers Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 75 additions & 4 deletions docs/wiki/ResourceDeletion.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
# AzOps Resource Deletion

- [Introduction](#introduction)
- [Deletion dependency validation](#deletion-dependency-validation)
- [Deletion of AzOps generated File](#deletion-of-azops-generated-file)
- [Deletion dependency validation](#deletion-dependency-validation)
- [Deletion dependency validation scenario](#deletion-dependency-validation-scenario)
- [Deletion of Custom Template](#deletion-of-custom-template)
- [Enable Deletion of Custom Template](#enable-deletion-of-custom-template)
- [Integration with AzOps Accelerator](#integration-with-azops-accelerator)
- [How to Add AzOps Resource Deletion to existing AzOps Push](#how-to-add-azops-resource-deletion-to-existing-azops-push-and-validate-pipelines)

## Introduction

**AzOps Resource Deletion** performs deletion of locks, policyAssignments, policyDefinitions, policyExemptions, policySetDefinitions and roleAssignments in Azure, based on `AzOps - Pull` generated templates at all Azure scope levels `(Management Group/Subscription/Resource Group)`.
**AzOps Resource Deletion** at a high level enables two scenarios.
1. [Deletion of AzOps generated File](#deletion-of-azops-generated-file) of supported resource type, resulting in AzOps removes the corresponding resource in Azure.
2. [Deletion of Custom Template](#deletion-of-custom-template), resulting in AzOps removes the corresponding resource in Azure.

```mermaid
flowchart TD
A[(Main Branch)] --> B[(1.Delete Branch)]
B -- Remove Template Files --> C([2. filename.json])
C --> D([3. Commit])
D --> B
B -- Pull Request to Main ----> E(((4. AzOps - Validate
'/tmp/diff.txt'
'/tmp/diffdeletedfiles.txt')))
E -- git diff --- A
E ---> F[5. Invoke-AzOpsPush -WhatIf:$true]
E -- Merge ---> A
A -- Automated trigger----> G(((6. AzOps - Push
'/tmp/diff.txt'
'/tmp/diffdeletedfiles.txt')))
G -- git diff --- A
G --> H[7. Invoke-AzOpsPush -WhatIf:$false]
```

- For any other resource type **deletion** is **not** supported by AzOps at this time.
## Deletion of AzOps generated File

By removing a AzOps generated file of a supported resource type AzOps removes the corresponding resource in Azure.

_Supported resource types include: locks, policyAssignments, policyDefinitions, policyExemptions, policySetDefinitions, roleAssignments and resourceGroups in Azure._

- For any other `AzOps - Pull` generated resource **deletion** is **not** supported by AzOps at this time.

**_Please Note_**

- SPN used for deletion/change action, requires below actions in its role definition. Choose which combination best suites your implementation.
Expand Down Expand Up @@ -68,7 +96,15 @@ By removing a AzOps generated file of a supported resource type AzOps removes th
OR
Microsoft.Authorization/roleAssignments/*
```
## Deletion dependency validation

- For Azure Resource group removal

```bash
Microsoft.Resources/subscriptions/resourceGroups/delete
OR
Microsoft.Resources/subscriptions/resourceGroups/*
```
### Deletion dependency validation
When deletion of a supported object is sent to AzOps it evaluates to ensure resource dependencies are included in the deletion job. If a dependency is missing the module will throw (exit with error) and post the result of missing dependencies to the pull request conversation asking you to add it and try again.

**_Please Note: For the validation pipeline to fail in the manner intended (applicable to implementations created prior to AzOps release v1.9.0)_**
Expand All @@ -91,6 +127,41 @@ Scenario: Deletion of a policy definition and policy assignment where the assign
- a) In the branch delete the dependent file corresponding to the resulting error.
- b) Delete the dependency in Azure and re-run validation.

## Deletion of Custom Template
Deletion of custom templates is a opt-in feature that you need to enable [see](#enable-deletion-of-custom-template).

Once enabled, deletion of `yourCustomTemplate.bicep`, `yourCustomTemplate.bicepparam`, `yourCustomTemplate.json` or `yourCustomTemplate.parameters.json` results in AzOps attempting deletion of the resolved Azure resources.

How does AzOps attempt deletion of custom template?

```mermaid
flowchart TD
A(((Invoke-AzOpsPush))) --> B[Validate Template
filename.parameters.json]
B -- Failed --> K[Skip]
B -- Success --> C[Resolve template files]
C -- No template found --> D
C -- Found template
filename.json --> D[Sort Templates
Attempt locks before other resources]
D --> E[(Process templates for deletion in series)]
E -- Success ---> A
E --> F([Identify resources within template by attempting a WhatIf deployment and gather returned resource ids])
F --> G([Attempt resource deletion for each identified resource id])
G -- Success ---> E
G -- Fail --> H([If resource fails deletion, recursively retry deletion in different order])
H -- Success ---> E
H -- Fail --> I([For resources still failing deletion, collect them for a last deletion attempt, once all other templates are processed])
I -- Success ---> A
I --Fail --> J([If resource deletion still fails, module will log error and throw])
J --Fail ---> A
```

### Enable Deletion of Custom Template
Set the `Core.CustomTemplateResourceDeletion` value in `settings.json` to `true`.

`AzOps - Push` will now evaluate and attempt deletion of corresponding resource (_from template_) in Azure when a custom template is deleted.

## Integration with AzOps Accelerator

The [AzOps Accelerator pipelines](https://github.com/azure/azops-accelerator) (including `Git Hub Actions` & `Azure Pipelines`) incorporates the execution of resource deletion.
Expand Down
Loading