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

chore: update the get examples matrix step to run some checks and be more readable #170

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 28 additions & 22 deletions .github/actions/e2e-getexamples/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@ runs:
- name: get examples
id: getexamples
run: |
# Loop through all directories (*/ means all directories ending with a /)
DIRS=$(for dir in */; do
# Check if the directory does NOT contain a .e2eignore file
if [ ! -f "$dir/.e2eignore" ]; then
# If the directory does not have a .e2eignore file, include it in the output
echo "$dir"
fi
done |
# Remove the trailing slash from each directory name
# -d/ specifies the delimiter as "/"
# -f1 selects the first field before the delimiter, which is the directory name
cut -d/ -f1 |
# Convert the list of directory names to a JSON array
# -R treats the input as raw strings instead of JSON
# -s slurps all lines into a single string
# -c generates a compact JSON output
# 'split("\n")[:-1]' splits the input into an array based on newline characters
# and [:-1] removes the last empty string caused by a trailing newline
jq -R -s -c 'split("\n")[:-1]')
# Get all the folders in the examples directory
$folders = Get-ChildItem -Directory

$examples = @()
$e2eTests = @()

foreach ($folder in $folders) {
# Check if the folder contains at least one .tf file
$files = Get-ChildItem -Path $folder.FullName -File -Filter "*.tf"
if($files.Count -ne 0) {
$examples += $folder.Name

# Check if the folder contains a .e2eignore file
$ignore = Get-ChildItem -Path $folder.FullName -File -Filter ".e2eignore"
if($ignore.Count -eq 0) {
$e2eTests += $folder.Name
}
}
}

if($examples.Count -eq 0) {
throw "At least one example must be present for Azure Verified Modules"
}

$e2eTestsJson = ConvertTo-Json $e2eTests

Write-Output "examples=$e2eTestsJson" >> $env:GITHUB_OUTPUT

# Set output for GitHub Actions using the $GITHUB_OUTPUT variable
echo examples="$DIRS" >> "$GITHUB_OUTPUT"
working-directory: examples
shell: bash
shell: pwsh
jaredfholgate marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}

run-e2e-tests:
if: github.event.repository.name != 'terraform-azurerm-avm-template' && github.event.pull_request.head.repo.fork == false
if: github.event.pull_request.head.repo.fork == false
uses: Azure/terraform-azurerm-avm-template/.github/workflows/test-examples-template.yml@main
name: end to end
secrets: inherit
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-examples-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
getexamples:
if: github.event.repository.name != 'terraform-azurerm-avm-template' && github.event.pull_request.head.repo.fork == false
if: github.event.pull_request.head.repo.fork == false
runs-on: ubuntu-latest
outputs:
examples: ${{ steps.getexamples.outputs.examples }}
Expand Down
Empty file.
141 changes: 141 additions & 0 deletions examples/ignore_example_for_e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!-- BEGIN_TF_DOCS -->
# Ignored example for e2e tests

This example will not be run as an e2e test as it has the .e2eignore file in the same directory.

```hcl
terraform {
required_version = "~> 1.5"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.74"
}
modtm = {
source = "azure/modtm"
version = "~> 0.3"
}
random = {
source = "hashicorp/random"
version = "~> 3.5"
}
}
}

provider "azurerm" {
features {}
}


## Section to provide a random Azure region for the resource group
# This allows us to randomize the region for the resource group.
module "regions" {
source = "Azure/avm-utl-regions/azurerm"
version = "~> 0.1"
}

# This allows us to randomize the region for the resource group.
resource "random_integer" "region_index" {
max = length(module.regions.regions) - 1
min = 0
}
## End of section to provide a random Azure region for the resource group

# This ensures we have unique CAF compliant names for our resources.
module "naming" {
source = "Azure/naming/azurerm"
version = "~> 0.3"
}

# This is required for resource modules
resource "azurerm_resource_group" "this" {
location = module.regions.regions[random_integer.region_index.result].name
name = module.naming.resource_group.name_unique
}

# This is the module call
# Do not specify location here due to the randomization above.
# Leaving location as `null` will cause the module to use the resource group location
# with a data source.
module "test" {
source = "../../"
# source = "Azure/avm-<res/ptn>-<name>/azurerm"
# ...
location = azurerm_resource_group.this.location
name = "TODO" # TODO update with module.naming.<RESOURCE_TYPE>.name_unique
resource_group_name = azurerm_resource_group.this.name

enable_telemetry = var.enable_telemetry # see variables.tf
}
```

<!-- markdownlint-disable MD033 -->
## Requirements

The following requirements are needed by this module:

- <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) (~> 1.5)

- <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) (~> 3.74)

- <a name="requirement_modtm"></a> [modtm](#requirement\_modtm) (~> 0.3)

- <a name="requirement_random"></a> [random](#requirement\_random) (~> 3.5)

## Resources

The following resources are used by this module:

- [azurerm_resource_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) (resource)
- [random_integer.region_index](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/integer) (resource)

<!-- markdownlint-disable MD013 -->
## Required Inputs

No required inputs.

## Optional Inputs

The following input variables are optional (have default values):

### <a name="input_enable_telemetry"></a> [enable\_telemetry](#input\_enable\_telemetry)

Description: This variable controls whether or not telemetry is enabled for the module.
For more information see <https://aka.ms/avm/telemetryinfo>.
If it is set to false, then no telemetry will be collected.

Type: `bool`

Default: `true`

## Outputs

No outputs.

## Modules

The following Modules are called:

### <a name="module_naming"></a> [naming](#module\_naming)

Source: Azure/naming/azurerm

Version: ~> 0.3

### <a name="module_regions"></a> [regions](#module\_regions)

Source: Azure/avm-utl-regions/azurerm

Version: ~> 0.1

### <a name="module_test"></a> [test](#module\_test)

Source: ../../

Version:

<!-- markdownlint-disable-next-line MD041 -->
## Data Collection

The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at <https://go.microsoft.com/fwlink/?LinkID=824704>. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.
<!-- END_TF_DOCS -->
4 changes: 4 additions & 0 deletions examples/ignore_example_for_e2e/_footer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- markdownlint-disable-next-line MD041 -->
## Data Collection

The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at <https://go.microsoft.com/fwlink/?LinkID=824704>. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.
3 changes: 3 additions & 0 deletions examples/ignore_example_for_e2e/_header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignored example for e2e tests

This example will not be run as an e2e test as it has the .e2eignore file in the same directory.
63 changes: 63 additions & 0 deletions examples/ignore_example_for_e2e/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
terraform {
required_version = "~> 1.5"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.74"
}
modtm = {
source = "azure/modtm"
version = "~> 0.3"
}
random = {
source = "hashicorp/random"
version = "~> 3.5"
}
}
}

provider "azurerm" {
features {}
}


## Section to provide a random Azure region for the resource group
# This allows us to randomize the region for the resource group.
module "regions" {
source = "Azure/avm-utl-regions/azurerm"
version = "~> 0.1"
}

# This allows us to randomize the region for the resource group.
resource "random_integer" "region_index" {
max = length(module.regions.regions) - 1
min = 0
}
## End of section to provide a random Azure region for the resource group

# This ensures we have unique CAF compliant names for our resources.
module "naming" {
source = "Azure/naming/azurerm"
version = "~> 0.3"
}

# This is required for resource modules
resource "azurerm_resource_group" "this" {
location = module.regions.regions[random_integer.region_index.result].name
name = module.naming.resource_group.name_unique
}

# This is the module call
# Do not specify location here due to the randomization above.
# Leaving location as `null` will cause the module to use the resource group location
# with a data source.
module "test" {
source = "../../"
# source = "Azure/avm-<res/ptn>-<name>/azurerm"
# ...
location = azurerm_resource_group.this.location
name = "TODO" # TODO update with module.naming.<RESOURCE_TYPE>.name_unique
resource_group_name = azurerm_resource_group.this.name

enable_telemetry = var.enable_telemetry # see variables.tf
}
9 changes: 9 additions & 0 deletions examples/ignore_example_for_e2e/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "enable_telemetry" {
type = bool
default = true
description = <<DESCRIPTION
This variable controls whether or not telemetry is enabled for the module.
For more information see <https://aka.ms/avm/telemetryinfo>.
If it is set to false, then no telemetry will be collected.
DESCRIPTION
}
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resource "azurerm_management_lock" "this" {

lock_level = var.lock.kind
name = coalesce(var.lock.name, "lock-${var.lock.kind}")
scope = azurerm_MY_RESOURCE.this.id # TODO: Replace with your azurerm resource name
scope = azurerm_resource_group.TODO.id # TODO: Replace with your azurerm resource name
notes = var.lock.kind == "CanNotDelete" ? "Cannot delete the resource or its child resources." : "Cannot delete or modify the resource or its child resources."
}

Expand Down