Skip to content

Commit

Permalink
bugfix: azapi_resource cannot handle tags with unknown values (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Apr 25, 2024
1 parent 1bdb8df commit 34332a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v1.13.1

BUG FIXES:
- Fix a bug that `azapi_resource` resource cannot handle tags with unknown values.
- Fix a bug that `null` value can't pass the schema validation.

## v1.13.0
Expand Down
23 changes: 18 additions & 5 deletions internal/azure/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ import (
)

func ExpandTags(input types.Map) map[string]string {
output := make(map[string]string)
output := make(map[string]types.String)
if diags := input.ElementsAs(context.Background(), &output, false); diags.HasError() {
return nil
}
return output
tags := make(map[string]string)
for k, v := range output {
// TODO: improve the validation based on the attr.Value
if v.IsUnknown() || v.IsNull() {
tags[k] = ""
} else {
tags[k] = v.ValueString()
}
}
return tags
}

func FlattenTags(input interface{}) types.Map {
Expand Down Expand Up @@ -49,7 +58,7 @@ func (v tagsValidator) ValidateMap(ctx context.Context, request validator.MapReq
return
}

tagsMap := make(map[string]string)
tagsMap := make(map[string]types.String)
if response.Diagnostics.Append(input.ElementsAs(context.Background(), &tagsMap, false)...); response.Diagnostics.HasError() {
return
}
Expand All @@ -63,8 +72,12 @@ func (v tagsValidator) ValidateMap(ctx context.Context, request validator.MapReq
response.Diagnostics.AddError("Invalid tags", fmt.Errorf("the maximum length for a tag key is 512 characters: %q is %d characters", k, len(k)).Error())
}

if len(v) > 256 {
response.Diagnostics.AddError("Invalid tags", fmt.Errorf("the maximum length for a tag value is 256 characters: the value for %q is %d characters", k, len(v)).Error())
if v.IsUnknown() || v.IsNull() {
continue
}

if len(v.ValueString()) > 256 {
response.Diagnostics.AddError("Invalid tags", fmt.Errorf("the maximum length for a tag value is 256 characters: the value for %q is %d characters", k, len(v.ValueString())).Error())
}
}

Expand Down

0 comments on commit 34332a3

Please sign in to comment.