provider/google: datasource subnetwork and network (#12442)

* first version of this datasource

* add network and subnetwork datasource and documentation

* modify sidebar reference in documentation

* fix elements after review on network and subnetwork datasources

* fix fmt on Google provider.go

* modify code with the review

* modify documentation layout order

* fix alphabetic order in provider.go

* fix rebase issue and documentation datasource => data
This commit is contained in:
Mathieu Herbert 2017-04-13 22:25:29 +02:00 committed by Dana Hoffman
parent aeb97165d6
commit 25cbbdea8a
8 changed files with 415 additions and 2 deletions

View File

@ -0,0 +1,73 @@
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/googleapi"
)
func dataSourceGoogleComputeNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleComputeNetworkRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"gateway_ipv4": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"subnetworks_self_links": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceGoogleComputeNetworkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
network, err := config.clientCompute.Networks.Get(
project, d.Get("name").(string)).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't exist anymore
return fmt.Errorf("Network Not Found : %s", d.Get("name"))
}
return fmt.Errorf("Error reading network: %s", err)
}
d.Set("gateway_ipv4", network.GatewayIPv4)
d.Set("self_link", network.SelfLink)
d.Set("description", network.Description)
d.Set("subnetworks_self_links", network.Subnetworks)
d.SetId(network.Name)
return nil
}

View File

@ -0,0 +1,68 @@
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"testing"
)
func TestAccDataSourceGoogleNetwork(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: TestAccDataSourceGoogleNetworkConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleNetworkCheck("data.google_compute_network.my_network", "google_compute_network.foobar"),
),
},
},
})
}
func testAccDataSourceGoogleNetworkCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[data_source_name]
if !ok {
return fmt.Errorf("root module has no resource called %s", data_source_name)
}
rs, ok := s.RootModule().Resources[resource_name]
if !ok {
return fmt.Errorf("can't find %s in state", resource_name)
}
ds_attr := ds.Primary.Attributes
rs_attr := rs.Primary.Attributes
network_attrs_to_test := []string{
"id",
"self_link",
"name",
"description",
}
for _, attr_to_check := range network_attrs_to_test {
if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
return fmt.Errorf(
"%s is %s; want %s",
attr_to_check,
ds_attr[attr_to_check],
rs_attr[attr_to_check],
)
}
}
return nil
}
}
var TestAccDataSourceGoogleNetworkConfig = `
resource "google_compute_network" "foobar" {
name = "network-test"
description = "my-description"
}
data "google_compute_network" "my_network" {
name = "${google_compute_network.foobar.name}"
}`

View File

@ -0,0 +1,87 @@
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/googleapi"
)
func dataSourceGoogleComputeSubnetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleComputeSubnetworkRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ip_cidr_range": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"gateway_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
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)
if err != nil {
return err
}
subnetwork, err := config.clientCompute.Subnetworks.Get(
project, region, d.Get("name").(string)).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't exist anymore
return fmt.Errorf("Subnetwork Not Found")
}
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
d.Set("ip_cidr_range", subnetwork.IpCidrRange)
d.Set("self_link", subnetwork.SelfLink)
d.Set("description", subnetwork.Description)
d.Set("gateway_address", subnetwork.GatewayAddress)
d.Set("network", subnetwork.Network)
//Subnet id creation is defined in resource_compute_subnetwork.go
subnetwork.Region = region
d.SetId(createSubnetID(subnetwork))
return nil
}

View File

@ -0,0 +1,81 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccDataSourceGoogleSubnetwork(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: TestAccDataSourceGoogleSubnetworkConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleSubnetworkCheck("data.google_compute_subnetwork.my_subnetwork", "google_compute_subnetwork.foobar"),
),
},
},
})
}
func testAccDataSourceGoogleSubnetworkCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[data_source_name]
if !ok {
return fmt.Errorf("root module has no resource called %s", data_source_name)
}
rs, ok := s.RootModule().Resources[resource_name]
if !ok {
return fmt.Errorf("can't find %s in state", resource_name)
}
ds_attr := ds.Primary.Attributes
rs_attr := rs.Primary.Attributes
subnetwork_attrs_to_test := []string{
"id",
"self_link",
"name",
"description",
"ip_cidr_range",
"network",
}
for _, attr_to_check := range subnetwork_attrs_to_test {
if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
return fmt.Errorf(
"%s is %s; want %s",
attr_to_check,
ds_attr[attr_to_check],
rs_attr[attr_to_check],
)
}
}
return nil
}
}
var TestAccDataSourceGoogleSubnetworkConfig = `
resource "google_compute_network" "foobar" {
name = "network-test"
description = "my-description"
}
resource "google_compute_subnetwork" "foobar" {
name = "subnetwork-test"
description = "my-description"
ip_cidr_range = "10.0.0.0/24"
network = "${google_compute_network.foobar.self_link}"
}
data "google_compute_subnetwork" "my_subnetwork" {
name = "${google_compute_subnetwork.foobar.name}"
}
`

View File

@ -48,8 +48,10 @@ func Provider() terraform.ResourceProvider {
},
DataSourcesMap: map[string]*schema.Resource{
"google_iam_policy": dataSourceGoogleIamPolicy(),
"google_compute_zones": dataSourceGoogleComputeZones(),
"google_compute_network": dataSourceGoogleComputeNetwork(),
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
"google_compute_zones": dataSourceGoogleComputeZones(),
"google_iam_policy": dataSourceGoogleIamPolicy(),
},
ResourcesMap: map[string]*schema.Resource{

View File

@ -0,0 +1,46 @@
---
layout: "google"
page_title: "Google: google_compute_network"
sidebar_current: "docs-google-datasource-compute-network"
description: |-
Get a network within GCE.
---
# google\_compute\_network
Get a network within GCE from its name.
## Example Usage
```tf
data "google_compute_network" "my-network" {
name = "default-us-east1"
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the network.
- - -
* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.
## Attributes Reference
In addition to the arguments listed above, the following attributes are exported:
* `network` - The network name or resource link to the parent
network of this network.
* `description` - Description of this network.
* `gateway_ipv4` - The IP address of the gateway.
* `subnetworks_self_links` - the list of subnetworks which belong to the network
* `self_link` - The URI of the resource.

View File

@ -0,0 +1,50 @@
---
layout: "google"
page_title: "Google: google_compute_subnetwork"
sidebar_current: "docs-google-datasource-compute-subnetwork"
description: |-
Get a subnetwork within GCE.
---
# google\_compute\_subnetwork
Get a subnetwork within GCE from its name and region.
## Example Usage
```tf
data "google_compute_subnetwork" "my-subnetwork" {
name = "default-us-east1"
region = "us-east1"
}
```
## Argument Reference
The following arguments are supported:
* `name` - The name of the subnetwork.
- - -
* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.
* `region` - (Optional) The region this subnetwork has been created in. If
unspecified, this defaults to the region configured in the provider.
## Attributes Reference
In addition to the arguments listed above, the following attributes are exported:
* `network` - The network name or resource link to the parent
network of this subnetwork.
* `description` - Description of this subnetwork.
* `ip_cidr_range` - The IP address range that machines in this
network are assigned to, represented as a CIDR block.
* `gateway_address` - The IP address of the gateway.
* `self_link` - The URI of the created resource.

View File

@ -13,6 +13,12 @@
<li<%= sidebar_current("docs-google-datasource") %>>
<a href="#">Google Cloud Platform Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-google-datasource-compute-network") %>>
<a href="/docs/providers/google/d/datasource_compute_network.html">google_compute_network</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-subnetwork") %>>
<a href="/docs/providers/google/d/datasource_compute_subnetwork.html">google_compute_subnetwork</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-zones") %>>
<a href="/docs/providers/google/d/google_compute_zones.html">google_compute_zones</a>
</li>