Skip to content

Commit

Permalink
Example/user defined types (#24)
Browse files Browse the repository at this point in the history
UDT Example
MegaLinter v8 bump
  • Loading branch information
riosengineer authored Sep 8, 2024
1 parent 05e4af8 commit a6639ac
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 6 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/mega-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ jobs:
id: ml
# You can override MegaLinter flavor used to have faster performances
# More info at https://megalinter.io/flavors/
uses: oxsecurity/megalinter@v7
uses: oxsecurity/megalinter@v8
env:
# All available variables are described in documentation
# https://megalinter.io/configuration/
VALIDATE_ALL_CODEBASE: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} # Validates all source when push on main, else just the git diff with main. Override with true if you always want to lint all sources
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# ADD YOUR CUSTOM ENV VARIABLES HERE OR DEFINE THEM IN A FILE .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
DISABLE: COPYPASTE,SPELL # Uncomment to disable copy-paste and spell checks
DISABLE_LINTERS: YAML_V8R,YAML_YAMLLINT,YAML_PRETTIER,REPOSITORY_CHECKOV,POWERSHELL_POWERSHELL,ACTION_ACTIONLINT,REPOSITORY_GITLEAKS,REPOSITORY_GRYPE
REPOSITORY_KICS_DISABLE_ERRORS: true
DISABLE_LINTERS: YAML_V8R,YAML_YAMLLINT,YAML_PRETTIER,REPOSITORY_CHECKOV,POWERSHELL_POWERSHELL,ACTION_ACTIONLINT,REPOSITORY_GITLEAKS,REPOSITORY_GRYPE,REPOSITORY_KICS

# Upload MegaLinter artifacts
- name: Archive production artifacts
Expand Down
4 changes: 3 additions & 1 deletion bicep-examples/conditions/main.bicep
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
targetScope = 'resourceGroup'

// Metadata
metadata name = 'Key Vault creation Bicep module'
metadata description = 'Showcasing Azure Bicep if conditions'
metadata owner = '[email protected]'

// Parameters
@description('Azure region for deployments chosen from the resource group.')
param location string = resourceGroup().location

Expand All @@ -30,7 +32,7 @@ var kvSku = env == 'prod' ? 'premium' : 'standard'
module KeyVault 'br/public:avm/res/key-vault/vault:0.7.0' = if (deployResource) {
name: '${uniqueString(deployment().name, location)}-${kvName}'
params: {
name: kvName
name: kvName2
location: location
sku: kvSku
enableSoftDelete: true
Expand Down
2 changes: 1 addition & 1 deletion bicep-examples/lambda-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ or PowerShell
```powershell
Connect-AzAccount
Set-AzContext -Subscription "subscription name or id"
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg -TemplateFile "main.bicep"
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg" -TemplateFile "main.bicep"
```
2 changes: 1 addition & 1 deletion bicep-examples/loops/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ or PowerShell
```powershell
Connect-AzAccount
Set-AzContext -Subscription "subscription name or id"
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg -TemplateFile "main.bicep"
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg" -TemplateFile "main.bicep"
```
62 changes: 62 additions & 0 deletions bicep-examples/user-defined-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Azure Bicep - User Defined Types

## Introduction

User-defined types in Azure Bicep allow you to create custom data structures within your Bicep templates. Instead of relying solely on built-in data types, you can define your own types tailored to your specific needs and requirements.

## 📃 Benefits of User Defined Types

✅ Type Safety: User-defined types (UDTs) enforce that only valid data structures are used. This helps prevent accidental misconfigurations.

✅ Maintainability: When you adopt UDTs, your Bicep code becomes easier to maintain. Changes to the types automatically propagate to all places where the type is used.

✅ Intellisense: The VS Code Bicep IntelliSense provides type auto completions and expected values, enhancing your development experience.

## User Defined Type Example

In this example, you’ll deploy a Storage Account using custom types as part of the configuration. Within the `main.bicep` file, you’ll find the two defined types:

```bicep
// User Defined Types
type storageSkuType = 'Premium_LRS' | 'Premium_ZRS' | 'Standard_GRS' | 'Standard_GZRS' | 'Standard_LRS' | 'Standard_RAGRS' | 'Standard_RAGZRS' | 'Standard_ZRS'
type storageConfType = {
name: string
sku: storageSkuType
location: string
kind: 'StorageV2' | 'BlobStorage' | 'FileStorage' | 'BlockBlobStorage' }
```

By defining the data structure in this way, you ensure both type safety and valid data for the Storage Account configuration.

Next, the Bicep parameter is defined for the type.

```bicep
@description('Storage account UDR param config.')
param storageConf storageConfType
```

Lastly, using the parameter you can complete the Storage Account config `storageConf.sku` `storageConf.kind` and so forth.

## 🚀 Deployment

> [!NOTE]
> You need to have a resource group deployed before trying this out.
In VisualStudio Code open a terminal and run:

CLI

```bash
az login
az account set --subscription 'subscription name or id'
az deployment group create -g 'your-rg' --confirm-with-what-if -f '.\main.bicep' -p 'main.bicepparam'
```

or PowerShell

```powershell
Connect-AzAccount
Set-AzContext -Subscription "subscription name or id"
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg" -TemplateFile "main.bicep" -TemplateParameterFile "main.bicepparam"
```
33 changes: 33 additions & 0 deletions bicep-examples/user-defined-types/main.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
targetScope = 'resourceGroup'

// Metadata
metadata name = 'User Defined Types'
metadata description = 'Showcasing Azure Bicep UDTs'
metadata owner = '[email protected]'

// User Defined Types
type storageSkuType = 'Premium_LRS' | 'Premium_ZRS' | 'Standard_GRS' | 'Standard_GZRS' | 'Standard_LRS' | 'Standard_RAGRS' | 'Standard_RAGZRS' | 'Standard_ZRS'

type storageConfType = {
name: string
sku: storageSkuType
location: string
kind: 'StorageV2' | 'BlobStorage' | 'FileStorage' | 'BlockBlobStorage' }

// Parameters
@description('Azure region used from the resource group.')
param location string = resourceGroup().location

@description('Storage account UDR param config.')
param storageConf storageConfType

// Storage Account
module storage 'br/public:avm/res/storage/storage-account:0.11.1' = {
name: 'uniqueString(deployment().name, location)-st'
params: {
name: storageConf.name
location: location
skuName: storageConf.sku
kind: storageConf.kind
}
}
8 changes: 8 additions & 0 deletions bicep-examples/user-defined-types/main.bicepparam
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using 'main.bicep'

param storageConf = {
name: 'stbicepifyudt001'
sku: 'Standard_LRS'
location: 'uksouth'
kind: 'StorageV2'
}

0 comments on commit a6639ac

Please sign in to comment.