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

test: added basic example #640

Merged
merged 9 commits into from
Oct 13, 2023
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ To attach access management tags to resources in this module, you need the follo
<!-- BEGIN EXAMPLES HOOK -->
## Examples

- [ Basic Example](examples/basic)
- [ Default Example](examples/default)
- [ Landing Zone example](examples/landing_zone)
- [ No Prefix Example](examples/no-prefix)
Expand Down
3 changes: 3 additions & 0 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Basic Example

A very basic example to provision SLZ VPC. It creates VPC with 3 subnets with 1 subnet is publically exposed.
50 changes: 50 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
##############################################################################
# Resource Group
##############################################################################

module "resource_group" {
source = "terraform-ibm-modules/resource-group/ibm"
version = "1.0.6"
# 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
}
rajatagarwal-ibm marked this conversation as resolved.
Show resolved Hide resolved

#############################################################################
# Provision VPC
#############################################################################

module "slz_vpc" {
source = "../../"
resource_group_id = module.resource_group.resource_group_id
region = var.region
name = var.name
prefix = var.prefix
tags = var.resource_tags
subnets = {
zone-1 = [
{
name = "subnet-a"
cidr = "10.10.10.0/24"
public_gateway = true
acl_name = "vpc-acl"
}
],
zone-2 = [
{
name = "subnet-b"
cidr = "10.20.10.0/24"
public_gateway = false
acl_name = "vpc-acl"
}
],
zone-3 = [
{
name = "subnet-c"
cidr = "10.30.10.0/24"
public_gateway = false
acl_name = "vpc-acl"
}
]
}
}
13 changes: 13 additions & 0 deletions examples/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
##############################################################################
# Outputs
##############################################################################

output "vpc_id" {
value = module.slz_vpc.vpc_id
description = "VPC id"
}

output "vpc_crn" {
value = module.slz_vpc.vpc_crn
description = "VPC crn"
}
4 changes: 4 additions & 0 deletions examples/basic/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = var.region
}
35 changes: 35 additions & 0 deletions examples/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
variable "ibmcloud_api_key" {
description = "APIkey that's associated with the account to provision resources to"
type = string
sensitive = true
}

variable "region" {
description = "The region to which to deploy the VPC"
type = string
default = "us-south"
}

variable "prefix" {
description = "The prefix that you would like to append to your resources"
type = string
default = "basic-slz-vpc"
}

variable "name" {
description = "The name of the vpc"
type = string
default = "vpc"
}

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"
default = null
}

variable "resource_tags" {
description = "List of Tags for the resource created"
type = list(string)
default = null
}
10 changes: 10 additions & 0 deletions examples/basic/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.3.0, <1.6.0"
required_providers {
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
ibm = {
source = "IBM-Cloud/ibm"
version = "1.56.0"
}
}
}
20 changes: 8 additions & 12 deletions examples/default/main.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
##############################################################################
# Resource Group
# (if var.resource_group is null, create a new RG using var.prefix)
##############################################################################

resource "ibm_resource_group" "resource_group" {
count = var.resource_group != null ? 0 : 1
name = "${var.prefix}-rg"
quota_id = null
}

data "ibm_resource_group" "existing_resource_group" {
count = var.resource_group != null ? 1 : 0
name = var.resource_group
module "resource_group" {
source = "terraform-ibm-modules/resource-group/ibm"
version = "1.0.6"
# 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
}

#############################################################################
Expand All @@ -21,7 +17,7 @@ data "ibm_resource_group" "existing_resource_group" {
resource "ibm_resource_instance" "cos_instance" {
count = var.enable_vpc_flow_logs ? 1 : 0
name = "${var.prefix}-vpc-logs-cos"
resource_group_id = var.resource_group != null ? data.ibm_resource_group.existing_resource_group[0].id : ibm_resource_group.resource_group[0].id
resource_group_id = module.resource_group.resource_group_id
service = "cloud-object-storage"
plan = var.cos_plan
location = var.cos_location
Expand All @@ -41,7 +37,7 @@ resource "ibm_cos_bucket" "cos_bucket" {

module "slz_vpc" {
source = "../../"
resource_group_id = var.resource_group != null ? data.ibm_resource_group.existing_resource_group[0].id : ibm_resource_group.resource_group[0].id
resource_group_id = module.resource_group.resource_group_id
region = var.region
name = var.name
prefix = var.prefix
Expand Down
2 changes: 1 addition & 1 deletion examples/default/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ variable "region" {
variable "prefix" {
description = "The prefix that you would like to append to your resources"
type = string
default = "test-landing-zone"
default = "def-slz-vpc"
}

variable "name" {
Expand Down
20 changes: 8 additions & 12 deletions examples/no-prefix/main.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
##############################################################################
# Resource Group
# (if var.resource_group is null, create a new RG using var.prefix)
##############################################################################

resource "ibm_resource_group" "resource_group" {
count = var.resource_group != null ? 0 : 1
name = "${var.prefix}-rg"
quota_id = null
}

data "ibm_resource_group" "existing_resource_group" {
count = var.resource_group != null ? 1 : 0
name = var.resource_group
module "resource_group" {
source = "terraform-ibm-modules/resource-group/ibm"
version = "1.0.6"
# 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
}

#############################################################################
Expand All @@ -20,7 +16,7 @@ data "ibm_resource_group" "existing_resource_group" {

resource "ibm_resource_instance" "cos_instance" {
name = "${var.prefix}-vpc-logs-cos"
resource_group_id = var.resource_group != null ? data.ibm_resource_group.existing_resource_group[0].id : ibm_resource_group.resource_group[0].id
resource_group_id = module.resource_group.resource_group_id
service = "cloud-object-storage"
plan = "standard"
location = "global"
Expand All @@ -39,7 +35,7 @@ resource "ibm_cos_bucket" "cos_bucket" {

module "slz_vpc" {
source = "../../"
resource_group_id = var.resource_group != null ? data.ibm_resource_group.existing_resource_group[0].id : ibm_resource_group.resource_group[0].id
resource_group_id = module.resource_group.resource_group_id
region = var.region
name = var.name
prefix = null
Expand Down
24 changes: 24 additions & 0 deletions tests/other_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Tests in this file are run in the PR pipeline
package test

import (
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
"testing"

"github.com/stretchr/testify/assert"
)

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

options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
Testing: t,
TerraformDir: basicExampleTerraformDir,
Prefix: "bas-slz",
ResourceGroup: resourceGroup,
})

output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
assert.NotNil(t, output, "Expected some output")
}
7 changes: 4 additions & 3 deletions tests/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
)

const basicExampleTerraformDir = "examples/basic"
const defaultExampleTerraformDir = "examples/default"
const landingZoneExampleTerraformDir = "examples/landing_zone"
const resourceGroup = "geretain-test-resources"
Expand Down Expand Up @@ -47,7 +48,7 @@ func setupOptions(t *testing.T, prefix string, terraformDir string) *testhelper.
return options
}

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

options := setupOptions(t, "slz-vpc", defaultExampleTerraformDir)
Expand All @@ -64,7 +65,7 @@ func TestRunNoPrefixExample(t *testing.T) {
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
Testing: t,
TerraformDir: "examples/no-prefix",
Prefix: "test-no-prefix-lz-vpc",
Prefix: "no-prefix-lz",
TerraformVars: map[string]interface{}{
"name": testName,
},
Expand All @@ -90,7 +91,7 @@ func TestRunLandingZoneExample(t *testing.T) {
assert.NotNil(t, output, "Expected some output")
}

func TestRunUpgradeBasicExample(t *testing.T) {
func TestRunUpgradeDefaultExample(t *testing.T) {

t.Parallel()

Expand Down