From d6a1aec3733a8c0c2c7160ac001226f35a6ae1ac Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 10:06:12 -0700 Subject: [PATCH 01/11] Change subnetwork to a versioned resource --- .../data_source_google_compute_subnetwork.go | 5 + google/resource_compute_subnetwork.go | 122 +++++++++++++----- 2 files changed, 97 insertions(+), 30 deletions(-) diff --git a/google/data_source_google_compute_subnetwork.go b/google/data_source_google_compute_subnetwork.go index 03a368bcdbb..26875149ff0 100644 --- a/google/data_source_google_compute_subnetwork.go +++ b/google/data_source_google_compute_subnetwork.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/compute/v1" "google.golang.org/api/googleapi" ) @@ -90,3 +91,7 @@ func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interfac d.SetId(createSubnetID(subnetwork)) return nil } + +func createSubnetID(s *compute.Subnetwork) string { + return fmt.Sprintf("%s/%s", s.Region, s.Name) +} diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index c078168ceb8..6e3ba145d6e 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -7,9 +7,13 @@ import ( "strings" "github.com/hashicorp/terraform/helper/schema" + computeBeta "google.golang.org/api/compute/v0.beta" "google.golang.org/api/compute/v1" ) +var SubnetworkBaseApiVersion = v1 +var SubnetworkVersionedFeatures = []Feature{} + func resourceComputeSubnetwork() *schema.Resource { return &schema.Resource{ Create: resourceComputeSubnetworkCreate, @@ -76,18 +80,8 @@ func resourceComputeSubnetwork() *schema.Resource { } } -func createSubnetID(s *compute.Subnetwork) string { - return fmt.Sprintf("%s/%s", s.Region, s.Name) -} - -func splitSubnetID(id string) (region string, name string) { - parts := strings.Split(id, "/") - region = parts[0] - name = parts[1] - return -} - func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) error { + computeApiVersion := getComputeApiVersion(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures) config := meta.(*Config) region, err := getRegion(d, config) @@ -106,7 +100,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e } // Build the subnetwork parameters - subnetwork := &compute.Subnetwork{ + subnetwork := &computeBeta.Subnetwork{ Name: d.Get("name").(string), Description: d.Get("description").(string), IpCidrRange: d.Get("ip_cidr_range").(string), @@ -115,11 +109,27 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e } log.Printf("[DEBUG] Subnetwork insert request: %#v", subnetwork) - op, err := config.clientCompute.Subnetworks.Insert( - project, region, subnetwork).Do() - if err != nil { - return fmt.Errorf("Error creating subnetwork: %s", err) + var op interface{} + switch computeApiVersion { + case v1: + subnetworkV1 := &compute.Subnetwork{} + err := Convert(subnetwork, subnetworkV1) + if err != nil { + return err + } + + op, err = config.clientCompute.Subnetworks.Insert( + project, region, subnetworkV1).Do() + if err != nil { + return fmt.Errorf("Error creating subnetwork: %s", err) + } + case v0beta: + op, err = config.clientComputeBeta.Subnetworks.Insert( + project, region, subnetwork).Do() + if err != nil { + return fmt.Errorf("Error creating subnetwork: %s", err) + } } // It probably maybe worked, so store the ID now. ID is a combination of region + subnetwork @@ -128,9 +138,9 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e // The same name can appear twice in a project, as long as each one is in a different region." // https://cloud.google.com/compute/docs/subnetworks subnetwork.Region = region - d.SetId(createSubnetID(subnetwork)) + d.SetId(createBetaSubnetID(subnetwork)) - err = computeOperationWait(config, op, project, "Creating Subnetwork") + err = computeSharedOperationWait(config, op, project, "Creating Subnetwork") if err != nil { return err } @@ -139,6 +149,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e } func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error { + computeApiVersion := getComputeApiVersion(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures) config := meta.(*Config) region, err := getRegion(d, config) @@ -153,10 +164,25 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err name := d.Get("name").(string) - subnetwork, err := config.clientCompute.Subnetworks.Get( - project, region, name).Do() - if err != nil { - return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name)) + subnetwork := &computeBeta.Subnetwork{} + switch computeApiVersion { + case v1: + subnetworkV1, err := config.clientCompute.Subnetworks.Get( + project, region, name).Do() + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name)) + } + + err = Convert(subnetworkV1, subnetwork) + if err != nil { + return err + } + case v0beta: + var err error + subnetwork, err = config.clientComputeBeta.Subnetworks.Get(project, region, name).Do() + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name)) + } } d.Set("name", subnetwork.Name) @@ -165,12 +191,13 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err d.Set("description", subnetwork.Description) d.Set("private_ip_google_access", subnetwork.PrivateIpGoogleAccess) d.Set("gateway_address", subnetwork.GatewayAddress) - d.Set("self_link", subnetwork.SelfLink) + d.Set("self_link", ConvertSelfLinkToV1(subnetwork.SelfLink)) return nil } func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) error { + computeApiVersion := getComputeApiVersionUpdate(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures, []Feature{}) config := meta.(*Config) region, err := getRegion(d, config) @@ -186,18 +213,34 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e d.Partial(true) if d.HasChange("private_ip_google_access") { - subnetworksSetPrivateIpGoogleAccessRequest := &compute.SubnetworksSetPrivateIpGoogleAccessRequest{ + subnetworksSetPrivateIpGoogleAccessRequest := &computeBeta.SubnetworksSetPrivateIpGoogleAccessRequest{ PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool), } log.Printf("[DEBUG] Updating Subnetwork PrivateIpGoogleAccess %q: %#v", d.Id(), subnetworksSetPrivateIpGoogleAccessRequest) - op, err := config.clientCompute.Subnetworks.SetPrivateIpGoogleAccess( - project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequest).Do() + + var op interface{} + switch computeApiVersion { + case v1: + subnetworksSetPrivateIpGoogleAccessRequestV1 := &compute.SubnetworksSetPrivateIpGoogleAccessRequest{} + err := Convert(subnetworksSetPrivateIpGoogleAccessRequest, subnetworksSetPrivateIpGoogleAccessRequestV1) + if err != nil { + return err + } + + op, err = config.clientCompute.Subnetworks.SetPrivateIpGoogleAccess( + project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequestV1).Do() + case v0beta: + op, err = config.clientComputeBeta.Subnetworks.SetPrivateIpGoogleAccess( + project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequest).Do() + + } + if err != nil { return fmt.Errorf("Error updating subnetwork PrivateIpGoogleAccess: %s", err) } - err = computeOperationWait(config, op, project, "Updating Subnetwork PrivateIpGoogleAccess") + err = computeSharedOperationWait(config, op, project, "Updating Subnetwork PrivateIpGoogleAccess") if err != nil { return err } @@ -211,6 +254,7 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e } func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) error { + computeApiVersion := getComputeApiVersion(d, SubnetworkBaseApiVersion, SubnetworkVersionedFeatures) config := meta.(*Config) region, err := getRegion(d, config) @@ -224,13 +268,20 @@ func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) e } // Delete the subnetwork - op, err := config.clientCompute.Subnetworks.Delete( - project, region, d.Get("name").(string)).Do() + var op interface{} + switch computeApiVersion { + case v1: + op, err = config.clientCompute.Subnetworks.Delete( + project, region, d.Get("name").(string)).Do() + case v0beta: + op, err = config.clientComputeBeta.Subnetworks.Delete( + project, region, d.Get("name").(string)).Do() + } if err != nil { return fmt.Errorf("Error deleting subnetwork: %s", err) } - err = computeOperationWait(config, op, project, "Deleting Subnetwork") + err = computeSharedOperationWait(config, op, project, "Deleting Subnetwork") if err != nil { return err } @@ -256,3 +307,14 @@ func resourceComputeSubnetworkImportState(d *schema.ResourceData, meta interface return []*schema.ResourceData{d}, nil } + +func createBetaSubnetID(s *computeBeta.Subnetwork) string { + return fmt.Sprintf("%s/%s", s.Region, s.Name) +} + +func splitSubnetID(id string) (region string, name string) { + parts := strings.Split(id, "/") + region = parts[0] + name = parts[1] + return +} From bcb3c20e923c70fa9531544bb563679c72b827f6 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 10:35:59 -0700 Subject: [PATCH 02/11] Fix beta operation waiting --- google/compute_operation.go | 11 +++++++++++ google/compute_shared_operation.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/google/compute_operation.go b/google/compute_operation.go index bce57bb6291..459ba336965 100644 --- a/google/compute_operation.go +++ b/google/compute_operation.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" + computeBeta "google.golang.org/api/compute/v0.beta" "google.golang.org/api/compute/v1" ) @@ -91,3 +92,13 @@ func computeOperationWaitTime(config *Config, op *compute.Operation, project, ac return nil } + +func computeBetaOperationWaitTime(config *Config, op *computeBeta.Operation, project, activity string, timeoutMin int) error { + opV1 := &compute.Operation{} + err := Convert(op, opV1) + if err != nil { + return err + } + + return computeOperationWaitTime(config, opV1, project, activity, 4) +} diff --git a/google/compute_shared_operation.go b/google/compute_shared_operation.go index 775e6680913..9be0e110ddc 100644 --- a/google/compute_shared_operation.go +++ b/google/compute_shared_operation.go @@ -18,7 +18,7 @@ func computeSharedOperationWaitTime(config *Config, op interface{}, project stri case *compute.Operation: return computeOperationWaitTime(config, op.(*compute.Operation), project, activity, minutes) case *computeBeta.Operation: - return computeBetaOperationWaitGlobalTime(config, op.(*computeBeta.Operation), project, activity, minutes) + return computeBetaOperationWaitTime(config, op.(*computeBeta.Operation), project, activity, minutes) default: panic("Attempted to wait on an Operation of unknown type.") } From 1d9ff17fad726dc16b63ea4e59d961869f796f64 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 10:36:47 -0700 Subject: [PATCH 03/11] Add method to compare self link resource names --- google/self_link_helpers.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/google/self_link_helpers.go b/google/self_link_helpers.go index bda74d4bb2a..019cb40d622 100644 --- a/google/self_link_helpers.go +++ b/google/self_link_helpers.go @@ -28,6 +28,17 @@ func compareSelfLinkRelativePaths(k, old, new string, d *schema.ResourceData) bo return false } +// This method should only be used if the self link references a global resource. +func compareSelfLinkResourceNames(k, old, new string, d *schema.ResourceData) bool { + oldParts := strings.Split(old, "/") + newParts := strings.Split(new, "/") + + if oldParts[len(oldParts)-1] == newParts[len(newParts)-1] { + return true + } + return false +} + // Hash the relative path of a self link. func selfLinkRelativePathHash(selfLink interface{}) int { path, _ := getRelativePath(selfLink.(string)) From 5bb2ed0cb6a69c14ab6506bb37a5225d8ca79b5b Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 10:37:35 -0700 Subject: [PATCH 04/11] Suppress diff for network name in subnetwork --- google/resource_compute_subnetwork.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index 6e3ba145d6e..db2f88fa6e1 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -41,7 +41,7 @@ func resourceComputeSubnetwork() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - DiffSuppressFunc: linkDiffSuppress, + DiffSuppressFunc: compareSelfLinkResourceNames, }, "description": &schema.Schema{ From 4b33f9a5adfd0d8c4f42199de853c2c797cfe882 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 11:06:26 -0700 Subject: [PATCH 05/11] Add documentation for beta secondary ip ranges for subnetwork --- website/docs/r/compute_subnetwork.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/website/docs/r/compute_subnetwork.html.markdown b/website/docs/r/compute_subnetwork.html.markdown index 55eed2b55e6..b0fd3aaeb73 100644 --- a/website/docs/r/compute_subnetwork.html.markdown +++ b/website/docs/r/compute_subnetwork.html.markdown @@ -56,6 +56,16 @@ The following arguments are supported: can access Google services without assigned external IP addresses. +- - - + +* `secondary_ip_range` - (Optional, Beta) An array of configurations for secondary IP ranges for VM instances contained in this subnetwork. Structure is documented below. + +The `secondary_ip_range` block supports: + +* `range_name` - (Required) The name associated with this subnetwork secondary range, used when adding an alias IP range to a VM instance. + +* `ip_cidr_range` - (Required) The range of IP addresses belonging to this subnetwork secondary range. Ranges must be unique and non-overlapping with all primary and secondary IP ranges within a network. + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are From ece8cde691d935b888e0e664fd4b14d0ccf96f31 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 11:06:54 -0700 Subject: [PATCH 06/11] Add secondary ip range beta support to subnetwork resource --- google/resource_compute_subnetwork.go | 56 +++++++++++++- google/resource_compute_subnetwork_test.go | 89 ++++++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index db2f88fa6e1..17287f5cbf1 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -12,7 +12,12 @@ import ( ) var SubnetworkBaseApiVersion = v1 -var SubnetworkVersionedFeatures = []Feature{} +var SubnetworkVersionedFeatures = []Feature{ + { + Version: v0beta, + Item: "secondary_ip_range", + }, +} func resourceComputeSubnetwork() *schema.Resource { return &schema.Resource{ @@ -72,6 +77,26 @@ func resourceComputeSubnetwork() *schema.Resource { Optional: true, }, + "secondary_ip_range": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "range_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateGCPName, + }, + "ip_cidr_range": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + }, + }, + "self_link": &schema.Schema{ Type: schema.TypeString, Computed: true, @@ -105,6 +130,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e Description: d.Get("description").(string), IpCidrRange: d.Get("ip_cidr_range").(string), PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool), + SecondaryIpRanges: expandSecondaryRanges(d.Get("secondary_ip_range").([]interface{})), Network: network, } @@ -191,6 +217,7 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err d.Set("description", subnetwork.Description) d.Set("private_ip_google_access", subnetwork.PrivateIpGoogleAccess) d.Set("gateway_address", subnetwork.GatewayAddress) + d.Set("secondary_ip_range", flattenSecondaryRanges(subnetwork.SecondaryIpRanges)) d.Set("self_link", ConvertSelfLinkToV1(subnetwork.SelfLink)) return nil @@ -318,3 +345,30 @@ func splitSubnetID(id string) (region string, name string) { name = parts[1] return } + +func expandSecondaryRanges(configured []interface{}) []*computeBeta.SubnetworkSecondaryRange { + secondaryRanges := make([]*computeBeta.SubnetworkSecondaryRange, 0, len(configured)) + for _, raw := range configured { + data := raw.(map[string]interface{}) + secondaryRange := computeBeta.SubnetworkSecondaryRange{ + RangeName: data["range_name"].(string), + IpCidrRange: data["ip_cidr_range"].(string), + } + + secondaryRanges = append(secondaryRanges, &secondaryRange) + } + return secondaryRanges +} + +func flattenSecondaryRanges(secondaryRanges []*computeBeta.SubnetworkSecondaryRange) []map[string]interface{} { + secondaryRangesSchema := make([]map[string]interface{}, 0, len(secondaryRanges)) + for _, secondaryRange := range secondaryRanges { + data := map[string]interface{}{ + "range_name": secondaryRange.RangeName, + "ip_cidr_range": secondaryRange.IpCidrRange, + } + + secondaryRangesSchema = append(secondaryRangesSchema, data) + } + return secondaryRangesSchema +} diff --git a/google/resource_compute_subnetwork_test.go b/google/resource_compute_subnetwork_test.go index 7ad178a4794..d6c67dd8996 100644 --- a/google/resource_compute_subnetwork_test.go +++ b/google/resource_compute_subnetwork_test.go @@ -8,6 +8,8 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "google.golang.org/api/compute/v1" + + computeBeta "google.golang.org/api/compute/v0.beta" ) func TestAccComputeSubnetwork_basic(t *testing.T) { @@ -70,6 +72,28 @@ func TestAccComputeSubnetwork_update(t *testing.T) { } } +func TestAccComputeSubnetwork_secondaryIpRanges(t *testing.T) { + var subnetwork computeBeta.Subnetwork + + cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + subnetworkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeSubnetworkDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeSubnetwork_secondaryIpRanges(cnName, subnetworkName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeBetaSubnetworkExists("google_compute_subnetwork.network-with-private-secondary-ip-range", &subnetwork), + testAccCheckComputeSubnetworkHasSecondaryIpRange(&subnetwork, "tf-test-secondary-range", "192.168.1.0/24"), + ), + }, + }, + }) +} + func testAccCheckComputeSubnetworkDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -119,6 +143,51 @@ func testAccCheckComputeSubnetworkExists(n string, subnetwork *compute.Subnetwor } } +func testAccCheckComputeBetaSubnetworkExists(n string, subnetwork *computeBeta.Subnetwork) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + config := testAccProvider.Meta().(*Config) + + region, subnet_name := splitSubnetID(rs.Primary.ID) + found, err := config.clientComputeBeta.Subnetworks.Get( + config.Project, region, subnet_name).Do() + if err != nil { + return err + } + + if found.Name != subnet_name { + return fmt.Errorf("Subnetwork not found") + } + + *subnetwork = *found + + return nil + } +} + +func testAccCheckComputeSubnetworkHasSecondaryIpRange(subnetwork *computeBeta.Subnetwork, rangeName, ipCidrRange string) resource.TestCheckFunc { + return func(s *terraform.State) error { + for _, secondaryRange := range subnetwork.SecondaryIpRanges { + if secondaryRange.RangeName == rangeName { + if secondaryRange.IpCidrRange == ipCidrRange { + return nil + } + return fmt.Errorf("Secondary range %s has the wrong ip_cidr_range. Expected %s, got %s", rangeName, ipCidrRange, secondaryRange.IpCidrRange) + } + } + + return fmt.Errorf("Secondary range %s not found", rangeName) + } +} + func testAccComputeSubnetwork_basic(cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name string) string { return fmt.Sprintf(` resource "google_compute_network" "custom-test" { @@ -183,3 +252,23 @@ resource "google_compute_subnetwork" "network-with-private-google-access" { } `, cnName, subnetworkName) } + +func testAccComputeSubnetwork_secondaryIpRanges(cnName, subnetworkName string) string { + return fmt.Sprintf(` +resource "google_compute_network" "custom-test" { + name = "%s" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "network-with-private-secondary-ip-range" { + name = "%s" + ip_cidr_range = "10.2.0.0/16" + region = "us-central1" + network = "${google_compute_network.custom-test.self_link}" + secondary_ip_range { + range_name = "tf-test-secondary-range" + ip_cidr_range = "192.168.1.0/24" + } +} +`, cnName, subnetworkName) +} From a0954122e5d2b279725c6ef15b679a75c3b7189f Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 11:11:42 -0700 Subject: [PATCH 07/11] Remove duplication of error checking --- google/resource_compute_subnetwork.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index 17287f5cbf1..89d631522c1 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -147,15 +147,13 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e op, err = config.clientCompute.Subnetworks.Insert( project, region, subnetworkV1).Do() - if err != nil { - return fmt.Errorf("Error creating subnetwork: %s", err) - } case v0beta: op, err = config.clientComputeBeta.Subnetworks.Insert( project, region, subnetwork).Do() - if err != nil { - return fmt.Errorf("Error creating subnetwork: %s", err) - } + } + + if err != nil { + return fmt.Errorf("Error creating subnetwork: %s", err) } // It probably maybe worked, so store the ID now. ID is a combination of region + subnetwork From da4f8284bfacef3d3f5d5952d865262bd818b8da Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 11:12:23 -0700 Subject: [PATCH 08/11] make fmt --- google/resource_compute_subnetwork.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index 89d631522c1..18cd2433257 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -151,7 +151,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e op, err = config.clientComputeBeta.Subnetworks.Insert( project, region, subnetwork).Do() } - + if err != nil { return fmt.Errorf("Error creating subnetwork: %s", err) } From b27758f7d2737c56ac6419bb5d3416fe1e07ddf5 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 8 Aug 2017 11:26:30 -0700 Subject: [PATCH 09/11] Add ForceNew to secondary_ip_range field --- google/resource_compute_subnetwork.go | 1 + 1 file changed, 1 insertion(+) diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index 18cd2433257..62c03120418 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -80,6 +80,7 @@ func resourceComputeSubnetwork() *schema.Resource { "secondary_ip_range": &schema.Schema{ Type: schema.TypeList, Optional: true, + ForceNew: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "range_name": &schema.Schema{ From 0230844fcf038f5d9dbd89204a04f3f3653d50a9 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Wed, 9 Aug 2017 10:37:03 -0700 Subject: [PATCH 10/11] Addressed Dana's comments --- google/data_source_google_compute_subnetwork.go | 5 ----- google/resource_compute_subnetwork.go | 4 ++++ google/self_link_helpers.go | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/google/data_source_google_compute_subnetwork.go b/google/data_source_google_compute_subnetwork.go index 26875149ff0..03a368bcdbb 100644 --- a/google/data_source_google_compute_subnetwork.go +++ b/google/data_source_google_compute_subnetwork.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/compute/v1" "google.golang.org/api/googleapi" ) @@ -91,7 +90,3 @@ func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interfac d.SetId(createSubnetID(subnetwork)) return nil } - -func createSubnetID(s *compute.Subnetwork) string { - return fmt.Sprintf("%s/%s", s.Region, s.Name) -} diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index 62c03120418..6d8797bd731 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -338,6 +338,10 @@ func createBetaSubnetID(s *computeBeta.Subnetwork) string { return fmt.Sprintf("%s/%s", s.Region, s.Name) } +func createSubnetID(s *compute.Subnetwork) string { + return fmt.Sprintf("%s/%s", s.Region, s.Name) +} + func splitSubnetID(id string) (region string, name string) { parts := strings.Split(id, "/") region = parts[0] diff --git a/google/self_link_helpers.go b/google/self_link_helpers.go index 019cb40d622..c13aa7df793 100644 --- a/google/self_link_helpers.go +++ b/google/self_link_helpers.go @@ -28,7 +28,7 @@ func compareSelfLinkRelativePaths(k, old, new string, d *schema.ResourceData) bo return false } -// This method should only be used if the self link references a global resource. +// Use this method when the field accepts either a name or a self_link referencing a global resource. func compareSelfLinkResourceNames(k, old, new string, d *schema.ResourceData) bool { oldParts := strings.Split(old, "/") newParts := strings.Split(new, "/") From 6aad76dd3fe513d325b3d60eac0357bf9bb88962 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Wed, 9 Aug 2017 14:03:23 -0700 Subject: [PATCH 11/11] Rename self link or name method comparison method --- google/resource_compute_subnetwork.go | 2 +- google/self_link_helpers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/google/resource_compute_subnetwork.go b/google/resource_compute_subnetwork.go index 6d8797bd731..b4fcbbcefe0 100644 --- a/google/resource_compute_subnetwork.go +++ b/google/resource_compute_subnetwork.go @@ -46,7 +46,7 @@ func resourceComputeSubnetwork() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - DiffSuppressFunc: compareSelfLinkResourceNames, + DiffSuppressFunc: compareGlobalSelfLinkOrResourceName, }, "description": &schema.Schema{ diff --git a/google/self_link_helpers.go b/google/self_link_helpers.go index c13aa7df793..75e246f6651 100644 --- a/google/self_link_helpers.go +++ b/google/self_link_helpers.go @@ -29,7 +29,7 @@ func compareSelfLinkRelativePaths(k, old, new string, d *schema.ResourceData) bo } // Use this method when the field accepts either a name or a self_link referencing a global resource. -func compareSelfLinkResourceNames(k, old, new string, d *schema.ResourceData) bool { +func compareGlobalSelfLinkOrResourceName(k, old, new string, d *schema.ResourceData) bool { oldParts := strings.Split(old, "/") newParts := strings.Split(new, "/")