Skip to content

Commit 4998032

Browse files
committed
workspace_id cannot be set if organization is set
1 parent 6c14bf8 commit 4998032

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

internal/provider/data_retention_policy.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,29 @@ func modelFromTFEDataRetentionPolicyDeleteOlder(ctx context.Context, model model
4242
}
4343
deleteOlderThanObject, diags := types.ObjectValueFrom(ctx, deleteOlderThan.AttributeTypes(), deleteOlderThan)
4444

45+
organization := types.StringNull()
46+
if model.WorkspaceID.IsNull() {
47+
organization = model.Organization
48+
}
49+
4550
return modelTFEDataRetentionPolicy{
4651
ID: types.StringValue(deleteOlder.ID),
47-
Organization: model.Organization,
52+
Organization: organization,
4853
WorkspaceID: model.WorkspaceID,
4954
DeleteOlderThan: deleteOlderThanObject,
5055
DontDelete: types.ObjectNull(map[string]attr.Type{}),
5156
}, diags
5257
}
5358

5459
func modelFromTFEDataRetentionPolicyDontDelete(model modelTFEDataRetentionPolicy, dontDelete *tfe.DataRetentionPolicyDontDelete) modelTFEDataRetentionPolicy {
60+
organization := types.StringNull()
61+
if model.WorkspaceID.IsNull() {
62+
organization = model.Organization
63+
}
64+
5565
return modelTFEDataRetentionPolicy{
5666
ID: types.StringValue(dontDelete.ID),
57-
Organization: model.Organization,
67+
Organization: organization,
5868
WorkspaceID: model.WorkspaceID,
5969
DeleteOlderThan: types.ObjectNull(modelTFEDeleteOlderThan{}.AttributeTypes()),
6070
DontDelete: DontDeleteEmptyObject(),

internal/provider/resource_tfe_data_retention_policy.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hashicorp/terraform-plugin-log/tflog"
2121
"log"
2222
"strings"
23+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
2324
)
2425

2526
// Ensure provider defined types fully satisfy framework interfaces.
@@ -68,11 +69,11 @@ func (r *resourceTFEDataRetentionPolicy) Schema(ctx context.Context, req resourc
6869
PlanModifiers: []planmodifier.String{
6970
stringplanmodifier.RequiresReplace(),
7071
},
71-
//Validators: []validator.String{
72-
// stringvalidator.ExactlyOneOf(
73-
// path.MatchRelative().AtParent().AtName("organization"),
74-
// ),
75-
//},
72+
Validators: []validator.String{
73+
stringvalidator.ConflictsWith(
74+
path.MatchRelative().AtParent().AtName("organization"),
75+
),
76+
},
7677
},
7778
},
7879
Blocks: map[string]schema.Block{
@@ -198,9 +199,6 @@ func (r *resourceTFEDataRetentionPolicy) createDeleteOlderThanRetentionPolicy(ct
198199
return
199200
}
200201

201-
// set organization if it is still not known after creating the data retention policy
202-
r.ensureOrganizationSetAfterApply(&result, &resp.Diagnostics)
203-
204202
// Save data into Terraform state
205203
diags = resp.State.Set(ctx, &result)
206204
resp.Diagnostics.Append(diags...)
@@ -232,25 +230,11 @@ func (r *resourceTFEDataRetentionPolicy) createDontDeleteRetentionPolicy(ctx con
232230

233231
result := modelFromTFEDataRetentionPolicyDontDelete(plan, dataRetentionPolicy)
234232

235-
// set organization if it is still not known after creating the data retention policy
236-
r.ensureOrganizationSetAfterApply(&result, &resp.Diagnostics)
237-
238233
// Save data into Terraform state
239234
diags = resp.State.Set(ctx, &result)
240235
resp.Diagnostics.Append(diags...)
241236
}
242237

243-
func (r *resourceTFEDataRetentionPolicy) ensureOrganizationSetAfterApply(policy *modelTFEDataRetentionPolicy, diags *diag.Diagnostics) {
244-
if policy.Organization.IsUnknown() {
245-
workspace, err := r.config.Client.Workspaces.ReadByID(ctx, policy.WorkspaceID.ValueString())
246-
if err != nil {
247-
diags.AddError("Unable to create data retention policy", err.Error())
248-
return
249-
}
250-
policy.Organization = types.StringValue(workspace.Organization.Name)
251-
}
252-
}
253-
254238
func (r *resourceTFEDataRetentionPolicy) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
255239
var state modelTFEDataRetentionPolicy
256240

@@ -353,7 +337,6 @@ func (r *resourceTFEDataRetentionPolicy) ImportState(ctx context.Context, req re
353337
req.ID = r.getPolicyID(policy)
354338
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), r.getPolicyID(policy))...)
355339
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("workspace_id"), workspaceID)...)
356-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("organization"), s[0])...)
357340
return
358341
}
359342

0 commit comments

Comments
 (0)