Skip to content

Commit

Permalink
feat: module template refactor (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocofaigh authored Jun 30, 2023
1 parent d7f778c commit 82bd85c
Show file tree
Hide file tree
Showing 35 changed files with 94 additions and 129 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ terraform.rc
# Ignore .tfsec
.tfsec/

# Ignore brew lock
Brewfile.lock.json

# Ignore Mac files
.DS_Store

Expand Down
2 changes: 1 addition & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "go.sum|^.secrets.baseline$",
"lines": null
},
"generated_at": "2023-06-03T04:41:19Z",
"generated_at": "2023-06-03T04:42:19Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Follow this process to create and submit a Terraform module.
1. Create a repository from this repository template by clicking `Use this template` in the upper right of the GitHub UI.
&emsp;&emsp;&emsp;&emsp;<br>For more information about creating a repository from a template, see the [GitHub docs](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template).
1. Select `terraform-ibm-modules` as the owner.
1. Enter a name for the module in format `terraform-ibm-<NAME>`, where `<NAME>` reflects the type of infrastructure that the module manages.
1. Enter a name for the module in format `terraform-ibm-<name>`, where `<name>` reflects the type of infrastructure that the module manages.
&emsp;&emsp;&emsp;&emsp;<br>Use hyphens as delimiters for names with multiple words (for example, terraform-ibm-`activity-tracker`).
1. Provide a short description of the module.
&emsp;&emsp;&emsp;&emsp;<br>The description is displayed under the repository name on the [organization page](https://github.com/terraform-ibm-modules) and in the **About** section of the repository. Use the description to help users understand the purpose of your module. For more information, see [module names and descriptions](https://terraform-ibm-modules.github.io/documentation/#/implementation-guidelines?id=module-names-and-descriptions) in the docs.
Expand Down Expand Up @@ -79,8 +79,6 @@ After you implement the logic for your module and create examples and tests, upd

After the first PR for your module is merged, follow these post-merge steps:

1. Create a PR to enable the upgrade test by removing the `t.Skip` line in `tests/pr_test.go`.

<!-- Remove the content in this previous H2 heading -->
## Reference architectures

Expand Down Expand Up @@ -136,16 +134,15 @@ statement instead the previous block.
<!-- BEGIN EXAMPLES HOOK -->
## Examples

- [ Default example](examples/default)
- [ Example that uses existing resources](examples/existing-resources)
- [ Non default example](examples/non-default)
- [ Basic example](examples/basic)
- [ Complete example](examples/complete)
<!-- END EXAMPLES HOOK -->
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3, < 1.6 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion common-dev-assets
Submodule common-dev-assets updated 37 files
+2 −2 common-go-assets/cloudinfo-region-power-prefs.yaml
+1 −0 common-go-assets/common-permanent-resources.yaml
+1 −1 module-assets/.pre-commit-config.yaml
+2 −3 module-assets/ci/module-template-automation/.terraform-docs-config-template-module.yaml
+0 −3 module-assets/ci/module-template-automation/common_code/.gitignore
+0 −201 module-assets/ci/module-template-automation/common_code/LICENSE
+0 −1 module-assets/ci/module-template-automation/common_code/catalogValidationValues.json.template
+0 −2 module-assets/ci/module-template-automation/common_code/outputs.tf
+2 −4 module-assets/ci/module-template-automation/common_code/variables.tf
+6 −5 module-assets/ci/module-template-automation/common_code/version.tf
+8 −0 module-assets/ci/module-template-automation/examples/basic/README.md
+7 −2 module-assets/ci/module-template-automation/examples/basic/main.tf
+0 −0 module-assets/ci/module-template-automation/examples/basic/outputs.tf
+4 −4 module-assets/ci/module-template-automation/examples/basic/provider.tf
+6 −2 module-assets/ci/module-template-automation/examples/basic/variables.tf
+13 −0 module-assets/ci/module-template-automation/examples/basic/version.tf
+4 −0 module-assets/ci/module-template-automation/examples/complete/README.md
+3 −0 module-assets/ci/module-template-automation/examples/complete/main.tf
+4 −4 module-assets/ci/module-template-automation/examples/complete/outputs.tf
+8 −0 module-assets/ci/module-template-automation/examples/complete/provider.tf
+1 −1 module-assets/ci/module-template-automation/examples/complete/variables.tf
+13 −0 module-assets/ci/module-template-automation/examples/complete/version.tf
+0 −8 module-assets/ci/module-template-automation/examples/default/README.md
+0 −4 module-assets/ci/module-template-automation/examples/default/provider.tf
+0 −10 module-assets/ci/module-template-automation/examples/default/version.tf
+0 −3 module-assets/ci/module-template-automation/examples/existing-resources/README.md
+0 −3 module-assets/ci/module-template-automation/examples/existing-resources/main.tf
+0 −4 module-assets/ci/module-template-automation/examples/existing-resources/provider.tf
+0 −15 module-assets/ci/module-template-automation/examples/existing-resources/variables.tf
+0 −10 module-assets/ci/module-template-automation/examples/existing-resources/version.tf
+0 −5 module-assets/ci/module-template-automation/examples/non-default/README.md
+0 −3 module-assets/ci/module-template-automation/examples/non-default/main.tf
+0 −4 module-assets/ci/module-template-automation/examples/non-default/provider.tf
+0 −10 module-assets/ci/module-template-automation/examples/non-default/version.tf
+2 −4 module-assets/ci/module-template-automation/tests/other_test.go
+5 −8 module-assets/ci/module-template-automation/tests/pr_test.go
+44 −33 scripts/update-source/run-update-src.py
2 changes: 1 addition & 1 deletion cra-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# More info about this file at https://github.com/terraform-ibm-modules/common-pipeline-assets/blob/main/.github/workflows/terraform-test-pipeline.md#cra-config-yaml
version: "v1"
CRA_TARGETS:
- CRA_TARGET: "examples/default" # Target directory for CRA scan. If not provided, the CRA Scan will not be run.
- CRA_TARGET: "examples/basic" # Target directory for CRA scan. If not provided, the CRA Scan will not be run.
CRA_IGNORE_RULES_FILE: "cra-tf-validate-ignore-rules.json" # CRA Ignore file to use. If not provided, it checks the repo root directory for `cra-tf-validate-ignore-rules.json`
# CRA_ENVIRONMENT_VARIABLES: # An optional map of environment variables for CRA, where the key is the variable name and value is the value. Useful for providing TF_VARs.
# TF_VAR_sample: "sample value"
Expand Down
8 changes: 8 additions & 0 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Basic example

<!-- There is a pre-commit hook that will take the title of each example add include it in the repos main README.md -->
<!-- The text below should describe exactly what resources are provisioned / configured by the example -->

An end-to-end basic example that will provision the following:
- A new resource group if one is not passed in.
- A new Cloud Object Storage instance.
9 changes: 7 additions & 2 deletions examples/default/main.tf → examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
##############################################################################
# Resource Group
# Resource group
##############################################################################

module "resource_group" {
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5"
source = "terraform-ibm-modules/resource-group/ibm"
version = "1.0.5"
# if an existing resource group is not set (null) create a new one using prefix
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
existing_resource_group_name = var.resource_group
}

##############################################################################
# COS instance
##############################################################################

resource "ibm_resource_instance" "cos_instance" {
name = "${var.prefix}-cos"
resource_group_id = module.resource_group.resource_group_id
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
##############################################################################
# Outputs
# Provider config
##############################################################################

output "vpc_id" {
description = "ID of the existing VPC"
value = data.ibm_is_vpc.example.id
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = var.region
}
8 changes: 6 additions & 2 deletions examples/default/variables.tf → examples/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
##############################################################################
# Input variables
##############################################################################

variable "ibmcloud_api_key" {
type = string
description = "The IBM Cloud API Key"
Expand All @@ -13,12 +17,12 @@ variable "region" {
variable "prefix" {
type = string
description = "Prefix to append to all resources created by this example"
default = "terraform"
default = "basic"
}

variable "resource_group" {
type = string
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
description = "The name of an existing resource group to provision resources in to. If not set a new resource group will be created using the prefix variable"
default = null
}

Expand Down
13 changes: 13 additions & 0 deletions examples/basic/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
# Since hashicorp can include breaking changes in minor version updates, ensure to lock into a range here.
required_version = ">= 1.3, < 1.6"

# Ensure that there is always 1 example locked into the lowest provider version of the range defined in the main
# module's version.tf (usually a basic example), and 1 example that will always use the latest provider version.
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.49.0"
}
}
}
4 changes: 4 additions & 0 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Complete example

<!-- There is a pre-commit hook that will take the title of each example add include it in the repos main README.md -->
<!-- Add text below should describe exactly what resources are provisioned / configured by the example -->
3 changes: 3 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
##############################################################################
# Complete example
##############################################################################
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
##############################################################################

output "region" {
description = "Region"
description = "The region all resources were provisioned in"
value = var.region
}

output "prefix" {
description = "Prefix"
description = "The prefix used to name all provisioned resources"
value = var.prefix
}

output "resource_group_name" {
description = "RG name"
description = "The name of the resource group used"
value = var.resource_group
}

output "resource_tags" {
description = "Tags"
description = "List of resource tags"
value = var.resource_tags
}
8 changes: 8 additions & 0 deletions examples/complete/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
##############################################################################
# Provider config
##############################################################################

provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = var.region
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ variable "region" {
variable "prefix" {
type = string
description = "Prefix to append to all resources created by this example"
default = "terraform"
default = "complete"
}

variable "resource_group" {
Expand Down
13 changes: 13 additions & 0 deletions examples/complete/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
# Since hashicorp can include breaking changes in minor version updates, ensure to lock into a range here.
required_version = ">= 1.3, < 1.6"

# Ensure that there is always 1 example locked into the lowest provider version of the range defined in the main
# module's version.tf (usually a basic example), and 1 example that will always use the latest provider version.
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = ">= 1.49.0, < 2.0.0"
}
}
}
8 changes: 0 additions & 8 deletions examples/default/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions examples/default/provider.tf

This file was deleted.

10 changes: 0 additions & 10 deletions examples/default/version.tf

This file was deleted.

3 changes: 0 additions & 3 deletions examples/existing-resources/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions examples/existing-resources/main.tf

This file was deleted.

4 changes: 0 additions & 4 deletions examples/existing-resources/provider.tf

This file was deleted.

15 changes: 0 additions & 15 deletions examples/existing-resources/variables.tf

This file was deleted.

10 changes: 0 additions & 10 deletions examples/existing-resources/version.tf

This file was deleted.

5 changes: 0 additions & 5 deletions examples/non-default/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions examples/non-default/main.tf

This file was deleted.

4 changes: 0 additions & 4 deletions examples/non-default/provider.tf

This file was deleted.

10 changes: 0 additions & 10 deletions examples/non-default/version.tf

This file was deleted.

2 changes: 1 addition & 1 deletion module-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"variables": {},
"outputs": {},
"required_core": [
"\u003e= 1.0.0"
"\u003e= 1.3, \u003c 1.6"
],
"required_providers": {},
"managed_resources": {},
Expand Down
2 changes: 0 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
# description = "Description of my output"
# value = "value"
#}

##############################################################################
6 changes: 2 additions & 4 deletions tests/other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"github.com/stretchr/testify/assert"
)

const nonDefaultExampleTerraformDir = "examples/non-default"

func TestRunNonDefaultExample(t *testing.T) {
func TestRunBasicExample(t *testing.T) {
t.Parallel()

options := setupOptions(t, "non-default-tmp", nonDefaultExampleTerraformDir)
options := setupOptions(t, "mod-template-basic", "examples/basic")

output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
Expand Down
13 changes: 5 additions & 8 deletions tests/pr_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Tests in this file are run in the PR pipeline
// Tests in this file are run in the PR pipeline and the continuous testing pipeline
package test

import (
Expand All @@ -10,7 +10,7 @@ import (

// Use existing resource group
const resourceGroup = "geretain-test-resources"
const defaultExampleTerraformDir = "examples/default"
const completeExampleDir = "examples/complete"

func setupOptions(t *testing.T, prefix string, dir string) *testhelper.TestOptions {
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
Expand All @@ -22,10 +22,10 @@ func setupOptions(t *testing.T, prefix string, dir string) *testhelper.TestOptio
return options
}

func TestRunDefaultExample(t *testing.T) {
func TestRunCompleteExample(t *testing.T) {
t.Parallel()

options := setupOptions(t, "mod-template", defaultExampleTerraformDir)
options := setupOptions(t, "mod-template", completeExampleDir)

output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
Expand All @@ -35,10 +35,7 @@ func TestRunDefaultExample(t *testing.T) {
func TestRunUpgradeExample(t *testing.T) {
t.Parallel()

// TODO: Remove this line after the first merge to primary branch is complete to enable upgrade test
t.Skip("Skipping upgrade test until initial code is in primary branch")

options := setupOptions(t, "mod-template-upg", defaultExampleTerraformDir)
options := setupOptions(t, "mod-template-upg", completeExampleDir)

output, err := options.RunTestUpgrade()
if !options.UpgradeTestSkipped {
Expand Down
6 changes: 2 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
# Input Variables
##############################################################################

#variable "myvariable" {
#variable "my_variable" {
# type = string
# description = "A description of my variable"
# default = ""
# default = "default_value"
#}

##############################################################################
Loading

0 comments on commit 82bd85c

Please sign in to comment.