Skip to content

Commit b75ef36

Browse files
committed
provider: private connection support on all clouds
Previously, the private_endpoint_connection resource only supported AWS private link connections. This commit updates the provider code to use the new CC API Private Endpoint Connections methods. These cloud-neutral methods will work for all cloud providers and cluster types, except Serverless clusters on Azure as that configuration is not yet supported.
1 parent e328294 commit b75ef36

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- The `private_endpoint_connection` resource can now be created on every
11+
supported cloud-provider and cluster type, expect Serverless clusters on
12+
Azure as that configuration is not yet available.
13+
1014
## [1.3.1] - 2023-12-01
1115

1216
### Fixed

docs/resources/private_endpoint_connection.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "cockroach_private_endpoint_connection Resource - terraform-provider-cockroach"
44
subcategory: ""
55
description: |-
6-
AWS PrivateLink Endpoint Connection.
6+
Private Endpoint Connection.
77
---
88

99
# cockroach_private_endpoint_connection (Resource)
1010

11-
AWS PrivateLink Endpoint Connection.
11+
Private Endpoint Connection.
1212

1313

1414

@@ -18,13 +18,13 @@ AWS PrivateLink Endpoint Connection.
1818
### Required
1919

2020
- `cluster_id` (String)
21-
- `endpoint_id` (String) Client side ID of the PrivateLink connection.
21+
- `endpoint_id` (String) Client side ID of the Private Endpoint Connection.
2222

2323
### Read-Only
2424

2525
- `cloud_provider` (String) Cloud provider associated with this connection.
2626
- `id` (String) Used with `terraform import`. Format is "<cluster ID>:<endpoint ID>".
2727
- `region_name` (String) Cloud provider region code associated with this connection.
28-
- `service_id` (String) Server side ID of the PrivateLink connection.
28+
- `service_id` (String) Server side ID of the Private Endpoint Connection.
2929

3030

internal/provider/private_endpoint_connection_resource.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (r *privateEndpointConnectionResource) Schema(
4848
_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse,
4949
) {
5050
resp.Schema = schema.Schema{
51-
MarkdownDescription: "AWS PrivateLink Endpoint Connection.",
51+
MarkdownDescription: "Private Endpoint Connection.",
5252
Attributes: map[string]schema.Attribute{
5353
"id": schema.StringAttribute{
5454
Computed: true,
@@ -76,14 +76,14 @@ func (r *privateEndpointConnectionResource) Schema(
7676
PlanModifiers: []planmodifier.String{
7777
stringplanmodifier.RequiresReplace(),
7878
},
79-
Description: "Client side ID of the PrivateLink connection.",
79+
Description: "Client side ID of the Private Endpoint Connection.",
8080
},
8181
"service_id": schema.StringAttribute{
8282
Computed: true,
8383
PlanModifiers: []planmodifier.String{
8484
stringplanmodifier.UseStateForUnknown(),
8585
},
86-
Description: "Server side ID of the PrivateLink connection.",
86+
Description: "Server side ID of the Private Endpoint Connection.",
8787
},
8888
"cluster_id": schema.StringAttribute{
8989
Required: true,
@@ -129,7 +129,8 @@ func (r *privateEndpointConnectionResource) Create(
129129
return
130130
}
131131

132-
cluster, _, err := r.provider.service.GetCluster(ctx, plan.ClusterID.ValueString())
132+
svc := r.provider.service
133+
cluster, _, err := svc.GetCluster(ctx, plan.ClusterID.ValueString())
133134
if err != nil {
134135
resp.Diagnostics.AddError(
135136
"Error getting cluster",
@@ -138,30 +139,22 @@ func (r *privateEndpointConnectionResource) Create(
138139
return
139140
}
140141

141-
if cluster.CloudProvider != client.CLOUDPROVIDERTYPE_AWS {
142-
resp.Diagnostics.AddError(
143-
"Incompatible cluster cloud provider",
144-
"Private endpoint services are only available for AWS clusters",
145-
)
146-
return
147-
}
148-
149-
connectionStateRequest := client.SetAwsEndpointConnectionStateRequest{
150-
Status: client.SETAWSENDPOINTCONNECTIONSTATUSTYPE_AVAILABLE,
142+
addRequest := client.AddPrivateEndpointConnectionRequest{
143+
EndpointId: plan.EndpointID.ValueString(),
151144
}
152145

153-
_, _, err = r.provider.service.SetAwsEndpointConnectionState(ctx, plan.ClusterID.ValueString(), plan.EndpointID.ValueString(), &connectionStateRequest)
146+
_, _, err = svc.AddPrivateEndpointConnection(ctx, cluster.Id, &addRequest)
154147
if err != nil {
155148
resp.Diagnostics.AddError(
156-
"Error establishing AWS Endpoint Connection",
157-
fmt.Sprintf("Could not establish AWS Endpoint Connection: %s", formatAPIErrorMessage(err)),
149+
"Error establishing Private Endpoint Connection",
150+
fmt.Sprintf("Could not establish Private Endpoint Connection: %s", formatAPIErrorMessage(err)),
158151
)
159152
return
160153
}
161154

162-
var connection client.AwsEndpointConnection
155+
var connection client.PrivateEndpointConnection
163156
err = sdk_resource.RetryContext(ctx, endpointConnectionCreateTimeout,
164-
waitForEndpointConnectionCreatedFunc(ctx, cluster.Id, plan.EndpointID.ValueString(), r.provider.service, &connection))
157+
waitForEndpointConnectionCreatedFunc(ctx, cluster.Id, plan.EndpointID.ValueString(), svc, &connection))
165158
if err != nil {
166159
resp.Diagnostics.AddError(
167160
"Error accepting private endpoint connection",
@@ -192,7 +185,7 @@ func (r *privateEndpointConnectionResource) Read(
192185
return
193186
}
194187

195-
connections, _, err := r.provider.service.ListAwsEndpointConnections(ctx, state.ClusterID.ValueString())
188+
connections, _, err := r.provider.service.ListPrivateEndpointConnections(ctx, state.ClusterID.ValueString())
196189
if err != nil {
197190
diags.AddError("Unable to get endpoint connection status",
198191
fmt.Sprintf("Unexpected error retrieving endpoint status: %s", formatAPIErrorMessage(err)))
@@ -212,14 +205,14 @@ func (r *privateEndpointConnectionResource) Read(
212205
}
213206

214207
func loadEndpointConnectionIntoTerraformState(
215-
apiConnection *client.AwsEndpointConnection, state *PrivateEndpointConnection,
208+
apiConnection *client.PrivateEndpointConnection, state *PrivateEndpointConnection,
216209
) {
217210
state.EndpointID = types.StringValue(apiConnection.GetEndpointId())
218211
state.ID = types.StringValue(fmt.Sprintf(
219212
privateEndpointConnectionIDFmt,
220213
state.ClusterID.ValueString(),
221214
apiConnection.GetEndpointId()))
222-
state.ServiceID = types.StringValue(apiConnection.GetServiceId())
215+
state.ServiceID = types.StringValue(apiConnection.GetEndpointServiceId())
223216
state.CloudProvider = types.StringValue(string(apiConnection.GetCloudProvider()))
224217
state.RegionName = types.StringValue(apiConnection.GetRegionName())
225218
}
@@ -240,13 +233,11 @@ func (r *privateEndpointConnectionResource) Delete(
240233
return
241234
}
242235

243-
_, httpResp, err := r.provider.service.SetAwsEndpointConnectionState(
236+
httpResp, err := r.provider.service.DeletePrivateEndpointConnection(
244237
ctx,
245238
state.ClusterID.ValueString(),
246239
state.EndpointID.ValueString(),
247-
&client.SetAwsEndpointConnectionStateRequest{
248-
Status: client.SETAWSENDPOINTCONNECTIONSTATUSTYPE_REJECTED,
249-
})
240+
)
250241
if err != nil && httpResp != nil && httpResp.StatusCode != http.StatusNotFound {
251242
diags.AddError("Couldn't delete connection",
252243
fmt.Sprintf("Unexpected error occurred while setting connection status: %s", formatAPIErrorMessage(err)))
@@ -284,10 +275,10 @@ func waitForEndpointConnectionCreatedFunc(
284275
ctx context.Context,
285276
clusterID, endpointID string,
286277
cl client.Service,
287-
connection *client.AwsEndpointConnection,
278+
connection *client.PrivateEndpointConnection,
288279
) sdk_resource.RetryFunc {
289280
return func() *sdk_resource.RetryError {
290-
connections, httpResp, err := cl.ListAwsEndpointConnections(ctx, clusterID)
281+
connections, httpResp, err := cl.ListPrivateEndpointConnections(ctx, clusterID)
291282
if err != nil {
292283
if httpResp != nil && httpResp.StatusCode < http.StatusInternalServerError {
293284
return sdk_resource.NonRetryableError(fmt.Errorf("error getting endpoint connections: %s", formatAPIErrorMessage(err)))
@@ -299,10 +290,10 @@ func waitForEndpointConnectionCreatedFunc(
299290
for _, *connection = range connections.GetConnections() {
300291
if connection.GetEndpointId() == endpointID {
301292
switch status := connection.GetStatus(); status {
302-
case client.AWSENDPOINTCONNECTIONSTATUSTYPE_AVAILABLE:
293+
case client.PRIVATEENDPOINTCONNECTIONSTATUS_AVAILABLE:
303294
return nil
304-
case client.AWSENDPOINTCONNECTIONSTATUSTYPE_PENDING,
305-
client.AWSENDPOINTCONNECTIONSTATUSTYPE_PENDING_ACCEPTANCE:
295+
case client.PRIVATEENDPOINTCONNECTIONSTATUS_PENDING,
296+
client.PRIVATEENDPOINTCONNECTIONSTATUS_PENDING_ACCEPTANCE:
306297
return sdk_resource.RetryableError(fmt.Errorf("endpoint connection is not ready yet"))
307298
default:
308299
return sdk_resource.NonRetryableError(fmt.Errorf("endpoint connection failed with state: %s", status))

0 commit comments

Comments
 (0)