Skip to content

Commit ed5979e

Browse files
committed
consolidate new validators
1 parent 8909002 commit ed5979e

File tree

4 files changed

+98
-152
lines changed

4 files changed

+98
-152
lines changed

internal/provider/resource_tfe_team_notification_configuration.go

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,21 @@ func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration)
6565
TeamID: types.StringValue(v.SubscribableChoice.Team.ID),
6666
}
6767

68-
emailAddresses := make([]attr.Value, len(v.EmailAddresses))
69-
for i, emailAddress := range v.EmailAddresses {
70-
emailAddresses[i] = types.StringValue(emailAddress)
68+
if emailAddresses, err := types.SetValueFrom(ctx, types.StringType, v.EmailAddresses); err == nil {
69+
result.EmailAddresses = emailAddresses
7170
}
72-
if len(emailAddresses) > 0 {
73-
result.EmailAddresses = types.SetValueMust(types.StringType, emailAddresses)
74-
} else {
75-
result.EmailAddresses = types.SetNull(types.StringType)
71+
72+
if len(v.Triggers) == 0 {
73+
result.Triggers = types.SetNull(types.StringType)
74+
} else if triggers, err := types.SetValueFrom(ctx, types.StringType, v.Triggers); err == nil {
75+
result.Triggers = triggers
7676
}
7777

7878
emailUserIDs := make([]attr.Value, len(v.EmailUsers))
7979
for i, emailUser := range v.EmailUsers {
8080
emailUserIDs[i] = types.StringValue(emailUser.ID)
8181
}
82-
if len(emailUserIDs) > 0 {
83-
result.EmailUserIDs = types.SetValueMust(types.StringType, emailUserIDs)
84-
} else {
85-
result.EmailUserIDs = types.SetNull(types.StringType)
86-
}
87-
88-
triggers := make([]attr.Value, len(v.Triggers))
89-
for i, trigger := range v.Triggers {
90-
triggers[i] = types.StringValue(trigger)
91-
}
92-
if len(v.Triggers) > 0 {
93-
result.Triggers = types.SetValueMust(types.StringType, triggers)
94-
} else {
95-
result.Triggers = types.SetNull(types.StringType)
96-
}
82+
result.EmailUserIDs = types.SetValueMust(types.StringType, emailUserIDs)
9783

9884
if v.Token != "" {
9985
result.Token = types.StringValue(v.Token)
@@ -147,14 +133,10 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
147133
Computed: true,
148134
ElementType: types.StringType,
149135
Validators: []validator.Set{
150-
validators.AttributeValueConflictSetValidator(
136+
validators.AttributeValueConflictValidator(
151137
"destination_type",
152138
[]string{"generic", "microsoft-teams", "slack"},
153139
),
154-
setvalidator.ConflictsWith(
155-
path.MatchRelative().AtParent().AtName("token"),
156-
path.MatchRelative().AtParent().AtName("url"),
157-
),
158140
},
159141
},
160142

@@ -164,14 +146,10 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
164146
Computed: true,
165147
ElementType: types.StringType,
166148
Validators: []validator.Set{
167-
validators.AttributeValueConflictSetValidator(
149+
validators.AttributeValueConflictValidator(
168150
"destination_type",
169151
[]string{"generic", "microsoft-teams", "slack"},
170152
),
171-
setvalidator.ConflictsWith(
172-
path.MatchRelative().AtParent().AtName("token"),
173-
path.MatchRelative().AtParent().AtName("url"),
174-
),
175153
},
176154
},
177155

@@ -187,7 +165,7 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
187165
Optional: true,
188166
Sensitive: true,
189167
Validators: []validator.String{
190-
validators.AttributeValueConflictStringValidator(
168+
validators.AttributeValueConflictValidator(
191169
"destination_type",
192170
[]string{"email", "microsoft-teams", "slack"},
193171
),
@@ -215,7 +193,7 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
215193
"destination_type",
216194
[]string{"generic", "microsoft-teams", "slack"},
217195
),
218-
validators.AttributeValueConflictStringValidator(
196+
validators.AttributeValueConflictValidator(
219197
"destination_type",
220198
[]string{"email"},
221199
),
@@ -264,10 +242,6 @@ func (r *resourceTFETeamNotificationConfiguration) Create(ctx context.Context, r
264242
return
265243
}
266244

267-
if resp.Diagnostics.HasError() {
268-
return
269-
}
270-
271245
// Get team
272246
teamID := plan.TeamID.ValueString()
273247

@@ -295,11 +269,12 @@ func (r *resourceTFETeamNotificationConfiguration) Create(ctx context.Context, r
295269
}
296270

297271
// Add email_addresses set to the options struct
298-
emailAddresses := make([]types.String, len(plan.EmailAddresses.Elements()))
272+
emailAddresses := make([]types.String, 0)
299273
if diags := plan.EmailAddresses.ElementsAs(ctx, &emailAddresses, true); diags != nil && diags.HasError() {
300274
resp.Diagnostics.Append(diags...)
301275
return
302276
}
277+
303278
options.EmailAddresses = []string{}
304279
for _, emailAddress := range emailAddresses {
305280
options.EmailAddresses = append(options.EmailAddresses, emailAddress.ValueString())
@@ -321,6 +296,8 @@ func (r *resourceTFETeamNotificationConfiguration) Create(ctx context.Context, r
321296
if err != nil {
322297
resp.Diagnostics.AddError("Unable to create team notification configuration", err.Error())
323298
return
299+
} else if len(tnc.EmailUsers) != len(plan.EmailUserIDs.Elements()) {
300+
resp.Diagnostics.AddError("Email user IDs produced an inconsistent result", "API returned a different number of email user IDs than were provided in the plan.")
324301
}
325302

326303
// Restore token from plan because it is write only
@@ -385,7 +362,7 @@ func (r *resourceTFETeamNotificationConfiguration) Update(ctx context.Context, r
385362
}
386363

387364
// Add triggers set to the options struct
388-
triggers := make([]types.String, len(plan.Triggers.Elements()))
365+
triggers := make([]types.String, 0)
389366
if diags := plan.Triggers.ElementsAs(ctx, &triggers, true); diags != nil && diags.HasError() {
390367
resp.Diagnostics.Append(diags...)
391368
return
@@ -396,7 +373,7 @@ func (r *resourceTFETeamNotificationConfiguration) Update(ctx context.Context, r
396373
}
397374

398375
// Add email_addresses set to the options struct
399-
emailAddresses := make([]types.String, len(plan.EmailAddresses.Elements()))
376+
emailAddresses := make([]types.String, 0)
400377
if diags := plan.EmailAddresses.ElementsAs(ctx, &emailAddresses, true); diags != nil && diags.HasError() {
401378
resp.Diagnostics.Append(diags...)
402379
return
@@ -407,7 +384,7 @@ func (r *resourceTFETeamNotificationConfiguration) Update(ctx context.Context, r
407384
}
408385

409386
// Add email_user_ids set to the options struct
410-
emailUserIDs := make([]types.String, len(plan.EmailUserIDs.Elements()))
387+
emailUserIDs := make([]types.String, 0)
411388
if diags := plan.EmailUserIDs.ElementsAs(ctx, &emailUserIDs, true); diags != nil && diags.HasError() {
412389
resp.Diagnostics.Append(diags...)
413390
return
@@ -422,6 +399,8 @@ func (r *resourceTFETeamNotificationConfiguration) Update(ctx context.Context, r
422399
if err != nil {
423400
resp.Diagnostics.AddError("Unable to update team notification configuration", err.Error())
424401
return
402+
} else if len(tnc.EmailUsers) != len(plan.EmailUserIDs.Elements()) {
403+
resp.Diagnostics.AddError("Email user IDs produced an inconsistent result", "API returned a different number of email user IDs than were provided in the plan.")
425404
}
426405

427406
// Restore token from plan because it is write only
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package validators
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/hashicorp/terraform-plugin-framework/path"
11+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
)
14+
15+
type attributeValueConflictValidator struct {
16+
attributeName string
17+
conflictingValues []string
18+
}
19+
20+
func (v attributeValueConflictValidator) Description(ctx context.Context) string {
21+
return fmt.Sprintf("Ensures the attribute is not set if %s is one of %v", v.attributeName, v.conflictingValues)
22+
}
23+
24+
func (v attributeValueConflictValidator) MarkdownDescription(ctx context.Context) string {
25+
return v.Description(ctx)
26+
}
27+
28+
func (v attributeValueConflictValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
29+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
30+
return
31+
}
32+
33+
var attributeValue types.String
34+
diags := req.Config.GetAttribute(ctx, path.Root(v.attributeName), &attributeValue)
35+
resp.Diagnostics.Append(diags...)
36+
if resp.Diagnostics.HasError() {
37+
return
38+
}
39+
40+
for _, conflictingValue := range v.conflictingValues {
41+
if attributeValue.ValueString() == conflictingValue {
42+
resp.Diagnostics.AddError(
43+
"Invalid Attribute Value",
44+
fmt.Sprintf("The attribute '%s' cannot be set when '%s' is '%s'", req.Path, v.attributeName, conflictingValue),
45+
)
46+
47+
return
48+
}
49+
}
50+
}
51+
52+
func (v attributeValueConflictValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
53+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
54+
return
55+
}
56+
57+
var attributeValue types.String
58+
diags := req.Config.GetAttribute(ctx, path.Root(v.attributeName), &attributeValue)
59+
resp.Diagnostics.Append(diags...)
60+
if resp.Diagnostics.HasError() {
61+
return
62+
}
63+
64+
for _, conflictingValue := range v.conflictingValues {
65+
if attributeValue.ValueString() == conflictingValue {
66+
resp.Diagnostics.AddError(
67+
"Invalid Attribute Value",
68+
fmt.Sprintf("The attribute '%s' cannot be set when '%s' is '%s'", req.Path, v.attributeName, conflictingValue),
69+
)
70+
return
71+
}
72+
}
73+
}
74+
75+
func AttributeValueConflictValidator(attributeName string, conflictingValues []string) attributeValueConflictValidator {
76+
return attributeValueConflictValidator{attributeName: attributeName, conflictingValues: conflictingValues}
77+
}

internal/provider/validators/attribute_value_conflict_set.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

internal/provider/validators/attribute_value_conflict_string.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)