-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️ organization and space data source (#49)
- Loading branch information
1 parent
4b97c45
commit 63399f4
Showing
12 changed files
with
533 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)...) | ||
} |
Oops, something went wrong.