Skip to content
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

Add compute_network_endpoint_group datasource #999

Merged
merged 1 commit into from
Aug 7, 2019
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
47 changes: 47 additions & 0 deletions google-beta/data_source_compute_network_endpoint_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package google

import (
"errors"
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceGoogleComputeNetworkEndpointGroup() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceComputeNetworkEndpointGroup().Schema)

// Set 'Optional' schema elements
addOptionalFieldsToSchema(dsSchema, "name")
addOptionalFieldsToSchema(dsSchema, "zone")
addOptionalFieldsToSchema(dsSchema, "self_link")

return &schema.Resource{
Read: dataSourceComputeNetworkEndpointGroupRead,
Schema: dsSchema,
}
}

func dataSourceComputeNetworkEndpointGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if name, ok := d.GetOk("name"); ok {
zone, err := getZone(d, config)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%s/%s", zone, name.(string)))
} else if selfLink, ok := d.GetOk("self_link"); ok {
parsed, err := ParseNetworkEndpointGroupFieldValue(selfLink.(string), d, config)
if err != nil {
return err
}
d.Set("name", parsed.Name)
d.Set("zone", parsed.Zone)
d.Set("project", parsed.Project)
d.SetId(fmt.Sprintf("%s/%s", parsed.Zone, parsed.Name))
} else {
return errors.New("Must provide either `self_link` or `zone/name`")
}

return resourceComputeNetworkEndpointGroupRead(d, meta)
}
95 changes: 95 additions & 0 deletions google-beta/data_source_compute_network_endpoint_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceComputeNetworkEndpointGroup(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(10),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceComputeNetworkEndpointGroupConfig(context),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceComputeNetworkEndpointGroupCheck("data.google_compute_network_endpoint_group.bar", "google_compute_network_endpoint_group.neg"),
),
},
},
})
}

func testAccDataSourceComputeNetworkEndpointGroupCheck(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{
"self_link",
"name",
"zone",
"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
}
}

func testAccDataSourceComputeNetworkEndpointGroupConfig(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network_endpoint_group" "neg" {
name = "my-lb-ds-neg%{random_suffix}"
network = "${google_compute_network.default.self_link}"
subnetwork = "${google_compute_subnetwork.default.self_link}"
default_port = "90"
zone = "us-central1-a"
}

resource "google_compute_network" "default" {
name = "ds-neg-network%{random_suffix}"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "default" {
name = "ds-neg-subnetwork%{random_suffix}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
network = "${google_compute_network.default.self_link}"
}

data "google_compute_network_endpoint_group" "bar" {
name = "${google_compute_network_endpoint_group.neg.name}"
zone = "us-central1-a"
}
`, context)
}
4 changes: 4 additions & 0 deletions google-beta/field_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func ParseSecurityPolicyFieldValue(securityPolicy string, d TerraformResourceDat
return parseGlobalFieldValue("securityPolicies", securityPolicy, "project", d, config, true)
}

func ParseNetworkEndpointGroupFieldValue(networkEndpointGroup string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) {
return parseZonalFieldValue("networkEndpointGroups", networkEndpointGroup, "project", "zone", d, config, false)
}

// ------------------------------------------------------------
// Base helpers used to create helpers for specific fields.
// ------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
"google_compute_network": dataSourceGoogleComputeNetwork(),
"google_compute_network_endpoint_group": dataSourceGoogleComputeNetworkEndpointGroup(),
"google_compute_node_types": dataSourceGoogleComputeNodeTypes(),
"google_compute_regions": dataSourceGoogleComputeRegions(),
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
layout: "google"
page_title: "Google: google_compute_network_endpoint_group"
sidebar_current: "docs-google-datasource-compute-network-endpoint-group"
description: |-
Retrieve Network Endpoint Group's details.
---

# google\_compute\_network\_endpoint\_group

Use this data source to access a Network Endpoint Group's attributes.

The NEG may be found by providing either a `self_link`, or a `name` and a `zone`.

## Example Usage

```hcl
data "google_compute_network_endpoint_group" "neg1" {
name = "k8s1-abcdef01-myns-mysvc-8080-4b6bac43"
zone = "us-central1-a"
}

data "google_compute_network_endpoint_group" "neg2" {
self_link = "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/networkEndpointGroups/k8s1-abcdef01-myns-mysvc-8080-4b6bac43"
}
```

## Argument Reference

The following arguments are supported:

* `project` - (Optional) The ID of the project to list versions in.
If it is not provided, the provider project is used.

* `name` - (Optional) The Network Endoint Group name.
Provide either this or a `self_link`.

* `zone` - (Optional) The Network Endoint Group availability zone.

* `self_link` - (Optional) The Network Endoint Group self\_link.

## Attributes Reference

In addition the arguments listed above, the following attributes are exported:

* `network` - The network to which all network endpoints in the NEG belong.
* `subnetwork` - subnetwork to which all network endpoints in the NEG belong.
* `description` - The NEG description.
* `network_endpoint_type` - Type of network endpoints in this network endpoint group.
* `default_port` - The NEG default port.
* `size` - Number of network endpoints in the network endpoint group.
3 changes: 3 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
<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-network-endpoint-group") %>>
<a href="/docs/providers/google/d/datasource_google_compute_network_endpoint_group.html">google_compute_network_endpoint_group</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-region-instance-group") %>>
<a href="/docs/providers/google/d/datasource_compute_region_instance_group.html">google_compute_region_instance_group</a>
</li>
Expand Down