Add VPN gateway data source (#1071)

This commit is contained in:
Julien Phalip 2018-02-12 16:28:02 -07:00 committed by Nathan McKinley
parent ae70019805
commit f6c5f01a95
5 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,78 @@
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)
func dataSourceGoogleComputeVpnGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleComputeVpnGatewayRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceGoogleComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region, err := getRegion(d, config)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
gateway, err := vpnGatewaysService.Get(project, region, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("VPN Gateway Not Found : %s", name))
}
d.Set("network", gateway.Network)
d.Set("region", gateway.Region)
d.Set("self_link", gateway.SelfLink)
d.Set("description", gateway.Description)
d.Set("project", project)
d.SetId(gateway.Name)
return nil
}

View File

@ -0,0 +1,77 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccDataSourceGoogleVpnGateway(t *testing.T) {
t.Parallel()
vpnGatewayName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceGoogleVpnGatewayConfig(vpnGatewayName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleVpnGatewayCheck("data.google_compute_vpn_gateway.my_vpn_gateway", "google_compute_vpn_gateway.foobar"),
),
},
},
})
}
func testAccDataSourceGoogleVpnGatewayCheck(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
vpn_gateway_attrs_to_test := []string{
"id",
"self_link",
"name",
"description",
"network",
}
for _, attr_to_check := range vpn_gateway_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
}
}
func testAccDataSourceGoogleVpnGatewayConfig(name string) string {
return fmt.Sprintf(`
resource "google_compute_vpn_gateway" "foobar" {
name = "%s"
description = "my-description"
network = "default"
}
data "google_compute_vpn_gateway" "my_vpn_gateway" {
name = "${google_compute_vpn_gateway.foobar.name}"
}`, name)
}

View File

@ -74,6 +74,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_zones": dataSourceGoogleComputeZones(),
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
"google_compute_vpn_gateway": dataSourceGoogleComputeVpnGateway(),
"google_container_cluster": dataSourceGoogleContainerCluster(),
"google_container_engine_versions": dataSourceGoogleContainerEngineVersions(),
"google_container_registry_repository": dataSourceGoogleContainerRepo(),

View File

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

View File

@ -37,6 +37,9 @@
<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-vpn-gateway") %>>
<a href="/docs/providers/google/d/datasource_compute_vpn_gateway.html">google_compute_vpn_gateway</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>