Skip to content

Commit 3800ae1

Browse files
authored
fix: fixed issue where the vpe_ips output may not contain any IPs (#372)
1 parent a661fdf commit 3800ae1

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ Brewfile.lock.json
5454

5555
# Visual Studio Code
5656
.vscode/
57+
*.code-workspace

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ No modules.
9292
| [ibm_is_subnet_reserved_ip.ip](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_subnet_reserved_ip) | resource |
9393
| [ibm_is_virtual_endpoint_gateway.vpe](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_virtual_endpoint_gateway) | resource |
9494
| [ibm_is_virtual_endpoint_gateway_ip.endpoint_gateway_ip](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_virtual_endpoint_gateway_ip) | resource |
95+
| [ibm_is_virtual_endpoint_gateway.vpe](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/is_virtual_endpoint_gateway) | data source |
9596

9697
### Inputs
9798

main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,13 @@ resource "ibm_is_virtual_endpoint_gateway_ip" "endpoint_gateway_ip" {
133133
}
134134

135135
##############################################################################
136+
137+
##############################################################################
138+
# Datasource to load endpoint gateways details once resources are fully created
139+
##############################################################################
140+
141+
data "ibm_is_virtual_endpoint_gateway" "vpe" {
142+
depends_on = [ibm_is_virtual_endpoint_gateway_ip.endpoint_gateway_ip]
143+
count = length(ibm_is_virtual_endpoint_gateway.vpe)
144+
name = ibm_is_virtual_endpoint_gateway.vpe[count.index].name
145+
}

module-metadata.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@
202202
}
203203
}
204204
},
205-
"data_resources": {},
205+
"data_resources": {
206+
"data.ibm_is_virtual_endpoint_gateway.vpe": {
207+
"mode": "data",
208+
"type": "ibm_is_virtual_endpoint_gateway",
209+
"name": "vpe",
210+
"provider": {
211+
"name": "ibm"
212+
},
213+
"pos": {
214+
"filename": "main.tf",
215+
"line": 141
216+
}
217+
}
218+
},
206219
"module_calls": {}
207220
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
output "vpe_ips" {
22
description = "The endpoint gateway reserved ips"
3-
value = { for vpe_pg in ibm_is_virtual_endpoint_gateway.vpe :
3+
value = { for vpe_pg in data.ibm_is_virtual_endpoint_gateway.vpe :
44
vpe_pg.name => vpe_pg.ips }
55
}
66

tests/pr_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
package test
33

44
import (
5+
"fmt"
6+
"reflect"
57
"testing"
68

9+
"github.com/gruntwork-io/terratest/modules/terraform"
710
"github.com/stretchr/testify/assert"
811
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
912
)
@@ -61,13 +64,69 @@ func setupOptions(t *testing.T, prefix string, dir string) *testhelper.TestOptio
6164
return options
6265
}
6366

67+
// ValidateOutputMapSliceContent takes a map of Terraform output keys and values and it expects that the
68+
// map contains a set of key => Slices couples
69+
// It checks that the input map has length > 0 and for each key the related Slice is not empty
70+
// If the value related to a key is not a slice it returns an error message
71+
// The function returns a list of the output keys whose Slice has length 0
72+
// and an error message that includes details about which keys were missing.
73+
// If the input map is empty it returns only the related error message
74+
func ValidateOutputMapOfSlicesContent(inputMap map[string]interface{}) ([]string, error) {
75+
var failedKeys []string
76+
var err error
77+
// Set up ANSI escape codes for blue and bold text
78+
blueBold := "\033[1;34m"
79+
reset := "\033[0m"
80+
81+
// mapLen := len(inputMap)
82+
// fmt.Println("Len of inputMap is ", mapLen)
83+
if len(inputMap) == 0 {
84+
err = fmt.Errorf("Output: %s'The input map has zero elements'%s\n", blueBold, reset)
85+
} else {
86+
// going through the inputMap keys
87+
for k, v := range inputMap {
88+
if reflect.TypeOf(v).String() == "[]interface {}" {
89+
vArray := v.([]interface{})
90+
if len(vArray) == 0 {
91+
failedKeys = append(failedKeys, k)
92+
err = fmt.Errorf("Output: The keys %s'%s'%s have empty slices\n", blueBold, failedKeys, reset)
93+
}
94+
} else {
95+
failedKeys = append(failedKeys, k)
96+
err = fmt.Errorf("Output: The key %s'%s'%s value is not a slice\n", blueBold, k, reset)
97+
break
98+
}
99+
}
100+
}
101+
102+
return failedKeys, err
103+
}
104+
64105
func TestRunDefaultExample(t *testing.T) {
65106
t.Parallel()
66107

67108
options := setupOptions(t, "vpe-default", defaultExampleTerraformDir)
109+
options.SkipTestTearDown = true
68110
output, err := options.RunTestConsistency()
69111
assert.Nil(t, err, "This should not have errored")
70112
assert.NotNil(t, output, "Expected some output")
113+
// checking vpe_ips to exist
114+
outputs := terraform.OutputAll(options.Testing, options.TerraformOptions)
115+
expectedOutputs := []string{"vpe_ips"}
116+
_, outputErr := testhelper.ValidateTerraformOutputs(outputs, expectedOutputs...)
117+
assert.NoErrorf(t, outputErr, "Some outputs not found or nil")
118+
// checking vpe_ips to contain a set on not empty slices as expected
119+
mapToValidate, ok := outputs["vpe_ips"].(map[string]interface{})
120+
var outputErrMap error
121+
if !ok {
122+
outputErrMap = fmt.Errorf("Output: Failed to read value of key %s\n", "vpe_ips")
123+
} else {
124+
_, outputErrMap = ValidateOutputMapOfSlicesContent(mapToValidate)
125+
}
126+
127+
assert.NoErrorf(t, outputErr, "Some outputs not found or nil")
128+
assert.NoErrorf(t, outputErrMap, "Some outputs not having the expected structure")
129+
options.TestTearDown()
71130
}
72131

73132
func TestRunUpgradeExample(t *testing.T) {

0 commit comments

Comments
 (0)