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

bugfix: skip setting empty tags in request body #466

Merged
merged 2 commits into from
Apr 25, 2024
Merged

bugfix: skip setting empty tags in request body #466

merged 2 commits into from
Apr 25, 2024

Conversation

ms-henglu
Copy link
Collaborator

@ms-henglu ms-henglu commented Apr 24, 2024

fixes #462

In the following function, to suppress the diff of config: tags = null and state: tags = {}, it'll set plan.tags to {} even if it's not defined in config when upgrading from previous provider.

And this will fail the schema validation if the resource doesn't support "tags". And bypassing the validation won't mitigate this, because the empty tags will be set in the request body and user will get a bad request error.

The fix is adding tags to the request body only if it's not empty.

func (r *AzapiResource) tagsWithDefaultTags(config types.Map, body map[string]interface{}, state *AzapiResourceModel, resourceDef *aztypes.ResourceType) types.Map {
if config.IsNull() {
switch {
case body["tags"] != nil:
return tags.FlattenTags(body["tags"])
case len(r.ProviderData.Features.DefaultTags) != 0 && canResourceHaveProperty(resourceDef, "tags"):
defaultTags := r.ProviderData.Features.DefaultTags
if state == nil || state.Tags.IsNull() {
return tags.FlattenTags(defaultTags)
} else {
currentTags := tags.ExpandTags(state.Tags)
if !reflect.DeepEqual(currentTags, defaultTags) {
return tags.FlattenTags(defaultTags)
} else {
return state.Tags
}
}
// To suppress the diff of config: tags = null and state: tags = {}
case state != nil && !state.Tags.IsUnknown() && len(state.Tags.Elements()) == 0:
return state.Tags
}
}
return config

@ms-henglu ms-henglu merged commit 3afdfe2 into main Apr 25, 2024
7 checks passed
@ms-henglu ms-henglu deleted the issue-462 branch April 25, 2024 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provider 1.13.0: tags is not expected here
2 participants