Skip to content

Commit

Permalink
Organization data source (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
erademacher committed Mar 16, 2023
1 parent a263059 commit af548c6
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions docs/data-sources/organization.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## Schema

### Read-Only

- `created_at` (String)
- `id` (String) The ID of this resource.
- `label` (String)
- `name` (String)


10 changes: 3 additions & 7 deletions examples/workflows/cockroach_cmek/main.tf
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -85,6 +79,8 @@ resource "cockroach_cluster" "example" {
]
}

data "cockroach_organization" "example" {}

resource "aws_iam_role" "example" {
name = "cmek_test_role"

Expand All @@ -99,7 +95,7 @@ resource "aws_iam_role" "example" {
},
"Condition" : {
"StringEquals" : {
"sts:ExternalId" : var.org_id
"sts:ExternalId" : data.cockroach_organization.example.id
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
92 changes: 92 additions & 0 deletions internal/provider/organization_data_source.go
Original file line number Diff line number Diff line change
@@ -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{}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (p *provider) DataSources(_ context.Context) []func() datasource.DataSource
NewClusterDataSource,
NewClusterCertDataSource,
NewConnectionStringDataSource,
NewOrganizationDataSource,
}
}

Expand Down

0 comments on commit af548c6

Please sign in to comment.