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

[ECS] Refactoring tags workflow #919

Merged
merged 5 commits into from
Mar 19, 2021
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
4 changes: 4 additions & 0 deletions docs/resources/ecs_instance_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ resource "opentelekomcloud_ecs_instance_v1" "basic" {

availability_zone = "eu-de-01"
key_name = "KeyPair-test"

tags = {
muh = "kuh"
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestAccEcsV1Instance_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "availability_zone", env.OS_AVAILABILITY_ZONE),
resource.TestCheckResourceAttr(resourceName, "auto_recovery", "true"),
resource.TestCheckResourceAttr(resourceName, "security_groups.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.muh", "value-create"),
),
},
{
Expand All @@ -40,6 +41,7 @@ func TestAccEcsV1Instance_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "availability_zone", env.OS_AVAILABILITY_ZONE),
resource.TestCheckResourceAttr(resourceName, "auto_recovery", "false"),
resource.TestCheckResourceAttr(resourceName, "security_groups.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.muh", "value-update"),
),
},
},
Expand Down Expand Up @@ -157,8 +159,8 @@ resource "opentelekomcloud_ecs_instance_v1" "instance_1" {
auto_recovery = true

tags = {
foo = "bar"
key = "value"
muh = "value-create"
kuh = "value-create"
}
}
`, env.OS_IMAGE_ID, env.OS_VPC_ID, env.OS_NETWORK_ID, env.OS_AVAILABILITY_ZONE)
Expand Down Expand Up @@ -186,8 +188,7 @@ resource "opentelekomcloud_ecs_instance_v1" "instance_1" {
delete_disks_on_termination = true

tags = {
foo = "bar1"
key1 = "value"
muh = "value-update"
}
}
`, env.OS_IMAGE_ID, env.OS_VPC_ID, env.OS_NETWORK_ID, env.OS_AVAILABILITY_ZONE)
Expand Down
5 changes: 3 additions & 2 deletions opentelekomcloud/common/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
// TagsSchema returns the schema to use for tags.
func TagsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Type: schema.TypeMap,
Optional: true,
ValidateFunc: ValidateTags,
}
}

Expand Down
47 changes: 33 additions & 14 deletions opentelekomcloud/common/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"strings"
)

var (
tagsPattern = regexp.MustCompile(`^[0-9a-zA-Z-_@]+$`)
k8sTagsPattern = regexp.MustCompile(`^[./\-_A-Za-z0-9]+$`)
)

// ValidateStringList
// Deprecated
// Use validation.StringInSlice instead
Expand Down Expand Up @@ -255,23 +260,37 @@ func ValidateAntiDdosAppTypeID(v interface{}, k string) (ws []string, errors []e
return
}

func ValidateECSTagValue(v interface{}, _ string) (ws []string, errors []error) {
tagmap := v.(map[string]interface{})
vv := regexp.MustCompile(`^[0-9a-zA-Z-_]+$`)
for k, v := range tagmap {
value := v.(string)
if !vv.MatchString(value) {
errors = append(errors, fmt.Errorf(
"tag value must be string only contains digits, letters, underscores(_) and hyphens(-), but got %s=%s", k, value))
break
func ValidateTags(v interface{}, k string) (ws []string, errors []error) {
tagMap := v.(map[string]interface{})

for key, value := range tagMap {
if !tagsPattern.MatchString(key) {
errors = append(errors, fmt.Errorf("key %q doesn't comply with restrictions (%q): %q", k, tagsPattern, key))
}
if len(key) < 1 {
errors = append(errors, fmt.Errorf("key %q cannot be shorter than 1 characters: %q", k, key))
}
if len(key) > 36 {
errors = append(errors, fmt.Errorf("key %q cannot be longer than 36 characters: %q", k, key))
}

valueString := value.(string)
if !tagsPattern.MatchString(valueString) {
errors = append(errors, fmt.Errorf("value %q doesn't comply with restrictions (%q): %q", k, tagsPattern, valueString))
}
if len(valueString) < 1 {
errors = append(errors, fmt.Errorf("value %q cannot be shorter than 1 characters: %q", k, value))
}
if len(valueString) > 43 {
errors = append(errors, fmt.Errorf("value %q cannot be longer than 43 characters: %q", k, value))
}
}

return
}

func ValidateK8sTagsMap(v interface{}, k string) (ws []string, errors []error) {
values := v.(map[string]interface{})
pattern := regexp.MustCompile(`^[./\-_A-Za-z0-9]+$`)

for key, value := range values {
valueString := value.(string)
Expand All @@ -291,12 +310,12 @@ func ValidateK8sTagsMap(v interface{}, k string) (ws []string, errors []error) {
errors = append(errors, fmt.Errorf("value %q cannot be longer than 63 characters: %q", k, value))
}

if !pattern.MatchString(key) {
errors = append(errors, fmt.Errorf("key %q doesn't comply with restrictions (%q): %q", k, pattern, key))
if !k8sTagsPattern.MatchString(key) {
errors = append(errors, fmt.Errorf("key %q doesn't comply with restrictions (%q): %q", k, k8sTagsPattern, key))
}

if !pattern.MatchString(valueString) {
errors = append(errors, fmt.Errorf("value %q doesn't comply with restrictions (%q): %q", k, pattern, valueString))
if !k8sTagsPattern.MatchString(valueString) {
errors = append(errors, fmt.Errorf("value %q doesn't comply with restrictions (%q): %q", k, k8sTagsPattern, valueString))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func ResourceComputeBMSInstanceV2() *schema.Resource {
"tags": {
Type: schema.TypeMap,
Optional: true,
ValidateFunc: common.ValidateECSTagValue,
ValidateFunc: common.ValidateTags,
},
"stop_before_destroy": {
Type: schema.TypeBool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,13 @@ func ResourceComputeInstanceV2() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
ConflictsWith: []string{"tag"},
ValidateFunc: common.ValidateECSTagValue,
ValidateFunc: common.ValidateTags,
},
"tag": {
Type: schema.TypeMap,
Optional: true,
ConflictsWith: []string{"tags"},
ValidateFunc: common.ValidateECSTagValue,
ValidateFunc: common.ValidateTags,
Deprecated: "Use field tags instead",
},
"all_metadata": {
Expand Down
Loading