|
| 1 | +/** |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +locals { |
| 18 | + # Emulate the default behaviour when --destination isn't set otherwise we don't know how to run the destroy command |
| 19 | + implied_destination = coalesce(var.destination_path, basename(var.source_path)) |
| 20 | + create_parameters = { |
| 21 | + "--project" = var.project_id |
| 22 | + "--location" = var.location |
| 23 | + "--environment" = var.environment |
| 24 | + "--source" = var.source_path |
| 25 | + "--destination" = var.destination_path |
| 26 | + } |
| 27 | + destroy_parameters = { |
| 28 | + "--project" = var.project_id |
| 29 | + "--location" = var.location |
| 30 | + "--environment" = var.environment |
| 31 | + } |
| 32 | + |
| 33 | + gcloud_cmd_body = "composer environments storage ${var.type}" |
| 34 | + create_cmd_body = "${local.gcloud_cmd_body} import ${join(" ", [for k, v in local.create_parameters : "${k}=${v}" if v != null])}" |
| 35 | + destroy_cmd_body = "${local.gcloud_cmd_body} delete ${join(" ", [for k, v in local.destroy_parameters : "${k}=${v}"])} ${local.implied_destination}" |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +module "gcloud" { |
| 40 | + source = "terraform-google-modules/gcloud/google" |
| 41 | + version = "~> 3.1" |
| 42 | + platform = "linux" |
| 43 | + create_cmd_body = local.create_cmd_body |
| 44 | + destroy_cmd_body = local.destroy_cmd_body |
| 45 | + |
| 46 | + create_cmd_triggers = { |
| 47 | + filesha1 = join(",", [ |
| 48 | + for f in fileset(var.source_path, "**") : "${f}:${filesha1("${var.source_path}/${f}")}" |
| 49 | + ]) |
| 50 | + } |
| 51 | +} |
0 commit comments