Update google_compute_target_pool to no longer have a plan/apply loop with instance URLs (#666)

* Using instance URLs in `google_compute_target_pool` no longer has a plan/apply loop

* global regex; parts length check
This commit is contained in:
Dana Hoffman 2017-11-03 17:02:02 -07:00 committed by GitHub
parent 8f8d36d04c
commit 1b8f450cb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View File

@ -3,12 +3,15 @@ package google
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)
var instancesSelfLinkPattern = regexp.MustCompile(fmt.Sprintf(zonalLinkBasePattern, "instances"))
func resourceComputeTargetPool() *schema.Resource {
return &schema.Resource{
Create: resourceComputeTargetPoolCreate,
@ -57,6 +60,25 @@ func resourceComputeTargetPool() *schema.Resource {
Computed: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeString},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// instances are stored in state as "zone/name"
oldParts := strings.Split(old, "/")
// instances can also be specified in the config as a URL
if parts := instancesSelfLinkPattern.FindStringSubmatch(new); len(oldParts) == 2 && len(parts) == 4 {
// parts[0] = full match
// parts[1] = project
// parts[2] = zone
// parts[3] = instance name
oZone, oName := oldParts[0], oldParts[1]
nZone, nName := parts[2], parts[3]
if oZone == nZone && oName == nName {
return true
}
}
return false
},
},
"project": {

View File

@ -79,12 +79,28 @@ resource "google_compute_http_health_check" "foobar" {
host = "example.com"
}
resource "google_compute_instance" "foobar" {
name = "inst-tp-test-%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params{
image = "debian-8-jessie-v20160803"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_target_pool" "foobar" {
description = "Resource created for Terraform acceptance testing"
instances = ["us-central1-a/foo", "us-central1-b/bar"]
instances = ["${google_compute_instance.foobar.self_link}", "us-central1-b/bar"]
name = "tpool-test-%s"
session_affinity = "CLIENT_IP_PROTO"
health_checks = [
"${google_compute_http_health_check.foobar.name}"
]
}`, acctest.RandString(10), acctest.RandString(10))
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))