Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Add project variable #28

Merged
merged 19 commits into from
Oct 9, 2018

Conversation

gfenn-newbury
Copy link

@gfenn-newbury gfenn-newbury commented Sep 21, 2018

What this MR does

This MR allows specifying of which project consul-cluster will deploy to, by adding a project variable for each compute instance as ${var.gcp_project_id}, as well as a project variable for the network and firewall rules, ${var.network_project_id}.

Why this MR is needed

The current method uses the project defined in the 'provider' block. This is good for single-project deployments, but when using consul-cluster with other terraform files which need deploying into other projects, there will be no project specified in provider. Each terraform object will have it's project specified instread. This MR will make sure that consul-cluster has the same ability.

Copy link
Collaborator

@josh-padnick josh-padnick left a comment

Choose a reason for hiding this comment

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

Thanks for this Pull Request! This looks like a nice addition. Other than the one comment I left, ihave just one additional request: Can you rename var.project to var.project_id to make it explicit that it's the project ID we want and not the project name or something else?

description = "All zones you want to deploy to within the specified region"
type = "list"
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why add this var? It doesn't appear to be used anywhere?

Copy link
Author

Choose a reason for hiding this comment

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

Removed in 19dd4d2

@gfenn-newbury
Copy link
Author

gfenn-newbury commented Sep 27, 2018

I've changed var.project to var.gcp_project_id to have consistency with the other modules in this repository.

I forgot I left var.gcp_zones in there. It was a change I was meaning to do for another branch to use google_compute_region_instance_group_manager.distribution_policy_zones to allow users to specify a list of zones to deploy within the given region, and single-zone deployments are no longer possible since the change from google_compute_instance_group_manager to google_compute_region_instance_group_manager.

@josh-padnick
Copy link
Collaborator

Looks good. The only thing I now realize we're missing is an update to https://github.com/hashicorp/terraform-google-consul/blob/master/main.tf to include the new project_id variable. Once that happens, I think we're ready to merge. Thanks for this contribution!

@gfenn-newbury
Copy link
Author

That should now be done

@gfenn-newbury
Copy link
Author

Major Change:

Similar to the change I made in hashicorp/terraform-google-vault#25, I've just added a new variable called network_project_id. This allows the user to use a network which isn't in the project the Consul instances will be running.

Copy link
Collaborator

@josh-padnick josh-padnick left a comment

Choose a reason for hiding this comment

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

Thanks for this updated PR. The ability to customize the network project seems like a nice addition. It turns out this PR is blocking hashicorp/terraform-google-vault#25, which is in turn blocking another PR, so I'm going to manually make these updates and merge this.

subnetwork = "${var.subnetwork_name != "" ? var.subnetwork_name : ""}"
network = "${var.subnetwork_name != "" ? "" : var.network_name}"
subnetwork = "${var.subnetwork_name != "" ? var.subnetwork_name : ""}"
subnetwork_project = "${var.network_project_id}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Requiring a value for network_project_id might be surprising to users who have all resources in a single network. Therefore, let's update this line as follows:

subnetwork         = "${var.subnetwork_name != "" ? var.subnetwork_name : ""}"
subnetwork_project = "${var.network_project_id != "" ? var.network_project_id : var.gcp_project_id}"

Can you update the default value and description of var.network_project_id accordingly? Specifically, var.network_project_id should now default to "" and the description should mention that, if var.network_project_id is empty, var.gcp_project_id will be used for the network.

@@ -93,7 +96,8 @@ resource "google_compute_instance_template" "consul_server_public" {
# Create the Instance Template that will be used to populate the Managed Instance Group.
# NOTE: This Compute Instance Template is only created if var.assign_public_ip_addresses is false.
resource "google_compute_instance_template" "consul_server_private" {
count = "${1 - var.assign_public_ip_addresses}"
project = "${var.gcp_project_id}"
count = "${1 - var.assign_public_ip_addresses}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a nit, but by convention, we always put the count property as the very first one in any resource and separate it from others by a blank line. So please update to:

resource "google_compute_instance_template" "consul_server_private" { 
   count = "${1 - var.assign_public_ip_addresses}"
 
   project = "${var.gcp_project_id}"
   ...
}

subnetwork = "${var.subnetwork_name != "" ? var.subnetwork_name : ""}"
network = "${var.subnetwork_name != "" ? "" : var.network_name}"
subnetwork = "${var.subnetwork_name != "" ? var.subnetwork_name : ""}"
subnetwork_project = "${var.network_project_id}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comment.

@@ -153,6 +158,8 @@ resource "google_compute_instance_template" "consul_server_private" {
# - This Firewall Rule may be redundant depnding on the settings of your VPC Network, but if your Network is locked down,
# this Rule will open up the appropriate ports.
resource "google_compute_firewall" "allow_intracluster_consul" {
project = "${var.network_project_id}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comment. Should be:

project = "${var.network_project_id != "" ? var.network_project_id : var.gcp_project_id}"

@@ -189,6 +196,8 @@ resource "google_compute_firewall" "allow_intracluster_consul" {
# - Note that public access to your Consul Cluster will only be permitted if var.assign_public_ip_addresses is true.
# - This Firewall Rule is only created if at least one source tag or source CIDR block is specified.
resource "google_compute_firewall" "allow_inbound_http_api" {
project = "${var.network_project_id}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comment.

@@ -213,7 +222,8 @@ resource "google_compute_firewall" "allow_inbound_http_api" {
# - Note that public access to your Consul Cluster will only be permitted if var.assign_public_ip_addresses is true.
# - This Firewall Rule is only created if at least one source tag or source CIDR block is specified.
resource "google_compute_firewall" "allow_inbound_dns" {
count = "${length(var.allowed_inbound_cidr_blocks_dns) + length(var.allowed_inbound_tags_dns) > 0 ? 1 : 0}"
project = "${var.network_project_id}"
count = "${length(var.allowed_inbound_cidr_blocks_dns) + length(var.allowed_inbound_tags_dns) > 0 ? 1 : 0}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above comments. Should be:

resource "google_compute_firewall" "allow_inbound_dns" {
   count   = "${length(var.allowed_inbound_cidr_blocks_dns) + length(var.allowed_inbound_tags_dns) > 0 ? 1 : 0}"

   project = "${var.network_project_id != "" ? var.network_project_id : var.gcp_project_id}"
   ...
}


variable "network_project_id" {
description = "The name of the GCP Project where the network is located. Useful when using networks shared between projects"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per above comments, update description to:

variable "network_project_id" { 
    description = "The name of the GCP Project where the network is located. Useful when using networks shared between projects. If empty, var.gcp_project_id will be used." 
    default = ""
}

variables.tf Outdated
description = "The name of the GCP Project where all resources will be launched."
}

variable "network_project_id" {
description = "The name of the GCP Project where the network is located. Useful for using networks shared between projects."
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per above, update to:

variable "network_project_id" { 
    description = "The name of the GCP Project where the network is located. Useful when using networks shared between projects. If empty, var.gcp_project_id will be used." 
    default = ""
}

@josh-padnick
Copy link
Collaborator

Merging now so I can validate with tests.

@josh-padnick josh-padnick merged commit 9bc26b2 into hashicorp:master Oct 9, 2018
@josh-padnick
Copy link
Collaborator

Validated in #30. Once that's merged, I'll issue a new release!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants