Skip to content

Commit

Permalink
Adding ConfigVariables to allow defining of Terraform variables withi…
Browse files Browse the repository at this point in the history
…n a TestStep (#150)
  • Loading branch information
bendbennett committed Jul 24, 2023
1 parent ea3044c commit 7b0ac16
Show file tree
Hide file tree
Showing 44 changed files with 776 additions and 10 deletions.
175 changes: 175 additions & 0 deletions config/variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package config

import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"os"
"path/filepath"

"golang.org/x/exp/constraints"
)

const autoTFVarsJson = "generated.auto.tfvars.json"

type Variables map[string]Variable

func (v Variables) Write(dest string) error {
buf := bytes.NewBuffer(nil)

buf.Write([]byte(`{`))

for k, val := range v {
j, err := val.MarshalJSON()

if err != nil {
return err
}

buf.Write([]byte(fmt.Sprintf("%q: ", k)))
buf.Write(j)
buf.Write([]byte(","))
}

b := bytes.TrimRight(buf.Bytes(), ",")

buf = bytes.NewBuffer(b)

buf.Write([]byte(`}`))

outFilename := filepath.Join(dest, autoTFVarsJson)

err := os.WriteFile(outFilename, buf.Bytes(), 0700)

if err != nil {
return err
}

return nil
}

type Variable interface {
json.Marshaler
}

func BoolVariable(value bool) boolVariable {
return boolVariable{
value: value,
}
}

type boolVariable struct {
value bool
}

func (t boolVariable) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
}

func ListVariable(value ...Variable) listVariable {
return listVariable{
value: value,
}
}

type listVariable struct {
value []Variable
}

func (t listVariable) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
}

func MapVariable(value map[string]Variable) mapVariable {
return mapVariable{
value: value,
}
}

type mapVariable struct {
value map[string]Variable
}

func (t mapVariable) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
}

func ObjectVariable(value map[string]Variable) objectVariable {
return objectVariable{
value: value,
}
}

type objectVariable struct {
value map[string]Variable
}

func (t objectVariable) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
}

type number interface {
constraints.Float | constraints.Integer | *big.Float
}

func NumberVariable[T number](value T) numberVariable {
return numberVariable{
value: value,
}
}

type numberVariable struct {
value any
}

func (t numberVariable) MarshalJSON() ([]byte, error) {
switch v := t.value.(type) {
case *big.Float:
return []byte(v.Text('g', -1)), nil
}

return json.Marshal(t.value)
}

func SetVariable(value ...Variable) setVariable {
return setVariable{
value: value,
}
}

type setVariable struct {
value []Variable
}

func (t setVariable) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
}

func StringVariable(value string) stringVariable {
return stringVariable{
value: value,
}
}

type stringVariable struct {
value string
}

func (t stringVariable) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
}

func TupleVariable(value ...Variable) tupleVariable {
return tupleVariable{
value: value,
}
}

type tupleVariable struct {
value []Variable
}

func (t tupleVariable) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "length" {
type = number
}

variable "numeric" {
type = bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}

provider "random" {}

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "length" {
type = number
}

variable "numeric" {
type = bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = 8

numeric = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}

provider "random" {}

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "length" {
type = number
}

variable "numeric" {
type = bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "length" {
type = number
}

variable "numeric" {
type = bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}

provider "random" {}

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "length" {
type = number
}

variable "numeric" {
type = bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}
Loading

0 comments on commit 7b0ac16

Please sign in to comment.