-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compute_network_endpoint_group datasource
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
d768791
commit 48cab4b
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
website/docs/datasource_google_compute_network_endpoint_group.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
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 | ||
|
||
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. | ||
* `self_link` - URI of the NEG. | ||
* `size` - Number of network endpoints in the network endpoint group. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters