Skip to content

Commit

Permalink
Cleaned up arguments being passed in to be more inline with other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NA2047 committed Mar 6, 2023
1 parent 2961a73 commit e669d6e
Showing 1 changed file with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1205,16 +1205,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.CryptoKey.Name),
"kms_key_name": GetResourceNameFromSelfLink(kmsKey.KeyRing.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 @@ -1235,16 +1240,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.CryptoKey.Name),
"kms_key_name": GetResourceNameFromSelfLink(kmsKey.KeyRing.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 @@ -3127,20 +3136,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 = "test-sa-%{random_suffix}"
display_name = "KMS Ops Account"
}

Expand All @@ -3156,7 +3165,7 @@ data "google_compute_image" "debian" {
}

resource "google_compute_disk" "persistent" {
name = "tf-test-debian-disk-%{suffix}"
name = "tf-test-debian-disk-%{random_suffix}"
image = data.google_compute_image.debian.self_link
size = 10
type = "pd-ssd"
Expand All @@ -3174,7 +3183,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 @@ -3191,23 +3200,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 @@ -3233,7 +3242,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 @@ -3250,5 +3259,5 @@ resource "google_compute_instance_template" "template" {
network = "default"
}
}
`, kmsRingName, kmsKeyName, suffix, suffix)
`, context)
}

0 comments on commit e669d6e

Please sign in to comment.