Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 space and org data source #111

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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() == "" {
mati007thm marked this conversation as resolved.
Show resolved Hide resolved
orgMrn = data.OrgMrn.ValueString()
} else if data.OrgID.ValueString() != "" {
} else if data.OrgID.ValueString() != "" && data.OrgMrn.ValueString() == "" {
mati007thm marked this conversation as resolved.
Show resolved Hide resolved
orgMrn = orgPrefix + data.OrgID.ValueString()
}
if orgMrn == "" {
} else {
mati007thm marked this conversation as resolved.
Show resolved Hide resolved
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() == "" {
mati007thm marked this conversation as resolved.
Show resolved Hide resolved
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
Loading