Skip to content

Commit

Permalink
feat: Add a skip_download var to use global gcloud instead (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Jan 30, 2020
1 parent a57245b commit 19c2263
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The [jq](https://stedolan.github.io/jq/) binary is also included in this module
| module\_depends\_on | List of modules or resources this module depends on. | list | `<list>` | no |
| platform | Platform CLI will run on. Defaults to linux. Valid values: linux, darwin | string | `"linux"` | no |
| service\_account\_key\_file | Path to service account key file to run `gcloud auth activate-service-account` with. Optional. | string | `""` | no |
| skip\_download | Whether to skip downloading gcloud (assumes gcloud is already available outside the module) | bool | `"false"` | no |
| upgrade | Whether to upgrade gcloud at runtime | bool | `"true"` | no |
| use\_tf\_google\_credentials\_env\_var | Use GOOGLE_CREDENTIALS environment variable to run `gcloud auth activate-service-account` with. Optional. | string | `"false"` | no |

Expand Down
15 changes: 9 additions & 6 deletions examples/dependency_example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ resource "random_pet" "filename" {
module "hello" {
source = "../.."

platform = "linux"
upgrade = false
platform = "linux"
upgrade = false
skip_download = true

create_cmd_entrypoint = "${path.module}/scripts/script.sh"
create_cmd_body = "${local.filename} hello"
Expand All @@ -37,8 +38,9 @@ module "hello" {
module "two" {
source = "../.."

platform = "linux"
upgrade = false
platform = "linux"
upgrade = false
skip_download = true

create_cmd_entrypoint = "${path.module}/scripts/script.sh"
create_cmd_body = "${local.filename} two"
Expand All @@ -47,8 +49,9 @@ module "two" {
module "goodbye" {
source = "../.."

platform = "linux"
upgrade = false
platform = "linux"
upgrade = false
skip_download = true

create_cmd_entrypoint = "${path.module}/scripts/script.sh"
create_cmd_body = "${local.filename} goodbye"
Expand Down
21 changes: 9 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ locals {
gcloud_bin_abs_path = abspath(local.gcloud_bin_path)
components = join(" ", var.additional_components)

gcloud = "${local.gcloud_bin_path}/gcloud"
gsutil = "${local.gcloud_bin_path}/gsutil"
bq = "${local.gcloud_bin_path}/bq"
kubectl = "${local.gcloud_bin_path}/kubectl"
jq = "${local.gcloud_bin_path}/jq"
gcloud = var.skip_download ? "gcloud" : "${local.gcloud_bin_path}/gcloud"

create_cmd_bin = "${local.gcloud_bin_path}/${var.create_cmd_entrypoint}"
destroy_cmd_bin = "${local.gcloud_bin_path}/${var.destroy_cmd_entrypoint}"
create_cmd_bin = var.skip_download ? var.create_cmd_entrypoint : "${local.gcloud_bin_path}/${var.create_cmd_entrypoint}"
destroy_cmd_bin = var.skip_download ? var.destroy_cmd_entrypoint : "${local.gcloud_bin_path}/${var.destroy_cmd_entrypoint}"
wait = length(null_resource.additional_components.*.triggers) + length(
null_resource.gcloud_auth_service_account_key_file.*.triggers,
Expand Down Expand Up @@ -62,7 +58,7 @@ resource "null_resource" "module_depends_on" {
}

resource "null_resource" "copy" {
count = var.enabled ? 1 : 0
count = (var.enabled && ! var.skip_download) ? 1 : 0

triggers = merge({
md5 = md5(var.create_cmd_entrypoint)
Expand All @@ -78,7 +74,7 @@ resource "null_resource" "copy" {
}

resource "null_resource" "decompress" {
count = var.enabled ? 1 : 0
count = (var.enabled && ! var.skip_download) ? 1 : 0

triggers = merge({
md5 = md5(var.create_cmd_entrypoint)
Expand All @@ -94,7 +90,7 @@ resource "null_resource" "decompress" {
}

resource "null_resource" "upgrade" {
count = (var.enabled && var.upgrade) ? 1 : 0
count = (var.enabled && var.upgrade && ! var.skip_download) ? 1 : 0

depends_on = [null_resource.decompress]

Expand Down Expand Up @@ -158,6 +154,7 @@ resource "null_resource" "run_command" {
count = var.enabled ? 1 : 0

depends_on = [
null_resource.module_depends_on,
null_resource.decompress,
null_resource.additional_components,
null_resource.gcloud_auth_google_credentials,
Expand Down Expand Up @@ -219,7 +216,7 @@ resource "null_resource" "additional_components_destroy" {
}

resource "null_resource" "upgrade_destroy" {
count = (var.enabled && var.upgrade) ? 1 : 0
count = (var.enabled && var.upgrade && ! var.skip_download) ? 1 : 0

depends_on = [
null_resource.additional_components_destroy,
Expand All @@ -234,7 +231,7 @@ resource "null_resource" "upgrade_destroy" {
}

resource "null_resource" "decompress_destroy" {
count = var.enabled ? 1 : 0
count = (var.enabled && ! var.skip_download) ? 1 : 0
depends_on = [null_resource.upgrade_destroy]

provisioner "local-exec" {
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ variable "upgrade" {
default = true
}

variable "skip_download" {
description = "Whether to skip downloading gcloud (assumes gcloud is already available outside the module)"
type = bool
default = false
}

variable "module_depends_on" {
description = "List of modules or resources this module depends on."
type = list
Expand Down

0 comments on commit 19c2263

Please sign in to comment.