Skip to content

Commit

Permalink
fix: space and org data source
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Theuermann <[email protected]>
  • Loading branch information
mati007thm committed Jun 14, 2024
1 parent 4f3c063 commit bec3f39
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 21 deletions.
12 changes: 11 additions & 1 deletion docs/data-sources/organization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

<!-- schema generated by tfplugindocs -->
Expand Down
7 changes: 6 additions & 1 deletion docs/data-sources/space.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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
}
```

<!-- schema generated by tfplugindocs -->
Expand Down
14 changes: 12 additions & 2 deletions examples/data-sources/mondoo_organization/data-source.tf
Original file line number Diff line number Diff line change
@@ -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
}
}

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
}
7 changes: 6 additions & 1 deletion examples/data-sources/mondoo_space/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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
}
4 changes: 1 addition & 3 deletions internal/provider/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions internal/provider/organization_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package provider
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -90,12 +91,11 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe

// we fetch the organization id from the service account
orgMrn := ""
if data.OrgMrn.ValueString() != "" {
if data.OrgMrn.ValueString() != "" && data.OrgID.ValueString() == "" {
orgMrn = data.OrgMrn.ValueString()
} else if data.OrgID.ValueString() != "" {
} else if data.OrgID.ValueString() != "" && data.OrgMrn.ValueString() == "" {
orgMrn = orgPrefix + data.OrgID.ValueString()
}
if orgMrn == "" {
} else {
resp.Diagnostics.AddError("Invalid Configuration", "Either `id` or `mrn` must be set")
return
}
Expand All @@ -106,8 +106,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)
Expand Down
12 changes: 5 additions & 7 deletions internal/provider/space_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package provider
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -90,12 +91,11 @@ func (d *SpaceDataSource) Read(ctx context.Context, req datasource.ReadRequest,

// we fetch the organization id from the service account
spaceMrn := ""
if data.SpaceMrn.ValueString() != "" {
if data.SpaceMrn.ValueString() != "" && data.SpaceID.ValueString() == "" {
spaceMrn = data.SpaceMrn.ValueString()
} else if data.SpaceMrn.ValueString() != "" {
spaceMrn = orgPrefix + data.SpaceID.ValueString()
}
if spaceMrn == "" {
} else if data.SpaceID.ValueString() != "" && data.SpaceMrn.ValueString() == "" {
spaceMrn = spacePrefix + data.SpaceID.ValueString()
} else {
resp.Diagnostics.AddError("Invalid Configuration", "Either `id` or `mrn` must be set")
return
}
Expand All @@ -106,8 +106,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)
Expand Down

0 comments on commit bec3f39

Please sign in to comment.