Skip to content

Commit

Permalink
Add docs about using lifecycle when recreating instance groups (#200)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @rileykarson
  • Loading branch information
modular-magician authored and rileykarson committed Dec 10, 2018
1 parent cddca8c commit 5bed62e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 3 deletions.
96 changes: 96 additions & 0 deletions google-beta/resource_compute_instance_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@ func TestAccComputeInstanceGroup_basic(t *testing.T) {
})
}

func TestAccComputeInstanceGroup_rename(t *testing.T) {
t.Parallel()

var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))
var instanceGroupName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))
var backendName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))
var healthName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccComputeInstanceGroup_destroy,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceGroup_rename(instanceName, instanceGroupName, backendName, healthName),
},
{
ResourceName: "google_compute_instance_group.basic",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeInstanceGroup_rename(instanceName, instanceGroupName+"2", backendName, healthName),
},
{
ResourceName: "google_compute_instance_group.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccComputeInstanceGroup_recreatedInstances(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -359,6 +392,69 @@ func testAccComputeInstanceGroup_basic(instance string) string {
}`, instance, instance, instance)
}

func testAccComputeInstanceGroup_rename(instance, instanceGroup, backend, health string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_instance" "ig_instance" {
name = "%s"
machine_type = "n1-standard-1"
can_ip_forward = false
zone = "us-central1-c"
boot_disk {
initialize_params {
image = "${data.google_compute_image.my_image.self_link}"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_instance_group" "basic" {
name = "%s"
zone = "us-central1-c"
instances = [ "${google_compute_instance.ig_instance.self_link}" ]
named_port {
name = "http"
port = "8080"
}
named_port {
name = "https"
port = "8443"
}
lifecycle {
create_before_destroy = true
}
}
resource "google_compute_backend_service" "default_backend" {
name = "%s"
port_name = "https"
protocol = "HTTPS"
backend {
group = "${google_compute_instance_group.basic.self_link}"
}
health_checks = [
"${google_compute_https_health_check.healthcheck.self_link}",
]
}
resource "google_compute_https_health_check" "healthcheck" {
name = "%s"
request_path = "/health_check"
}
`, instance, instanceGroup, backend, health)
}

func testAccComputeInstanceGroup_update(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
73 changes: 70 additions & 3 deletions website/docs/r/compute_instance_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Creates a group of dissimilar Compute Engine virtual machine instances.
For more information, see [the official documentation](https://cloud.google.com/compute/docs/instance-groups/#unmanaged_instance_groups)
and [API](https://cloud.google.com/compute/docs/reference/latest/instanceGroups)

## Example Usage
-> Recreating an instance group that's in use by another resource will give a
`resourceInUseByAnotherResource` error. You can avoid this error with a
Terraform `lifecycle` block as outlined in the example below.

### Empty instance group
## Example Usage - Empty instance group

```hcl
resource "google_compute_instance_group" "test" {
Expand All @@ -25,7 +27,7 @@ resource "google_compute_instance_group" "test" {
}
```

### With instances and named ports
### Example Usage - With instances and named ports

```hcl
resource "google_compute_instance_group" "webservers" {
Expand All @@ -51,6 +53,71 @@ resource "google_compute_instance_group" "webservers" {
}
```

### Example Usage - Recreating an instance group in use
Recreating an instance group that's in use by another resource will give a
`resourceInUseByAnotherResource` error. Use `lifecycle.create_before_destroy`
as shown in this example to avoid this type of error.

```hcl
resource "google_compute_instance_group" "staging_group" {
name = "staging-instance-group"
zone = "us-central1-c"
instances = [ "${google_compute_instance.staging_vm.self_link}" ]
named_port {
name = "http"
port = "8080"
}
named_port {
name = "https"
port = "8443"
}
lifecycle {
create_before_destroy = true
}
}
data "google_compute_image" "debian_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_instance" "staging_vm" {
name = "staging-vm"
machine_type = "n1-standard-1"
zone = "us-central1-c"
boot_disk {
initialize_params {
image = "${data.google_compute_image.debian_image.self_link}"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_backend_service" "staging_service" {
name = "staging-service"
port_name = "https"
protocol = "HTTPS"
backend {
group = "${google_compute_instance_group.staging_group.self_link}"
}
health_checks = [
"${google_compute_https_health_check.staging_health.self_link}",
]
}
resource "google_compute_https_health_check" "staging_health" {
name = "staging-health"
request_path = "/health_check"
}
```

## Argument Reference

The following arguments are supported:
Expand Down

0 comments on commit 5bed62e

Please sign in to comment.