From 8fb69aea88ba98102875a817589a975abb31405b Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 15 Feb 2019 10:26:44 -0800 Subject: [PATCH] Add self_link to subnetwork datasource (#3040) Signed-off-by: Modular Magician --- .../data_source_google_compute_subnetwork.go | 27 +++++-------------- ...a_source_google_compute_subnetwork_test.go | 6 ++++- ...atasource_compute_subnetwork.html.markdown | 12 ++++----- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/google/data_source_google_compute_subnetwork.go b/google/data_source_google_compute_subnetwork.go index ec94c28c..5319f26f 100644 --- a/google/data_source_google_compute_subnetwork.go +++ b/google/data_source_google_compute_subnetwork.go @@ -14,14 +14,13 @@ func dataSourceGoogleComputeSubnetwork() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, - Required: true, - }, - - "description": { - Type: schema.TypeString, - Computed: true, + Optional: true, }, "self_link": { + Type: schema.TypeString, + Optional: true, + }, + "description": { Type: schema.TypeString, Computed: true, }, @@ -75,15 +74,10 @@ func dataSourceGoogleComputeSubnetwork() *schema.Resource { func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - project, err := getProject(d, config) + project, region, name, err := GetRegionalResourcePropertiesFromSelfLinkOrSchema(d, config) if err != nil { return err } - region, err := getRegion(d, config) - if err != nil { - return err - } - name := d.Get("name").(string) subnetwork, err := config.clientCompute.Subnetworks.Get(project, region, name).Do() if err != nil { @@ -98,12 +92,9 @@ func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interfac d.Set("network", subnetwork.Network) d.Set("project", project) d.Set("region", region) - // Flattening code defined in resource_compute_subnetwork.go d.Set("secondary_ip_range", flattenSecondaryRanges(subnetwork.SecondaryIpRanges)) - //Subnet id creation is defined in resource_compute_subnetwork.go - subnetwork.Region = region - d.SetId(createSubnetID(subnetwork)) + d.SetId(fmt.Sprintf("%s/%s", region, name)) return nil } @@ -119,7 +110,3 @@ func flattenSecondaryRanges(secondaryRanges []*compute.SubnetworkSecondaryRange) } return secondaryRangesSchema } - -func createSubnetID(s *compute.Subnetwork) string { - return fmt.Sprintf("%s/%s", s.Region, s.Name) -} diff --git a/google/data_source_google_compute_subnetwork_test.go b/google/data_source_google_compute_subnetwork_test.go index 19b8c885..b8fa4516 100644 --- a/google/data_source_google_compute_subnetwork_test.go +++ b/google/data_source_google_compute_subnetwork_test.go @@ -72,11 +72,11 @@ func testAccDataSourceGoogleSubnetworkCheck(data_source_name string, resource_na func testAccDataSourceGoogleSubnetwork() string { return fmt.Sprintf(` - resource "google_compute_network" "foobar" { name = "%s" description = "my-description" } + resource "google_compute_subnetwork" "foobar" { name = "subnetwork-test" description = "my-description" @@ -92,5 +92,9 @@ resource "google_compute_subnetwork" "foobar" { data "google_compute_subnetwork" "my_subnetwork" { name = "${google_compute_subnetwork.foobar.name}" } + +data "google_compute_subnetwork" "my_subnetwork_self_link" { + self_link = "${google_compute_subnetwork.foobar.self_link}" +} `, acctest.RandomWithPrefix("network-test")) } diff --git a/website/docs/d/datasource_compute_subnetwork.html.markdown b/website/docs/d/datasource_compute_subnetwork.html.markdown index a4b4349e..19493e34 100644 --- a/website/docs/d/datasource_compute_subnetwork.html.markdown +++ b/website/docs/d/datasource_compute_subnetwork.html.markdown @@ -12,7 +12,7 @@ Get a subnetwork within GCE from its name and region. ## Example Usage -```tf +```hcl data "google_compute_subnetwork" "my-subnetwork" { name = "default-us-east1" region = "us-east1" @@ -23,9 +23,11 @@ data "google_compute_subnetwork" "my-subnetwork" { The following arguments are supported: -* `name` - The name of the subnetwork. +* `self_link` - (Optional) The self link of the subnetwork. If `self_link` is +specified, `name`, `project`, and `region` are ignored. -- - - +* `name` - (Optional) The name of the subnetwork. One of `name` or `self_link` +must be specified. * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. @@ -54,12 +56,10 @@ In addition to the arguments listed above, the following attributes are exported * `secondary_ip_range` - An array of configurations for secondary IP ranges for VM instances contained in this subnetwork. Structure is documented below. -* `self_link` - The URI of the created resource. - The `secondary_ip_range` block supports: * `range_name` - The name associated with this subnetwork secondary range, used when adding an alias IP range to a VM instance. * `ip_cidr_range` - The range of IP addresses belonging to this subnetwork - secondary range. \ No newline at end of file + secondary range.