-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e69f99
commit fa4f1be
Showing
7 changed files
with
195 additions
and
3 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,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) |
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,9 @@ | ||
provider "temporalcloud" { | ||
|
||
} | ||
|
||
data "temporalcloud_regions" "regions" {} | ||
|
||
output "regions" { | ||
value = data.temporalcloud_regions.regions.regions | ||
} |
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,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 = ®ionsDataSource{} | ||
_ datasource.DataSourceWithConfigure = ®ionsDataSource{} | ||
) | ||
|
||
// NewCoffeesDataSource is a helper function to simplify the provider implementation. | ||
func NewRegionsDataSource() datasource.DataSource { | ||
return ®ionsDataSource{} | ||
} | ||
|
||
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...) | ||
} |
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,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" {} | ||
` | ||
} |