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
Merged

HCS-1716: Add HCP Consul federation support #68

merged 13 commits into from
Feb 22, 2021

Conversation

riddhi89
Copy link
Contributor

Adds federation support for HCP Consul.

Copy link
Member

@mkeeler mkeeler left a comment

Choose a reason for hiding this comment

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

If you don't have federation enabled and attempt to use it from the TF provider how does this present itself to the user?

@riddhi89
Copy link
Contributor Author

@mkeeler : The user would get an error like:

hcp_consul_cluster.secondary: Creating...
Error: unable to create Consul cluster (consul-cluster-secondary): [POST /consul/2020-08-26/organizations/{cluster.location.organization_id}/projects/{cluster.location.project_id}/clusters][400] ConsulService_Create default  &{Code:3 Details:[] Error:federation is unavailable for this cluster Message:federation is unavailable for this cluster}

Copy link
Member

@hanshasselberg hanshasselberg left a comment

Choose a reason for hiding this comment

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

This is exciting. Which changes were generated and which are the ones you did? Mostly asking because that would make it a little easier to review.

return diag.Errorf("unable to check for presence of an existing primary Consul cluster (%s): %v", primary.ID, err)
}
primary.Location.Region = primaryConsulCluster.Location.Region
}
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if this kind of validation should live in cloud-consul-service.

Copy link
Contributor

Choose a reason for hiding this comment

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

💭 The provider does some basic validation, like input format and verifying the existence of dependent resources, but I'd agree that this seems closer to Consul business logic. That being said, I could see the advantage of having the request fail sooner, giving the user quicker feedback. How much later down the line would the consul service check for the primary?

Copy link
Contributor Author

@riddhi89 riddhi89 Feb 18, 2021

Choose a reason for hiding this comment

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

Let me verify if this is done in the consul-service. If not, definitely sounds like a good place to include it vs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nevermind. The reason I have this check in here is because I need the primary's region config (primary.Location.Region). Else, the below error is raised:


Error: unable to create Consul cluster (consul-cluster-secondary): [POST /consul/2020-08-26/organizations/{cluster.location.organization_id}/projects/{cluster.location.project_id}/clusters][400] ConsulService_Create default  &{Code:3 Details:[0xc000151e60] Error:config: (consul_config: (primary: (location: (region: cannot be blank.).).).). Message:config: (consul_config: (primary: (location: (region: cannot be blank.).).).).}```

Copy link
Contributor

@roaks3 roaks3 left a comment

Choose a reason for hiding this comment

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

🎉 Code LGTM, just had two comments

internal/clients/response.go Show resolved Hide resolved
internal/provider/resource_consul_cluster.go Show resolved Hide resolved
@xargs-P
Copy link
Contributor

xargs-P commented Feb 17, 2021

Hi @riddhi89 , things are looking good. 👌

Can you add a /examples/guides/ markdown for federation? And also update the example in docs/index.md ? Customers will be very interested in this functionality. So it would be sweet to give them as much help as possible.

@@ -0,0 +1,15 @@
data "hcp_hvn" "example" {
Copy link
Contributor

Choose a reason for hiding this comment

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

In these examples, we've generally been using resource for all the HCP resources, to encourage managing all those resources with the provider. We use data sources only for external resources like an AWS arn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

internal/clients/response.go Show resolved Hide resolved
Copy link
Contributor

@bcmdarroch bcmdarroch left a comment

Choose a reason for hiding this comment

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

This looks great! There's a few open questions I'm curious about: the necessity of self_link and whether or not we should push the verification of the primary cluster's existence into consul service.

internal/provider/data_source_consul_cluster.go Outdated Show resolved Hide resolved
internal/provider/data_source_consul_cluster.go Outdated Show resolved Hide resolved
internal/provider/resource_consul_cluster.go Outdated Show resolved Hide resolved
internal/provider/resource_consul_cluster.go Outdated Show resolved Hide resolved
@@ -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 😅

return diag.Errorf("unable to check for presence of an existing primary Consul cluster (%s): %v", primary.ID, err)
}
primary.Location.Region = primaryConsulCluster.Location.Region
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💭 The provider does some basic validation, like input format and verifying the existence of dependent resources, but I'd agree that this seems closer to Consul business logic. That being said, I could see the advantage of having the request fail sooner, giving the user quicker feedback. How much later down the line would the consul service check for the primary?

@@ -417,6 +451,22 @@ func setConsulClusterResourceData(d *schema.ResourceData, cluster *consulmodels.
return err
}

// self_link is equal to the terraform ID of the resource
if err := d.Set("self_link", d.Id()); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the attribute self_link needed if it's the same as the terraform ID?

Copy link
Contributor

Choose a reason for hiding this comment

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

The reason it's a separate field is to be explicit to the user that it is a separate property so they aren't confused between, id and cluster_id. It also protects against the Terraform id format being changed in the future (not likely but 🤷 ).

That being said, I think it might be best to "re-build" the link using the newLink and linkUrl functions just so we aren't relying on the id.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

might be best to "re-build" the link

Makes sense. Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -75,9 +75,9 @@ 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) {
clusterID, datacenter, consulVersion string, numServers int32, private, connectEnabled bool, network *sharedmodels.HashicorpCloudLocationLink, primary *sharedmodels.HashicorpCloudLocationLink) (*consulmodels.HashicorpCloudConsul20200826CreateResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There are a lot of params to this function and the two links at the end make me think it would be easy to flip the hvn and primary in the future. What do you think about it taking in a single consulmodels.HashicorpCloudConsul20200826Cluster struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good improvement to me. Will update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

riddhi89 and others added 6 commits February 18, 2021 15:54
Adds federation support for HCP Consul.
Co-authored-by: Brenna Hewer-Darroch <brenna@hashicorp.com>
Co-authored-by: Brenna Hewer-Darroch <brenna@hashicorp.com>
Co-authored-by: Brenna Hewer-Darroch <brenna@hashicorp.com>
Co-authored-by: Brenna Hewer-Darroch <brenna@hashicorp.com>
@riddhi89
Copy link
Contributor Author

riddhi89 commented Feb 18, 2021

@i0rek

Which changes were generated and which are the ones you did?

All the changes within docs/* were generated.

@xargs-P
Copy link
Contributor

xargs-P commented Feb 19, 2021

Thanks @riddhi89 .
Can you also update the example in docs/index.md ?


# Federate a new HCP Consul cluster with an existing one

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

Choose a reason for hiding this comment

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

Thank you adding this guide. ❤️
Can we add an a to the sentence here?

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

Copy link
Member

@hanshasselberg hanshasselberg left a comment

Choose a reason for hiding this comment

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

Looks great!

if err != nil {
return diag.Errorf(err.Error())
}
if primary.Location.OrganizationID == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: With the implementation you have here, you can probably take this block of code out of the primary.Location.OrganizationID == "" conditional. parseLinkURL() parses the URL without attempting to resolve any additional information, so primary.Location.OrganizationID would always be empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@xargs-P xargs-P left a comment

Choose a reason for hiding this comment

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

Thank you!

@riddhi89 riddhi89 merged commit 8136e61 into main Feb 22, 2021
@riddhi89 riddhi89 deleted the hcs-1716 branch February 22, 2021 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants