## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Reviewed-on: #58
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
39 lines
1.4 KiB
Go
39 lines
1.4 KiB
Go
// Copyright (c) STACKIT
|
|
|
|
package utils
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
|
)
|
|
|
|
// AdaptRegion rewrites the region of a terraform plan
|
|
func AdaptRegion(ctx context.Context, configRegion types.String, planRegion *types.String, defaultRegion string, resp *resource.ModifyPlanResponse) {
|
|
// Get the intended region. This is either set directly set in the individual
|
|
// config or the provider region has to be used
|
|
var intendedRegion types.String
|
|
if configRegion.IsNull() {
|
|
if defaultRegion == "" {
|
|
core.LogAndAddError(ctx, &resp.Diagnostics, "set region", "no region defined in config or provider")
|
|
return
|
|
}
|
|
intendedRegion = types.StringValue(defaultRegion)
|
|
} else {
|
|
intendedRegion = configRegion
|
|
}
|
|
|
|
// check if the currently configured region corresponds to the planned region
|
|
// on mismatch override the planned region with the intended region
|
|
// and force a replacement of the resource
|
|
p := path.Root("region")
|
|
if !intendedRegion.Equal(*planRegion) {
|
|
resp.RequiresReplace.Append(p)
|
|
*planRegion = intendedRegion
|
|
}
|
|
resp.Diagnostics.Append(resp.Plan.SetAttribute(ctx, p, *planRegion)...)
|
|
}
|