diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 0000000..18b001a --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,4 @@ +version = 1 + +[[analyzers]] +name = "terraform" \ No newline at end of file diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 3f93574..d389fdd 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,19 +5,78 @@ version: 2 updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + open-pull-requests-limit: 3 + assignees: + - "clouddrove-ci" + reviewers: + - "approvers" + - package-ecosystem: "terraform" # See documentation for possible values directory: "/" # Location of package manifests schedule: interval: "weekly" + # Add assignees + assignees: + - "clouddrove-ci" + # Add reviewer + reviewers: + - "approvers" + # Allow up to 3 open pull requests for pip dependencies + open-pull-requests-limit: 3 + + - package-ecosystem: "terraform" # See documentation for possible values + directory: "/_example/basic" # Location of package manifests + schedule: + interval: "weekly" + # Add assignees + assignees: + - "clouddrove-ci" + # Add reviewer + reviewers: + - "approvers" + # Allow up to 3 open pull requests for pip dependencies + open-pull-requests-limit: 3 + - package-ecosystem: "terraform" # See documentation for possible values - directory: "_example/default_subnet" # Location of package manifests + directory: "/_example/complete" # Location of package manifests schedule: interval: "weekly" + # Add assignees + assignees: + - "clouddrove-ci" + # Add reviewer + reviewers: + - "approvers" + # Allow up to 3 open pull requests for pip dependencies + open-pull-requests-limit: 3 + - package-ecosystem: "terraform" # See documentation for possible values - directory: "_example/name-specific_subnet" # Location of package manifests + directory: "/_example/name-specific_subnet" # Location of package manifests schedule: interval: "weekly" + # Add assignees + assignees: + - "clouddrove-ci" + # Add reviewer + reviewers: + - "approvers" + # Allow up to 3 open pull requests for pip dependencies + open-pull-requests-limit: 3 + - package-ecosystem: "terraform" # See documentation for possible values - directory: "_example/nat-gateway_subnet" # Location of package manifests + directory: "/_example/nat-gateway_subnet" # Location of package manifests schedule: interval: "weekly" + # Add assignees + assignees: + - "clouddrove-ci" + # Add reviewer + reviewers: + - "approvers" + # Allow up to 3 open pull requests for pip dependencies + open-pull-requests-limit: 3 diff --git a/.github/workflows/auto_assignee.yml b/.github/workflows/auto_assignee.yml new file mode 100644 index 0000000..751c4bc --- /dev/null +++ b/.github/workflows/auto_assignee.yml @@ -0,0 +1,14 @@ +name: Auto Assign PRs + +on: + pull_request: + types: [opened, reopened] + + workflow_dispatch: +jobs: + assignee: + uses: clouddrove/github-shared-workflows/.github/workflows/auto_assignee.yml@1.0.9 + secrets: + GITHUB: ${{ secrets.GITHUB }} + with: + assignees: 'clouddrove-ci' diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml index 3e88b85..b3d9eb2 100644 --- a/.github/workflows/changelog.yml +++ b/.github/workflows/changelog.yml @@ -6,8 +6,8 @@ on: - "*" workflow_dispatch: jobs: - call-workflow-changelog: - uses: clouddrove/github-shared-workflows/.github/workflows/changelog.yml@master + changelog: + uses: clouddrove/github-shared-workflows/.github/workflows/changelog.yml@1.0.9 secrets: inherit with: - branch: 'master' \ No newline at end of file + branch: 'master' diff --git a/.github/workflows/readme.yml b/.github/workflows/readme.yml index d3cbfb6..1eb0243 100644 --- a/.github/workflows/readme.yml +++ b/.github/workflows/readme.yml @@ -13,29 +13,28 @@ jobs: uses: actions/checkout@master - name: 'Set up Python 3.7' - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: '3.x' - name: 'create readme' - uses: 'clouddrove/github-actions@v9.0.2' + uses: 'clouddrove/github-actions@9.0.3' with: actions_subcommand: 'readme' github_token: '${{ secrets.GITHUB }}' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: 'pre-commit check errors' - uses: pre-commit/action@v2.0.0 + uses: pre-commit/action@v3.0.0 continue-on-error: true - name: 'pre-commit fix erros' - uses: pre-commit/action@v2.0.0 + uses: pre-commit/action@v3.0.0 continue-on-error: true - name: 'push readme' - uses: 'clouddrove/github-actions@v9.0.2' + uses: 'clouddrove/github-actions@9.0.3' continue-on-error: true with: actions_subcommand: 'push' @@ -51,4 +50,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required - if: always() \ No newline at end of file + if: always() diff --git a/.github/workflows/semantic-releaser.yml b/.github/workflows/semantic-releaser.yml deleted file mode 100644 index df7e7ce..0000000 --- a/.github/workflows/semantic-releaser.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Release - -on: - push: - branches: - - main - paths: - - '**.tf' - - '!examples/**.tf' - -jobs: - release: - name: Release - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - with: - fetch-depth: 0 - persist-credentials: false - - - name: Setup Node.js - uses: actions/setup-node@v1 - with: - node-version: 14 - - - name: Release - env: - GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }} - run: npx semantic-release \ No newline at end of file diff --git a/.github/workflows/static-checks.yml b/.github/workflows/static-checks.yml deleted file mode 100644 index 8e8ef4a..0000000 --- a/.github/workflows/static-checks.yml +++ /dev/null @@ -1,74 +0,0 @@ -name: static-checks - -on: - pull_request: - -jobs: - versionExtract: - name: Get min/max versions - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Terraform min/max versions - id: minMax - uses: clowdhaus/terraform-min-max@main - outputs: - minVersion: ${{ steps.minMax.outputs.minVersion }} - maxVersion: ${{ steps.minMax.outputs.maxVersion }} - - versionEvaluate: - name: Evaluate Terraform versions - runs-on: ubuntu-latest - needs: versionExtract - strategy: - fail-fast: false - matrix: - version: - - ${{ needs.versionExtract.outputs.minVersion }} - - ${{ needs.versionExtract.outputs.maxVersion }} - directory: - - _example/ - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Install Terraform v${{ matrix.version }} - uses: hashicorp/setup-terraform@v1 - with: - terraform_version: ${{ matrix.version }} - - - name: Init & validate v${{ matrix.version }} - run: | - cd ${{ matrix.directory }} - terraform init - terraform validate - - name: tflint - uses: reviewdog/action-tflint@master - with: - tflint_version: v0.29.0 - github_token: ${{ secrets.GITHUB_TOKEN }} - working_directory: ${{ matrix.directory }} - fail_on_error: 'true' - filter_mode: 'nofilter' - flags: '--module' - - format: - name: Check code format - runs-on: ubuntu-latest - needs: versionExtract - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Install Terraform v${{ needs.versionExtract.outputs.maxVersion }} - uses: hashicorp/setup-terraform@v1 - with: - terraform_version: ${{ needs.versionExtract.outputs.maxVersion }} - - - name: Check Terraform format changes - run: terraform fmt --recursive -check=true \ No newline at end of file diff --git a/.github/workflows/tf-checks.yml b/.github/workflows/tf-checks.yml new file mode 100644 index 0000000..3ea4436 --- /dev/null +++ b/.github/workflows/tf-checks.yml @@ -0,0 +1,26 @@ +name: tf-checks +on: + push: + branches: [ master ] + pull_request: + workflow_dispatch: +jobs: + basic-example: + uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@1.0.9 + with: + working_directory: './_example/basic/' + + complete-example: + uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@1.0.9 + with: + working_directory: './_example/complete/' + + name-specific_subnet: + uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@1.0.9 + with: + working_directory: './_example/name-specific_subnet/' + + nat-gateway_subnet: + uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@1.0.9 + with: + working_directory: './_example/nat-gateway_subnet/' diff --git a/.github/workflows/tflint.yml b/.github/workflows/tflint.yml new file mode 100644 index 0000000..0875a68 --- /dev/null +++ b/.github/workflows/tflint.yml @@ -0,0 +1,11 @@ +name: tf-lint +on: + push: + branches: [ master ] + pull_request: + workflow_dispatch: +jobs: + tf-lint: + uses: clouddrove/github-shared-workflows/.github/workflows/tf-lint.yml@1.0.9 + secrets: + GITHUB: ${{ secrets.GITHUB }} diff --git a/.github/workflows/tfsec.yml b/.github/workflows/tfsec.yml index 9aaf588..daefede 100644 --- a/.github/workflows/tfsec.yml +++ b/.github/workflows/tfsec.yml @@ -5,7 +5,7 @@ on: workflow_dispatch: jobs: tfsec: - uses: clouddrove/github-shared-workflows/.github/workflows/tfsec.yml@master + uses: clouddrove/github-shared-workflows/.github/workflows/tfsec.yml@1.0.9 secrets: inherit with: - working_directory: '.' \ No newline at end of file + working_directory: '.' diff --git a/README.yaml b/README.yaml index ae2a398..c67425d 100644 --- a/README.yaml +++ b/README.yaml @@ -18,9 +18,12 @@ github_repo: clouddrove/terraform-azure-subnet # Badges to display badges: - - name: "Terraform" - image: "https://img.shields.io/badge/Terraform-v1.0.0-green" - url: "https://www.terraform.io" + - name: "Latest Release" + image: "https://img.shields.io/github/release/clouddrove/terraform-azure-subnet.svg" + url: "https://github.com/clouddrove/terraform-azure-subnet/releases/latest" + - name: "tfsec" + image: "https://github.com/clouddrove/terraform-azure-subnet/actions/workflows/tfsec.yml/badge.svg" + url: "https://github.com/clouddrove/terraform-azure-subnet/actions/workflows/tfsec.yml" - name: "Licence" image: "https://img.shields.io/badge/License-APACHE-blue.svg" url: "LICENSE.md" @@ -41,18 +44,20 @@ usage: |- ```hcl module "subnet" { source = "clouddrove/subnet/azure" - name = "app" - environment = "test" - label_order = ["name", "environment"] + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location virtual_network_name = join("", module.vnet.vnet_name) #subnet - subnet_names = ["subnet1", "subnet2"] - subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24"] + subnet_names = ["subnet1", "subnet2"] + subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24"] # route_table + enable_route_table = true + route_table_name = "default_subnet" routes = [ { name = "rt-test" @@ -66,9 +71,9 @@ usage: |- ```hcl module "name_specific_subnet" { source = "clouddrove/subnet/azure" - name = "app" - environment = "test" - label_order = ["name", "environment"] + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location virtual_network_name = join("", module.vnet.vnet_name) @@ -79,6 +84,8 @@ usage: |- subnet_prefixes = ["10.0.1.0/24"] # route_table + enable_route_table = true + route_table_name = "name_specific_subnet" routes = [ { name = "rt-test" @@ -92,19 +99,21 @@ usage: |- ```hcl module "name_specific_subnet" { source = "clouddrove/subnet/azure" - name = "app" - environment = "test" - label_order = ["name", "environment"] + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location virtual_network_name = join("", module.vnet.vnet_name) #subnet - create_nat_gateway = true - subnet_names = ["subnet1", "subnet2"] - subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24"] + create_nat_gateway = true + subnet_names = ["subnet1", "subnet2"] + subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24"] # route_table + enable_route_table = true + route_table_name = "nat_gateway" routes = [ { name = "rt-test" @@ -113,4 +122,4 @@ usage: |- } ] } - ``` \ No newline at end of file + ``` diff --git a/_example/basic/example.tf b/_example/basic/example.tf new file mode 100644 index 0000000..a159196 --- /dev/null +++ b/_example/basic/example.tf @@ -0,0 +1,37 @@ +provider "azurerm" { + features {} +} + +locals { + name = "app" + environment = "test" + label_order = ["name", "environment"] +} + +##----------------------------------------------------------------------------- +## Subnet module call. +##----------------------------------------------------------------------------- +module "subnet" { + source = "../.." + name = local.name + environment = local.environment + label_order = local.label_order + resource_group_name = "/subscription/***************" + location = "/subscription/***************" + virtual_network_name = "/subscription/***************" + + #subnet + subnet_names = ["subnet1", "subnet2"] + subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24"] + + # route_table + enable_route_table = false + route_table_name = "pub" + routes = [ + { + name = "rt-test" + address_prefix = "0.0.0.0/0" + next_hop_type = "Internet" + } + ] +} \ No newline at end of file diff --git a/_example/basic/outputs.tf b/_example/basic/outputs.tf new file mode 100644 index 0000000..1659827 --- /dev/null +++ b/_example/basic/outputs.tf @@ -0,0 +1,24 @@ +output "subnet_name" { + value = module.subnet.default_subnet_name + description = "The name of the subnet." +} + +output "subnet_id" { + value = module.subnet.default_subnet_id + description = "The subnet ID." +} + +output "address_prefixes" { + value = module.subnet.default_subnet_address_prefixes + description = "The address prefixes for the subnet." +} + +output "route_table_id" { + value = module.subnet.route_table_id + description = "The Route Table ID." +} + +output "route_table_associated_subnets" { + value = module.subnet[*].route_table_associated_subnets[0] + description = "The collection of Subnets associated with this route table." +} diff --git a/version.tf b/_example/basic/versions.tf similarity index 68% rename from version.tf rename to _example/basic/versions.tf index 51d04a8..9ffba34 100644 --- a/version.tf +++ b/_example/basic/versions.tf @@ -1,13 +1,13 @@ # Terraform version terraform { - required_version = ">= 1.0.0" + required_version = ">= 1.6.6" } terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = ">=2.90.0" + version = ">=3.85.0" } } -} \ No newline at end of file +} diff --git a/_example/default_subnet/example.tf b/_example/complete/example.tf similarity index 55% rename from _example/default_subnet/example.tf rename to _example/complete/example.tf index f0e84eb..604116a 100644 --- a/_example/default_subnet/example.tf +++ b/_example/complete/example.tf @@ -2,36 +2,41 @@ provider "azurerm" { features {} } -module "resource_group" { - source = "clouddrove/resource-group/azure" - version = "1.0.2" - +locals { name = "app" environment = "test" - label_order = ["name", "environment", ] + label_order = ["name", "environment"] +} + +module "resource_group" { + source = "clouddrove/resource-group/azure" + version = "1.0.2" + name = local.name + environment = local.environment + label_order = local.label_order location = "North Europe" } module "vnet" { - source = "clouddrove/vnet/azure" - version = "1.0.3" - - name = "app" - environment = "test" + source = "clouddrove/vnet/azure" + version = "1.0.4" + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location - address_space = "10.0.0.0/16" + address_spaces = ["10.0.0.0/16"] } module "subnet" { - source = "../.." - - name = "app" - environment = "test" - + source = "../.." + enable = false + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location - virtual_network_name = join("", module.vnet.vnet_name) + virtual_network_name = module.vnet.vnet_name #subnet subnet_names = ["subnet1", "subnet2"] @@ -39,7 +44,7 @@ module "subnet" { # route_table enable_route_table = true - route_table_name = "default_subnet" + route_table_name = "pub" routes = [ { name = "rt-test" @@ -50,14 +55,13 @@ module "subnet" { } module "subnet_2" { - source = "../.." - - name = "app" - environment = "test" - + source = "../.." + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location - virtual_network_name = join("", module.vnet.vnet_name) + virtual_network_name = module.vnet.vnet_name #subnet subnet_names = ["sub3", "sub4"] @@ -65,7 +69,7 @@ module "subnet_2" { # route_table enable_route_table = true - route_table_name = "test_rt" + route_table_name = "pvt" routes = [ { name = "rt-test" diff --git a/_example/default_subnet/output.tf b/_example/complete/outputs.tf similarity index 76% rename from _example/default_subnet/output.tf rename to _example/complete/outputs.tf index 978432e..d878740 100644 --- a/_example/default_subnet/output.tf +++ b/_example/complete/outputs.tf @@ -1,34 +1,29 @@ output "subnet_name" { - description = "The name of the subnet." value = module.subnet.default_subnet_name + description = "The name of the subnet." } output "subnet_id" { - description = "The subnet ID." value = module.subnet.default_subnet_id + description = "The subnet ID." } output "resource_group_name" { - description = "The name of the resource group in which the subnet is created in." value = module.resource_group.resource_group_name + description = "The name of the resource group in which the subnet is created in." } output "virtual_network_name" { + value = module.vnet.vnet_name description = "The name of the virtual network in which the subnet is created in." - value = join("", module.vnet.vnet_name) } output "address_prefixes" { - description = "The address prefixes for the subnet." value = module.subnet.default_subnet_address_prefixes + description = "The address prefixes for the subnet." } output "route_table_id" { - description = "The Route Table ID." value = module.subnet.route_table_id + description = "The Route Table ID." } - -output "route_table_associated_subnets" { - description = "The collection of Subnets associated with this route table." - value = module.subnet.*.route_table_associated_subnets[0] -} \ No newline at end of file diff --git a/_example/complete/versions.tf b/_example/complete/versions.tf new file mode 100644 index 0000000..9ffba34 --- /dev/null +++ b/_example/complete/versions.tf @@ -0,0 +1,13 @@ +# Terraform version +terraform { + required_version = ">= 1.6.6" +} + +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.85.0" + } + } +} diff --git a/_example/name-specific_subnet/example.tf b/_example/name-specific_subnet/example.tf index bbc77f2..a64f75e 100644 --- a/_example/name-specific_subnet/example.tf +++ b/_example/name-specific_subnet/example.tf @@ -2,40 +2,44 @@ provider "azurerm" { features {} } -module "resource_group" { - source = "clouddrove/resource-group/azure" - version = "1.0.2" - +locals { name = "app" environment = "test" - label_order = ["name", "environment", ] + label_order = ["name", "environment"] +} + +module "resource_group" { + source = "clouddrove/resource-group/azure" + version = "1.0.2" + name = local.name + environment = local.environment + label_order = local.label_order location = "North Europe" } module "vnet" { - source = "clouddrove/vnet/azure" - version = "1.0.3" - - name = "app" - environment = "test" + source = "clouddrove/vnet/azure" + version = "1.0.4" + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location - address_space = "10.0.0.0/16" + address_spaces = ["10.0.0.0/16"] } module "name_specific_subnet" { - source = "../.." - - name = "app" - environment = "test" - + source = "../.." + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location - virtual_network_name = join("", module.vnet.vnet_name) + virtual_network_name = module.vnet.vnet_name #subnet specific_name_subnet = true - specific_subnet_names = "SpecificSubnet" + specific_subnet_names = ["SpecificSubnet"] subnet_prefixes = ["10.0.1.0/24"] # route_table diff --git a/_example/name-specific_subnet/output.tf b/_example/name-specific_subnet/outputs.tf similarity index 88% rename from _example/name-specific_subnet/output.tf rename to _example/name-specific_subnet/outputs.tf index 5986c76..00f6145 100644 --- a/_example/name-specific_subnet/output.tf +++ b/_example/name-specific_subnet/outputs.tf @@ -1,35 +1,34 @@ output "specific_subnet_id" { - description = "The subnet ID." value = module.name_specific_subnet.specific_subnet_id + description = "The subnet ID." } output "specific_subnet_name" { - description = "The name of the subnet." value = module.name_specific_subnet.specific_subnet_name + description = "The name of the subnet." } output "resource_group_name" { - description = "The name of the resource group in which the subnet is created in." value = module.resource_group.resource_group_name + description = "The name of the resource group in which the subnet is created in." } output "virtual_network_name" { + value = module.vnet.vnet_name description = "The name of the virtual network in which the subnet is created in." - value = join("", module.vnet.vnet_name) } output "address_prefixes" { - description = "The address prefixes for the subnet." value = module.name_specific_subnet.specific_subnet_address_prefixes + description = "The address prefixes for the subnet." } output "route_table_id" { - description = "The Route Table ID." value = module.name_specific_subnet.route_table_id + description = "The Route Table ID." } output "route_table_associated_subnets" { + value = module.name_specific_subnet[*].route_table_associated_subnets[0] description = "The collection of Subnets associated with this route table." - value = module.name_specific_subnet.*.route_table_associated_subnets[0] } - diff --git a/_example/name-specific_subnet/versions.tf b/_example/name-specific_subnet/versions.tf new file mode 100644 index 0000000..9ffba34 --- /dev/null +++ b/_example/name-specific_subnet/versions.tf @@ -0,0 +1,13 @@ +# Terraform version +terraform { + required_version = ">= 1.6.6" +} + +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.85.0" + } + } +} diff --git a/_example/nat-gateway_subnet/example.tf b/_example/nat-gateway_subnet/example.tf index 4769022..0dd4aa1 100644 --- a/_example/nat-gateway_subnet/example.tf +++ b/_example/nat-gateway_subnet/example.tf @@ -2,36 +2,40 @@ provider "azurerm" { features {} } -module "resource_group" { - source = "clouddrove/resource-group/azure" - version = "1.0.2" - +locals { name = "app" environment = "test" - label_order = ["name", "environment", ] + label_order = ["name", "environment"] +} + +module "resource_group" { + source = "clouddrove/resource-group/azure" + version = "1.0.2" + name = local.name + environment = local.environment + label_order = local.label_order location = "North Europe" } module "vnet" { - source = "clouddrove/vnet/azure" - version = "1.0.3" - - name = "app" - environment = "test" + source = "clouddrove/vnet/azure" + version = "1.0.4" + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location - address_space = "10.0.0.0/16" + address_spaces = ["10.0.0.0/16"] } module "subnet" { - source = "../.." - - name = "app" - environment = "test" - + source = "../.." + name = local.name + environment = local.environment + label_order = local.label_order resource_group_name = module.resource_group.resource_group_name location = module.resource_group.resource_group_location - virtual_network_name = join("", module.vnet.vnet_name) + virtual_network_name = module.vnet.vnet_name #subnet create_nat_gateway = true diff --git a/_example/nat-gateway_subnet/output.tf b/_example/nat-gateway_subnet/outputs.tf similarity index 91% rename from _example/nat-gateway_subnet/output.tf rename to _example/nat-gateway_subnet/outputs.tf index be0e8d0..178a593 100644 --- a/_example/nat-gateway_subnet/output.tf +++ b/_example/nat-gateway_subnet/outputs.tf @@ -1,49 +1,49 @@ output "subnet_name" { - description = "The name of the subnet." value = module.subnet.default_subnet_name + description = "The name of the subnet." } output "subnet_id" { - description = "The subnet ID." value = module.subnet.default_subnet_id + description = "The subnet ID." } output "resource_group_name" { - description = "The name of the resource group in which the subnet is created in." value = module.resource_group.resource_group_name + description = "The name of the resource group in which the subnet is created in." } output "virtual_network_name" { + value = module.vnet.vnet_name description = "The name of the virtual network in which the subnet is created in." - value = join("", module.vnet.vnet_name) } output "address_prefixes" { - description = "The address prefixes for the subnet." value = module.subnet.default_subnet_address_prefixes + description = "The address prefixes for the subnet." } output "nat_gateway_id" { - description = "The ID of the NAT Gateway." value = module.subnet.nat_gateway_id + description = "The ID of the NAT Gateway." } output "public_ip_address" { - description = "The IP address value that was allocated." value = module.subnet.public_ip_address + description = "The IP address value that was allocated." } output "public_ip_id" { - description = " The ID of this Public IP." value = module.subnet.public_ip_id + description = " The ID of this Public IP." } output "route_table_id" { - description = "The Route Table ID." value = module.subnet.route_table_id + description = "The Route Table ID." } output "route_table_associated_subnets" { + value = module.subnet[*].route_table_associated_subnets[0] description = "The collection of Subnets associated with this route table." - value = module.subnet.*.route_table_associated_subnets[0] } diff --git a/_example/nat-gateway_subnet/versions.tf b/_example/nat-gateway_subnet/versions.tf new file mode 100644 index 0000000..9ffba34 --- /dev/null +++ b/_example/nat-gateway_subnet/versions.tf @@ -0,0 +1,13 @@ +# Terraform version +terraform { + required_version = ">= 1.6.6" +} + +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.85.0" + } + } +} diff --git a/_test/azure_subnet_test.go b/_test/azure_subnet_test.go deleted file mode 100644 index 91f1b29..0000000 --- a/_test/azure_subnet_test.go +++ /dev/null @@ -1,34 +0,0 @@ -// Managed By : CloudDrove -// Description : This Terratest is used to test the Terraform VPC module. -// Copyright @ CloudDrove. All Right Reserved. -package test - -import ( - "testing" - "github.com/gruntwork-io/terratest/modules/terraform" - "github.com/stretchr/testify/assert" -) - -func Test(t *testing.T) { - t.Parallel() - - terraformOptions := &terraform.Options{ - // Source path of Terraform directory. - TerraformDir: "../_example", - } - - // This will run 'terraform init' and 'terraform application' and will fail the test if any errors occur - terraform.InitAndApply(t, terraformOptions) - - // To clean up any resources that have been created, run 'terraform destroy' towards the end of the test - defer terraform.Destroy(t, terraformOptions) - - // To get the value of an output variable, run 'terraform output' - Id := terraform.Output(t, terraformOptions, "subnet_id") - Tags := terraform.OutputMap(t, terraformOptions, "tags") - - // Check that we get back the outputs that we expect - assert.Equal(t, "test-clouddrove-subnet", Tags["Name"]) - assert.Contains(t, Id, "/subscriptions") -} -} \ No newline at end of file diff --git a/main.tf b/main.tf index 281388f..c3a1154 100644 --- a/main.tf +++ b/main.tf @@ -1,13 +1,19 @@ - ## Managed By : CloudDrove ## Copyright @ CloudDrove. All Right Reserved. +##----------------------------------------------------------------------------- +## Locals declaration +##----------------------------------------------------------------------------- +locals { + subnet = var.specific_name_subnet == false ? length(var.subnet_names) : length(var.specific_subnet_names) +} +##----------------------------------------------------------------------------- +## Labels module callled that will be used for naming and tags. +##----------------------------------------------------------------------------- module "labels" { - - source = "clouddrove/labels/azure" - version = "1.0.0" - + source = "clouddrove/labels/azure" + version = "1.0.0" name = var.name environment = var.environment managedby = var.managedby @@ -15,41 +21,19 @@ module "labels" { repository = var.repository } -#Subnet +##----------------------------------------------------------------------------- +## Below resource will deploy subnet in your azure environment. +##----------------------------------------------------------------------------- resource "azurerm_subnet" "subnet" { - count = var.enable && var.specific_name_subnet == false ? length(var.subnet_names) : 0 - name = "${var.name}-${var.subnet_names[count.index]}" - resource_group_name = var.resource_group_name - address_prefixes = [var.subnet_prefixes[count.index]] - virtual_network_name = var.virtual_network_name - private_endpoint_network_policies_enabled = lookup(var.subnet_enforce_private_link_endpoint_network_policies, var.subnet_names[count.index], false) - service_endpoints = var.service_endpoints - private_link_service_network_policies_enabled = var.subnet_enforce_private_link_service_network_policies - - dynamic "delegation" { - for_each = var.delegation - content { - name = delegation.key - dynamic "service_delegation" { - for_each = toset(delegation.value) - content { - name = service_delegation.value.name - actions = service_delegation.value.actions - } - } - } - } -} - -resource "azurerm_subnet" "subnet2" { - count = var.enable && var.specific_name_subnet == true ? 1 : 0 - name = var.specific_subnet_names + count = var.enable ? local.subnet : 0 + name = var.specific_name_subnet == false ? "${var.name}-${element(var.subnet_names, count.index)}" : var.specific_subnet_names[0] resource_group_name = var.resource_group_name address_prefixes = [var.subnet_prefixes[count.index]] virtual_network_name = var.virtual_network_name - private_endpoint_network_policies_enabled = lookup(var.subnet_enforce_private_link_endpoint_network_policies, var.specific_subnet_names, false) service_endpoints = var.service_endpoints + service_endpoint_policy_ids = var.service_endpoint_policy_ids private_link_service_network_policies_enabled = var.subnet_enforce_private_link_service_network_policies + private_endpoint_network_policies_enabled = var.subnet_enforce_private_link_endpoint_network_policies dynamic "delegation" { for_each = var.delegation @@ -66,48 +50,53 @@ resource "azurerm_subnet" "subnet2" { } } -#Nat Gateway +##----------------------------------------------------------------------------- +## Below resource will deploy Nat Gateway in your azure environment. +##----------------------------------------------------------------------------- resource "azurerm_public_ip" "pip" { - count = var.create_nat_gateway ? 1 : 0 - allocation_method = "Static" - location = var.location + count = var.enable && var.create_nat_gateway ? 1 : 0 name = format("%s-nat-gateway-ip", module.labels.id) + allocation_method = var.allocation_method + location = var.location resource_group_name = var.resource_group_name - zones = var.public_ip_zones - sku = "Standard" - - tags = module.labels.tags + sku = var.sku + tags = module.labels.tags } resource "azurerm_nat_gateway" "natgw" { - count = var.create_nat_gateway ? 1 : 0 - location = var.location + count = var.enable && var.create_nat_gateway ? 1 : 0 name = format("%s-nat-gateway", module.labels.id) + location = var.location resource_group_name = var.resource_group_name - sku_name = "Standard" + sku_name = var.sku_name idle_timeout_in_minutes = var.nat_gateway_idle_timeout - - tags = module.labels.tags + zones = var.zones + tags = module.labels.tags } resource "azurerm_nat_gateway_public_ip_association" "pip_assoc" { - count = var.create_nat_gateway ? 1 : 0 - nat_gateway_id = join("", azurerm_nat_gateway.natgw.*.id) + count = var.enable && var.create_nat_gateway ? 1 : 0 + nat_gateway_id = join("", azurerm_nat_gateway.natgw[*].id) public_ip_address_id = azurerm_public_ip.pip[0].id } resource "azurerm_subnet_nat_gateway_association" "subnet_assoc" { - count = var.create_nat_gateway ? (var.specific_name_subnet == false ? length(azurerm_subnet.subnet.*.id) : length(azurerm_subnet.subnet2.*.id)) : 0 - nat_gateway_id = join("", azurerm_nat_gateway.natgw.*.id) - subnet_id = var.specific_name_subnet == false ? azurerm_subnet.subnet.*.id[count.index] : azurerm_subnet.subnet2.*.id[count.index] + count = var.enable && var.create_nat_gateway ? local.subnet : 0 + nat_gateway_id = join("", azurerm_nat_gateway.natgw[*].id) + subnet_id = element(azurerm_subnet.subnet[*].id, count.index) } -#Route Table +##------------------------------------------------------------------------------------------- +## Below resource will deploy Route Table in your azure environment and associate with subnet +##------------------------------------------------------------------------------------------- resource "azurerm_route_table" "rt" { - count = var.enable && var.enable_route_table ? 1 : 0 - name = var.route_table_name == null ? format("%s-route-table", module.labels.id) : format("%s-%s-route-table", module.labels.id, var.route_table_name) - location = var.location - resource_group_name = var.resource_group_name + count = var.enable && var.enable_route_table ? 1 : 0 + name = var.route_table_name == null ? format("%s-route-table", module.labels.id) : format("%s-%s-route-table", module.labels.id, var.route_table_name) + location = var.location + resource_group_name = var.resource_group_name + disable_bgp_route_propagation = var.disable_bgp_route_propagation + tags = module.labels.tags + dynamic "route" { for_each = var.routes content { @@ -117,18 +106,10 @@ resource "azurerm_route_table" "rt" { next_hop_in_ip_address = lookup(route.value, "next_hop_in_ip_address", null) } } - disable_bgp_route_propagation = var.disable_bgp_route_propagation - tags = module.labels.tags } resource "azurerm_subnet_route_table_association" "main" { - count = var.enable && var.enable_route_table && var.specific_name_subnet == false ? length(var.subnet_prefixes) : 0 - subnet_id = element(azurerm_subnet.subnet.*.id, count.index) - route_table_id = join("", azurerm_route_table.rt.*.id) -} - -resource "azurerm_subnet_route_table_association" "main2" { - count = var.enable && var.enable_route_table && var.specific_name_subnet ? length(var.subnet_prefixes) : 0 - subnet_id = element(azurerm_subnet.subnet2.*.id, count.index) - route_table_id = join("", azurerm_route_table.rt.*.id) + count = var.enable && var.enable_route_table ? local.subnet : 0 + subnet_id = element(azurerm_subnet.subnet[*].id, count.index) + route_table_id = azurerm_route_table.rt[0].id } diff --git a/output.tf b/outputs.tf similarity index 56% rename from output.tf rename to outputs.tf index 28151f6..d5fc459 100644 --- a/output.tf +++ b/outputs.tf @@ -1,55 +1,54 @@ output "specific_subnet_name" { + value = azurerm_subnet.subnet[*].name description = "The name of the subnet." - value = azurerm_subnet.subnet2.*.name } output "specific_subnet_id" { - description = "The subnet ID." - value = azurerm_subnet.subnet2.*.id + value = azurerm_subnet.subnet[*].id + description = "The name of the subnet." } output "specific_subnet_address_prefixes" { description = "The address prefixes for the subnet." - value = azurerm_subnet.subnet2.*.address_prefixes + value = azurerm_subnet.subnet[*].address_prefixes } output "default_subnet_name" { + value = azurerm_subnet.subnet[*].name description = "The name of the subnet." - value = azurerm_subnet.subnet.*.name } output "default_subnet_id" { description = "The subnet ID." - value = azurerm_subnet.subnet.*.id + value = azurerm_subnet.subnet[*].id } output "default_subnet_address_prefixes" { + value = azurerm_subnet.subnet[*].address_prefixes description = "The address prefixes for the subnet." - value = azurerm_subnet.subnet.*.address_prefixes } output "nat_gateway_id" { + value = azurerm_nat_gateway.natgw[*].id description = "The ID of the NAT Gateway." - value = azurerm_nat_gateway.natgw.*.id } output "public_ip_address" { + value = azurerm_public_ip.pip[*].ip_address description = "The IP address value that was allocated." - value = azurerm_public_ip.pip.*.ip_address } output "public_ip_id" { + value = azurerm_public_ip.pip[*].id description = " The ID of this Public IP." - value = azurerm_public_ip.pip.*.id } - output "route_table_id" { + value = var.enable && var.enable_route_table ? azurerm_route_table.rt[*].id : null description = "The Route Table ID." - value = var.enable_route_table ? azurerm_route_table.rt.*.id : null } output "route_table_associated_subnets" { + value = var.enable && var.enable_route_table ? azurerm_route_table.rt[*].subnets : null description = "The collection of Subnets associated with this route table." - value = var.enable_route_table ? azurerm_route_table.rt.*.subnets[0] : null -} \ No newline at end of file +} diff --git a/variable.tf b/variables.tf similarity index 78% rename from variable.tf rename to variables.tf index 0b5daa5..4d0f574 100644 --- a/variable.tf +++ b/variables.tf @@ -1,9 +1,6 @@ -#Module : LABEL -#Description : Terraform label module variables. - variable "name" { type = string - default = "" + default = null description = "Name (e.g. `app` or `cluster`)." } @@ -21,7 +18,7 @@ variable "repository" { variable "environment" { type = string - default = "" + default = null description = "Environment (e.g. `prod`, `dev`, `staging`)." } @@ -31,24 +28,6 @@ variable "label_order" { description = "Label order, e.g. `name`,`application`." } -variable "attributes" { - type = list(any) - default = [] - description = "Additional attributes (e.g. `1`)." -} - -variable "delimiter" { - type = string - default = "-" - description = "Delimiter to be used between `organization`, `environment`, `name` and `attributes`." -} - -variable "tags" { - type = map(any) - default = {} - description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)." -} - variable "managedby" { type = string default = "hello@clouddrove.com" @@ -63,13 +42,13 @@ variable "enable" { variable "resource_group_name" { type = string - default = "" + default = null description = "The name of an existing resource group to be imported." } variable "location" { type = string - default = "" + default = null description = "The location/region where the virtual network is created. Changing this forces a new resource to be created." } @@ -86,8 +65,8 @@ variable "subnet_names" { } variable "subnet_enforce_private_link_endpoint_network_policies" { - type = map(bool) - default = {} + type = bool + default = false description = "A map with key (string) `subnet name`, value (bool) `true` or `false` to indicate enable or disable network policies for the private link endpoint on the subnet. Default value is false." } @@ -118,32 +97,33 @@ EOD } variable "specific_name_subnet" { - type = bool - default = false + type = bool + default = false + description = "fName of the specific subnet." } variable "specific_subnet_names" { - type = string - default = "" + type = list(string) + default = [""] description = "A list of subnets inside the vNet." } variable "virtual_network_name" { type = string - default = "" + default = null description = "The name of the virtual network in which the subnet is created in" } -variable "public_ip_zones" { - description = "Public ip Zones to configure." +variable "zones" { type = list(string) default = null + description = "Public ip Zones to configure." } variable "nat_gateway_idle_timeout" { - description = "Idle timeout configuration in minutes for Nat Gateway" type = number default = 4 + description = "Idle timeout configuration in minutes for Nat Gateway" } variable "create_nat_gateway" { @@ -162,7 +142,6 @@ variable "routes" { type = list(map(string)) default = [] description = "List of objects that represent the configuration of each route." - /*ROUTES = [{ name = "", address_prefix = "", next_hop_type = "", next_hop_in_ip_address = "" }]*/ } variable "route_table_name" { @@ -176,3 +155,27 @@ variable "disable_bgp_route_propagation" { default = false description = "Boolean flag which controls propagation of routes learned by BGP on that route table." } + +variable "service_endpoint_policy_ids" { + type = list(string) + default = null + description = "The list of IDs of Service Endpoint Policies to associate with the subnet." +} + +variable "allocation_method" { + type = string + default = "Static" + description = "Defines the allocation method for this IP address. Possible values are Static or Dynamic." +} + +variable "sku" { + type = string + default = "Standard" + description = "The SKU of the Public IP. Accepted values are Basic and Standard. Defaults to Basic. Changing this forces a new resource to be created." +} + +variable "sku_name" { + type = string + default = "Standard" + description = "The SKU which should be used. At this time the only supported value is Standard. Defaults to Standard." +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..9ffba34 --- /dev/null +++ b/versions.tf @@ -0,0 +1,13 @@ +# Terraform version +terraform { + required_version = ">= 1.6.6" +} + +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.85.0" + } + } +}