Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions internal/resources/report_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/stacklet/terraform-provider-stacklet/internal/errors"
"github.com/stacklet/terraform-provider-stacklet/internal/models"
"github.com/stacklet/terraform-provider-stacklet/internal/providerdata"
"github.com/stacklet/terraform-provider-stacklet/internal/schemavalidate"
tftypes "github.com/stacklet/terraform-provider-stacklet/internal/types"
)

Expand Down Expand Up @@ -143,7 +142,7 @@ func (r *reportGroupResource) Schema(_ context.Context, _ resource.SchemaRequest
},
},
Validators: []validator.Object{
schemavalidate.ValidRecipient(),
validRecipient{},
},
},
},
Expand Down Expand Up @@ -193,7 +192,7 @@ func (r *reportGroupResource) Schema(_ context.Context, _ resource.SchemaRequest
},
},
Validators: []validator.Object{
schemavalidate.ValidRecipient(),
validRecipient{},
},
},
},
Expand Down Expand Up @@ -239,7 +238,7 @@ func (r *reportGroupResource) Schema(_ context.Context, _ resource.SchemaRequest
},
},
Validators: []validator.Object{
schemavalidate.ValidRecipient(),
validRecipient{},
},
},
},
Expand Down Expand Up @@ -285,7 +284,7 @@ func (r *reportGroupResource) Schema(_ context.Context, _ resource.SchemaRequest
},
},
Validators: []validator.Object{
schemavalidate.ValidRecipient(),
validRecipient{},
},
},
},
Expand Down Expand Up @@ -343,7 +342,7 @@ func (r *reportGroupResource) Schema(_ context.Context, _ resource.SchemaRequest
},
},
Validators: []validator.Object{
schemavalidate.ValidRecipient(),
validRecipient{},
},
},
},
Expand Down Expand Up @@ -401,7 +400,7 @@ func (r *reportGroupResource) Schema(_ context.Context, _ resource.SchemaRequest
},
},
Validators: []validator.Object{
schemavalidate.ValidRecipient(),
validRecipient{},
},
},
},
Expand Down Expand Up @@ -523,3 +522,45 @@ func (r reportGroupResource) updateReportGroupModel(m *models.ReportGroupResourc
m.GroupBy = tftypes.StringsList(reportGroup.GroupBy)
m.UseMessageSettings = types.BoolValue(reportGroup.UseMessageSettings)
}

type validRecipient struct{}

func (v validRecipient) Description(ctx context.Context) string {
return "Ensures exactly one recipient field is set: either one of the owners flags are true, or exactly one of tag and value is set"
}

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

func (v validRecipient) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

setCount := 0
for _, attr := range req.ConfigValue.Attributes() {
if attr.IsNull() || attr.IsUnknown() {
continue
}
switch a := attr.(type) {
case types.Bool:
if a.ValueBool() {
setCount++
}
case types.String:
if a.ValueString() != "" {
setCount++
}
}
}

if setCount != 1 {
resp.Diagnostics.AddAttributeError(
req.Path,
"Invalid recipient configuration",
"Exactly one recipient field must be set.",
)
return
}
}
59 changes: 0 additions & 59 deletions internal/schemavalidate/valid_recipient.go

This file was deleted.