Skip to content

Commit

Permalink
feat: added new field vm_tags to the workstation config (#11015) (#7644)
Browse files Browse the repository at this point in the history
[upstream:19fa1816cf373f84daa252027cf7e10855f37b14]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 27, 2024
1 parent cc82e88 commit e5654d8
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .changelog/11015.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:REPLACEME
Added new field vm_tags to the workstation config

```
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ func TestAccWorkstationsWorkstationConfigIamPolicyGenerated(t *testing.T) {

func testAccWorkstationsWorkstationConfigIamMember_basicGenerated(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = ""
name = ""
org_id = ""
}
resource "google_tags_tag_key" "tag_key1" {
provider = "google-beta"
parent = "organizations/"
short_name = "tf_test_tag_key1%{random_suffix}"
}
resource "google_tags_tag_value" "tag_value1" {
provider = "google-beta"
parent = "tagKeys/${google_tags_tag_key.tag_key1.name}"
short_name = "tf_test_tag_value1%{random_suffix}"
}
resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
Expand Down Expand Up @@ -179,6 +197,9 @@ resource "google_workstations_workstation_config" "default" {
boot_disk_size_gb = 35
disable_public_ip_addresses = true
disable_ssh = false
vm_tags = {
"tagKeys/${google_tags_tag_key.tag_key1.short_name}" = "tagValues/${google_tags_tag_value.tag_value1.short_name}"
}
}
}
}
Expand All @@ -197,6 +218,24 @@ resource "google_workstations_workstation_config_iam_member" "foo" {

func testAccWorkstationsWorkstationConfigIamPolicy_basicGenerated(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = ""
name = ""
org_id = ""
}
resource "google_tags_tag_key" "tag_key1" {
provider = "google-beta"
parent = "organizations/"
short_name = "tf_test_tag_key1%{random_suffix}"
}
resource "google_tags_tag_value" "tag_value1" {
provider = "google-beta"
parent = "tagKeys/${google_tags_tag_key.tag_key1.name}"
short_name = "tf_test_tag_value1%{random_suffix}"
}
resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
Expand Down Expand Up @@ -251,6 +290,9 @@ resource "google_workstations_workstation_config" "default" {
boot_disk_size_gb = 35
disable_public_ip_addresses = true
disable_ssh = false
vm_tags = {
"tagKeys/${google_tags_tag_key.tag_key1.short_name}" = "tagValues/${google_tags_tag_value.tag_value1.short_name}"
}
}
}
}
Expand Down Expand Up @@ -287,6 +329,24 @@ data "google_workstations_workstation_config_iam_policy" "foo" {

func testAccWorkstationsWorkstationConfigIamPolicy_emptyBinding(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = ""
name = ""
org_id = ""
}
resource "google_tags_tag_key" "tag_key1" {
provider = "google-beta"
parent = "organizations/"
short_name = "tf_test_tag_key1%{random_suffix}"
}
resource "google_tags_tag_value" "tag_value1" {
provider = "google-beta"
parent = "tagKeys/${google_tags_tag_key.tag_key1.name}"
short_name = "tf_test_tag_value1%{random_suffix}"
}
resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
Expand Down Expand Up @@ -341,6 +401,9 @@ resource "google_workstations_workstation_config" "default" {
boot_disk_size_gb = 35
disable_public_ip_addresses = true
disable_ssh = false
vm_tags = {
"tagKeys/${google_tags_tag_key.tag_key1.short_name}" = "tagValues/${google_tags_tag_value.tag_value1.short_name}"
}
}
}
}
Expand All @@ -362,6 +425,24 @@ resource "google_workstations_workstation_config_iam_policy" "foo" {

func testAccWorkstationsWorkstationConfigIamBinding_basicGenerated(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = ""
name = ""
org_id = ""
}
resource "google_tags_tag_key" "tag_key1" {
provider = "google-beta"
parent = "organizations/"
short_name = "tf_test_tag_key1%{random_suffix}"
}
resource "google_tags_tag_value" "tag_value1" {
provider = "google-beta"
parent = "tagKeys/${google_tags_tag_key.tag_key1.name}"
short_name = "tf_test_tag_value1%{random_suffix}"
}
resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
Expand Down Expand Up @@ -416,6 +497,9 @@ resource "google_workstations_workstation_config" "default" {
boot_disk_size_gb = 35
disable_public_ip_addresses = true
disable_ssh = false
vm_tags = {
"tagKeys/${google_tags_tag_key.tag_key1.short_name}" = "tagValues/${google_tags_tag_value.tag_value1.short_name}"
}
}
}
}
Expand All @@ -434,6 +518,24 @@ resource "google_workstations_workstation_config_iam_binding" "foo" {

func testAccWorkstationsWorkstationConfigIamBinding_updateGenerated(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = ""
name = ""
org_id = ""
}
resource "google_tags_tag_key" "tag_key1" {
provider = "google-beta"
parent = "organizations/"
short_name = "tf_test_tag_key1%{random_suffix}"
}
resource "google_tags_tag_value" "tag_value1" {
provider = "google-beta"
parent = "tagKeys/${google_tags_tag_key.tag_key1.name}"
short_name = "tf_test_tag_value1%{random_suffix}"
}
resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
Expand Down Expand Up @@ -488,6 +590,9 @@ resource "google_workstations_workstation_config" "default" {
boot_disk_size_gb = 35
disable_public_ip_addresses = true
disable_ssh = false
vm_tags = {
"tagKeys/${google_tags_tag_key.tag_key1.short_name}" = "tagValues/${google_tags_tag_value.tag_value1.short_name}"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ See https://cloud.google.com/workstations/docs/reference/rest/v1beta/projects.lo
Type: schema.TypeString,
},
},
"vm_tags": {
Type: schema.TypeMap,
Optional: true,
Description: `Resource manager tags to be bound to the VM instances backing the Workstations.
Tag keys and values have the same definition as
https://cloud.google.com/resource-manager/docs/tags/tags-overview
Keys must be in the format 'tagKeys/{tag_key_id}', and
values are in the format 'tagValues/456'.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
Expand Down Expand Up @@ -1032,7 +1042,8 @@ func resourceWorkstationsWorkstationConfigUpdate(d *schema.ResourceData, meta in
"host.gceInstance.confidentialInstanceConfig.enableConfidentialCompute",
"host.gceInstance.accelerators",
"host.gceInstance.boostConfigs",
"host.gceInstance.disableSsh")
"host.gceInstance.disableSsh",
"host.gceInstance.vmTags")
}

if d.HasChange("persistent_directories") {
Expand Down Expand Up @@ -1296,6 +1307,8 @@ func flattenWorkstationsWorkstationConfigHostGceInstance(v interface{}, d *schem
flattenWorkstationsWorkstationConfigHostGceInstanceAccelerators(original["accelerators"], d, config)
transformed["boost_configs"] =
flattenWorkstationsWorkstationConfigHostGceInstanceBoostConfigs(original["boostConfigs"], d, config)
transformed["vm_tags"] =
flattenWorkstationsWorkstationConfigHostGceInstanceVmTags(original["vmTags"], d, config)
return []interface{}{transformed}
}
func flattenWorkstationsWorkstationConfigHostGceInstanceMachineType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -1553,6 +1566,10 @@ func flattenWorkstationsWorkstationConfigHostGceInstanceBoostConfigsAccelerators
return v // let terraform core handle it otherwise
}

func flattenWorkstationsWorkstationConfigHostGceInstanceVmTags(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenWorkstationsWorkstationConfigPersistentDirectories(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -2030,6 +2047,13 @@ func expandWorkstationsWorkstationConfigHostGceInstance(v interface{}, d tpgreso
transformed["boostConfigs"] = transformedBoostConfigs
}

transformedVmTags, err := expandWorkstationsWorkstationConfigHostGceInstanceVmTags(original["vm_tags"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedVmTags); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["vmTags"] = transformedVmTags
}

return transformed, nil
}

Expand Down Expand Up @@ -2288,6 +2312,17 @@ func expandWorkstationsWorkstationConfigHostGceInstanceBoostConfigsAcceleratorsC
return v, nil
}

func expandWorkstationsWorkstationConfigHostGceInstanceVmTags(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}

func expandWorkstationsWorkstationConfigPersistentDirectories(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ func TestAccWorkstationsWorkstationConfig_workstationConfigBasicExample(t *testi

func testAccWorkstationsWorkstationConfig_workstationConfigBasicExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = ""
name = ""
org_id = ""
}
resource "google_tags_tag_key" "tag_key1" {
provider = "google-beta"
parent = "organizations/"
short_name = "tf_test_tag_key1%{random_suffix}"
}
resource "google_tags_tag_value" "tag_value1" {
provider = "google-beta"
parent = "tagKeys/${google_tags_tag_key.tag_key1.name}"
short_name = "tf_test_tag_value1%{random_suffix}"
}
resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
Expand Down Expand Up @@ -111,6 +129,9 @@ resource "google_workstations_workstation_config" "default" {
boot_disk_size_gb = 35
disable_public_ip_addresses = true
disable_ssh = false
vm_tags = {
"tagKeys/${google_tags_tag_key.tag_key1.short_name}" = "tagValues/${google_tags_tag_value.tag_value1.short_name}"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1292,3 +1292,89 @@ resource "google_workstations_workstation_config" "default" {
}
`, context)
}

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

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

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckWorkstationsWorkstationConfigDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccWorkstationsWorkstationConfig_vmTags(context),
},
{
ResourceName: "google_workstations_workstation_cluster.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag"},
},
},
})
}

func testAccWorkstationsWorkstationConfig_vmTags(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_project" "project" {
provider = "google-beta"
}
resource "google_tags_tag_key" "tag_key1" {
provider = google-beta
parent = "projects/${data.google_project.project.number}"
short_name = "tf_test_tag_key1%{random_suffix}"
}
resource "google_tags_tag_value" "tag_value1" {
provider = google-beta
parent = "tagKeys/${google_tags_tag_key.tag_key1.name}"
short_name = "tf_test_tag_value1%{random_suffix}"
}
resource "google_compute_network" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "default" {
provider = google-beta
name = "tf-test-workstation-cluster%{random_suffix}"
ip_cidr_range = "10.0.0.0/24"
region = "us-central1"
network = google_compute_network.default.name
}
resource "google_workstations_workstation_cluster" "default" {
provider = google-beta
workstation_cluster_id = "tf-test-workstation-cluster%{random_suffix}"
network = google_compute_network.default.id
subnetwork = google_compute_subnetwork.default.id
location = "us-central1"
}
resource "google_workstations_workstation_config" "default" {
provider = google-beta
workstation_config_id = "tf-test-workstation-config%{random_suffix}"
workstation_cluster_id = google_workstations_workstation_cluster.default.workstation_cluster_id
location = "us-central1"
host {
gce_instance {
machine_type = "e2-standard-4"
boot_disk_size_gb = 35
disable_public_ip_addresses = true
vm_tags = {
"tagKeys/${google_tags_tag_key.tag_key1.name}" = "tagValues/${google_tags_tag_value.tag_value1.name}"
}
}
}
}
`, context)
}
Loading

0 comments on commit e5654d8

Please sign in to comment.