Skip to content

Commit 5788d9c

Browse files
authored
Adding connectivity rule datasource (#346)
* Adding connectivity rule datasource * fix linting error * address comments
1 parent df1b738 commit 5788d9c

File tree

7 files changed

+435
-4
lines changed

7 files changed

+435
-4
lines changed

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,3 @@ testacc:
1414
.PHONY: test-namespace-export-sink
1515
test-namespace-export-sink:
1616
TF_ACC=1 go test ./internal/provider -run TestAccNamespaceExportSink_GCS -v $(TESTARGS) -timeout 120m
17-
18-
test-connectivity-rule:
19-
TF_ACC=1 go test ./internal/provider -run TestAccNamespaceWithCodecServer -v $(TESTARGS) -timeout 120m
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "temporalcloud_connectivity_rule Data Source - terraform-provider-temporalcloud"
4+
subcategory: ""
5+
description: |-
6+
Fetches details about a connectivity rule.
7+
---
8+
9+
# temporalcloud_connectivity_rule (Data Source)
10+
11+
Fetches details about a connectivity rule.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `id` (String) The unique identifier of the connectivity rule across all Temporal Cloud tenants.
21+
22+
### Read-Only
23+
24+
- `connection_id` (String) The ID of the connection to the connectivity rule.
25+
- `connectivity_type` (String) The type of connectivity.
26+
- `created_at` (String) The time the connectivity rule was created.
27+
- `gcp_project_id` (String) The GCP project ID of the connectivity rule.
28+
- `region` (String) The region of the connectivity rule.
29+
- `state` (String) The current state of the connectivity rule.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
terraform {
2+
required_providers {
3+
temporalcloud = {
4+
source = "temporalio/temporalcloud"
5+
}
6+
}
7+
}
8+
9+
provider "temporalcloud" {
10+
11+
}
12+
13+
// Create private connectivity rule for AWS
14+
resource "temporalcloud_connectivity_rule" "test_private" {
15+
connectivity_type = "private"
16+
connection_id = "vpce-12345678"
17+
region = "aws-us-west-2"
18+
}
19+
20+
data "temporalcloud_connectivity_rule" "test_private" {
21+
id = temporalcloud_connectivity_rule.test_private.id
22+
}
23+
24+
output "connectivity_rule" {
25+
value = data.temporalcloud_connectivity_rule.test_private
26+
}
27+
28+
// Create public connectivity Rule
29+
resource "temporalcloud_connectivity_rule" "test_public" {
30+
connectivity_type = "public"
31+
}
32+
33+
data "temporalcloud_connectivity_rule" "test_public" {
34+
id = temporalcloud_connectivity_rule.test_public.id
35+
}
36+
37+
output "connectivity_rule" {
38+
value = data.temporalcloud_connectivity_rule.test_public
39+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
"github.com/temporalio/terraform-provider-temporalcloud/internal/client"
13+
"github.com/temporalio/terraform-provider-temporalcloud/internal/provider/enums"
14+
"go.temporal.io/cloud-sdk/api/cloudservice/v1"
15+
connectivityrulev1 "go.temporal.io/cloud-sdk/api/connectivityrule/v1"
16+
)
17+
18+
var (
19+
_ datasource.DataSource = &connectivityRuleDataSource{}
20+
_ datasource.DataSourceWithConfigure = &connectivityRuleDataSource{}
21+
)
22+
23+
func NewConnectivityRuleDataSource() datasource.DataSource {
24+
return &connectivityRuleDataSource{}
25+
}
26+
27+
type (
28+
connectivityRuleDataSource struct {
29+
client *client.Client
30+
}
31+
32+
connectivityRuleDataModel struct {
33+
ID types.String `tfsdk:"id"`
34+
ConnectivityType types.String `tfsdk:"connectivity_type"`
35+
ConnectionID types.String `tfsdk:"connection_id"`
36+
Region types.String `tfsdk:"region"`
37+
GcpProjectID types.String `tfsdk:"gcp_project_id"`
38+
39+
State types.String `tfsdk:"state"`
40+
CreatedAt types.String `tfsdk:"created_at"`
41+
}
42+
)
43+
44+
func connectivityRuleDataSourceSchema(idRequired bool) map[string]schema.Attribute {
45+
idAttribute := schema.StringAttribute{
46+
Description: "The unique identifier of the connectivity rule across all Temporal Cloud tenants.",
47+
}
48+
49+
switch idRequired {
50+
case true:
51+
idAttribute.Required = true
52+
case false:
53+
idAttribute.Computed = true
54+
}
55+
56+
return map[string]schema.Attribute{
57+
"id": idAttribute,
58+
"connectivity_type": schema.StringAttribute{
59+
Computed: true,
60+
Description: "The type of connectivity.",
61+
},
62+
"connection_id": schema.StringAttribute{
63+
Computed: true,
64+
Description: "The ID of the connection to the connectivity rule.",
65+
},
66+
"region": schema.StringAttribute{
67+
Computed: true,
68+
Description: "The region of the connectivity rule.",
69+
},
70+
"gcp_project_id": schema.StringAttribute{
71+
Computed: true,
72+
Description: "The GCP project ID of the connectivity rule.",
73+
},
74+
"state": schema.StringAttribute{
75+
Computed: true,
76+
Description: "The current state of the connectivity rule.",
77+
},
78+
"created_at": schema.StringAttribute{
79+
Computed: true,
80+
Description: "The time the connectivity rule was created.",
81+
},
82+
}
83+
}
84+
85+
func (d *connectivityRuleDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
86+
if req.ProviderData == nil {
87+
return
88+
}
89+
90+
client, ok := req.ProviderData.(*client.Client)
91+
if !ok {
92+
resp.Diagnostics.AddError(
93+
"Unexpected Data Source Configure Type",
94+
fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
95+
)
96+
97+
return
98+
}
99+
100+
d.client = client
101+
}
102+
103+
func (d *connectivityRuleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
104+
resp.TypeName = req.ProviderTypeName + "_connectivity_rule"
105+
}
106+
107+
func (d *connectivityRuleDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
108+
resp.Schema = schema.Schema{
109+
Description: "Fetches details about a connectivity rule.",
110+
Attributes: connectivityRuleDataSourceSchema(true),
111+
}
112+
}
113+
114+
func (d *connectivityRuleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
115+
var input connectivityRuleDataModel
116+
resp.Diagnostics.Append(req.Config.Get(ctx, &input)...)
117+
if resp.Diagnostics.HasError() {
118+
return
119+
}
120+
121+
if len(input.ID.ValueString()) == 0 {
122+
resp.Diagnostics.AddError("invalid connectivity rule id", "connectivity rule id is required")
123+
return
124+
}
125+
126+
crResp, err := d.client.CloudService().GetConnectivityRule(ctx, &cloudservice.GetConnectivityRuleRequest{
127+
ConnectivityRuleId: input.ID.ValueString(),
128+
})
129+
if err != nil {
130+
resp.Diagnostics.AddError("Failed to get connectivity rule", err.Error())
131+
return
132+
}
133+
134+
model, diags := connectivityRuleToConnectivityRuleDataModel(crResp.GetConnectivityRule())
135+
resp.Diagnostics.Append(diags...)
136+
if resp.Diagnostics.HasError() {
137+
return
138+
}
139+
140+
diags = resp.State.Set(ctx, model)
141+
resp.Diagnostics.Append(diags...)
142+
}
143+
144+
func connectivityRuleToConnectivityRuleDataModel(connectivityRule *connectivityrulev1.ConnectivityRule) (*connectivityRuleDataModel, diag.Diagnostics) {
145+
var diags diag.Diagnostics
146+
147+
stateStr, err := enums.FromResourceState(connectivityRule.State)
148+
if err != nil {
149+
diags.AddError("Unable to convert connectivity rule state", err.Error())
150+
return nil, diags
151+
}
152+
153+
model := new(connectivityRuleDataModel)
154+
model.ID = types.StringValue(connectivityRule.GetId())
155+
model.State = types.StringValue(stateStr)
156+
model.CreatedAt = types.StringValue(connectivityRule.GetCreatedTime().AsTime().Format(time.RFC3339))
157+
158+
if connectivityRule.GetSpec().GetPrivateRule() != nil {
159+
model.ConnectivityType = types.StringValue(connectivityRuleTypePrivate)
160+
model.ConnectionID = types.StringValue(connectivityRule.GetSpec().GetPrivateRule().GetConnectionId())
161+
model.Region = types.StringValue(connectivityRule.GetSpec().GetPrivateRule().GetRegion())
162+
if connectivityRule.GetSpec().GetPrivateRule().GetGcpProjectId() != "" {
163+
model.GcpProjectID = types.StringValue(connectivityRule.GetSpec().GetPrivateRule().GetGcpProjectId())
164+
} else {
165+
model.GcpProjectID = types.StringNull()
166+
}
167+
} else if connectivityRule.GetSpec().GetPublicRule() != nil {
168+
model.ConnectivityType = types.StringValue(connectivityRuleTypePublic)
169+
model.ConnectionID = types.StringNull()
170+
model.Region = types.StringNull()
171+
model.GcpProjectID = types.StringNull()
172+
} else {
173+
diags.AddError("Invalid connectivity rule", "connectivity rule must be either public or private")
174+
return nil, diags
175+
}
176+
return model, diags
177+
}

0 commit comments

Comments
 (0)