Skip to content

Commit

Permalink
fix: implemented suggested validation
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 bec3f39 commit dcf7bc4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
23 changes: 20 additions & 3 deletions internal/provider/organization_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,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 @@ -45,11 +48,23 @@ func (d *OrganizationDataSource) Schema(ctx context.Context, req datasource.Sche
MarkdownDescription: "Organization ID",
Computed: true,
Optional: true,
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("mrn"),
}...),
},
},
"mrn": schema.StringAttribute{
MarkdownDescription: "Organization MRN",
Computed: true,
Optional: true,
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("id"),
}...),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Organization name",
Expand Down Expand Up @@ -91,11 +106,13 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe

// we fetch the organization id from the service account
orgMrn := ""
if data.OrgMrn.ValueString() != "" && data.OrgID.ValueString() == "" {
if data.OrgMrn.ValueString() != "" {
orgMrn = data.OrgMrn.ValueString()
} else if data.OrgID.ValueString() != "" && data.OrgMrn.ValueString() == "" {
} else if data.OrgID.ValueString() != "" {
orgMrn = orgPrefix + data.OrgID.ValueString()
} else {
}

if orgMrn == "" {
resp.Diagnostics.AddError("Invalid Configuration", "Either `id` or `mrn` must be set")
return
}
Expand Down
23 changes: 20 additions & 3 deletions internal/provider/space_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,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 @@ -45,11 +48,23 @@ func (d *SpaceDataSource) Schema(ctx context.Context, req datasource.SchemaReque
MarkdownDescription: "Space ID",
Computed: true,
Optional: true,
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("mrn"),
}...),
},
},
"mrn": schema.StringAttribute{
MarkdownDescription: "Space MRN",
Computed: true,
Optional: true,
Validators: []validator.String{
// Validate only this attribute or other_attr is configured.
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("id"),
}...),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Space name",
Expand Down Expand Up @@ -91,11 +106,13 @@ func (d *SpaceDataSource) Read(ctx context.Context, req datasource.ReadRequest,

// we fetch the organization id from the service account
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

0 comments on commit dcf7bc4

Please sign in to comment.