Skip to content

Commit

Permalink
chore: implement generator plop (hadenlabs#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
luismayta committed May 8, 2022
1 parent 59779e8 commit 024d458
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 5 deletions.
4 changes: 2 additions & 2 deletions provision/generators/plop/generators/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const testGenerator: PlopGeneratorConfig = {
],
actions: (data) => {
const answers = data as Answers
const containerPath = `${testPath}/replace_name-${slugify(answers.testName, '-')}`
const containerPath = `${testPath}/user-${slugify(answers.testName, '-')}`
const containerDocsPath = `${containerPath}/docs`

if (!pathExists(containerPath)) {
Expand All @@ -37,7 +37,7 @@ export const testGenerator: PlopGeneratorConfig = {
actions.push({
type: 'add',
templateFile: `${baseTemplatesPath}/test.add.hbs`,
path: `${testPath}/replace_name_${slugify(answers.testName, '_')}_test.go`,
path: `${testPath}/user_${slugify(answers.testName, '_')}_test.go`,
abortOnFail: true
})

Expand Down
34 changes: 31 additions & 3 deletions provision/generators/plop/templates/test.add.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/hadenlabs/terraform-aws-iam-s3-user/config"
"github.com/stretchr/testify/assert"
"github.com/hadenlabs/terraform-aws-iam-s3-user/internal/common/log"
"github.com/hadenlabs/terraform-aws-iam-s3-user/internal/app/external/faker"
"github.com/hadenlabs/terraform-aws-iam-s3-user/internal/testutil"
)

func Test{{pascalCase testName}}Success(t *testing.T) {
Expand All @@ -17,12 +19,38 @@ func Test{{pascalCase testName}}Success(t *testing.T) {
logger.Debugf(
"values for test terraform-aws-iam-s3-user is",
)
tags := map[string]interface{}{
"tag1": "tags1",
}
namespace := testutil.Company
stage := testutil.Stage
name := faker.Server().Name()
enabled := true
useFullName := true
s3Actions := []string{
"s3:GetObject",
"s3:GetObjectAcl",
"s3:ListObjects",
"s3:ListBucket",
"s3:ListAllMyBuckets",
}
s3Resources := []string{
"arn:aws:s3:::bucket-name/*",
}

terraformOptions := &terraform.Options{
// The path to where your Terraform code is located
TerraformDir: "replace-name-{{dashCase testName}}",
TerraformDir: "user-{{dashCase testName}}",
Upgrade: true,
Vars: map[string]interface{}{
"namespace": namespace,
"stage": stage,
"name": name,
"enabled": enabled,
"tags": tags,
"use_fullname": useFullName,
"s3_actions": s3Actions,
"s3_resources": s3Resources,
},
}

Expand All @@ -31,6 +59,6 @@ func Test{{pascalCase testName}}Success(t *testing.T) {

// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
outputID := terraform.Output(t, terraformOptions, "id")
assert.NotEmpty(t, outputID, outputID)
outputUserName := terraform.Output(t, terraformOptions, "user_name")
assert.NotEmpty(t, outputUserName, outputUserName)
}
9 changes: 9 additions & 0 deletions provision/generators/plop/templates/test/main.add.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module "main" {
source = "../.."
depends_on = []
enabled = var.enabled
name = var.name
stage = var.stage
namespace = var.namespace
tags = var.tags
use_fullname = var.use_fullname
s3_actions = var.s3_actions
s3_resources = var.s3_resources
}
36 changes: 36 additions & 0 deletions provision/generators/plop/templates/test/outputs.add.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
output "enabled" {
description = "Enabled property of module"
value = module.main.enabled
}

output "user_name" {
description = "Normalized IAM user name"
value = module.main.user_name
}

output "user_arn" {
description = "The ARN assigned by AWS for this user"
value = module.main.user_arn
}

output "user_unique_id" {
description = "The unique ID assigned by AWS"
value = module.main.user_unique_id
}

output "access_key_id" {
sensitive = true
description = "The access key ID"
value = module.main.access_key_id
}

output "secret_access_key" {
sensitive = true
description = "The secret access key. This will be written to the state file in plain-text"
value = module.main.secret_access_key
}

output "use_fullname" {
description = "return if enabled use fullname"
value = module.main.use_fullname
}
47 changes: 47 additions & 0 deletions provision/generators/plop/templates/test/variables.add.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variable "namespace" {
type = string
default = null
description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique"
}

variable "stage" {
type = string
default = null
description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'"
}

variable "name" {
type = string
description = "name"
}

variable "tags" {
type = map(string)
description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`"
default = {}
}

variable "enabled" {
type = bool
default = true
description = "Set to false to prevent the module from creating any resources"
}

variable "use_fullname" {
type = bool
default = false
description = <<-EOT
If set to 'true' then the full ID for the IAM user name (e.g. `[var.namespace]-[var.stage]-[var.name]`) will be used.
EOT
}

variable "s3_actions" {
type = list(string)
default = ["s3:GetObject"]
description = "Actions to allow in the policy"
}

variable "s3_resources" {
type = list(string)
description = "S3 resources to apply the actions specified in the policy"
}
4 changes: 4 additions & 0 deletions provision/generators/plop/templates/test/versions.add.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ terraform {
required_version = ">= 0.12.20, < 2.0"

required_providers {
aws = {
version = ">= 2.51, < 4.0"
source = "hashicorp/aws"
}
}
}

0 comments on commit 024d458

Please sign in to comment.