Skip to content

Commit

Permalink
Fix attachment of multiple private endpoints to the same service (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
whites11 authored Nov 15, 2024
1 parent 49e33c6 commit 70e99de
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: |-
Breaking changes
Upgrading to version >= 1.0.0 of the Clickhouse Terraform Provider
If you are upgrading from version < 1.0.0 to anything >= 1.0.0 and you are using the clickhouse_private_endpoint_registration resource or the private_endpoint_ids attribute of the clickhouse_service resource,
then a manual process is required after the upgrade. Please visit https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes for more details.
then a manual process is required after the upgrade. Please visit https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes-and-deprecations https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes-and-deprecations for more details.
---

# clickhouse Provider
Expand All @@ -31,7 +31,7 @@ Visit [https://clickhouse.com/docs/en/cloud-quick-start](https://clickhouse.com/
### Upgrading to version >= 1.0.0 of the Clickhouse Terraform Provider

If you are upgrading from version < 1.0.0 to anything >= 1.0.0 and you are using the `clickhouse_private_endpoint_registration` resource or the `private_endpoint_ids` attribute of the `clickhouse_service` resource,
then a manual process is required after the upgrade. Please visit [https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes](https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes) for more details.
then a manual process is required after the upgrade. Please visit [https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes-and-deprecations](https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes-and-deprecations) for more details.

## Example Usage

Expand Down
4 changes: 4 additions & 0 deletions docs/resources/service_private_endpoints_attachment.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ page_title: "clickhouse_service_private_endpoints_attachment Resource - clickhou
subcategory: ""
description: |-
Use the clickhouse_service_private_endpoints_attachment resource to attach a ClickHouse service to a Private Endpoint.
Important: Please note that if you want to attach the same ClickHouse service to multiple Private Endpoints you have to specify all the Private Endpoint IDs in a single clickhouse_service_private_endpoints_attachment resource.
Having multiple clickhouse_service_private_endpoints_attachment resources for the same service is unsupported and the outcome is unpredictable.
See private_endpoint_registration https://registry.terraform.io/providers/ClickHouse/clickhouse/latest/docs/resources/private_endpoint_registration for how to create a private endpoint.
See full example https://github.com/ClickHouse/terraform-provider-clickhouse/tree/main/examples/full/private_endpoint on GitHub.
---

# clickhouse_service_private_endpoints_attachment (Resource)

Use the *clickhouse_service_private_endpoints_attachment* resource to attach a ClickHouse *service* to a *Private Endpoint*.
Important: Please note that if you want to attach the same ClickHouse *service* to multiple *Private Endpoints* you have to specify all the *Private Endpoint IDs* in a single *clickhouse_service_private_endpoints_attachment* resource.
Having multiple *clickhouse_service_private_endpoints_attachment* resources for the same service is unsupported and the outcome is unpredictable.

See [private_endpoint_registration](https://registry.terraform.io/providers/ClickHouse/clickhouse/latest/docs/resources/private_endpoint_registration) for how to create a *private endpoint*.

Expand Down
3 changes: 2 additions & 1 deletion examples/full/private_endpoint/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ To run this example, the AWS user you provide credentials for needs the followin
"ec2:RevokeSecurityGroupIngress",
"ec2:DeleteSubnet",
"ec2:DeleteVpc",
"ec2:DeleteSecurityGroup"
"ec2:DeleteSecurityGroup",
"ec2:DescribeAvailabilityZones"
],
"Resource": "*"
}
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"

"github.com/hashicorp/terraform-plugin-framework/providerserver"

Expand All @@ -12,7 +13,12 @@ import (
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs generate --provider-name clickhouse

func main() {
providerserver.Serve(context.Background(), provider.New, providerserver.ServeOpts{ // nolint:errcheck
var debug bool

flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()
providerserver.Serve(context.Background(), provider.New, providerserver.ServeOpts{ //nolint:errcheck
Address: "clickhouse.cloud/terraform/clickhouse",
Debug: debug,
})
}
2 changes: 1 addition & 1 deletion pkg/provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Visit [https://clickhouse.com/docs/en/cloud-quick-start](https://clickhouse.com/
### Upgrading to version >= 1.0.0 of the Clickhouse Terraform Provider

If you are upgrading from version < 1.0.0 to anything >= 1.0.0 and you are using the `clickhouse_private_endpoint_registration` resource or the `private_endpoint_ids` attribute of the `clickhouse_service` resource,
then a manual process is required after the upgrade. Please visit [https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes](https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes) for more details.
then a manual process is required after the upgrade. Please visit [https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes-and-deprecations](https://github.com/ClickHouse/terraform-provider-clickhouse#breaking-changes-and-deprecations) for more details.
13 changes: 11 additions & 2 deletions pkg/resource/service_private_endpoints_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (r *ServicePrivateEndpointsAttachmentResource) Schema(_ context.Context, _
},
},
MarkdownDescription: `Use the *clickhouse_service_private_endpoints_attachment* resource to attach a ClickHouse *service* to a *Private Endpoint*.
Important: Please note that if you want to attach the same ClickHouse *service* to multiple *Private Endpoints* you have to specify all the *Private Endpoint IDs* in a single *clickhouse_service_private_endpoints_attachment* resource.
Having multiple *clickhouse_service_private_endpoints_attachment* resources for the same service is unsupported and the outcome is unpredictable.
See [private_endpoint_registration](https://registry.terraform.io/providers/ClickHouse/clickhouse/latest/docs/resources/private_endpoint_registration) for how to create a *private endpoint*.
Expand Down Expand Up @@ -126,7 +128,14 @@ func (r *ServicePrivateEndpointsAttachmentResource) Create(ctx context.Context,
return
}

serviceUpdate.PrivateEndpointIds.Remove = append(serviceUpdate.PrivateEndpointIds.Remove, service.PrivateEndpointIds...)
for _, existingEndpointID := range service.PrivateEndpointIds {
for _, desiredEndpointID := range plan.PrivateEndpointIDs.Elements() {
if desiredEndpointID.Equal(types.StringValue(existingEndpointID)) {
// Private endpoint needs to be recreated.
serviceUpdate.PrivateEndpointIds.Remove = append(serviceUpdate.PrivateEndpointIds.Remove, existingEndpointID)
}
}
}
}

servicePrivateEndpointIds := make([]types.String, 0, len(plan.PrivateEndpointIDs.Elements()))
Expand Down Expand Up @@ -210,7 +219,7 @@ func (r *ServicePrivateEndpointsAttachmentResource) Update(ctx context.Context,
servicePrivateEndpointIds = make([]types.String, 0, len(state.PrivateEndpointIDs.Elements()))
state.PrivateEndpointIDs.ElementsAs(ctx, &servicePrivateEndpointIds, false)
for _, item := range servicePrivateEndpointIds {
service.PrivateEndpointIds.Remove = append(service.PrivateEndpointIds.Add, item.ValueString())
service.PrivateEndpointIds.Remove = append(service.PrivateEndpointIds.Remove, item.ValueString())
}

_, err := r.client.UpdateService(ctx, plan.ServiceID.ValueString(), service)
Expand Down

0 comments on commit 70e99de

Please sign in to comment.