Skip to content

Commit

Permalink
Add regions data source
Browse files Browse the repository at this point in the history
  • Loading branch information
swgillespie committed Jan 4, 2024
1 parent 2e69f99 commit fa4f1be
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 3 deletions.
30 changes: 30 additions & 0 deletions docs/data-sources/regions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "temporalcloud_regions Data Source - terraform-provider-temporalcloud"
subcategory: ""
description: |-
---

# temporalcloud_regions (Data Source)





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

### Read-Only

- `regions` (Attributes List) (see [below for nested schema](#nestedatt--regions))

<a id="nestedatt--regions"></a>
### Nested Schema for `regions`

Read-Only:

- `cloud_provider` (String)
- `cloud_provider_region` (String)
- `id` (String)
- `location` (String)
2 changes: 1 addition & 1 deletion docs/resources/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ description: |-

### Read-Only

- `namespace` (String)
- `id` (String) The ID of this resource.
- `resource_version` (String)

<a id="nestedblock--timeouts"></a>
Expand Down
9 changes: 9 additions & 0 deletions examples/data-sources/regions/regions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "temporalcloud" {

}

data "temporalcloud_regions" "regions" {}

output "regions" {
value = data.temporalcloud_regions.regions.regions
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/temporalio/terraform-provider-temporalcloud
go 1.19

require (
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.3.5
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1
Expand Down Expand Up @@ -36,6 +35,7 @@ require (
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.10 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.5.2 // indirect
github.com/hashicorp/hcl/v2 v2.17.0 // indirect
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ func (p *TerraformCloudProvider) Resources(ctx context.Context) []func() resourc
}

func (p *TerraformCloudProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{}
return []func() datasource.DataSource{
NewRegionsDataSource,
}
}

func New(version string) func() provider.Provider {
Expand Down
122 changes: 122 additions & 0 deletions internal/provider/regions_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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"
cloudservicev1 "github.com/temporalio/terraform-provider-temporalcloud/proto/go/temporal/api/cloud/cloudservice/v1"
)

var (
_ datasource.DataSource = &regionsDataSource{}
_ datasource.DataSourceWithConfigure = &regionsDataSource{}
)

// NewCoffeesDataSource is a helper function to simplify the provider implementation.
func NewRegionsDataSource() datasource.DataSource {
return &regionsDataSource{}
}

type (
regionsDataSource struct {
client cloudservicev1.CloudServiceClient
}

regionsDataModel struct {
// https://developer.hashicorp.com/terraform/plugin/framework/acctests#no-id-found-in-attributes
ID types.String `tfsdk:"id"`
Regions []regionDataModel `tfsdk:"regions"`
}

regionDataModel struct {
ID types.String `tfsdk:"id"`
CloudProvider types.String `tfsdk:"cloud_provider"`
CloudProviderRegion types.String `tfsdk:"cloud_provider_region"`
Location types.String `tfsdk:"location"`
}
)

// Configure adds the provider configured client to the data source.
func (d *regionsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

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

return
}

d.client = client
}

// Metadata returns the data source type name.
func (d *regionsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_regions"
}

// Schema defines the schema for the data source.
func (d *regionsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"regions": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"cloud_provider": schema.StringAttribute{
Computed: true,
},
"cloud_provider_region": schema.StringAttribute{
Computed: true,
},
"location": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}

// Read refreshes the Terraform state with the latest data.
func (d *regionsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state regionsDataModel

regions, err := d.client.GetRegions(ctx, &cloudservicev1.GetRegionsRequest{})
if err != nil {
resp.Diagnostics.AddError("Unable to fetch regions", err.Error())
return
}

for _, region := range regions.GetRegions() {
regionModel := regionDataModel{
ID: types.StringValue(region.GetId()),
CloudProvider: types.StringValue(region.GetCloudProvider()),
CloudProviderRegion: types.StringValue(region.GetCloudProviderRegion()),
Location: types.StringValue(region.GetLocation()),
}

state.Regions = append(state.Regions, regionModel)
}

// Silly, but temporarily necessary:
// https://developer.hashicorp.com/terraform/plugin/framework/acctests#no-id-found-in-attributes
state.ID = types.StringValue("terraform")
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
29 changes: 29 additions & 0 deletions internal/provider/regions_datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccRegion(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccRegionConfig(),
},
},
})
}

func testAccRegionConfig() string {
return `
provider "temporalcloud" {
}
data "temporalcloud_regions" "test" {}
`
}

0 comments on commit fa4f1be

Please sign in to comment.