-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 instance group data source #267
Merged
rosbo
merged 2 commits into
hashicorp:master
from
aadidenko:gce-data-instance-group-instances
Aug 11, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,83 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceGoogleComputeInstanceGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceComputeInstanceGroupRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"zone": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"instances": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"named_port": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"port": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"network": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"self_link": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"size": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
zone := d.Get("zone").(string) | ||
name := d.Get("name").(string) | ||
|
||
d.SetId(fmt.Sprintf("%s/%s", zone, name)) | ||
|
||
return resourceComputeInstanceGroupRead(d, meta) | ||
} |
253 changes: 253 additions & 0 deletions
253
google/data_source_google_compute_instance_group_test.go
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,253 @@ | ||
package google | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccDataSourceGoogleComputeInstanceGroup_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckDataSourceGoogleComputeInstanceGroupConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDataSourceGoogleComputeInstanceGroup("data.google_compute_instance_group.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceGoogleComputeInstanceGroup_withNamedPort(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckDataSourceGoogleComputeInstanceGroupConfigWithNamedPort, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDataSourceGoogleComputeInstanceGroup("data.google_compute_instance_group.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckDataSourceGoogleComputeInstanceGroup(dataSourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
dsFullName := "data.google_compute_instance_group.test" | ||
rsFullName := "google_compute_instance_group.test" | ||
ds, ok := s.RootModule().Resources[dsFullName] | ||
if !ok { | ||
return fmt.Errorf("cant' find resource called %s in state", dsFullName) | ||
} | ||
|
||
rs, ok := s.RootModule().Resources[rsFullName] | ||
if !ok { | ||
return fmt.Errorf("can't find data source called %s in state", rsFullName) | ||
} | ||
|
||
dsAttrs := ds.Primary.Attributes | ||
rsAttrs := rs.Primary.Attributes | ||
|
||
attrsToTest := []string{ | ||
"id", | ||
"name", | ||
"zone", | ||
"project", | ||
"description", | ||
"network", | ||
"self_link", | ||
"size", | ||
} | ||
|
||
for _, attrToTest := range attrsToTest { | ||
if dsAttrs[attrToTest] != rsAttrs[attrToTest] { | ||
return fmt.Errorf("%s is %s; want %s", attrToTest, dsAttrs[attrToTest], rsAttrs[attrToTest]) | ||
} | ||
} | ||
|
||
dsNamedPortsCount, ok := dsAttrs["named_port.#"] | ||
if !ok { | ||
return errors.New("can't find 'named_port' attribute in data source") | ||
} | ||
|
||
dsNoOfNamedPorts, err := strconv.Atoi(dsNamedPortsCount) | ||
if err != nil { | ||
return errors.New("failed to read number of named ports in data source") | ||
} | ||
|
||
rsNamedPortsCount, ok := rsAttrs["named_port.#"] | ||
if !ok { | ||
return errors.New("can't find 'named_port' attribute in resource") | ||
} | ||
|
||
rsNoOfNamedPorts, err := strconv.Atoi(rsNamedPortsCount) | ||
if err != nil { | ||
return errors.New("failed to read number of named ports in resource") | ||
} | ||
|
||
if dsNoOfNamedPorts != rsNoOfNamedPorts { | ||
return fmt.Errorf( | ||
"expected %d number of named port, received %d, this is most likely a bug", | ||
rsNoOfNamedPorts, | ||
dsNoOfNamedPorts, | ||
) | ||
} | ||
|
||
namedPortItemKeys := []string{"name", "value"} | ||
for i := 0; i < dsNoOfNamedPorts; i++ { | ||
for _, key := range namedPortItemKeys { | ||
idx := fmt.Sprintf("named_port.%d.%s", i, key) | ||
if dsAttrs[idx] != rsAttrs[idx] { | ||
return fmt.Errorf("%s is %s; want %s", idx, dsAttrs[idx], rsAttrs[idx]) | ||
} | ||
} | ||
} | ||
|
||
dsInstancesCount, ok := dsAttrs["instances.#"] | ||
if !ok { | ||
return errors.New("can't find 'instances' attribute in data source") | ||
} | ||
|
||
dsNoOfInstances, err := strconv.Atoi(dsInstancesCount) | ||
if err != nil { | ||
return errors.New("failed to read number of named ports in data source") | ||
} | ||
|
||
rsInstancesCount, ok := rsAttrs["instances.#"] | ||
if !ok { | ||
return errors.New("can't find 'instances' attribute in resource") | ||
} | ||
|
||
rsNoOfInstances, err := strconv.Atoi(rsInstancesCount) | ||
if err != nil { | ||
return errors.New("failed to read number of instances in resource") | ||
} | ||
|
||
if dsNoOfInstances != rsNoOfInstances { | ||
return fmt.Errorf( | ||
"expected %d number of instances, received %d, this is most likely a bug", | ||
rsNoOfInstances, | ||
dsNoOfInstances, | ||
) | ||
} | ||
|
||
// We don't know the exact keys of the elements, so go through the whole list looking for matching ones | ||
dsInstancesValues := []string{} | ||
for k, v := range dsAttrs { | ||
if strings.HasPrefix(k, "instances") && !strings.HasSuffix(k, "#") { | ||
dsInstancesValues = append(dsInstancesValues, v) | ||
} | ||
} | ||
|
||
rsInstancesValues := []string{} | ||
for k, v := range rsAttrs { | ||
if strings.HasPrefix(k, "instances") && !strings.HasSuffix(k, "#") { | ||
rsInstancesValues = append(rsInstancesValues, v) | ||
} | ||
} | ||
|
||
sort.Strings(dsInstancesValues) | ||
sort.Strings(rsInstancesValues) | ||
|
||
if !reflect.DeepEqual(dsInstancesValues, rsInstancesValues) { | ||
return fmt.Errorf("expected %v list of instances, received %v", rsInstancesValues, dsInstancesValues) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccCheckDataSourceGoogleComputeInstanceGroupConfig = fmt.Sprintf(` | ||
resource "google_compute_instance" "test" { | ||
name = "tf-test-%s" | ||
machine_type = "n1-standard-1" | ||
zone = "us-central1-a" | ||
|
||
boot_disk { | ||
initialize_params { | ||
image = "debian-cloud/debian-8" | ||
} | ||
} | ||
|
||
network_interface { | ||
network = "default" | ||
|
||
access_config { | ||
// Ephemeral IP | ||
} | ||
} | ||
} | ||
|
||
resource "google_compute_instance_group" "test" { | ||
name = "tf-test-%s" | ||
zone = "${google_compute_instance.test.zone}" | ||
|
||
instances = [ | ||
"${google_compute_instance.test.self_link}", | ||
] | ||
} | ||
|
||
data "google_compute_instance_group" "test" { | ||
name = "${google_compute_instance_group.test.name}" | ||
zone = "${google_compute_instance_group.test.zone}" | ||
} | ||
`, acctest.RandString(10), acctest.RandString(10)) | ||
|
||
var testAccCheckDataSourceGoogleComputeInstanceGroupConfigWithNamedPort = fmt.Sprintf(` | ||
resource "google_compute_instance" "test" { | ||
name = "tf-test-%s" | ||
machine_type = "n1-standard-1" | ||
zone = "us-central1-a" | ||
|
||
boot_disk { | ||
initialize_params { | ||
image = "debian-cloud/debian-8" | ||
} | ||
} | ||
|
||
network_interface { | ||
network = "default" | ||
|
||
access_config { | ||
// Ephemeral IP | ||
} | ||
} | ||
} | ||
|
||
resource "google_compute_instance_group" "test" { | ||
name = "tf-test-%s" | ||
zone = "${google_compute_instance.test.zone}" | ||
|
||
named_port { | ||
name = "http" | ||
port = "8080" | ||
} | ||
|
||
named_port { | ||
name = "https" | ||
port = "8443" | ||
} | ||
|
||
instances = [ | ||
"${google_compute_instance.test.self_link}", | ||
] | ||
} | ||
|
||
data "google_compute_instance_group" "test" { | ||
name = "${google_compute_instance_group.test.name}" | ||
zone = "${google_compute_instance_group.test.zone}" | ||
} | ||
`, acctest.RandString(10), acctest.RandString(10)) |
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
49 changes: 49 additions & 0 deletions
49
website/docs/d/google_compute_instance_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,49 @@ | ||
--- | ||
layout: "google" | ||
page_title: "Google: google_compute_instance_group" | ||
sidebar_current: "docs-google-datasource-compute-instance-group" | ||
description: |- | ||
Get a Compute Instance Group within GCE. | ||
--- | ||
|
||
# google\_compute\_instance\_group | ||
|
||
Get a Compute Instance Group within GCE. | ||
For more information, see [the official documentation](https://cloud.google.com/compute/docs/instance-groups/#unmanaged_instance_groups) | ||
and [API](https://cloud.google.com/compute/docs/reference/latest/instanceGroups) | ||
|
||
``` | ||
data "google_compute_instance_group" "all" { | ||
name = "instance-group-name" | ||
zone = "us-central1-a" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the instance group. | ||
|
||
* `zone` - (Required) The zone of the instance group. | ||
|
||
- - - | ||
|
||
* `project` - (Optional) The project in which the resource belongs. If it | ||
is not provided, the provider project is used. | ||
|
||
## Attributes Reference | ||
|
||
The following arguments are exported: | ||
|
||
* `description` - Textual description of the instance group. | ||
|
||
* `instances` - List of instances in the group. | ||
|
||
* `named_port` - List of named ports in the group. | ||
|
||
* `network` - The URL of the network the instance group is in. | ||
|
||
* `self_link` - The URI of the resource. | ||
|
||
* `size` - The number of instances in the group. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the parent is Computed, it probably doesn't make a difference but in the context of a data source, this should be
Computed: true
notRequired: true