Skip to content

Commit

Permalink
consolidate new validators
Browse files Browse the repository at this point in the history
  • Loading branch information
notchairmk committed Jan 15, 2025
1 parent 8909002 commit ed5979e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 152 deletions.
63 changes: 21 additions & 42 deletions internal/provider/resource_tfe_team_notification_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,21 @@ func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration)
TeamID: types.StringValue(v.SubscribableChoice.Team.ID),
}

emailAddresses := make([]attr.Value, len(v.EmailAddresses))
for i, emailAddress := range v.EmailAddresses {
emailAddresses[i] = types.StringValue(emailAddress)
if emailAddresses, err := types.SetValueFrom(ctx, types.StringType, v.EmailAddresses); err == nil {
result.EmailAddresses = emailAddresses
}
if len(emailAddresses) > 0 {
result.EmailAddresses = types.SetValueMust(types.StringType, emailAddresses)
} else {
result.EmailAddresses = types.SetNull(types.StringType)

if len(v.Triggers) == 0 {
result.Triggers = types.SetNull(types.StringType)
} else if triggers, err := types.SetValueFrom(ctx, types.StringType, v.Triggers); err == nil {
result.Triggers = triggers
}

emailUserIDs := make([]attr.Value, len(v.EmailUsers))
for i, emailUser := range v.EmailUsers {
emailUserIDs[i] = types.StringValue(emailUser.ID)
}
if len(emailUserIDs) > 0 {
result.EmailUserIDs = types.SetValueMust(types.StringType, emailUserIDs)
} else {
result.EmailUserIDs = types.SetNull(types.StringType)
}

triggers := make([]attr.Value, len(v.Triggers))
for i, trigger := range v.Triggers {
triggers[i] = types.StringValue(trigger)
}
if len(v.Triggers) > 0 {
result.Triggers = types.SetValueMust(types.StringType, triggers)
} else {
result.Triggers = types.SetNull(types.StringType)
}
result.EmailUserIDs = types.SetValueMust(types.StringType, emailUserIDs)

if v.Token != "" {
result.Token = types.StringValue(v.Token)
Expand Down Expand Up @@ -147,14 +133,10 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
Computed: true,
ElementType: types.StringType,
Validators: []validator.Set{
validators.AttributeValueConflictSetValidator(
validators.AttributeValueConflictValidator(
"destination_type",
[]string{"generic", "microsoft-teams", "slack"},
),
setvalidator.ConflictsWith(
path.MatchRelative().AtParent().AtName("token"),
path.MatchRelative().AtParent().AtName("url"),
),
},
},

Expand All @@ -164,14 +146,10 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
Computed: true,
ElementType: types.StringType,
Validators: []validator.Set{
validators.AttributeValueConflictSetValidator(
validators.AttributeValueConflictValidator(
"destination_type",
[]string{"generic", "microsoft-teams", "slack"},
),
setvalidator.ConflictsWith(
path.MatchRelative().AtParent().AtName("token"),
path.MatchRelative().AtParent().AtName("url"),
),
},
},

Expand All @@ -187,7 +165,7 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
Optional: true,
Sensitive: true,
Validators: []validator.String{
validators.AttributeValueConflictStringValidator(
validators.AttributeValueConflictValidator(
"destination_type",
[]string{"email", "microsoft-teams", "slack"},
),
Expand Down Expand Up @@ -215,7 +193,7 @@ func (r *resourceTFETeamNotificationConfiguration) Schema(ctx context.Context, r
"destination_type",
[]string{"generic", "microsoft-teams", "slack"},
),
validators.AttributeValueConflictStringValidator(
validators.AttributeValueConflictValidator(
"destination_type",
[]string{"email"},
),
Expand Down Expand Up @@ -264,10 +242,6 @@ func (r *resourceTFETeamNotificationConfiguration) Create(ctx context.Context, r
return
}

if resp.Diagnostics.HasError() {
return
}

// Get team
teamID := plan.TeamID.ValueString()

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

// Add email_addresses set to the options struct
emailAddresses := make([]types.String, len(plan.EmailAddresses.Elements()))
emailAddresses := make([]types.String, 0)
if diags := plan.EmailAddresses.ElementsAs(ctx, &emailAddresses, true); diags != nil && diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

options.EmailAddresses = []string{}
for _, emailAddress := range emailAddresses {
options.EmailAddresses = append(options.EmailAddresses, emailAddress.ValueString())
Expand All @@ -321,6 +296,8 @@ func (r *resourceTFETeamNotificationConfiguration) Create(ctx context.Context, r
if err != nil {
resp.Diagnostics.AddError("Unable to create team notification configuration", err.Error())
return
} else if len(tnc.EmailUsers) != len(plan.EmailUserIDs.Elements()) {
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.")
}

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

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

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

// Add email_user_ids set to the options struct
emailUserIDs := make([]types.String, len(plan.EmailUserIDs.Elements()))
emailUserIDs := make([]types.String, 0)
if diags := plan.EmailUserIDs.ElementsAs(ctx, &emailUserIDs, true); diags != nil && diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -422,6 +399,8 @@ func (r *resourceTFETeamNotificationConfiguration) Update(ctx context.Context, r
if err != nil {
resp.Diagnostics.AddError("Unable to update team notification configuration", err.Error())
return
} else if len(tnc.EmailUsers) != len(plan.EmailUserIDs.Elements()) {
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.")
}

// Restore token from plan because it is write only
Expand Down
77 changes: 77 additions & 0 deletions internal/provider/validators/attribute_value_conflict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validators

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type attributeValueConflictValidator struct {
attributeName string
conflictingValues []string
}

func (v attributeValueConflictValidator) Description(ctx context.Context) string {
return fmt.Sprintf("Ensures the attribute is not set if %s is one of %v", v.attributeName, v.conflictingValues)
}

func (v attributeValueConflictValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

func (v attributeValueConflictValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

var attributeValue types.String
diags := req.Config.GetAttribute(ctx, path.Root(v.attributeName), &attributeValue)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

for _, conflictingValue := range v.conflictingValues {
if attributeValue.ValueString() == conflictingValue {
resp.Diagnostics.AddError(
"Invalid Attribute Value",
fmt.Sprintf("The attribute '%s' cannot be set when '%s' is '%s'", req.Path, v.attributeName, conflictingValue),
)

return
}
}
}

func (v attributeValueConflictValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

var attributeValue types.String
diags := req.Config.GetAttribute(ctx, path.Root(v.attributeName), &attributeValue)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

for _, conflictingValue := range v.conflictingValues {
if attributeValue.ValueString() == conflictingValue {
resp.Diagnostics.AddError(
"Invalid Attribute Value",
fmt.Sprintf("The attribute '%s' cannot be set when '%s' is '%s'", req.Path, v.attributeName, conflictingValue),
)
return
}
}
}

func AttributeValueConflictValidator(attributeName string, conflictingValues []string) attributeValueConflictValidator {
return attributeValueConflictValidator{attributeName: attributeName, conflictingValues: conflictingValues}
}
54 changes: 0 additions & 54 deletions internal/provider/validators/attribute_value_conflict_set.go

This file was deleted.

56 changes: 0 additions & 56 deletions internal/provider/validators/attribute_value_conflict_string.go

This file was deleted.

0 comments on commit ed5979e

Please sign in to comment.