Skip to content

Commit

Permalink
⭐️ organization and space data source (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Mar 3, 2024
1 parent 4b97c45 commit 63399f4
Show file tree
Hide file tree
Showing 12 changed files with 533 additions and 7 deletions.
46 changes: 46 additions & 0 deletions docs/data-sources/organization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "mondoo_organization Data Source - terraform-provider-mondoo"
subcategory: ""
description: |-
Organization data source
---

# mondoo_organization (Data Source)

Organization data source

## Example Usage

```terraform
terraform {
required_providers {
mondoo = {
source = "mondoohq/mondoo"
}
}
}
provider "mondoo" {
}
data "mondoo_organization" "org" {
id = "reverent-ride-275852"
}
output "org_mrn" {
value = data.mondoo_organization.org.mrn
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `id` (String) Organization ID
- `mrn` (String) Organization MRN

### Read-Only

- `name` (String) Organization name
59 changes: 59 additions & 0 deletions docs/data-sources/space.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "mondoo_space Data Source - terraform-provider-mondoo"
subcategory: ""
description: |-
Space data source
---

# mondoo_space (Data Source)

Space data source

## Example Usage

```terraform
terraform {
required_providers {
mondoo = {
source = "mondoohq/mondoo"
}
}
}
provider "mondoo" {
}
data "mondoo_organization" "org" {
id = "reverent-ride-275852"
}
resource "mondoo_space" "test" {
org_id = mondoo_organization.org.id
name = "test-space"
}
data "mondoo_space" "space" {
id = mondoo_space.test.id
depends_on = [
mondoo_space.test
]
}
output "space_name" {
value = data.mondoo_space.name
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `id` (String) Space ID
- `mrn` (String) Space MRN

### Read-Only

- `name` (String) Space name
14 changes: 11 additions & 3 deletions docs/resources/scim_group_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ terraform {
provider "mondoo" {
}
data "mondoo_organization" "org" {
id = "reverent-ride-275852"
}
resource "mondoo_space" "my_space_1" {
name = "My Space 1"
org_id = "your-org-1234567"
org_id = data.mondoo_organization.org.id
}
resource "mondoo_scim_group_mapping" "MondooAdmin" {
org_id = "your-org-1234567"
org_id = data.mondoo_organization.org.id
group = "MondooAdmin"
mappings = [
# Give admin group access to the organization
{
org_mrn : "//captain.api.mondoo.app/organizations/your-org-1234567",
org_mrn : data.mondoo_organization.org.mrn,
iam_role : "//iam.api.mondoo.app/roles/editor"
},
# Give admin group access to the space
Expand All @@ -49,6 +53,10 @@ resource "mondoo_scim_group_mapping" "MondooAdmin" {
mondoo_space.my_space_1
]
}
output "org_mrn" {
value = data.mondoo_organization.org.mrn
}
```

<!-- schema generated by tfplugindocs -->
Expand Down
18 changes: 18 additions & 0 deletions examples/data-sources/mondoo_organization/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
mondoo = {
source = "mondoohq/mondoo"
}
}
}

provider "mondoo" {
}

data "mondoo_organization" "org" {
id = "reverent-ride-275852"
}

output "org_mrn" {
value = data.mondoo_organization.org.mrn
}
31 changes: 31 additions & 0 deletions examples/data-sources/mondoo_space/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
terraform {
required_providers {
mondoo = {
source = "mondoohq/mondoo"
}
}
}

provider "mondoo" {
}

data "mondoo_organization" "org" {
id = "reverent-ride-275852"
}

resource "mondoo_space" "test" {
org_id = mondoo_organization.org.id
name = "test-space"
}

data "mondoo_space" "space" {
id = mondoo_space.test.id

depends_on = [
mondoo_space.test
]
}

output "space_name" {
value = data.mondoo_space.name
}
14 changes: 11 additions & 3 deletions examples/resources/mondoo_scim_group_mapping/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ terraform {
provider "mondoo" {
}

data "mondoo_organization" "org" {
id = "reverent-ride-275852"
}

resource "mondoo_space" "my_space_1" {
name = "My Space 1"
org_id = "your-org-1234567"
org_id = data.mondoo_organization.org.id
}

resource "mondoo_scim_group_mapping" "MondooAdmin" {
org_id = "your-org-1234567"
org_id = data.mondoo_organization.org.id
group = "MondooAdmin"
mappings = [
# Give admin group access to the organization
{
org_mrn : "//captain.api.mondoo.app/organizations/your-org-1234567",
org_mrn : data.mondoo_organization.org.mrn,
iam_role : "//iam.api.mondoo.app/roles/editor"
},
# Give admin group access to the space
Expand All @@ -34,3 +38,7 @@ resource "mondoo_scim_group_mapping" "MondooAdmin" {
mondoo_space.my_space_1
]
}

output "org_mrn" {
value = data.mondoo_organization.org.mrn
}
22 changes: 22 additions & 0 deletions internal/provider/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ func (r *ExtendedGqlClient) GetSpace(ctx context.Context, mrn string) (spacePayl
return q.Space, nil
}

type orgPayload struct {
Id string
Mrn string
Name string
}

func (r *ExtendedGqlClient) GetOrganization(ctx context.Context, mrn string) (orgPayload, error) {
var q struct {
Organization orgPayload `graphql:"organization(mrn: $mrn)"`
}
variables := map[string]interface{}{
"mrn": mondoov1.String(mrn),
}

err := r.Query(ctx, &q, variables)
if err != nil {
return orgPayload{}, err
}

return q.Organization, nil
}

type setCustomPolicyPayload struct {
PolicyMrns []mondoov1.String
}
Expand Down
117 changes: 117 additions & 0 deletions internal/provider/organization_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

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"
mondoov1 "go.mondoo.com/mondoo-go"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &OrganizationDataSource{}

func NewOrganizationDataSource() datasource.DataSource {
return &OrganizationDataSource{}
}

// OrganizationDataSource defines the data source implementation.
type OrganizationDataSource struct {
client *ExtendedGqlClient
}

// OrganizationDataSourceModel describes the data source data model.
type OrganizationDataSourceModel struct {
OrgID types.String `tfsdk:"id"`
OrgMrn types.String `tfsdk:"mrn"`
Name types.String `tfsdk:"name"`
}

func (d *OrganizationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_organization"
}

func (d *OrganizationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Organization data source",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Organization ID",
Computed: true,
Optional: true,
},
"mrn": schema.StringAttribute{
MarkdownDescription: "Organization MRN",
Computed: true,
Optional: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Organization name",
Computed: true,
},
},
}
}

func (d *OrganizationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*mondoov1.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *mondoov1.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = &ExtendedGqlClient{client}
}

func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data OrganizationDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// we fetch the organization id from the service account
orgMrn := ""
if data.OrgMrn.ValueString() != "" {
orgMrn = data.OrgMrn.ValueString()
} 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
}

payload, err := d.client.GetOrganization(ctx, orgMrn)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to fetch organization, got error: %s", err))
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)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading

0 comments on commit 63399f4

Please sign in to comment.