diff --git a/docs/data-sources/organization.md b/docs/data-sources/organization.md index 801da32..526a860 100644 --- a/docs/data-sources/organization.md +++ b/docs/data-sources/organization.md @@ -16,13 +16,23 @@ Organization data source provider "mondoo" {} data "mondoo_organization" "org" { - id = "reverent-ride-275852" + id = "your-org-1234567" } output "org_mrn" { description = "MRN of the organization" value = data.mondoo_organization.org.mrn } + +output "org_name" { + description = "Name of the organization" + value = data.mondoo_organization.org.name +} + +output "org_id" { + description = "ID of the organization" + value = data.mondoo_organization.org.id +} ``` diff --git a/docs/data-sources/space.md b/docs/data-sources/space.md index 64fbced..21fff70 100644 --- a/docs/data-sources/space.md +++ b/docs/data-sources/space.md @@ -21,7 +21,7 @@ variable "mondoo_org" { provider "mondoo" {} resource "mondoo_space" "test" { - org_id = var.mondoo_org.value + org_id = var.mondoo_org name = "test-space" } @@ -42,6 +42,11 @@ output "space_mrn" { description = "The MRN of the space" value = data.mondoo_space.space.mrn } + +output "space_id" { + description = "The ID of the space" + value = data.mondoo_space.space.id +} ``` diff --git a/examples/data-sources/mondoo_organization/data-source.tf b/examples/data-sources/mondoo_organization/data-source.tf index 85b7329..d9f9347 100644 --- a/examples/data-sources/mondoo_organization/data-source.tf +++ b/examples/data-sources/mondoo_organization/data-source.tf @@ -1,10 +1,20 @@ provider "mondoo" {} data "mondoo_organization" "org" { - id = "reverent-ride-275852" + id = "your-org-1234567" } output "org_mrn" { description = "MRN of the organization" value = data.mondoo_organization.org.mrn -} \ No newline at end of file +} + +output "org_name" { + description = "Name of the organization" + value = data.mondoo_organization.org.name +} + +output "org_id" { + description = "ID of the organization" + value = data.mondoo_organization.org.id +} diff --git a/examples/data-sources/mondoo_space/data-source.tf b/examples/data-sources/mondoo_space/data-source.tf index 82c6244..cfb6976 100644 --- a/examples/data-sources/mondoo_space/data-source.tf +++ b/examples/data-sources/mondoo_space/data-source.tf @@ -6,7 +6,7 @@ variable "mondoo_org" { provider "mondoo" {} resource "mondoo_space" "test" { - org_id = var.mondoo_org.value + org_id = var.mondoo_org name = "test-space" } @@ -27,3 +27,8 @@ output "space_mrn" { description = "The MRN of the space" value = data.mondoo_space.space.mrn } + +output "space_id" { + description = "The ID of the space" + value = data.mondoo_space.space.id +} diff --git a/internal/provider/gql.go b/internal/provider/gql.go index 4e092d0..a3235f4 100644 --- a/internal/provider/gql.go +++ b/internal/provider/gql.go @@ -89,9 +89,7 @@ type spacePayload struct { Id string Mrn string Name string - Organization struct { - Id string - } + Organization orgPayload } func (c *ExtendedGqlClient) GetSpace(ctx context.Context, mrn string) (spacePayload, error) { diff --git a/internal/provider/organization_data_source.go b/internal/provider/organization_data_source.go index deddc08..ff691c8 100644 --- a/internal/provider/organization_data_source.go +++ b/internal/provider/organization_data_source.go @@ -6,8 +6,12 @@ package provider 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" ) @@ -44,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", @@ -95,6 +111,7 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe } else if data.OrgID.ValueString() != "" { orgMrn = orgPrefix + data.OrgID.ValueString() } + if orgMrn == "" { resp.Diagnostics.AddError("Invalid Configuration", "Either `id` or `mrn` must be set") return @@ -106,8 +123,6 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe return } - // For the purposes of this example code, hardcoding a response value to - // save into the Terraform state. data.OrgID = types.StringValue(payload.Id) data.OrgMrn = types.StringValue(payload.Mrn) data.Name = types.StringValue(payload.Name) diff --git a/internal/provider/space_data_source.go b/internal/provider/space_data_source.go index 822a21a..b837baf 100644 --- a/internal/provider/space_data_source.go +++ b/internal/provider/space_data_source.go @@ -6,8 +6,12 @@ package provider 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" ) @@ -44,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", @@ -92,9 +108,10 @@ func (d *SpaceDataSource) Read(ctx context.Context, req datasource.ReadRequest, spaceMrn := "" if data.SpaceMrn.ValueString() != "" { spaceMrn = data.SpaceMrn.ValueString() - } else if data.SpaceMrn.ValueString() != "" { - spaceMrn = orgPrefix + data.SpaceID.ValueString() + } else if data.SpaceID.ValueString() != "" { + spaceMrn = spacePrefix + data.SpaceID.ValueString() } + if spaceMrn == "" { resp.Diagnostics.AddError("Invalid Configuration", "Either `id` or `mrn` must be set") return @@ -106,8 +123,6 @@ func (d *SpaceDataSource) Read(ctx context.Context, req datasource.ReadRequest, return } - // For the purposes of this example code, hardcoding a response value to - // save into the Terraform state. data.SpaceID = types.StringValue(payload.Id) data.SpaceMrn = types.StringValue(payload.Mrn) data.Name = types.StringValue(payload.Name)