diff --git a/Makefile b/Makefile index ed0ce7fe..c5fd938c 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ HOSTNAME=registry.terraform.io NAMESPACE=cockroachdb NAME=cockroach BINARY=terraform-provider-${NAME} -VERSION=0.4.2 +VERSION=0.4.3 OS_ARCH=darwin_amd64 default: install diff --git a/docs/data-sources/organization.md b/docs/data-sources/organization.md new file mode 100644 index 00000000..ed41086f --- /dev/null +++ b/docs/data-sources/organization.md @@ -0,0 +1,25 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "cockroach_organization Data Source - terraform-provider-cockroach" +subcategory: "" +description: |- + Information about the organization associated with the user's API key +--- + +# cockroach_organization (Data Source) + +Information about the organization associated with the user's API key + + + + +## Schema + +### Read-Only + +- `created_at` (String) +- `id` (String) The ID of this resource. +- `label` (String) +- `name` (String) + + diff --git a/examples/workflows/cockroach_cmek/main.tf b/examples/workflows/cockroach_cmek/main.tf index f04be422..cea54d69 100644 --- a/examples/workflows/cockroach_cmek/main.tf +++ b/examples/workflows/cockroach_cmek/main.tf @@ -1,9 +1,3 @@ -# Your Organization ID can be found at https://cockroachlabs.cloud/information -variable "org_id" { - type = string - nullable = false -} - # Required to assign yourself permission to update the key. variable "iam_user" { type = string @@ -85,6 +79,8 @@ resource "cockroach_cluster" "example" { ] } +data "cockroach_organization" "example" {} + resource "aws_iam_role" "example" { name = "cmek_test_role" @@ -99,7 +95,7 @@ resource "aws_iam_role" "example" { }, "Condition" : { "StringEquals" : { - "sts:ExternalId" : var.org_id + "sts:ExternalId" : data.cockroach_organization.example.id } } } diff --git a/internal/provider/models.go b/internal/provider/models.go index c1b611cb..259feecd 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -166,6 +166,13 @@ type FinalizeVersionUpgrade struct { ID types.String `tfsdk:"id"` } +type Organization struct { + ID types.String `tfsdk:"id"` + Label types.String `tfsdk:"label"` + Name types.String `tfsdk:"name"` + CreatedAt types.String `tfsdk:"created_at"` +} + func (e *APIErrorMessage) String() string { return fmt.Sprintf("%v-%v", e.Code, e.Message) } diff --git a/internal/provider/organization_data_source.go b/internal/provider/organization_data_source.go new file mode 100644 index 00000000..34f682fa --- /dev/null +++ b/internal/provider/organization_data_source.go @@ -0,0 +1,92 @@ +/* +Copyright 2022 The Cockroach Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +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" +) + +type organizationDataSource struct { + provider *provider +} + +func (d *organizationDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Information about the organization associated with the user's API key", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "label": schema.StringAttribute{ + Computed: true, + }, + "name": schema.StringAttribute{ + Computed: true, + }, + "created_at": schema.StringAttribute{ + Computed: true, + }, + }, + } +} + +func (d *organizationDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_organization" +} + +func (d *organizationDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + var ok bool + if d.provider, ok = req.ProviderData.(*provider); !ok { + resp.Diagnostics.AddError("Internal provider error", + fmt.Sprintf("Error in Configure: expected %T but got %T", provider{}, req.ProviderData)) + } +} + +func (d *organizationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + if d.provider == nil || !d.provider.configured { + addConfigureProviderErr(&resp.Diagnostics) + return + } + + apiOrg, _, err := d.provider.service.GetOrganizationInfo(ctx) + if err != nil { + resp.Diagnostics.AddError("Error retrieving organization info", formatAPIErrorMessage(err)) + return + } + + org := &Organization{ + ID: types.StringValue(apiOrg.Id), + Label: types.StringValue(apiOrg.Label), + Name: types.StringValue(apiOrg.Name), + CreatedAt: types.StringValue(apiOrg.CreatedAt.String()), + } + + diags := resp.State.Set(ctx, org) + resp.Diagnostics.Append(diags...) +} + +func NewOrganizationDataSource() datasource.DataSource { + return &organizationDataSource{} +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index dcdd64f8..b5508e59 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -129,6 +129,7 @@ func (p *provider) DataSources(_ context.Context) []func() datasource.DataSource NewClusterDataSource, NewClusterCertDataSource, NewConnectionStringDataSource, + NewOrganizationDataSource, } }