Skip to content

Commit

Permalink
fix: added validation for id and mrn
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Theuermann <[email protected]>
  • Loading branch information
mati007thm committed Jun 21, 2024
1 parent d3a895f commit 23c11aa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
23 changes: 20 additions & 3 deletions internal/provider/active_policy_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -49,11 +52,23 @@ func (d *activePolicyDataSource) Schema(ctx context.Context, req datasource.Sche
Computed: true,
Optional: true,
MarkdownDescription: "Space ID",
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("space_mrn"),
}...),
},
},
"space_mrn": schema.StringAttribute{
Computed: true,
Optional: true,
MarkdownDescription: "Space MRN",
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("space_id"),
}...),
},
},
"policies": schema.ListNestedAttribute{
Computed: true,
Expand Down Expand Up @@ -131,11 +146,13 @@ func (d *activePolicyDataSource) Read(ctx context.Context, req datasource.ReadRe

// generate space mrn
spaceMrn := ""
if data.SpaceMrn.ValueString() != "" && data.SpaceID.ValueString() == "" {
if data.SpaceMrn.ValueString() != "" {
spaceMrn = data.SpaceMrn.ValueString()
} else if data.SpaceID.ValueString() != "" && data.SpaceMrn.ValueString() == "" {
} else if data.SpaceID.ValueString() != "" {
spaceMrn = spacePrefix + data.SpaceID.ValueString()
} else {
}

if spaceMrn == "" {
resp.Diagnostics.AddError("Invalid Configuration", "Either `id` or `mrn` must be set")
return
}
Expand Down
23 changes: 20 additions & 3 deletions internal/provider/policy_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -40,11 +43,23 @@ func (d *policyDataSource) Schema(ctx context.Context, req datasource.SchemaRequ
Computed: true,
Optional: true,
MarkdownDescription: "Space ID",
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("space_mrn"),
}...),
},
},
"space_mrn": schema.StringAttribute{
Computed: true,
Optional: true,
MarkdownDescription: "Space MRN",
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("space_id"),
}...),
},
},
"catalog_type": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -132,11 +147,13 @@ func (d *policyDataSource) Read(ctx context.Context, req datasource.ReadRequest,

// generate space mrn
scopeMrn := ""
if data.SpaceMrn.ValueString() != "" && data.SpaceID.ValueString() == "" {
if data.SpaceMrn.ValueString() != "" {
scopeMrn = data.SpaceMrn.ValueString()
} else if data.SpaceID.ValueString() != "" && data.SpaceMrn.ValueString() == "" {
} else if data.SpaceID.ValueString() != "" {
scopeMrn = spacePrefix + data.SpaceID.ValueString()
} else {
}

if scopeMrn == "" {
resp.Diagnostics.AddError("Invalid Configuration", "Either `id` or `mrn` must be set")
return
}
Expand Down

0 comments on commit 23c11aa

Please sign in to comment.