Skip to content

Commit ad2d02b

Browse files
author
Anna Khmelnitsky
committed
Remove member type validation in group criteria
This follows NSX allowing mixing some member types within same expression block, which was not allowed earlier. Rather then following NSX validation for which member types mixes are permitted, terraform will let NSX do the validation for this part. Other validations on terraform side - such as same expression type - are not affected by this change. Signed-off-by: Anna Khmelnitsky <akhmelnitsky@vmware.com>
1 parent d7712eb commit ad2d02b

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

nsxt/resource_nsxt_policy_group.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package nsxt
66
import (
77
"fmt"
88
"log"
9-
"strings"
109

1110
"github.com/vmware/terraform-provider-nsxt/api/infra/domains"
1211
utl "github.com/vmware/terraform-provider-nsxt/api/utl"
@@ -352,22 +351,8 @@ func resourceNsxtPolicyGroupExistsInDomainPartial(domain string) func(sessionCon
352351
}
353352
}
354353

355-
func validateNestedGroupConditions(conditions []interface{}) (string, error) {
356-
memberType := ""
357-
for _, cond := range conditions {
358-
condMap := cond.(map[string]interface{})
359-
condMemberType := condMap["member_type"].(string)
360-
if memberType != "" && condMemberType != memberType {
361-
return "", fmt.Errorf("Nested conditions must all use the same member_type, but found '%v' with '%v'", condMemberType, memberType)
362-
}
363-
memberType = condMemberType
364-
}
365-
return memberType, nil
366-
}
367-
368354
type criteriaMeta struct {
369355
ExpressionType string
370-
MemberType string
371356
IsNested bool
372357
criteriaBlocks []interface{}
373358
}
@@ -381,25 +366,12 @@ func validateGroupCriteriaSets(criteriaSets []interface{}) ([]criteriaMeta, erro
381366
seenExp := ""
382367
criteriaMap := criteriaBlock.(map[string]interface{})
383368
for expName, expVal := range criteriaMap {
384-
memberType := ""
385369
expValList := expVal.([]interface{})
386370
if len(expValList) > 0 {
387371
if seenExp != "" {
388-
return nil, fmt.Errorf("Criteria blocks are homogeneous, but found '%v' with '%v'", expName, seenExp)
389-
}
390-
if expName == "condition" {
391-
mType, err := validateNestedGroupConditions(expValList)
392-
if err != nil {
393-
return nil, err
394-
}
395-
memberType = mType
396-
} else if strings.HasSuffix(expName, "_expression") {
397-
memberType = ""
398-
} else {
399-
return nil, fmt.Errorf("Unknown criteria: %v", expName)
372+
return nil, fmt.Errorf("Criteria blocks should be homogeneous, but found '%v' with '%v'", expName, seenExp)
400373
}
401374
criteriaType := criteriaMeta{
402-
MemberType: memberType,
403375
ExpressionType: expName,
404376
IsNested: len(expValList) > 1,
405377
criteriaBlocks: expValList}
@@ -422,10 +394,6 @@ func validateGroupConjunctions(conjunctions []interface{}, criteriaMeta []criter
422394
return fmt.Errorf("AND conjunctions must use the same types of criteria expressions, but got %v and %v",
423395
metaA.ExpressionType, metaB.ExpressionType)
424396
}
425-
if metaA.MemberType != metaB.MemberType {
426-
return fmt.Errorf("AND conjunctions with conditions must have the same member types, but got %v and %v",
427-
metaA.MemberType, metaB.MemberType)
428-
}
429397
}
430398
}
431399
return nil

nsxt/resource_nsxt_policy_group_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@ func TestAccResourceNsxtPolicyGroup_basicImport_multitenancy(t *testing.T) {
5959
})
6060
}
6161

62+
func TestAccResourceNsxtPolicyGroup_mixedCriteria(t *testing.T) {
63+
name := getAccTestResourceName()
64+
resourceName := "nsxt_policy_group"
65+
testResourceName := fmt.Sprintf("%s.test", resourceName)
66+
67+
resource.ParallelTest(t, resource.TestCase{
68+
PreCheck: func() { testAccPreCheck(t) },
69+
Providers: testAccProviders,
70+
CheckDestroy: func(state *terraform.State) error {
71+
return testAccNsxtPolicyGroupCheckDestroy(state, name, defaultDomain)
72+
},
73+
Steps: []resource.TestStep{
74+
{
75+
Config: testAccNsxtPolicyGroupMixedCriteriaTemplate(name),
76+
Check: resource.ComposeTestCheckFunc(
77+
testAccNsxtPolicyGroupExists(testResourceName, defaultDomain),
78+
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
79+
resource.TestCheckResourceAttrSet(testResourceName, "path"),
80+
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
81+
resource.TestCheckResourceAttr(testResourceName, "criteria.#", "1"),
82+
resource.TestCheckResourceAttr(testResourceName, "criteria.0.condition.#", "2"),
83+
),
84+
},
85+
},
86+
})
87+
}
88+
6289
func TestAccResourceNsxtPolicyGroup_empty(t *testing.T) {
6390
testAccResourceNsxtPolicyGroupEmpty(t, false, func() {
6491
testAccPreCheck(t)
@@ -1575,3 +1602,25 @@ resource "nsxt_policy_group" "test" {
15751602
}
15761603
`, name)
15771604
}
1605+
1606+
func testAccNsxtPolicyGroupMixedCriteriaTemplate(name string) string {
1607+
return fmt.Sprintf(`
1608+
resource "nsxt_policy_group" "test" {
1609+
display_name = "%s"
1610+
1611+
criteria {
1612+
condition {
1613+
key = "Tag"
1614+
member_type = "Segment"
1615+
operator = "EQUALS"
1616+
value = "blue"
1617+
}
1618+
condition {
1619+
key = "Tag"
1620+
member_type = "SegmentPort"
1621+
operator = "EQUALS"
1622+
value = "orange"
1623+
}
1624+
}
1625+
}`, name)
1626+
}

0 commit comments

Comments
 (0)