Skip to content

Commit

Permalink
Update Invoke-PolicyToBicep.ps1 Scripts to Handle -'s, 's (spac…
Browse files Browse the repository at this point in the history
…es) & `'` apostrophes + Fix #254 (#288)

* update policy to bicep script to handle spaces and hyphens for txt file var outputs

* add support for apostrophe escaping

* adding parameters

* update path and name vars

* add more params

* remove comment

* add param vars for sets

* fixing empty paramters file bug

* ps1 updates

* updates

* linter fixes

* add should process

* surpress false positives

* update azure public policies

* update mc policies

* wiki update

* update codetour

* more docs

* add hyphen support

* updates to indents

* docs

* Update .github/scripts/Invoke-PolicyToBicep-China.ps1

Co-authored-by: Jan Faurskov <[email protected]>

* Update .github/scripts/Invoke-PolicyToBicep.ps1

Co-authored-by: Jan Faurskov <[email protected]>

Co-authored-by: SeSeicht <[email protected]>
Co-authored-by: Jan Faurskov <[email protected]>
  • Loading branch information
3 people authored Jul 19, 2022
1 parent 6424893 commit 948e79d
Show file tree
Hide file tree
Showing 11 changed files with 2,532 additions and 2,278 deletions.
293 changes: 192 additions & 101 deletions .github/scripts/Invoke-PolicyToBicep-China.ps1

Large diffs are not rendered by default.

293 changes: 192 additions & 101 deletions .github/scripts/Invoke-PolicyToBicep.ps1

Large diffs are not rendered by default.

50 changes: 24 additions & 26 deletions .vscode/tours/upstreamPolicyUpdateProcess.tour
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@
},
"end": {
"line": 11,
"character": 155
"character": 18
}
},
"description": "Create and overwrite the `_policyDefinitionsBicepInput.txt` file in the specified directory with a new empty file with `utf8` encoding"
"description": "Create and overwrite the `_policyDefinitionsBicepInput.txt` file in the specified directory with a new empty file with `utf8` encoding",
"line": 44
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
Expand All @@ -223,7 +224,8 @@
"character": 264
}
},
"description": "For every `.json` file in the `lib/policy_definitions` directory extract the policy definition name and the file path and then write them (append) into the `_policyDefinitionsBicepInput.txt` file in a Bicep formatted output (see end of line 21)"
"description": "For every `.json` file in the `lib/policy_definitions` directory extract the policy definition name and the file path and then write them (append) into the `_policyDefinitionsBicepInput.txt` file in a Bicep formatted output (see end of line 54)",
"line": 47
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
Expand All @@ -237,49 +239,42 @@
"character": 102
}
},
"description": "Count the number of policy definitions in the `lib/policy_definitions` directory and output to the console the total number"
"description": "Count the number of policy definitions in the `lib/policy_definitions` directory and output to the console the total number",
"line": 57
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
"selection": {
"start": {
"line": 30,
"line": 110,
"character": 1
},
"end": {
"line": 31,
"character": 162
"line": 112,
"character": 117
}
},
"description": "Create and overwrite the `_policySetDefinitionsBicepInput.txt` file in the specified directory with a new empty file with `utf8` encoding"
"description": "Count the number of policy set definitions in the `lib/policy_set_definitions` directory and output to the console the total number"
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
"selection": {
"start": {
"line": 35,
"line": 30,
"character": 1
},
"end": {
"line": 108,
"character": 2
"line": 31,
"character": 162
}
},
"description": "For every `.json` file in the `lib/policy_set_definitions` directory extract the policy set definitions (initiative) name and the file path and then write them (append) into the `_policySetDefinitionsBicepInput.txt` file in a Bicep formatted output (see end of line 93, 101 and 106).\r\n\r\nIt also creates a parameters file (not an ARM complaint one, just a JSON compliant document). And it also loops through all the child policy definitions of each policy set definition (initiative) and extracts the definition reference ID and the definition ID and adds them to the output in the `_policySetDefinitionsBicepInput.txt` file"
"description": "Create and overwrite the `_policySetDefinitionsBicepInput.txt` file in the specified directory with a new empty file with `utf8` encoding",
"line": 69
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
"selection": {
"start": {
"line": 110,
"character": 1
},
"end": {
"line": 112,
"character": 117
}
},
"description": "Count the number of policy set definitions in the `lib/policy_set_definitions` directory and output to the console the total number"
"description": "For every `.json` file in the `lib/policy_set_definitions` directory extract the policy set definitions (initiative) name and the file path and then write them (append) into the `_policySetDefinitionsBicepInput.txt` file in a Bicep formatted output (see end of line 145, 168 and 172).\r\n\r\nIt also creates a parameters file (not an ARM complaint one, just a JSON compliant document). And it also loops through all the child policy definitions of each policy set definition (initiative) and extracts the definition reference ID and the definition ID and adds them to the output in the `_policySetDefinitionsBicepInput.txt` file",
"line": 64
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
Expand All @@ -293,7 +288,8 @@
"character": 155
}
},
"description": "Create and overwrite the `_policyAssignmentsBicepInput.txt` file in the specified directory with a new empty file with `utf8` encoding"
"description": "Create and overwrite the `_policyAssignmentsBicepInput.txt` file in the specified directory with a new empty file with `utf8` encoding",
"line": 200
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
Expand All @@ -307,7 +303,8 @@
"character": 2
}
},
"description": "For every `.json` file in the `lib/policy_assignments` directory extract the policy assignment's name and the file path and then write them (append) into the `_policyAssignmentsBicepInput.txt` file in a Bicep formatted output (see end of line 131)."
"description": "For every `.json` file in the `lib/policy_assignments` directory extract the policy assignment's name and the file path and then write them (append) into the `_policyAssignmentsBicepInput.txt` file in a Bicep formatted output (see end of line 131).",
"line": 203
},
{
"file": ".github/scripts/Invoke-PolicyToBicep.ps1",
Expand All @@ -321,7 +318,8 @@
"character": 109
}
},
"description": "Count the number of policy assignments in the `lib/policy_assignments` directory and output to the console the total number"
"description": "Count the number of policy assignments in the `lib/policy_assignments` directory and output to the console the total number",
"line": 218
},
{
"file": ".github/workflows/update-policy.yml",
Expand Down
8 changes: 5 additions & 3 deletions docs/wiki/AddingPolicyDefs.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ We recommend were possible to use option 1 and extend the current `ALZ-Bicep` ap

## How do I extend the `ALZ-Bicep` Custom Policy Definitions module?

> ✋ Where possible avoid using `-`'s (hyphens/dashes) as these are an unsupported character in Bicep. camelCase or use `_` instead 👍
To extend the [Custom Policy Definitions module](https://github.com/Azure/ALZ-Bicep/tree/main/infra-as-code/bicep/modules/policy/definitions) from `ALZ-Bicep` follow the below process.

1. Navigate to the relevant `lib` directory
Expand All @@ -38,7 +40,7 @@ To extend the [Custom Policy Definitions module](https://github.com/Azure/ALZ-Bi
4. Run the [`Invoke-PolicyToBicep.ps1`](https://github.com/Azure/ALZ-Bicep/blob/main/.github/scripts/Invoke-PolicyToBicep.ps1) script to update the `_policyDefinitionsBicepInput.txt` and/or `_policySetDefinitionsBicepInput.txt` files in the `lib` folders
1. Copy the entire contents of the relevant `.txt` file and replace the contents of the associated variable in the [Custom Policy Definitions module](https://github.com/Azure/ALZ-Bicep/tree/main/infra-as-code/bicep/modules/policy/definitions)
- Policy Definition = `_policyDefinitionsBicepInput.txt` placed into variable named `varCustomPolicyDefinitionsArray` (place copied contents inside of array `[]`)
- Policy Initiative (Set) = `_policySetDefinitionsBicepInput.txt` placed into variable named `varCustomPolicySetDefinitionsArray` (place copied contents inside of array `[]`)
- Policy Initiative (Set) & Parameter Variables = Copy the entire contents of `_policySetDefinitionsBicepInput.txt` and replace variable named `varCustomPolicySetDefinitionsArray` and parameter variables below it with copied contents
5. Redeploy the updated [Custom Policy Definitions module](https://github.com/Azure/ALZ-Bicep/tree/main/infra-as-code/bicep/modules/policy/definitions) via your configured method (locally via Azure CLI or PowerShell or via Azure DevOps pipeline or GitHub action)
6. New Policy Definitions now deployed to intermediate root Management Group (e.g. `Contoso`)

Expand All @@ -48,9 +50,9 @@ To extend the [Custom Policy Definitions module](https://github.com/Azure/ALZ-Bi

As Bicep is ultimately just compiling ARM templates behind the scenes, deployments of Bicep files/modules are still subject to the same limits as ARM Templates. The main limit to be aware of here is the `4 MB` total size limit for a single ARM Template.

Today the [Custom Policy Definitions module](https://github.com/Azure/ALZ-Bicep/tree/main/infra-as-code/bicep/modules/policy/definitions) from `ALZ-Bicep` results in a `2.56 MB` ARM Template file `JSON` file being created.
Today the [Custom Policy Definitions module](https://github.com/Azure/ALZ-Bicep/tree/main/infra-as-code/bicep/modules/policy/definitions) from `ALZ-Bicep` results in a `874 KB` ARM Template file `JSON` file being created.

This has plenty of room for expansion but it is worth keeping in mind as you may hit the 4MB limit and see the errors of `JobSizeExceededException` or `DeploymentJobSizeExceededException`.
This has plenty of room for expansion but it is worth keeping in mind as you may hit the `4 MB` limit and see the errors of `JobSizeExceededException` or `DeploymentJobSizeExceededException`.

### The fix

Expand Down
Loading

0 comments on commit 948e79d

Please sign in to comment.