forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add datasource for google_compute_region_instance_group_manager (Goog…
…leCloudPlatform#11720) Co-authored-by: Thomas Rodgers <[email protected]>
- Loading branch information
Showing
5 changed files
with
304 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
63 changes: 63 additions & 0 deletions
63
...ty/terraform/services/compute/data_source_google_compute_region_instance_group_manager.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,63 @@ | ||
package compute | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-google/google/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" | ||
) | ||
|
||
func DataSourceGoogleComputeRegionInstanceGroupManager() *schema.Resource { | ||
|
||
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceComputeRegionInstanceGroupManager().Schema) | ||
tpgresource.AddOptionalFieldsToSchema(dsSchema, "name", "self_link", "project", "region") | ||
|
||
return &schema.Resource{ | ||
Read: dataSourceComputeRegionInstanceGroupManagerRead, | ||
Schema: dsSchema, | ||
} | ||
} | ||
|
||
func dataSourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
if selfLink, ok := d.Get("self_link").(string); ok && selfLink != "" { | ||
parsed, err := tpgresource.ParseRegionalInstanceGroupManagersFieldValue(selfLink, d, config) | ||
if err != nil { | ||
return fmt.Errorf("InstanceGroup name, region or project could not be parsed from %s: %v", selfLink, err) | ||
} | ||
if err := d.Set("name", parsed.Name); err != nil { | ||
return fmt.Errorf("Error setting name: %s", err) | ||
} | ||
if err := d.Set("region", parsed.Region); err != nil { | ||
return fmt.Errorf("Error setting region: %s", err) | ||
} | ||
if err := d.Set("project", parsed.Project); err != nil { | ||
return fmt.Errorf("Error setting project: %s", err) | ||
} | ||
d.SetId(fmt.Sprintf("projects/%s/regions/%s/instanceGroupManagers/%s", parsed.Project, parsed.Region, parsed.Name)) | ||
} else if name, ok := d.Get("name").(string); ok && name != "" { | ||
region, err := tpgresource.GetRegion(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
project, err := tpgresource.GetProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId(fmt.Sprintf("projects/%s/regions/%s/instanceGroupManagers/%s", project, region, name)) | ||
} else { | ||
return errors.New("Must provide either `self_link` or `region/name`") | ||
} | ||
|
||
err := resourceComputeRegionInstanceGroupManagerRead(d, meta) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if d.Id() == "" { | ||
return errors.New("Regional Instance Manager Group not found") | ||
} | ||
return nil | ||
} |
199 changes: 199 additions & 0 deletions
199
...rraform/services/compute/data_source_google_compute_region_instance_group_manager_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,199 @@ | ||
package compute_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google/google/acctest" | ||
"github.com/hashicorp/terraform-provider-google/google/envvar" | ||
) | ||
|
||
func TestAccDataSourceGoogleComputeRegionInstanceGroupManager(t *testing.T) { | ||
t.Parallel() | ||
|
||
regionName := "us-central1" | ||
igmName := "tf-test-igm" + acctest.RandString(t, 6) | ||
|
||
context := map[string]interface{}{ | ||
"regionName": regionName, | ||
"igmName": igmName, | ||
"baseName": "tf-test-igm-base" + acctest.RandString(t, 6), | ||
"poolName": "tf-test-pool" + acctest.RandString(t, 6), | ||
"templateName": "tf-test-templt" + acctest.RandString(t, 6), | ||
"autoHealName": "tf-test-ah-name" + acctest.RandString(t, 6), | ||
} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingSelfLink(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "project", envvar.GetTestProjectFromEnv()), | ||
resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "region", regionName), | ||
resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "name", igmName)), | ||
}, | ||
{ | ||
Config: testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingNameAndRegion(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "project", envvar.GetTestProjectFromEnv()), | ||
resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "region", regionName), | ||
resource.TestCheckResourceAttr("data.google_compute_region_instance_group_manager.data_source", "name", igmName)), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingSelfLink(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
resource "google_compute_health_check" "autohealing" { | ||
name = "%{autoHealName}" | ||
check_interval_sec = 5 | ||
timeout_sec = 5 | ||
healthy_threshold = 2 | ||
unhealthy_threshold = 10 # 50 seconds | ||
http_health_check { | ||
request_path = "/healthz" | ||
port = "8080" | ||
} | ||
} | ||
resource "google_compute_region_instance_group_manager" "appserver" { | ||
name = "%{igmName}" | ||
base_instance_name = "%{baseName}" | ||
region = "us-central1" | ||
version { | ||
instance_template = google_compute_instance_template.igm-basic.id | ||
name = "primary" | ||
} | ||
target_pools = [google_compute_target_pool.igm-basic.id] | ||
target_size = 2 | ||
named_port { | ||
name = "customhttp" | ||
port = 8888 | ||
} | ||
auto_healing_policies { | ||
health_check = google_compute_health_check.autohealing.id | ||
initial_delay_sec = 300 | ||
} | ||
} | ||
data "google_compute_region_instance_group_manager" "data_source" { | ||
self_link = google_compute_region_instance_group_manager.appserver.instance_group | ||
} | ||
resource "google_compute_target_pool" "igm-basic" { | ||
description = "Resource created for Terraform acceptance testing" | ||
name = "%{poolName}" | ||
session_affinity = "CLIENT_IP_PROTO" | ||
} | ||
data "google_compute_image" "my_image" { | ||
family = "debian-11" | ||
project = "debian-cloud" | ||
} | ||
resource "google_compute_instance_template" "igm-basic" { | ||
name = "%{templateName}" | ||
machine_type = "e2-medium" | ||
can_ip_forward = false | ||
tags = ["foo", "bar"] | ||
disk { | ||
source_image = data.google_compute_image.my_image.self_link | ||
auto_delete = true | ||
boot = true | ||
} | ||
network_interface { | ||
network = "default" | ||
} | ||
service_account { | ||
scopes = ["userinfo-email", "compute-ro", "storage-ro"] | ||
} | ||
}`, context) | ||
} | ||
|
||
func testAccDataSourceGoogleComputeRegionInstanceGroupManager_usingNameAndRegion(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
resource "google_compute_health_check" "autohealing" { | ||
name = "%{autoHealName}" | ||
check_interval_sec = 5 | ||
timeout_sec = 5 | ||
healthy_threshold = 2 | ||
unhealthy_threshold = 10 # 50 seconds | ||
http_health_check { | ||
request_path = "/healthz" | ||
port = "8080" | ||
} | ||
} | ||
resource "google_compute_region_instance_group_manager" "appserver" { | ||
name = "%{igmName}" | ||
base_instance_name = "%{baseName}" | ||
region = "us-central1" | ||
version { | ||
instance_template = google_compute_instance_template.igm-basic.id | ||
name = "primary" | ||
} | ||
target_pools = [google_compute_target_pool.igm-basic.id] | ||
target_size = 2 | ||
named_port { | ||
name = "customhttp" | ||
port = 8888 | ||
} | ||
auto_healing_policies { | ||
health_check = google_compute_health_check.autohealing.id | ||
initial_delay_sec = 300 | ||
} | ||
} | ||
data "google_compute_region_instance_group_manager" "data_source" { | ||
name = "%{igmName}" | ||
region = "us-central1" | ||
} | ||
resource "google_compute_target_pool" "igm-basic" { | ||
description = "Resource created for Terraform acceptance testing" | ||
name = "%{poolName}" | ||
session_affinity = "CLIENT_IP_PROTO" | ||
} | ||
data "google_compute_image" "my_image" { | ||
family = "debian-11" | ||
project = "debian-cloud" | ||
} | ||
resource "google_compute_instance_template" "igm-basic" { | ||
name = "%{templateName}" | ||
machine_type = "e2-medium" | ||
can_ip_forward = false | ||
tags = ["foo", "bar"] | ||
disk { | ||
source_image = data.google_compute_image.my_image.self_link | ||
auto_delete = true | ||
boot = true | ||
} | ||
network_interface { | ||
network = "default" | ||
} | ||
service_account { | ||
scopes = ["userinfo-email", "compute-ro", "storage-ro"] | ||
} | ||
}`, 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
38 changes: 38 additions & 0 deletions
38
...ty/terraform/website/docs/d/compute_region_instance_group_manager.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,38 @@ | ||
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" "rigm" { | ||
name = "my-igm" | ||
region = "us-central1" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `self_link` - (Optional) The self link of the instance group. Either `name` or `self_link` must be provided. | ||
|
||
* `name` - (Optional) The name of the instance group. Either `name` or `self_link` must be provided. | ||
|
||
* `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. |