Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCS-1716: Add HCP Consul federation support #68

Merged
merged 13 commits into from
Feb 22, 2021
2 changes: 2 additions & 0 deletions docs/data-sources/consul_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ data "hcp_consul_cluster" "example" {
- **datacenter** (String) The Consul data center name of the cluster. If not specified, it is defaulted to the value of `cluster_id`.
- **hvn_id** (String) The ID of the HVN this HCP Consul cluster is associated to.
- **organization_id** (String) The ID of the organization the project for this HCP Consul cluster is located.
- **primary_link** (String) The `self_link` of the HCP Consul cluster which is the primary in the federation setup with this HCP Consul cluster. If not specified, it is a standalone cluster.
- **project_id** (String) The ID of the project this HCP Consul cluster is located.
- **public_endpoint** (Boolean) Denotes that the cluster has a public endpoint for the Consul UI. Defaults to false.
- **region** (String) The region where the HCP Consul cluster is located.
- **scale** (Number) The the number of Consul server nodes in the cluster.
- **self_link** (String) A unique URL identifying the HCP Consul cluster.
- **tier** (String) The tier that the HCP Consul cluster will be provisioned as. Only 'development' and 'standard' are available at this time.

<a id="nestedblock--timeouts"></a>
Expand Down
31 changes: 31 additions & 0 deletions docs/guides/consul-federation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
subcategory: ""
page_title: "Federate HCP Consul clusters - HCP Provider"
description: |-
An example of federating a new HCP Consul cluster with an existing one.
---

# Federate a new HCP Consul cluster with an existing one

Once you have a HCP Consul cluster, you can create a new Consul cluster to federate with the existing one.

```terraform
resource "hcp_hvn" "example" {
hvn_id = var.hvn_id
cloud_provider = var.cloud_provider
region = var.region
}

resource "hcp_consul_cluster" "primary" {
hvn_id = hcp_hvn.example.hvn_id
cluster_id = var.primary_cluster_id
tier = "development"
}

resource "hcp_consul_cluster" "secondary" {
hvn_id = hcp_hvn.example.hvn_id
cluster_id = var.secondary_cluster_id
tier = "development"
primary_link = hcp_consul_cluster.primary.self_link
}
```
8 changes: 8 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ resource "hcp_consul_cluster" "example" {
cluster_id = "hcp-tf-example-consul-cluster"
tier = "development"
}

// Create a secondary Consul cluster to federate with the existing Consul cluster
resource "hcp_consul_cluster" "example_secondary" {
hvn_id = hcp_hvn.example_hvn.hvn_id
cluster_id = "hcp-tf-example-consul-cluster-secondary"
tier = "development"
primary_link = hcp_consul_cluster.example.self_link
}
```

## Schema
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/consul_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ resource "hcp_consul_cluster" "example" {
- **datacenter** (String) The Consul data center name of the cluster. If not specified, it is defaulted to the value of `cluster_id`.
- **id** (String) The ID of this resource.
- **min_consul_version** (String) The minimum Consul version of the cluster. If not specified, it is defaulted to the version that is currently recommended by HCP.
- **primary_link** (String) The `self_link` of the HCP Consul cluster which is the primary in the federation setup with this HCP Consul cluster. If not specified, it is a standalone cluster.
- **public_endpoint** (Boolean) Denotes that the cluster has a public endpoint for the Consul UI. Defaults to false.
- **timeouts** (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

Expand All @@ -60,6 +61,7 @@ resource "hcp_consul_cluster" "example" {
- **project_id** (String) The ID of the project this HCP Consul cluster is located in.
- **region** (String) The region where the HCP Consul cluster is located.
- **scale** (Number) The number of Consul server nodes in the cluster.
- **self_link** (String) A unique URL identifying the HCP Consul cluster.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down
1 change: 1 addition & 0 deletions examples/guides/consul_cluster_federation/_config.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
provider "hcp" {}
18 changes: 18 additions & 0 deletions examples/guides/consul_cluster_federation/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resource "hcp_hvn" "example" {
hvn_id = var.hvn_id
cloud_provider = var.cloud_provider
region = var.region
}

resource "hcp_consul_cluster" "primary" {
hvn_id = hcp_hvn.example.hvn_id
cluster_id = var.primary_cluster_id
tier = "development"
}

resource "hcp_consul_cluster" "secondary" {
hvn_id = hcp_hvn.example.hvn_id
cluster_id = var.secondary_cluster_id
tier = "development"
primary_link = hcp_consul_cluster.primary.self_link
}
24 changes: 24 additions & 0 deletions examples/guides/consul_cluster_federation/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "hvn_id" {
description = "The ID of the HCP HVN."
type = string
}

variable "cloud_provider" {
description = "The cloud provider of the HCP HVN and Consul cluster."
type = string
}

variable "region" {
description = "The region of the HCP HVN and Consul cluster."
type = string
}

variable "primary_cluster_id" {
description = "The ID of the HCP Consul cluster which is the primary in the federation."
type = string
}

variable "secondary_cluster_id" {
description = "The ID of the HCP Consul cluster which is the secondary in the federation."
type = string
}
8 changes: 8 additions & 0 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ resource "hcp_consul_cluster" "example" {
hvn_id = hcp_hvn.example_hvn.hvn_id
cluster_id = "hcp-tf-example-consul-cluster"
tier = "development"
}

// Create a secondary Consul cluster to federate with the existing Consul cluster
resource "hcp_consul_cluster" "example_secondary" {
hvn_id = hcp_hvn.example_hvn.hvn_id
cluster_id = "hcp-tf-example-consul-cluster-secondary"
tier = "development"
primary_link = hcp_consul_cluster.example.self_link
}
19 changes: 19 additions & 0 deletions examples/resources/hcp_consul_cluster/federation.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resource "hcp_hvn" "example" {
hvn_id = "hvn"
cloud_provider = "aws"
region = "us-west-2"
cidr_block = "172.25.16.0/20"
}

resource "hcp_consul_cluster" "primary" {
hvn_id = hcp_hvn.example.hvn_id
cluster_id = "consul-cluster-primary"
tier = "development"
}

resource "hcp_consul_cluster" "secondary" {
hvn_id = hcp_hvn.example.hvn_id
cluster_id = "consul-cluster-secondary"
tier = "development"
primary_link = hcp_consul_cluster.primary.self_link
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/go-openapi/runtime v0.19.20
github.com/google/uuid v1.1.1
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/hcp-sdk-go v0.1.0
github.com/hashicorp/hcp-sdk-go v0.2.0
github.com/hashicorp/terraform-plugin-docs v0.3.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.0
github.com/stretchr/testify v1.6.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl/v2 v2.3.0 h1:iRly8YaMwTBAKhn1Ybk7VSdzbnopghktCD031P8ggUE=
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
github.com/hashicorp/hcp-sdk-go v0.1.0 h1:3UQRLDsgXHnK6++hQ9uFKwLbsQ8lLXm+nGO/ZbUtlg0=
github.com/hashicorp/hcp-sdk-go v0.1.0/go.mod h1:vpV5eSGZVmfCFcksi4AH8d/QSybuyLSH5UQcwmnRUQk=
github.com/hashicorp/hcp-sdk-go v0.2.0 h1:kY+1WSAE1NiWz+DwqzzgJDz6AUETVOhex6itGlJ4Rpw=
github.com/hashicorp/hcp-sdk-go v0.2.0/go.mod h1:vpV5eSGZVmfCFcksi4AH8d/QSybuyLSH5UQcwmnRUQk=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.9.0/go.mod h1:tOT8j1J8rP05bZBGWXfMyU3HkLi1LWyqL3Bzsc3CJjo=
Expand Down
60 changes: 20 additions & 40 deletions internal/clients/consul_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
func GetConsulClusterByID(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
consulClusterID string) (*consulmodels.HashicorpCloudConsul20200826Cluster, error) {

getParams := consul_service.NewGetParams()
getParams := consul_service.NewConsulServiceGetParams()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking care of this update! Awkward timing with my tech debt chore in the sdk 😅

getParams.Context = ctx
getParams.ID = consulClusterID
getParams.LocationOrganizationID = loc.OrganizationID
getParams.LocationProjectID = loc.ProjectID

getResp, err := client.Consul.Get(getParams, nil)
getResp, err := client.Consul.ConsulServiceGet(getParams, nil)
if err != nil {
return nil, err
}
Expand All @@ -33,13 +33,13 @@ func GetConsulClusterByID(ctx context.Context, client *Client, loc *sharedmodels
func GetConsulClientConfigFiles(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
consulClusterID string) (*consulmodels.HashicorpCloudConsul20200826GetClientConfigResponse, error) {

p := consul_service.NewGetClientConfigParams()
p := consul_service.NewConsulServiceGetClientConfigParams()
p.Context = ctx
p.ID = consulClusterID
p.LocationOrganizationID = loc.OrganizationID
p.LocationProjectID = loc.ProjectID

resp, err := client.Consul.GetClientConfig(p, nil)
resp, err := client.Consul.ConsulServiceGetClientConfig(p, nil)
if err != nil {
return nil, err
}
Expand All @@ -54,7 +54,7 @@ func GetConsulClientConfigFiles(ctx context.Context, client *Client, loc *shared
func CreateCustomerRootACLToken(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
consulClusterID string) (*consulmodels.HashicorpCloudConsul20200826CreateCustomerMasterACLTokenResponse, error) {

p := consul_service.NewCreateCustomerMasterACLTokenParams()
p := consul_service.NewConsulServiceCreateCustomerMasterACLTokenParams()
p.Context = ctx
p.ID = consulClusterID
p.Body = &consulmodels.HashicorpCloudConsul20200826CreateCustomerMasterACLTokenRequest{
Expand All @@ -64,7 +64,7 @@ func CreateCustomerRootACLToken(ctx context.Context, client *Client, loc *shared
p.LocationOrganizationID = loc.OrganizationID
p.LocationProjectID = loc.ProjectID

resp, err := client.Consul.CreateCustomerMasterACLToken(p, nil)
resp, err := client.Consul.ConsulServiceCreateCustomerMasterACLToken(p, nil)
if err != nil {
return nil, err
}
Expand All @@ -75,36 +75,16 @@ func CreateCustomerRootACLToken(ctx context.Context, client *Client, loc *shared
// CreateConsulCluster will make a call to the Consul service to initiate the create Consul
// cluster workflow.
func CreateConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
clusterID, datacenter, consulVersion string, numServers int32, private, connectEnabled bool, network *sharedmodels.HashicorpCloudLocationLink) (*consulmodels.HashicorpCloudConsul20200826CreateResponse, error) {
consulCluster *consulmodels.HashicorpCloudConsul20200826Cluster) (*consulmodels.HashicorpCloudConsul20200826CreateResponse, error) {

p := consul_service.NewCreateParams()
p := consul_service.NewConsulServiceCreateParams()
p.Context = ctx
p.Body = &consulmodels.HashicorpCloudConsul20200826CreateRequest{
Cluster: &consulmodels.HashicorpCloudConsul20200826Cluster{
Config: &consulmodels.HashicorpCloudConsul20200826ClusterConfig{
CapacityConfig: &consulmodels.HashicorpCloudConsul20200826CapacityConfig{
NumServers: numServers,
},
ConsulConfig: &consulmodels.HashicorpCloudConsul20200826ConsulConfig{
ConnectEnabled: connectEnabled,
Datacenter: datacenter,
},
MaintenanceConfig: nil,
NetworkConfig: &consulmodels.HashicorpCloudConsul20200826NetworkConfig{
Network: network,
Private: private,
},
},
ConsulVersion: consulVersion,
ID: clusterID,
Location: loc,
},
}
p.Body = &consulmodels.HashicorpCloudConsul20200826CreateRequest{Cluster: consulCluster}

p.ClusterLocationOrganizationID = loc.OrganizationID
p.ClusterLocationProjectID = loc.ProjectID

resp, err := client.Consul.Create(p, nil)
resp, err := client.Consul.ConsulServiceCreate(p, nil)
if err != nil {
return nil, err
}
Expand All @@ -115,12 +95,12 @@ func CreateConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.
// GetAvailableHCPConsulVersionsForLocation gets the list of available Consul versions that HCP supports for
// the provided location.
func GetAvailableHCPConsulVersionsForLocation(ctx context.Context, loc *sharedmodels.HashicorpCloudLocationLocation, client *Client) ([]*consulmodels.HashicorpCloudConsul20200826Version, error) {
p := consul_service.NewListVersionsParams()
p := consul_service.NewConsulServiceListVersionsParams()
p.Context = ctx
p.LocationProjectID = loc.ProjectID
p.LocationOrganizationID = loc.OrganizationID

resp, err := client.Consul.ListVersions(p, nil)
resp, err := client.Consul.ConsulServiceListVersions(p, nil)

if err != nil {
return nil, err
Expand All @@ -131,10 +111,10 @@ func GetAvailableHCPConsulVersionsForLocation(ctx context.Context, loc *sharedmo

// GetAvailableHCPConsulVersions gets the list of available Consul versions that HCP supports.
func GetAvailableHCPConsulVersions(ctx context.Context, client *Client) ([]*consulmodels.HashicorpCloudConsul20200826Version, error) {
p := consul_service.NewListVersions2Params()
p := consul_service.NewConsulServiceListVersions2Params()
p.Context = ctx

resp, err := client.Consul.ListVersions2(p, nil)
resp, err := client.Consul.ConsulServiceListVersions2(p, nil)

if err != nil {
return nil, err
Expand All @@ -148,13 +128,13 @@ func GetAvailableHCPConsulVersions(ctx context.Context, client *Client) ([]*cons
func DeleteConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
clusterID string) (*consulmodels.HashicorpCloudConsul20200826DeleteResponse, error) {

p := consul_service.NewDeleteParams()
p := consul_service.NewConsulServiceDeleteParams()
p.Context = ctx
p.ID = clusterID
p.LocationOrganizationID = loc.OrganizationID
p.LocationProjectID = loc.ProjectID

deleteResp, err := client.Consul.Delete(p, nil)
deleteResp, err := client.Consul.ConsulServiceDelete(p, nil)
if err != nil {
return nil, err
}
Expand All @@ -167,13 +147,13 @@ func DeleteConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.
func ListConsulUpgradeVersions(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
clusterID string) ([]*consulmodels.HashicorpCloudConsul20200826Version, error) {

p := consul_service.NewListUpgradeVersionsParams()
p := consul_service.NewConsulServiceListUpgradeVersionsParams()
p.Context = ctx
p.ID = clusterID
p.LocationOrganizationID = loc.OrganizationID
p.LocationProjectID = loc.ProjectID

resp, err := client.Consul.ListUpgradeVersions(p, nil)
resp, err := client.Consul.ConsulServiceListUpgradeVersions(p, nil)

if err != nil {
return nil, err
Expand All @@ -200,15 +180,15 @@ func UpdateConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.
},
}

updateParams := consul_service.NewUpdateParams()
updateParams := consul_service.NewConsulServiceUpdateParams()
updateParams.Context = ctx
updateParams.ClusterID = cluster.ID
updateParams.ClusterLocationProjectID = loc.ProjectID
updateParams.ClusterLocationOrganizationID = loc.OrganizationID
updateParams.Body = &cluster

// Invoke update cluster endpoint
updateResp, err := client.Consul.Update(updateParams, nil)
updateResp, err := client.Consul.ConsulServiceUpdate(updateParams, nil)
if err != nil {
return nil, err
}
Expand Down
Loading