Skip to content

Commit 25aa9f7

Browse files
authored
Merge pull request #1592 from vmware/ease-id-validation
Ease validation on project and vpc ids
2 parents 5aa5075 + c1c22db commit 25aa9f7

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Diff for: nsxt/policy_utils.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"log"
1111
"net/url"
12-
"regexp"
1312
"strings"
1413
"time"
1514

@@ -539,11 +538,7 @@ func isPolicyPath(policyPath string) bool {
539538
}
540539

541540
func isValidID(id string) bool {
542-
v, err := regexp.MatchString("^[0-9a-zA-Z_\\-]+$", id)
543-
if err != nil {
544-
return false
545-
}
546-
return v
541+
return !strings.ContainsAny(id, "/&")
547542
}
548543

549544
func getPolicyIDFromPath(path string) string {

Diff for: nsxt/resource_nsxt_vpc_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ func TestAccResourceNsxtVpc_basic(t *testing.T) {
117117
})
118118
}
119119

120+
func TestAccResourceNsxtVpc_withID(t *testing.T) {
121+
testResourceName := "nsxt_vpc.test"
122+
123+
resource.Test(t, resource.TestCase{
124+
PreCheck: func() { testAccPreCheck(t); testAccOnlyVPC(t) },
125+
Providers: testAccProviders,
126+
CheckDestroy: func(state *terraform.State) error {
127+
return testAccNsxtVpcCheckDestroy(state, accTestVpcCreateAttributes["display_name"])
128+
},
129+
Steps: []resource.TestStep{
130+
{
131+
Config: testAccNsxtVpcWithID(),
132+
Check: resource.ComposeTestCheckFunc(
133+
testAccNsxtVpcExists(accTestVpcCreateAttributes["display_name"], testResourceName),
134+
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestVpcCreateAttributes["display_name"]),
135+
resource.TestCheckResourceAttr(testResourceName, "short_id", accTestVpcCreateAttributes["short_id"]),
136+
),
137+
},
138+
},
139+
})
140+
}
141+
120142
func TestAccResourceNsxtVpc_importBasic(t *testing.T) {
121143
name := getAccTestResourceName()
122144
testResourceName := "nsxt_vpc.test"
@@ -262,6 +284,17 @@ resource "nsxt_vpc" "test" {
262284
}`, testAccNsxtProjectContext(), accTestVpcUpdateAttributes["display_name"], accTestVpcUpdateAttributes["short_id"])
263285
}
264286

287+
func testAccNsxtVpcWithID() string {
288+
return testAccNsxtVpcPrerequisites() + fmt.Sprintf(`
289+
resource "nsxt_vpc" "test" {
290+
%s
291+
292+
nsx_id = "test.%s"
293+
display_name = "%s"
294+
short_id = "%s"
295+
}`, testAccNsxtProjectContext(), accTestVpcCreateAttributes["display_name"], accTestVpcCreateAttributes["display_name"], accTestVpcCreateAttributes["short_id"])
296+
}
297+
265298
// We use short_id as nsx_id to make sure NSX populates the short_id correctly
266299
func testAccNsxtVpcMinimalisticNoShortId() string {
267300
return testAccNsxtVpcPrerequisites() + fmt.Sprintf(`

0 commit comments

Comments
 (0)