func()

in internal/services/azapi_resource.go [1095:1135]


func (r *AzapiResource) tagsWithDefaultTags(config types.Map, state *AzapiResourceModel, body types.Dynamic, resourceDef *aztypes.ResourceType) types.Map {
	// 1. use the tags in config if it's not null
	if !config.IsNull() {
		return config
	}

	// 2. use the tags in body if it's not null
	if !body.IsNull() && !body.IsUnknown() && !body.IsUnderlyingValueNull() && !body.IsUnderlyingValueUnknown() {
		if bodyObject, ok := body.UnderlyingValue().(types.Object); ok {
			if v, ok := bodyObject.Attributes()["tags"]; ok && v != nil {
				return tags.FlattenTags(v)
			}
		}
	}

	// 3. use the default tags if it's not null and the resource supports tags
	if len(r.ProviderData.Features.DefaultTags) != 0 && canResourceHaveProperty(resourceDef, "tags") {
		defaultTags := r.ProviderData.Features.DefaultTags

		// if it's a new resource or the tags in state is null, use the default tags
		if state == nil || state.Tags.IsNull() {
			return tags.FlattenTags(defaultTags)
		}

		// if the tags in state is not null and the tags in state is not equal to the default tags, use the default tags
		currentTags := tags.ExpandTags(state.Tags)
		if !reflect.DeepEqual(currentTags, defaultTags) {
			return tags.FlattenTags(defaultTags)
		}

		return state.Tags
	}

	// 4. To suppress the diff of config: tags = null and state: tags = {}
	if state != nil && !state.Tags.IsUnknown() && len(state.Tags.Elements()) == 0 {
		return state.Tags
	}

	// 5. return null if all the above cases are null
	return types.MapNull(types.StringType)
}