diff --git a/.kitchen.yml b/.kitchen.yml index 9b70d52b..deab526b 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -102,4 +102,4 @@ suites: driver: name: terraform command_timeout: 1800 - root_module_directory: test/fixtures/mig_with_percent/simple + root_module_directory: test/fixtures/mig_with_percent/simple \ No newline at end of file diff --git a/autogen/main.tf.tmpl b/autogen/main.tf.tmpl index 58d87549..57029d71 100644 --- a/autogen/main.tf.tmpl +++ b/autogen/main.tf.tmpl @@ -91,6 +91,22 @@ resource "google_compute_region_instance_group_manager" "{{ module_name }}" { } } + dynamic "stateful_internal_ip" { + for_each = [for static_ip in var.stateful_ips: static_ip if static_ip["is_external"] == false] + content { + interface_name = stateful_internal_ip.value.interface_name + delete_rule = lookup(stateful_internal_ip.value, "delete_rule", null) + } + } + + dynamic "stateful_external_ip" { + for_each = [for static_ip in var.stateful_ips: static_ip if static_ip["is_external"] == true] + content { + interface_name = stateful_external_ip.value.interface_name + delete_rule = lookup(stateful_external_ip.value, "delete_rule", null) + } + } + distribution_policy_zones = local.distribution_policy_zones dynamic "update_policy" { for_each = var.update_policy diff --git a/autogen/variables.tf.tmpl b/autogen/variables.tf.tmpl index 1efa9a1d..3ee18c11 100644 --- a/autogen/variables.tf.tmpl +++ b/autogen/variables.tf.tmpl @@ -92,6 +92,19 @@ variable "stateful_disks" { default = [] } +################# +# Stateful IPs +################# +variable "stateful_ips" { + description = "Statful IPs created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-ip-addresses-in-migs" + type = list(object({ + interface_name = string + delete_rule = string + is_external = bool + })) + default = [] +} + ################# # Rolling Update ################# diff --git a/autogen/versions.tf.tmpl b/autogen/versions.tf.tmpl index eb971f2b..8754c0dd 100644 --- a/autogen/versions.tf.tmpl +++ b/autogen/versions.tf.tmpl @@ -17,8 +17,8 @@ terraform { required_version = ">=0.13.0" required_providers { - google = ">= 3.71, < 5.0" - google-beta = ">= 3.71, < 5.0" + google = ">= 4.48, < 5.0" + google-beta = ">= 4.48, < 5.0" } provider_meta "google" { module_name = "blueprints/terraform/terraform-google-vm:{% if mig %}mig{% else %}mig_with_percent{% endif %}/v8.0.1" diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml index 92601d83..3f95e1ea 100644 --- a/build/int.cloudbuild.yaml +++ b/build/int.cloudbuild.yaml @@ -228,7 +228,28 @@ steps: - verify-mig-with-percent-simple-local name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy mig-with-percent-simple-local'] - +- id: go-init-statful-mig + waitFor: + - create-all + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'cft test run TestMigStatefulModule --stage create --verbose'] +- id: go-apply-statful-mig + waitFor: + - go-init-statful-mig + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'cft test run TestMigStatefulModule --stage apply --verbose'] + timeout: 3600s +- id: go-verify-statful-mig + waitFor: + - go-apply-statful-mig + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'cft test run TestMigStatefulModule --stage verify --verbose'] +- id: go-destroy-statful-mig + waitFor: + - go-verify-statful-mig + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'cft test run TestMigStatefulModule --stage destroy --verbose'] + timeout: 1800s tags: - 'ci' - 'integration' diff --git a/examples/mig_stateful/README.md b/examples/mig_stateful/README.md new file mode 100644 index 00000000..2b5e1a23 --- /dev/null +++ b/examples/mig_stateful/README.md @@ -0,0 +1,24 @@ +# mig-simple + +This is a simple, minimal example of how to use the MIG module to create a +managed instance group. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| project\_id | The GCP project to use for integration tests | `string` | n/a | yes | +| region | The GCP region to create and test resources in | `string` | `"us-central1"` | no | +| service\_account | Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#service_account. |
object({
email = string
scopes = set(string)
})
| `null` | no | +| subnetwork | The subnetwork to host the compute instances in | `any` | n/a | yes | +| target\_size | The target number of running instances for this managed instance group. This value should always be explicitly set unless this resource is attached to an autoscaler, in which case it should never be set. | `any` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| region | The GCP region to create and test resources in | +| self\_link | Self-link of the managed instance group | + + diff --git a/examples/mig_stateful/main.tf b/examples/mig_stateful/main.tf new file mode 100644 index 00000000..fd3d2b9f --- /dev/null +++ b/examples/mig_stateful/main.tf @@ -0,0 +1,78 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +provider "google" { +} + +provider "google-beta" { +} + +resource "random_string" "suffix" { + length = 4 + special = "false" + upper = "false" +} + +/** Network **/ + +resource "google_compute_network" "main" { + project = var.project_id + name = "cft-vm-test-${random_string.suffix.result}" + auto_create_subnetworks = "false" +} + +resource "google_compute_subnetwork" "main" { + project = var.project_id + region = var.region + name = "cft-vm-test-${random_string.suffix.result}" + ip_cidr_range = "10.128.0.0/20" + network = google_compute_network.main.self_link +} + +module "instance_template" { + source = "../../modules/instance_template" + project_id = var.project_id + region = var.region + subnetwork = var.subnetwork + service_account = var.service_account + subnetwork_project = var.project_id +} + +module "mig" { + source = "../../modules/mig" + project_id = var.project_id + region = var.region + target_size = var.target_size + hostname = "mig-stateful" + instance_template = module.instance_template.self_link + stateful_ips = [{ + interface_name = "nic0" + delete_rule = "ON_PERMANENT_INSTANCE_DELETION" + is_external = true + }] + + update_policy = [{ + max_surge_fixed = 0 + instance_redistribution_type = "NONE" + max_surge_percent = null + max_unavailable_fixed = 4 + max_unavailable_percent = null + min_ready_sec = 180 + replacement_method = "RECREATE" + minimal_action = "REFRESH" + type = "OPPORTUNISTIC" + }] +} \ No newline at end of file diff --git a/examples/mig_stateful/outputs.tf b/examples/mig_stateful/outputs.tf new file mode 100644 index 00000000..2107246d --- /dev/null +++ b/examples/mig_stateful/outputs.tf @@ -0,0 +1,25 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +output "self_link" { + description = "Self-link of the managed instance group" + value = module.mig.self_link +} + +output "region" { + description = "The GCP region to create and test resources in" + value = var.region +} diff --git a/examples/mig_stateful/variables.tf b/examples/mig_stateful/variables.tf new file mode 100644 index 00000000..1e628a8c --- /dev/null +++ b/examples/mig_stateful/variables.tf @@ -0,0 +1,46 @@ +/** + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + + +variable "project_id" { + description = "The GCP project to use for integration tests" + type = string +} + +variable "region" { + description = "The GCP region to create and test resources in" + type = string + default = "us-central1" +} + +variable "subnetwork" { + description = "The subnetwork to host the compute instances in" +} + +variable "target_size" { + description = "The target number of running instances for this managed instance group. This value should always be explicitly set unless this resource is attached to an autoscaler, in which case it should never be set." +} + +variable "service_account" { + default = null + type = object({ + email = string + scopes = set(string) + }) + description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#service_account." +} + diff --git a/examples/mig_stateful/versions.tf b/examples/mig_stateful/versions.tf new file mode 100644 index 00000000..396bd738 --- /dev/null +++ b/examples/mig_stateful/versions.tf @@ -0,0 +1,29 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +terraform { + required_version = ">= 0.13" + required_providers { + google = { + source = "hashicorp/google" + version = "~> 4.0" + } + google-beta = { + source = "hashicorp/google-beta" + version = "~> 4.0" + } + } +} diff --git a/modules/mig/README.md b/modules/mig/README.md index ec21e7e1..732d0d0c 100644 --- a/modules/mig/README.md +++ b/modules/mig/README.md @@ -40,6 +40,7 @@ The current version is 2.X. The following guides are available to assist with up | region | The GCP region where the managed instance group resides. | `string` | n/a | yes | | scaling\_schedules | Autoscaling, scaling schedule block. https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_autoscaler#scaling_schedules |
list(object({
disabled = bool
duration_sec = number
min_required_replicas = number
name = string
schedule = string
time_zone = string
}))
| `[]` | no | | stateful\_disks | Disks created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-disks-in-migs |
list(object({
device_name = string
delete_rule = string
}))
| `[]` | no | +| stateful\_ips | Statful IPs created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-ip-addresses-in-migs |
list(object({
interface_name = string
delete_rule = string
is_external = bool
}))
| `[]` | no | | target\_pools | The target load balancing pools to assign this group to. | `list(string)` | `[]` | no | | target\_size | The target number of running instances for this managed instance group. This value should always be explicitly set unless this resource is attached to an autoscaler, in which case it should never be set. | `number` | `1` | no | | update\_policy | The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager#rolling_update_policy |
list(object({
max_surge_fixed = number
instance_redistribution_type = string
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
replacement_method = string
minimal_action = string
type = string
}))
| `[]` | no | diff --git a/modules/mig/main.tf b/modules/mig/main.tf index 7464e7fd..a500543f 100644 --- a/modules/mig/main.tf +++ b/modules/mig/main.tf @@ -75,6 +75,22 @@ resource "google_compute_region_instance_group_manager" "mig" { } } + dynamic "stateful_internal_ip" { + for_each = [for static_ip in var.stateful_ips : static_ip if static_ip["is_external"] == false] + content { + interface_name = stateful_internal_ip.value.interface_name + delete_rule = lookup(stateful_internal_ip.value, "delete_rule", null) + } + } + + dynamic "stateful_external_ip" { + for_each = [for static_ip in var.stateful_ips : static_ip if static_ip["is_external"] == true] + content { + interface_name = stateful_external_ip.value.interface_name + delete_rule = lookup(stateful_external_ip.value, "delete_rule", null) + } + } + distribution_policy_zones = local.distribution_policy_zones dynamic "update_policy" { for_each = var.update_policy diff --git a/modules/mig/variables.tf b/modules/mig/variables.tf index e4e19dee..38391ed2 100644 --- a/modules/mig/variables.tf +++ b/modules/mig/variables.tf @@ -74,6 +74,19 @@ variable "stateful_disks" { default = [] } +################# +# Stateful IPs +################# +variable "stateful_ips" { + description = "Statful IPs created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-ip-addresses-in-migs" + type = list(object({ + interface_name = string + delete_rule = string + is_external = bool + })) + default = [] +} + ################# # Rolling Update ################# diff --git a/modules/mig/versions.tf b/modules/mig/versions.tf index 62293184..5e29f1ce 100644 --- a/modules/mig/versions.tf +++ b/modules/mig/versions.tf @@ -17,8 +17,8 @@ terraform { required_version = ">=0.13.0" required_providers { - google = ">= 3.71, < 5.0" - google-beta = ">= 3.71, < 5.0" + google = ">= 4.48, < 5.0" + google-beta = ">= 4.48, < 5.0" } provider_meta "google" { module_name = "blueprints/terraform/terraform-google-vm:mig/v8.0.1" diff --git a/modules/mig_with_percent/README.md b/modules/mig_with_percent/README.md index c47bdd8d..b9c74f01 100644 --- a/modules/mig_with_percent/README.md +++ b/modules/mig_with_percent/README.md @@ -41,6 +41,7 @@ The current version is 2.X. The following guides are available to assist with up | region | The GCP region where the managed instance group resides. | `string` | n/a | yes | | scaling\_schedules | Autoscaling, scaling schedule block. https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_autoscaler#scaling_schedules |
list(object({
disabled = bool
duration_sec = number
min_required_replicas = number
name = string
schedule = string
time_zone = string
}))
| `[]` | no | | stateful\_disks | Disks created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-disks-in-migs |
list(object({
device_name = string
delete_rule = string
}))
| `[]` | no | +| stateful\_ips | Statful IPs created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-ip-addresses-in-migs |
list(object({
interface_name = string
delete_rule = string
is_external = bool
}))
| `[]` | no | | target\_pools | The target load balancing pools to assign this group to. | `list(string)` | `[]` | no | | target\_size | The target number of running instances for this managed instance group. This value should always be explicitly set unless this resource is attached to an autoscaler, in which case it should never be set. | `number` | `1` | no | | update\_policy | The rolling update policy. https://www.terraform.io/docs/providers/google/r/compute_region_instance_group_manager#rolling_update_policy |
list(object({
max_surge_fixed = number
instance_redistribution_type = string
max_surge_percent = number
max_unavailable_fixed = number
max_unavailable_percent = number
min_ready_sec = number
replacement_method = string
minimal_action = string
type = string
}))
| `[]` | no | diff --git a/modules/mig_with_percent/main.tf b/modules/mig_with_percent/main.tf index ed8d8fc0..a3831621 100644 --- a/modules/mig_with_percent/main.tf +++ b/modules/mig_with_percent/main.tf @@ -84,6 +84,22 @@ resource "google_compute_region_instance_group_manager" "mig_with_percent" { } } + dynamic "stateful_internal_ip" { + for_each = [for static_ip in var.stateful_ips : static_ip if static_ip["is_external"] == false] + content { + interface_name = stateful_internal_ip.value.interface_name + delete_rule = lookup(stateful_internal_ip.value, "delete_rule", null) + } + } + + dynamic "stateful_external_ip" { + for_each = [for static_ip in var.stateful_ips : static_ip if static_ip["is_external"] == true] + content { + interface_name = stateful_external_ip.value.interface_name + delete_rule = lookup(stateful_external_ip.value, "delete_rule", null) + } + } + distribution_policy_zones = local.distribution_policy_zones dynamic "update_policy" { for_each = var.update_policy diff --git a/modules/mig_with_percent/variables.tf b/modules/mig_with_percent/variables.tf index 68c848f7..dc5a48ac 100644 --- a/modules/mig_with_percent/variables.tf +++ b/modules/mig_with_percent/variables.tf @@ -84,6 +84,19 @@ variable "stateful_disks" { default = [] } +################# +# Stateful IPs +################# +variable "stateful_ips" { + description = "Statful IPs created on the instances that will be preserved on instance delete. https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-ip-addresses-in-migs" + type = list(object({ + interface_name = string + delete_rule = string + is_external = bool + })) + default = [] +} + ################# # Rolling Update ################# diff --git a/modules/mig_with_percent/versions.tf b/modules/mig_with_percent/versions.tf index 52d63523..6c5986eb 100644 --- a/modules/mig_with_percent/versions.tf +++ b/modules/mig_with_percent/versions.tf @@ -17,8 +17,8 @@ terraform { required_version = ">=0.13.0" required_providers { - google = ">= 3.71, < 5.0" - google-beta = ">= 3.71, < 5.0" + google = ">= 4.48, < 5.0" + google-beta = ">= 4.48, < 5.0" } provider_meta "google" { module_name = "blueprints/terraform/terraform-google-vm:mig_with_percent/v8.0.1" diff --git a/test/fixtures/mig_stateful/main.tf b/test/fixtures/mig_stateful/main.tf new file mode 100644 index 00000000..983f12d6 --- /dev/null +++ b/test/fixtures/mig_stateful/main.tf @@ -0,0 +1,24 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +module "mig_stateful" { + source = "../../../examples/mig_stateful" + project_id = var.project_id + region = "us-central1" + subnetwork = google_compute_subnetwork.main.name + target_size = 3 + service_account = var.service_account +} diff --git a/test/fixtures/mig_stateful/network.tf b/test/fixtures/mig_stateful/network.tf new file mode 120000 index 00000000..98e7464a --- /dev/null +++ b/test/fixtures/mig_stateful/network.tf @@ -0,0 +1 @@ +../shared/network.tf \ No newline at end of file diff --git a/test/fixtures/mig_stateful/outputs.tf b/test/fixtures/mig_stateful/outputs.tf new file mode 100644 index 00000000..371a9623 --- /dev/null +++ b/test/fixtures/mig_stateful/outputs.tf @@ -0,0 +1,33 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +output "mig_self_link" { + description = "Self-link to managed instance group" + value = module.mig_stateful.self_link +} + +output "project_id" { + description = "The GCP project to use for integration tests" + value = var.project_id +} + +output "region" { + description = "The GCP region to create and test resources in" + value = module.mig_stateful.region +} + + + diff --git a/test/fixtures/mig_stateful/variables.tf b/test/fixtures/mig_stateful/variables.tf new file mode 100644 index 00000000..78e5ea2f --- /dev/null +++ b/test/fixtures/mig_stateful/variables.tf @@ -0,0 +1,31 @@ +/** + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + + +variable "project_id" { + description = "The GCP project to use for integration tests" +} + +variable "service_account" { + default = null + type = object({ + email = string + scopes = list(string) + }) + description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#service_account." +} + diff --git a/test/fixtures/mig_stateful/versions.tf b/test/fixtures/mig_stateful/versions.tf new file mode 100644 index 00000000..fb3fee63 --- /dev/null +++ b/test/fixtures/mig_stateful/versions.tf @@ -0,0 +1,19 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +terraform { + required_version = ">=0.12.6" +} diff --git a/test/integration/mig_stateful/mig_stateful_test.go b/test/integration/mig_stateful/mig_stateful_test.go new file mode 100644 index 00000000..b15e388b --- /dev/null +++ b/test/integration/mig_stateful/mig_stateful_test.go @@ -0,0 +1,51 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mig_stateful + +import ( + "fmt" + "testing" + + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft" + "github.com/stretchr/testify/assert" +) + +func TestMigStatefulModule(t *testing.T) { + + const instanceGroupNamePrefix = "mig-stateful" + + expected_instance_groups := 1 + expected_instances := 3 + + migStatefulT := tft.NewTFBlueprintTest(t) + migStatefulT.DefineVerify(func(assert *assert.Assertions) { + migStatefulT.DefaultVerify(assert) + + instances := gcloud.Run(t, fmt.Sprintf("compute instances list --project %s --filter name~%s", migStatefulT.GetStringOutput("project_id"), instanceGroupNamePrefix)) + assert.Equal(expected_instances, len(instances.Array()), fmt.Sprintf("found %d gce instances", expected_instances)) + + instanceGroups := gcloud.Run(t, fmt.Sprintf("compute instance-groups list --project %s --filter name~%s", migStatefulT.GetStringOutput("project_id"), instanceGroupNamePrefix)) + assert.Equal(expected_instance_groups, len(instanceGroups.Array()), fmt.Sprintf("found %d gce instance group", expected_instance_groups)) + + managedInstanceGroups := gcloud.Run(t, fmt.Sprintf("compute instance-groups managed list --project %s --filter name~%s", migStatefulT.GetStringOutput("project_id"), instanceGroupNamePrefix)) + assert.Equal(expected_instance_groups, len(managedInstanceGroups.Array()), fmt.Sprintf("found %d gce managed instance group", expected_instance_groups)) + for _, mig := range managedInstanceGroups.Array() { + assert.Equal(true, mig.Get("status").Get("stateful").Get("hasStatefulConfig").Bool(), fmt.Sprintf("%s has stateful config", mig.Get("name").String())) + } + + }) + migStatefulT.Test() +}