Skip to content

Commit

Permalink
Add self_link to subnetwork datasource (#429)
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 Feb 15, 2019
1 parent c7d363f commit 1ea16c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
25 changes: 6 additions & 19 deletions google-beta/data_source_google_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ func dataSourceGoogleComputeSubnetwork() *schema.Resource {
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Optional: true,
},

"description": {
"self_link": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"self_link": {
"description": {
Type: schema.TypeString,
Computed: true,
},
Expand Down Expand Up @@ -75,15 +74,10 @@ func dataSourceGoogleComputeSubnetwork() *schema.Resource {
func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}
region, err := getRegion(d, config)
project, region, name, err := GetRegionalResourcePropertiesFromSelfLinkOrSchema(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 {
Expand All @@ -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
}

Expand All @@ -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)
}
6 changes: 5 additions & 1 deletion google-beta/data_source_google_compute_subnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"))
}
12 changes: 6 additions & 6 deletions website/docs/d/datasource_compute_subnetwork.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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.
secondary range.

0 comments on commit 1ea16c5

Please sign in to comment.