-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ConfigVariables to allow defining of Terraform variables withi…
…n a TestStep (#150)
- Loading branch information
1 parent
ea3044c
commit 7b0ac16
Showing
44 changed files
with
776 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
4 changes: 4 additions & 0 deletions
4
...st_ConfigDirectory_TestNameDirectory_AttributeDoesNotExist_MultipleFiles_Vars/provider.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" {} |
8 changes: 8 additions & 0 deletions
8
...Test_ConfigDirectory_TestNameDirectory_AttributeDoesNotExist_MultipleFiles_Vars/random.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
...t_ConfigDirectory_TestNameDirectory_AttributeDoesNotExist_MultipleFiles_Vars/terraform.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...stTest_ConfigDirectory_TestNameDirectory_AttributeDoesNotExist_MultipleFiles_Vars/vars.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variable "length" { | ||
type = number | ||
} | ||
|
||
variable "numeric" { | ||
type = bool | ||
} |
19 changes: 19 additions & 0 deletions
19
...tory_test/TestTest_ConfigDirectory_TestNameDirectory_AttributeDoesNotExist_Vars/random.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
...ectory_test/TestTest_ConfigDirectory_TestNameDirectory_AttributeDoesNotExist_Vars/vars.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variable "length" { | ||
type = number | ||
} | ||
|
||
variable "numeric" { | ||
type = bool | ||
} |
4 changes: 4 additions & 0 deletions
4
..._directory_test/TestTest_ConfigDirectory_TestNameDirectory_MultipleFiles_Vars/provider.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" {} |
8 changes: 8 additions & 0 deletions
8
...ig_directory_test/TestTest_ConfigDirectory_TestNameDirectory_MultipleFiles_Vars/random.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
...directory_test/TestTest_ConfigDirectory_TestNameDirectory_MultipleFiles_Vars/terraform.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
config_directory_test/TestTest_ConfigDirectory_TestNameDirectory_Vars/random.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
config_directory_test/TestTest_ConfigDirectory_TestNameDirectory_Vars/vars.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variable "length" { | ||
type = number | ||
} | ||
|
||
variable "numeric" { | ||
type = bool | ||
} |
4 changes: 4 additions & 0 deletions
4
..._ConfigDirectory_TestStepDirectory_AttributeDoesNotExist_MultipleFiles_Vars/1/provider.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" {} |
8 changes: 8 additions & 0 deletions
8
...st_ConfigDirectory_TestStepDirectory_AttributeDoesNotExist_MultipleFiles_Vars/1/random.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
...ConfigDirectory_TestStepDirectory_AttributeDoesNotExist_MultipleFiles_Vars/1/terraform.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...Test_ConfigDirectory_TestStepDirectory_AttributeDoesNotExist_MultipleFiles_Vars/1/vars.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variable "length" { | ||
type = number | ||
} | ||
|
||
variable "numeric" { | ||
type = bool | ||
} |
19 changes: 19 additions & 0 deletions
19
...ry_test/TestTest_ConfigDirectory_TestStepDirectory_AttributeDoesNotExist_Vars/1/random.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
...tory_test/TestTest_ConfigDirectory_TestStepDirectory_AttributeDoesNotExist_Vars/1/vars.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variable "length" { | ||
type = number | ||
} | ||
|
||
variable "numeric" { | ||
type = bool | ||
} |
4 changes: 4 additions & 0 deletions
4
...irectory_test/TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_Vars/1/provider.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" {} |
8 changes: 8 additions & 0 deletions
8
..._directory_test/TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_Vars/1/random.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
...rectory_test/TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_Vars/1/terraform.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
Oops, something went wrong.