Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make name argument to support Chinese character in vpc, subnet and peering #600

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func dataSourceVpcPeeringConnectionV2() *schema.Resource {
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateName,
ValidateFunc: validateString64WithChinese,
},
"status": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func testAccCheckVpcPeeringConnectionV2DataSourceID(n string) resource.TestCheck
func testAccDataSourceVpcPeeringConnectionV2Config(rName string) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc" "vpc_1" {
name = "%s"
name = "%s_1"
cidr = "192.168.0.0/16"
}

resource "huaweicloud_vpc" "vpc_2" {
name = "%s"
name = "%s_2"
cidr = "192.168.0.0/16"
}

Expand All @@ -94,5 +94,5 @@ data "huaweicloud_vpc_peering_connection" "by_vpc_ids" {
vpc_id = huaweicloud_vpc_peering_connection.peering_1.vpc_id
peer_vpc_id = huaweicloud_vpc_peering_connection.peering_1.peer_vpc_id
}
`, rName, rName+"2", rName)
`, rName, rName, rName)
}
2 changes: 1 addition & 1 deletion huaweicloud/resource_huaweicloud_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ResourceVirtualPrivateCloudV1() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validateName,
ValidateFunc: validateString64WithChinese,
},
"cidr": {
Type: schema.TypeString,
Expand Down
10 changes: 5 additions & 5 deletions huaweicloud/resource_huaweicloud_vpc_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ func ResourceVpcPeeringConnectionV2() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validateName,
},
"status": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validateString64WithChinese,
},
"vpc_id": {
Type: schema.TypeString,
Expand All @@ -58,6 +54,10 @@ func ResourceVpcPeeringConnectionV2() *schema.Resource {
ForceNew: true,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func testAccCheckVpcPeeringConnectionV2Exists(n string, peering *peerings.Peerin
func testAccVpcPeeringConnectionV2_basic(rName string) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc" "test" {
name = "%s"
name = "%s_1"
cidr = "192.168.0.0/16"
}

resource "huaweicloud_vpc" "test2" {
name = "%s"
name = "%s_2"
cidr = "192.168.0.0/16"
}

Expand All @@ -116,5 +116,5 @@ resource "huaweicloud_vpc_peering_connection" "test" {
vpc_id = huaweicloud_vpc.test.id
peer_vpc_id = huaweicloud_vpc.test2.id
}
`, rName, rName+"2", rName)
`, rName, rName, rName)
}
2 changes: 1 addition & 1 deletion huaweicloud/resource_huaweicloud_vpc_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ResourceVpcSubnetV1() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validateName,
ValidateFunc: validateString64WithChinese,
},
"cidr": {
Type: schema.TypeString,
Expand Down
17 changes: 17 additions & 0 deletions huaweicloud/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ func validateName(v interface{}, k string) (ws []string, errors []error) {
return
}

func validateString64WithChinese(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 64 characters: %q", k, value))
}

pattern := "^[\\-._A-Za-z0-9\u4e00-\u9fa5]+$"
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}

return
}

func validateCIDR(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, ipnet, err := net.ParseCIDR(value)
Expand Down