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

Added data source for compute region instance group manager #7061

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package google

import (
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGoogleComputeRegionInstanceGroupManager() *schema.Resource {

dsSchema := datasourceSchemaFromResourceSchema(resourceComputeRegionInstanceGroupManager().Schema)
addRequiredFieldsToSchema(dsSchema, "name")
addOptionalFieldsToSchema(dsSchema, "project", "region")
dsSchema["wait_for_instances"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
}

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

func dataSourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if name, ok := d.GetOk("name"); ok {
region, err := getRegion(d, config)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("projects/%s/regions/%s/instanceGroupManagers/%s", project, region, name.(string)))
} else {
return errors.New("Must provide either `resource/name`")
}

err := resourceComputeRegionInstanceGroupManagerRead(d, meta)

if err != nil {
return err
}
if d.Id() == "" {
return errors.New("Instance Manager Group not found")
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package google

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

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

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRegionInstanceGroupManagerDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleComputeRegionInstanceGroupManager_basic(context),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState("data.google_compute_region_instance_group_manager.foo", "google_compute_region_instance_group_manager.appserver"),
),
},
},
})
}

func testAccDataSourceGoogleComputeRegionInstanceGroupManager_basic(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_instance_template" "appserver" {
name = "template%{random_suffix}"
machine_type = "e2-medium"

disk {
source_image = "debian-cloud/debian-11"
auto_delete = true
disk_size_gb = 100
boot = true
}

network_interface {
network = "default"
}

metadata = {
foo = "bar"
}

can_ip_forward = true
}

resource "google_compute_region_instance_group_manager" "appserver" {
name = "appserver-igm%{random_suffix}"

base_instance_name = "app"
region = "us-central1"
distribution_policy_zones = ["us-central1-a"]

version {
instance_template = google_compute_instance_template.appserver.id
}


named_port {
name = "custom"
port = 8888
}
wait_for_instances = false
}

data "google_compute_region_instance_group_manager" "foo" {
name = google_compute_region_instance_group_manager.appserver.name
}

`, context)

}
1 change: 1 addition & 0 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func Provider() *schema.Provider {
"google_compute_regions": dataSourceGoogleComputeRegions(),
"google_compute_region_network_endpoint_group": dataSourceGoogleComputeRegionNetworkEndpointGroup(),
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
"google_compute_region_instance_group_manager": dataSourceGoogleComputeRegionInstanceGroupManager(),
"google_compute_region_ssl_certificate": dataSourceGoogleRegionComputeSslCertificate(),
"google_compute_resource_policy": dataSourceGoogleComputeResourcePolicy(),
"google_compute_router": dataSourceGoogleComputeRouter(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
subcategory: "Compute Engine"
page_title: "Google: google_compute_region_instance_group_manager"
description: |-
Get a Compute Region Instance Group within GCE.
---

# google\_compute\_region\_instance\_group\_manager

Get a Compute Region Instance Group Manager within GCE.
For more information, see [the official documentation](https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups)
and [API](https://cloud.google.com/compute/docs/reference/rest/v1/regionInstanceGroupManagers)

## Example Usage

```hcl
data "google_compute_region_instance_group_manager" "igm1" {
name = "my-igm"
}

```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the instance group.

* `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.

* `Region` - (Optional) The region where the managed instance group resides. If not provided, the provider region is used.

---

## Attributes Reference

See [google_compute_region_instance_group_manager](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_region_instance_group_manager) resource for details of all the available attributes.