Skip to content

Commit

Permalink
Add tags support to (regional) instance group manager (#10217)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjotrekk authored Apr 11, 2024
1 parent 0349bc1 commit c8ed4ea
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,26 @@ func ResourceComputeInstanceGroupManager() *schema.Resource {
},
},
},
<% unless version == "ga" -%>
"params": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Description: `Input only additional params for instance group manager creation.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_manager_tags": {
Type: schema.TypeMap,
Optional: true,
// This field is intentionally not updatable. The API overrides all existing tags on the field when updated.
ForceNew: true,
Description: `Resource manager tags to bind to the managed instance group. The tags are key-value pairs. Keys must be in the format tagKeys/123 and values in the format tagValues/456.`,
},
},
},
},
<% end -%>
"wait_for_instances": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -594,6 +614,9 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})),
AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})),
StatefulPolicy: expandStatefulPolicy(d),
<% unless version == "ga" -%>
Params: expandInstanceGroupManagerParams(d),
<% end -%>

// Force send TargetSize to allow a value of 0.
ForceSendFields: []string{"TargetSize"},
Expand Down Expand Up @@ -1258,6 +1281,18 @@ func expandUpdatePolicy(configured []interface{}) *compute.InstanceGroupManagerU
return updatePolicy
}

<% unless version == "ga" -%>
func expandInstanceGroupManagerParams(d *schema.ResourceData) *compute.InstanceGroupManagerParams {
params := &compute.InstanceGroupManagerParams{}

if _, ok := d.GetOk("params.0.resource_manager_tags"); ok {
params.ResourceManagerTags = tpgresource.ExpandStringMap(d, "params.0.resource_manager_tags")
}

return params
}
<% end -%>

func flattenAutoHealingPolicies(autoHealingPolicies []*compute.InstanceGroupManagerAutoHealingPolicy) []map[string]interface{} {
autoHealingPoliciesSchema := make([]map[string]interface{}, 0, len(autoHealingPolicies))
for _, autoHealingPolicy := range autoHealingPolicies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
<% unless version == "ga" -%>
"github.com/hashicorp/terraform-provider-google/google/envvar"
<% end -%>
)

func TestAccInstanceGroupManager_basic(t *testing.T) {
Expand Down Expand Up @@ -424,6 +427,34 @@ func TestAccInstanceGroupManager_waitForStatus(t *testing.T) {
})
}

<% unless version == "ga" -%>
func TestAccInstanceGroupManager_resourceManagerTags(t *testing.T) {
t.Parallel()

tag_name := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
template_name := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
igm_name := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
project_id := envvar.GetTestProjectFromEnv()

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckInstanceGroupManagerDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccInstanceGroupManager_resourceManagerTags(template_name, tag_name, igm_name, project_id),
},
{
ResourceName: "google_compute_instance_group_manager.igm-tags",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"status", "params"},
},
},
})
}
<% end -%>

func testAccCheckInstanceGroupManagerDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)
Expand Down Expand Up @@ -1816,3 +1847,59 @@ resource "google_compute_per_instance_config" "per-instance" {
}
`, template, target, igm, perInstanceConfig)
}

<% unless version == "ga" -%>
func testAccInstanceGroupManager_resourceManagerTags(template_name, tag_name, igm_name, project_id string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_instance_template" "igm-tags" {
name = "%s"
description = "Terraform test instance template."
machine_type = "e2-medium"

disk {
source_image = data.google_compute_image.my_image.self_link
}

network_interface {
network = "default"
}
}

resource "google_tags_tag_key" "igm-key" {
description = "Terraform test tag key."
parent = "projects/%s"
short_name = "%s"
}

resource "google_tags_tag_value" "igm-value" {
description = "Terraform test tag value."
parent = "tagKeys/${google_tags_tag_key.igm-key.name}"
short_name = "%s"
}

resource "google_compute_instance_group_manager" "igm-tags" {
description = "Terraform test instance group manager."
name = "%s"
base_instance_name = "tf-igm-tags-test"
zone = "us-central1-a"
target_size = 0

version {
name = "prod"
instance_template = google_compute_instance_template.igm-tags.self_link
}

params {
resource_manager_tags = {
"tagKeys/${google_tags_tag_key.igm-key.name}" = "tagValues/${google_tags_tag_value.igm-value.name}"
}
}
}
`, template_name, project_id, tag_name, tag_name, igm_name)
}
<% end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,26 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
},
},
},
<% unless version == "ga" -%>
"params": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Description: `Input only additional params for instance group manager creation.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_manager_tags": {
Type: schema.TypeMap,
Optional: true,
// This field is intentionally not updatable. The API overrides all existing tags on the field when updated.
ForceNew: true,
Description: `Resource manager tags to bind to the managed instance group. The tags are key-value pairs. Keys must be in the format tagKeys/123 and values in the format tagValues/456.`,
},
},
},
},
<% end -%>
},
UseJSONNumber: true,
}
Expand Down Expand Up @@ -568,6 +588,9 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})),
DistributionPolicy: expandDistributionPolicy(d),
StatefulPolicy: expandStatefulPolicy(d),
<% unless version == "ga" -%>
Params: expandInstanceGroupManagerParams(d),
<% end -%>
// Force send TargetSize to allow size of 0.
ForceSendFields: []string{"TargetSize"},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
<% unless version == "ga" -%>
"github.com/hashicorp/terraform-provider-google/google/envvar"
<% end -%>
)

func TestAccRegionInstanceGroupManager_basic(t *testing.T) {
Expand Down Expand Up @@ -393,6 +396,34 @@ func TestAccRegionInstanceGroupManager_APISideListRecordering(t *testing.T) {
})
}

<% unless version == "ga" -%>
func TestAccRegionInstanceGroupManager_resourceManagerTags(t *testing.T) {
t.Parallel()

tag_name := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
template_name := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
igm_name := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
project_id := envvar.GetTestProjectFromEnv()

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckInstanceGroupManagerDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccRegionInstanceGroupManager_resourceManagerTags(template_name, tag_name, igm_name, project_id),
},
{
ResourceName: "google_compute_region_instance_group_manager.rigm-tags",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"status", "params"},
},
},
})
}
<% end -%>

func testAccCheckRegionInstanceGroupManagerDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)
Expand Down Expand Up @@ -1682,4 +1713,60 @@ resource "google_compute_region_instance_group_manager" "igm-basic" {

}
`, context)
}
}

<% unless version == "ga" -%>
func testAccRegionInstanceGroupManager_resourceManagerTags(template_name, tag_name, igm_name, project_id string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_instance_template" "rigm-tags" {
name = "%s"
description = "Terraform test instance template."
machine_type = "e2-medium"

disk {
source_image = data.google_compute_image.my_image.self_link
}

network_interface {
network = "default"
}
}

resource "google_tags_tag_key" "rigm-key" {
description = "Terraform test tag key."
parent = "projects/%s"
short_name = "%s"
}

resource "google_tags_tag_value" "rigm-value" {
description = "Terraform test tag value."
parent = "tagKeys/${google_tags_tag_key.rigm-key.name}"
short_name = "%s"
}

resource "google_compute_region_instance_group_manager" "rigm-tags" {
description = "Terraform test instance group manager."
name = "%s"
base_instance_name = "tf-rigm-tags-test"
region = "us-central1"
target_size = 0

version {
name = "prod"
instance_template = google_compute_instance_template.rigm-tags.self_link
}

params {
resource_manager_tags = {
"tagKeys/${google_tags_tag_key.rigm-key.name}" = "tagValues/${google_tags_tag_value.rigm-value.name}"
}
}
}
`, template_name, project_id, tag_name, tag_name, igm_name)
}
<% end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ group. You can specify only one value. Structure is [documented below](#nested_a

* `stateful_external_ip` - (Optional) External network IPs assigned to the instances that will be preserved on instance delete, update, etc. This map is keyed with the network interface name. Structure is [documented below](#nested_stateful_external_ip).

* `update_policy` - (Optional) The update policy for this managed instance group. Structure is [documented below](#nested_update_policy). For more information, see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/updating-managed-instance-groups) and [API](https://cloud.google.com/compute/docs/reference/rest/v1/instanceGroupManagers/patch)
* `update_policy` - (Optional) The update policy for this managed instance group. Structure is [documented below](#nested_update_policy). For more information, see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/updating-managed-instance-groups) and [API](https://cloud.google.com/compute/docs/reference/rest/v1/instanceGroupManagers/patch).

* `params` - (Optional [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Input only additional params for instance group manager creation. Structure is [documented below](#nested_params). For more information, see [API](https://cloud.google.com/compute/docs/reference/rest/beta/instanceGroupManagers/insert).

- - -

Expand Down Expand Up @@ -306,6 +308,18 @@ one of which has a `target_size.percent` of `60` will create 2 instances of that

* `delete_rule` - (Optional), A value that prescribes what should happen to the external ip when the VM instance is deleted. The available options are `NEVER` and `ON_PERMANENT_INSTANCE_DELETION`. `NEVER` - detach the ip when the VM is deleted, but do not delete the ip. `ON_PERMANENT_INSTANCE_DELETION` will delete the external ip when the VM is permanently deleted from the instance group.

<a name="nested_params"></a>The `params` block supports:

```hcl
params{
resource_manager_tags = {
"tagKeys/123": "tagValues/123"
}
}
```

* `resource_manager_tags` - (Optional) Resource manager tags to bind to the managed instance group. The tags are key-value pairs. Keys must be in the format tagKeys/123 and values in the format tagValues/456. For more information, see [Manage tags for resources](https://cloud.google.com/compute/docs/tag-resources)

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ group. You can specify one or more values. For more information, see the [offici

* `stateful_external_ip` - (Optional) External network IPs assigned to the instances that will be preserved on instance delete, update, etc. This map is keyed with the network interface name. Structure is [documented below](#nested_stateful_external_ip).

* `params` - (Optional [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Input only additional params for instance group manager creation. Structure is [documented below](#nested_params). For more information, see [API](https://cloud.google.com/compute/docs/reference/rest/beta/instanceGroupManagers/insert).

- - -

<a name="nested_update_policy"></a>The `update_policy` block supports:
Expand Down Expand Up @@ -317,6 +319,18 @@ one of which has a `target_size.percent` of `60` will create 2 instances of that

* `delete_rule` - (Optional), A value that prescribes what should happen to the external ip when the VM instance is deleted. The available options are `NEVER` and `ON_PERMANENT_INSTANCE_DELETION`. `NEVER` - detach the ip when the VM is deleted, but do not delete the ip. `ON_PERMANENT_INSTANCE_DELETION` will delete the external ip when the VM is permanently deleted from the instance group.

<a name="nested_params"></a>The `params` block supports:

```hcl
params{
resource_manager_tags = {
"tagKeys/123": "tagValues/123"
}
}
```

* `resource_manager_tags` - (Optional) Resource manager tags to bind to the managed instance group. The tags are key-value pairs. Keys must be in the format tagKeys/123 and values in the format tagValues/456. For more information, see [Manage tags for resources](https://cloud.google.com/compute/docs/tag-resources)

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down

0 comments on commit c8ed4ea

Please sign in to comment.