diff --git a/google-beta/data_source_compute_network_endpoint_group.go b/google-beta/data_source_compute_network_endpoint_group.go new file mode 100644 index 0000000000..3dc97f7fee --- /dev/null +++ b/google-beta/data_source_compute_network_endpoint_group.go @@ -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) +} diff --git a/google-beta/data_source_compute_network_endpoint_group_test.go b/google-beta/data_source_compute_network_endpoint_group_test.go new file mode 100644 index 0000000000..beecd0d58a --- /dev/null +++ b/google-beta/data_source_compute_network_endpoint_group_test.go @@ -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) +} diff --git a/google-beta/field_helpers.go b/google-beta/field_helpers.go index 981a170659..771fcc9a92 100644 --- a/google-beta/field_helpers.go +++ b/google-beta/field_helpers.go @@ -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. // ------------------------------------------------------------ diff --git a/google-beta/provider.go b/google-beta/provider.go index 5903995cef..5ffd82fc90 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -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(), diff --git a/website/docs/d/datasource_google_compute_network_endpoint_group.html.markdown b/website/docs/d/datasource_google_compute_network_endpoint_group.html.markdown new file mode 100644 index 0000000000..7fec0140c4 --- /dev/null +++ b/website/docs/d/datasource_google_compute_network_endpoint_group.html.markdown @@ -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. diff --git a/website/google.erb b/website/google.erb index d44810788a..e44756c9c9 100644 --- a/website/google.erb +++ b/website/google.erb @@ -78,6 +78,9 @@