Skip to content

Commit

Permalink
Changing name field on google_compute_disk for TestAccComputeInstance…
Browse files Browse the repository at this point in the history
…Template_sourceSnapshotEncryptionKey to include randomly generated string (#7392)

* Changed name within google_compute_disk  on line 3162 to inclue and randomly generated string

* Cleaned up arguments being passed in to be more inline with other tests

* Updated based on addtional PR comments

* Fixed swapping of values within new context
  • Loading branch information
NA2047 authored Mar 14, 2023
1 parent 6595653 commit eb6d15f
Showing 1 changed file with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1242,16 +1242,21 @@ func TestAccComputeInstanceTemplate_sourceSnapshotEncryptionKey(t *testing.T) {

var instanceTemplate compute.InstanceTemplate
kmsKey := BootstrapKMSKeyInLocation(t, "us-central1")
kmsKeyName := GetResourceNameFromSelfLink(kmsKey.CryptoKey.Name)
kmsRingName := GetResourceNameFromSelfLink(kmsKey.KeyRing.Name)

context := map[string]interface{}{
"kms_ring_name": GetResourceNameFromSelfLink(kmsKey.KeyRing.Name),
"kms_key_name": GetResourceNameFromSelfLink(kmsKey.CryptoKey.Name),
"random_suffix": RandString(t, 10),
}


VcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: TestAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_sourceSnapshotEncryptionKey(kmsRingName, kmsKeyName, RandString(t, 10)),
Config: testAccComputeInstanceTemplate_sourceSnapshotEncryptionKey(context),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.template", &instanceTemplate),
Expand All @@ -1272,16 +1277,20 @@ func TestAccComputeInstanceTemplate_sourceImageEncryptionKey(t *testing.T) {

var instanceTemplate compute.InstanceTemplate
kmsKey := BootstrapKMSKeyInLocation(t, "us-central1")
kmsKeyName := GetResourceNameFromSelfLink(kmsKey.CryptoKey.Name)
kmsRingName := GetResourceNameFromSelfLink(kmsKey.KeyRing.Name)

context := map[string]interface{}{
"kms_ring_name": GetResourceNameFromSelfLink(kmsKey.KeyRing.Name),
"kms_key_name": GetResourceNameFromSelfLink(kmsKey.CryptoKey.Name),
"random_suffix": RandString(t, 10),
}

VcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: TestAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_sourceImageEncryptionKey(kmsRingName, kmsKeyName, RandString(t, 10)),
Config: testAccComputeInstanceTemplate_sourceImageEncryptionKey(context),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.template", &instanceTemplate),
Expand Down Expand Up @@ -3221,20 +3230,20 @@ resource "google_compute_instance_template" "foobar" {
`, suffix)
}

func testAccComputeInstanceTemplate_sourceSnapshotEncryptionKey(kmsRingName, kmsKeyName, suffix string) string {
return fmt.Sprintf(`
func testAccComputeInstanceTemplate_sourceSnapshotEncryptionKey(context map[string]interface{}) string {
return Nprintf(`
data "google_kms_key_ring" "ring" {
name = "%s"
name = "%{kms_ring_name}"
location = "us-central1"
}

data "google_kms_crypto_key" "key" {
name = "%s"
name = "%{kms_key_name}"
key_ring = data.google_kms_key_ring.ring.id
}

resource "google_service_account" "test" {
account_id = "test-sa-%s"
account_id = "tf-test-sa-%{random_suffix}"
display_name = "KMS Ops Account"
}

Expand All @@ -3250,7 +3259,7 @@ data "google_compute_image" "debian" {
}

resource "google_compute_disk" "persistent" {
name = "debian-disk"
name = "tf-test-debian-disk-%{random_suffix}"
image = data.google_compute_image.debian.self_link
size = 10
type = "pd-ssd"
Expand All @@ -3268,7 +3277,7 @@ resource "google_compute_snapshot" "snapshot" {
}

resource "google_compute_instance_template" "template" {
name = "tf-test-instance-template-%s"
name = "tf-test-instance-template-%{random_suffix}"
machine_type = "e2-medium"

disk {
Expand All @@ -3285,23 +3294,23 @@ resource "google_compute_instance_template" "template" {
network = "default"
}
}
`, kmsRingName, kmsKeyName, suffix, suffix)
`, context)
}

func testAccComputeInstanceTemplate_sourceImageEncryptionKey(kmsRingName, kmsKeyName, suffix string) string {
return fmt.Sprintf(`
func testAccComputeInstanceTemplate_sourceImageEncryptionKey(context map[string]interface{}) string {
return Nprintf(`
data "google_kms_key_ring" "ring" {
name = "%s"
name = "%{kms_ring_name}"
location = "us-central1"
}

data "google_kms_crypto_key" "key" {
name = "%s"
name = "%{kms_key_name}"
key_ring = data.google_kms_key_ring.ring.id
}

resource "google_service_account" "test" {
account_id = "tf-test-sa-%s"
account_id = "tf-test-sa-%{random_suffix}"
display_name = "KMS Ops Account"
}

Expand All @@ -3327,7 +3336,7 @@ resource "google_compute_image" "image" {


resource "google_compute_instance_template" "template" {
name = "tf-test-instance-template-%s"
name = "tf-test-instance-template-%{random_suffix}"
machine_type = "e2-medium"

disk {
Expand All @@ -3344,5 +3353,5 @@ resource "google_compute_instance_template" "template" {
network = "default"
}
}
`, kmsRingName, kmsKeyName, suffix, suffix)
`, context)
}

0 comments on commit eb6d15f

Please sign in to comment.