Skip to content

Add VPN gateway data source #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions google/data_source_google_compute_vpn_gateway.go
Original file line number Diff line number Diff line change
@@ -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
}
77 changes: 77 additions & 0 deletions google/data_source_google_compute_vpn_gateway_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
46 changes: 46 additions & 0 deletions website/docs/d/datasource_compute_vpn_gateway.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down