func()

in internal/services/azapi_resource.go [1137:1193]


func (r *AzapiResource) locationWithDefaultLocation(configLocation types.String, planLocation types.String, state *AzapiResourceModel, body types.Dynamic, resourceDef *aztypes.ResourceType) types.String {
	// location field has a field level plan modifier which suppresses the diff if the location is not actually changed
	config := planLocation
	// For the following cases, we need to use the location in config as the specified location
	// case 1. To create a new resource, the location is not specified in config, then the planned location will be unknown
	// case 2. To update a resource, the location is not specified in config, then the planned location will be the state location
	if config.IsUnknown() || configLocation.IsNull() {
		config = configLocation
	}

	// 1. use the location in config if it's not null
	if !config.IsNull() {
		return config
	}

	// 2. use the location 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()["location"]; ok && v != nil {
				if strV, ok := v.(types.String); ok {
					return strV
				}
			}
		}
	}

	// 3. use the state location if it's not specified in config but returned by the API
	if state != nil && state.Location.ValueString() != "" {
		return state.Location
	}

	// 4. use the default location if it's not null and the resource supports location
	if len(r.ProviderData.Features.DefaultLocation) != 0 && canResourceHaveProperty(resourceDef, "location") {
		defaultLocation := r.ProviderData.Features.DefaultLocation

		// if it's a new resource or the location in state is null, use the default location
		if state == nil || state.Location.IsNull() {
			return types.StringValue(defaultLocation)
		}

		// if the location in state is not null and the location in state is not equal to the default location, use the default location
		currentLocation := state.Location.ValueString()
		if location.Normalize(currentLocation) != location.Normalize(defaultLocation) {
			return types.StringValue(defaultLocation)
		}

		return state.Location
	}

	// 5. To suppress the diff of config: location = null and state: location = ""
	if state != nil && !state.Location.IsUnknown() && state.Location.ValueString() == "" {
		return state.Location
	}

	// 6. return null if all the above cases are null
	return types.StringNull()
}