Skip to content

Commit dfab5d7

Browse files
committed
registry_gpg_key: Add plan mod for provider org default changes
1 parent 233580a commit dfab5d7

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

internal/provider/provider_custom_diffs.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package provider
33
import (
44
"context"
55

6+
"github.com/hashicorp/terraform-plugin-framework/path"
7+
"github.com/hashicorp/terraform-plugin-framework/resource"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
69
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
710
)
811

@@ -13,11 +16,30 @@ func customizeDiffIfProviderDefaultOrganizationChanged(c context.Context, diff *
1316
plannedOrg := diff.Get("organization").(string)
1417

1518
if configOrg.IsNull() && config.Organization != plannedOrg {
16-
// There is no organization configured on the resource, yet it is different from
17-
// the state organization. We must conclude that the provider default organization changed.
19+
// There is no organization configured on the resource, yet the provider org is different from
20+
// the planned organization. We must conclude that the provider default organization changed.
1821
if err := diff.SetNew("organization", config.Organization); err != nil {
1922
return err
2023
}
2124
}
2225
return nil
2326
}
27+
28+
func modifyPlanForDefaultOrganizationChange(ctx context.Context, providerDefaultOrg string, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
29+
if req.State.Raw.IsNull() {
30+
return
31+
}
32+
33+
orgPath := path.Root("organization")
34+
35+
var configOrg, plannedOrg *string
36+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, orgPath, &configOrg)...)
37+
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, orgPath, &plannedOrg)...)
38+
39+
if configOrg == nil && plannedOrg != nil && providerDefaultOrg != *plannedOrg {
40+
// There is no organization configured on the resource, yet the provider org is different from
41+
// the planned organization value. We must conclude that the provider default organization changed.
42+
resp.Plan.SetAttribute(ctx, orgPath, types.StringValue(providerDefaultOrg))
43+
resp.RequiresReplace.Append(orgPath)
44+
}
45+
}

internal/provider/resource_tfe_registry_gpg_key.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (r *resourceTFERegistryGPGKey) Metadata(ctx context.Context, req resource.M
3737
resp.TypeName = req.ProviderTypeName + "_registry_gpg_key"
3838
}
3939

40+
func (r *resourceTFERegistryGPGKey) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
41+
modifyPlanForDefaultOrganizationChange(ctx, r.config.Organization, req, resp)
42+
}
43+
4044
func (r *resourceTFERegistryGPGKey) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
4145
resp.Schema = schema.Schema{
4246
Description: "Manages a public key of the GPG key pair used to sign releases of private providers in the private registry.",
@@ -57,6 +61,9 @@ func (r *resourceTFERegistryGPGKey) Schema(ctx context.Context, req resource.Sch
5761
Validators: []validator.String{
5862
stringvalidator.LengthAtLeast(1),
5963
},
64+
PlanModifiers: []planmodifier.String{
65+
stringplanmodifier.RequiresReplace(),
66+
},
6067
},
6168
"ascii_armor": schema.StringAttribute{
6269
Description: "ASCII-armored representation of the GPG key.",
@@ -175,12 +182,10 @@ func (r *resourceTFERegistryGPGKey) Update(ctx context.Context, req resource.Upd
175182

176183
// Read Terraform plan data into the model
177184
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
178-
if resp.Diagnostics.HasError() {
179-
return
180-
}
181185

182186
// Read Terraform prior state data into the model
183187
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
188+
184189
if resp.Diagnostics.HasError() {
185190
return
186191
}

0 commit comments

Comments
 (0)