Skip to content

Commit 0372717

Browse files
authored
Fix Project read issue when the constraints size is 0 (#237)
This commit fixes the issue seen on vRA 8.1, when there are no constraints in the project. * Fix Project read issue when the constraints size is 0 * Add tests to expand and flatten constraints methods Issues fixed: #235 Signed-off-by: Deepak Mettem <[email protected]>
1 parent 9f7fd18 commit 0372717

File tree

6 files changed

+256
-8
lines changed

6 files changed

+256
-8
lines changed

examples/project/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ resource "vra_project" "this" {
6565

6666
data "vra_project" "this" {
6767
name = vra_project.this.name
68+
69+
depends_on = [vra_project.this]
6870
}

vra/constraints.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func flattenConstraints(constraints []models.Constraint) []interface{} {
6969

7070
for _, constraint := range constraints {
7171
helper := make(map[string]interface{})
72-
helper["mandatory"] = constraint.Mandatory
73-
helper["expression"] = constraint.Expression
72+
helper["mandatory"] = *constraint.Mandatory
73+
helper["expression"] = *constraint.Expression
7474

7575
configConstraints = append(configConstraints, helper)
7676
}

vra/constraints_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package vra
2+
3+
import (
4+
"github.com/vmware/vra-sdk-go/pkg/models"
5+
6+
"testing"
7+
)
8+
9+
func TestExpandConstraints(t *testing.T) {
10+
c1 := map[string]interface{}{"mandatory": true, "expression": "Foo:Bar"}
11+
c2 := map[string]interface{}{"mandatory": false, "expression": "Env:Test"}
12+
13+
constraints := make([]interface{}, 0)
14+
expandedConstraints := expandConstraints(constraints)
15+
16+
if len(expandedConstraints) != 0 {
17+
t.Errorf("error while expanding when there are no constraints")
18+
}
19+
20+
constraints = append(constraints, c1)
21+
constraints = append(constraints, c2)
22+
23+
expandedConstraints = expandConstraints(constraints)
24+
25+
if len(expandedConstraints) != 2 {
26+
t.Errorf("not all constraints are expanded correctly")
27+
}
28+
29+
if *expandedConstraints[0].Expression != c1["expression"] || *expandedConstraints[0].Mandatory != c1["mandatory"] {
30+
t.Errorf("constraint %#v is not expanded correctly", c1)
31+
}
32+
33+
if *expandedConstraints[1].Expression != c2["expression"] || *expandedConstraints[1].Mandatory != c2["mandatory"] {
34+
t.Errorf("constraint %#v is not expanded correctly", c2)
35+
}
36+
}
37+
38+
func TestExpandConstraintsForProject(t *testing.T) {
39+
c1 := map[string]interface{}{"mandatory": true, "expression": "Foo:Bar"}
40+
c2 := map[string]interface{}{"mandatory": false, "expression": "Env:Test"}
41+
42+
constraints := make([]interface{}, 0)
43+
expandedConstraints := expandConstraintsForProject(constraints)
44+
45+
if len(expandedConstraints) != 0 {
46+
t.Errorf("error while expanding when there are no constraints")
47+
}
48+
49+
constraints = append(constraints, c1)
50+
constraints = append(constraints, c2)
51+
52+
expandedConstraints = expandConstraintsForProject(constraints)
53+
54+
if len(expandedConstraints) != 2 {
55+
t.Errorf("not all constraints are expanded correctly")
56+
}
57+
58+
if *expandedConstraints[0].Expression != c1["expression"] || *expandedConstraints[0].Mandatory != c1["mandatory"] {
59+
t.Errorf("constraint %#v is not expanded correctly", c1)
60+
}
61+
62+
if *expandedConstraints[1].Expression != c2["expression"] || *expandedConstraints[1].Mandatory != c2["mandatory"] {
63+
t.Errorf("constraint %#v is not expanded correctly", c2)
64+
}
65+
}
66+
67+
func TestFlattenConstraints(t *testing.T) {
68+
constraints := make([]models.Constraint, 0)
69+
flattenedConstraints := flattenConstraints(constraints)
70+
71+
if len(flattenedConstraints) != 0 {
72+
t.Errorf("error while flattening when there are no constraints")
73+
}
74+
75+
constraint1 := models.Constraint{Expression: withString("Foo:Bar"), Mandatory: withBool(true)}
76+
constraint2 := models.Constraint{Expression: withString("Env:Test"), Mandatory: withBool(false)}
77+
78+
constraints = append(constraints, constraint1)
79+
constraints = append(constraints, constraint2)
80+
81+
flattenedConstraints = flattenConstraints(constraints)
82+
83+
if len(flattenedConstraints) != 2 {
84+
t.Errorf("not all constraints are flattened correctly")
85+
}
86+
87+
fc1 := flattenedConstraints[0].(map[string]interface{})
88+
if fc1["expression"].(string) != *constraint1.Expression || fc1["mandatory"].(bool) != *constraint1.Mandatory {
89+
t.Errorf("constraint %#v, %#v is not flattened correctly", *constraint2.Expression, *constraint2.Mandatory)
90+
}
91+
92+
fc2 := flattenedConstraints[1].(map[string]interface{})
93+
if fc2["expression"].(string) != *constraint2.Expression || fc2["mandatory"].(bool) != *constraint2.Mandatory {
94+
t.Errorf("constraint %#v, %#v is not flattened correctly", *constraint2.Expression, *constraint2.Mandatory)
95+
}
96+
}

vra/data_source_project_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func TestAccDataSourceVRAProject(t *testing.T) {
2929
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
3030
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
3131
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
32+
resource.TestCheckResourceAttr(resourceName1, "constraints.#", "1"),
33+
resource.TestCheckResourceAttr(dataSourceName1, "constraints.#", "1"),
3234
),
3335
},
3436
{
@@ -37,6 +39,8 @@ func TestAccDataSourceVRAProject(t *testing.T) {
3739
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName2, "description"),
3840
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName2, "id"),
3941
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName2, "name"),
42+
resource.TestCheckResourceAttr(resourceName1, "constraints.#", "1"),
43+
resource.TestCheckResourceAttr(dataSourceName2, "constraints.#", "1"),
4044
),
4145
},
4246
},
@@ -49,6 +53,35 @@ func testAccDataSourceVRAProject(rInt int) string {
4953
resource "vra_project" "my-project" {
5054
name = "my-project-%d"
5155
description = "test project"
56+
57+
constraints {
58+
extensibility {
59+
expression = "foo:bar"
60+
mandatory = false
61+
}
62+
extensibility {
63+
expression = "environment:Test"
64+
mandatory = true
65+
}
66+
67+
network {
68+
expression = "foo:bar"
69+
mandatory = false
70+
}
71+
network {
72+
expression = "environment:Test"
73+
mandatory = true
74+
}
75+
76+
storage {
77+
expression = "foo:bar"
78+
mandatory = false
79+
}
80+
storage {
81+
expression = "environment:Test"
82+
mandatory = true
83+
}
84+
}
5285
}`, rInt)
5386
}
5487

vra/resource_project.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func resourceProjectUpdate(d *schema.ResourceData, m interface{}) error {
191191

192192
id := d.Id()
193193
administrators := expandUserList(d.Get("administrators").(*schema.Set).List())
194+
constraints := expandProjectConstraints(d.Get("constraints").(*schema.Set).List())
194195
description := d.Get("description").(string)
195196
machineNamingTemplate := d.Get("machine_naming_template").(string)
196197
members := expandUserList(d.Get("members").(*schema.Set).List())
@@ -202,6 +203,7 @@ func resourceProjectUpdate(d *schema.ResourceData, m interface{}) error {
202203

203204
_, err := apiClient.Project.UpdateProject(project.NewUpdateProjectParams().WithID(id).WithBody(&models.ProjectSpecification{
204205
Administrators: administrators,
206+
Constraints: constraints,
205207
Description: description,
206208
MachineNamingTemplate: machineNamingTemplate,
207209
Members: members,
@@ -325,7 +327,7 @@ func expandProjectConstraints(configProjectConstraints []interface{}) map[string
325327
}
326328

327329
func flattenProjectConstraints(projectConstraints map[string][]models.Constraint) []map[string]interface{} {
328-
if projectConstraints == nil {
330+
if len(projectConstraints) == 0 {
329331
return nil
330332
}
331333

vra/resource_project_test.go

Lines changed: 120 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,104 @@
11
package vra
22

33
import (
4-
"fmt"
5-
"os"
6-
"strconv"
7-
"testing"
8-
94
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
105
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
117
"github.com/hashicorp/terraform-plugin-sdk/terraform"
128
"github.com/vmware/vra-sdk-go/pkg/client/cloud_account"
139
"github.com/vmware/vra-sdk-go/pkg/client/location"
1410
"github.com/vmware/vra-sdk-go/pkg/client/project"
11+
"github.com/vmware/vra-sdk-go/pkg/models"
12+
13+
"fmt"
14+
"math/rand"
15+
"os"
16+
"strconv"
17+
"testing"
1518
)
1619

20+
func TestExpandProjectConstraints(t *testing.T) {
21+
c1 := map[string]interface{}{"mandatory": true, "expression": "Foo:Bar"}
22+
c2 := map[string]interface{}{"mandatory": false, "expression": "Env:Test"}
23+
24+
projectConstraints := make([]interface{}, 0)
25+
expandedConstraints := expandProjectConstraints(projectConstraints)
26+
27+
if len(expandedConstraints) != 0 {
28+
t.Errorf("error while expanding when there are no project constraints")
29+
}
30+
31+
constraints := make([]interface{}, 0)
32+
constraints = append(constraints, c1)
33+
constraints = append(constraints, c2)
34+
35+
pc1 := make(map[string]interface{})
36+
pc1["extensibility"] = schema.NewSet(testSetFunc, constraints)
37+
pc1["storage"] = schema.NewSet(testSetFunc, constraints)
38+
pc1["network"] = schema.NewSet(testSetFunc, constraints)
39+
40+
pc := make([]interface{}, 0)
41+
42+
pc = append(pc, pc1)
43+
44+
expandedConstraints = expandProjectConstraints(pc)
45+
46+
if expandedConstraints == nil {
47+
t.Errorf("expanded constraints is nil")
48+
}
49+
50+
if len(expandedConstraints) != 3 {
51+
t.Errorf("not all project constraints expanded correctly")
52+
}
53+
54+
if len(expandedConstraints["extensibility"]) != 2 || len(expandedConstraints["network"]) != 2 || len(expandedConstraints["storage"]) != 2 {
55+
t.Errorf("not all extensibility / network / storage constraints expanded correctly")
56+
}
57+
}
58+
59+
func testSetFunc(_ interface{}) int {
60+
return rand.Int()
61+
}
62+
63+
func TestFlattenProjectConstraints(t *testing.T) {
64+
projectConstraints := make(map[string][]models.Constraint)
65+
flattenedConstraints := flattenProjectConstraints(projectConstraints)
66+
67+
if len(flattenedConstraints) != 0 {
68+
t.Errorf("error while flattening when there are no project constraints")
69+
}
70+
71+
constraint1 := models.Constraint{Expression: withString("Foo:Bar"), Mandatory: withBool(true)}
72+
constraint2 := models.Constraint{Expression: withString("Env:Test"), Mandatory: withBool(false)}
73+
74+
constraints := make([]models.Constraint, 0)
75+
constraints = append(constraints, constraint1)
76+
constraints = append(constraints, constraint2)
77+
78+
projectConstraints["extensibility"] = constraints
79+
projectConstraints["network"] = constraints
80+
projectConstraints["storage"] = constraints
81+
82+
flattenedConstraints = flattenProjectConstraints(projectConstraints)
83+
84+
if len(flattenedConstraints) != 1 {
85+
t.Errorf("not all project constraints are flattened correctly")
86+
}
87+
88+
fc1 := flattenedConstraints[0]
89+
if len(fc1["extensibility"].([]interface{})) != 2 {
90+
t.Errorf("extensibility constraints are not flattened correctly")
91+
}
92+
93+
if len(fc1["network"].([]interface{})) != 2 {
94+
t.Errorf("network constraints are not flattened correctly")
95+
}
96+
97+
if len(fc1["storage"].([]interface{})) != 2 {
98+
t.Errorf("storage constraints are not flattened correctly")
99+
}
100+
}
101+
17102
func TestAccVRAProjectBasic(t *testing.T) {
18103
rInt := acctest.RandInt()
19104
resource.Test(t, resource.TestCase{
@@ -39,6 +124,7 @@ func TestAccVRAProjectBasic(t *testing.T) {
39124
"vra_project.my-project", "description", "update test project"),
40125
resource.TestCheckResourceAttr(
41126
"vra_project.my-project", "zone_assignments.#", "1"),
127+
resource.TestCheckResourceAttr("vra_project.my-project", "constraints.#", "1"),
42128
),
43129
},
44130
},
@@ -134,6 +220,35 @@ resource "vra_project" "my-project" {
134220
memory_limit_mb = 8192
135221
storage_limit_gb = 65536
136222
}
223+
224+
constraints {
225+
extensibility {
226+
expression = "foo:bar"
227+
mandatory = false
228+
}
229+
extensibility {
230+
expression = "environment:Test"
231+
mandatory = true
232+
}
233+
234+
network {
235+
expression = "foo:bar"
236+
mandatory = false
237+
}
238+
network {
239+
expression = "environment:Test"
240+
mandatory = true
241+
}
242+
243+
storage {
244+
expression = "foo:bar"
245+
mandatory = false
246+
}
247+
storage {
248+
expression = "environment:Test"
249+
mandatory = true
250+
}
251+
}
137252
}`, id, secret, rInt, rInt)
138253
}
139254

0 commit comments

Comments
 (0)