terraform-provider-google/google/data_source_google_kms_crypto_key_test.go
The Magician 30fe927df6 Data Sources for KMS Key Ring and Key (#2891)
<!-- This change is generated by MagicModules. -->
/cc @kierachell
2019-01-17 16:12:22 -08:00

44 lines
1.2 KiB
Go

package google
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceGoogleKmsCryptoKey_basic(t *testing.T) {
kms := BootstrapKMSKey(t)
// Name in the KMS client is in the format projects/<project>/locations/<location>/keyRings/<keyRingName>/cryptoKeys/<keyId>
keyParts := strings.Split(kms.CryptoKey.Name, "/")
cryptoKeyId := keyParts[len(keyParts)-1]
fmt.Println(testAccDataSourceGoogleKmsCryptoKey_basic(kms.KeyRing.Name, cryptoKeyId))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleKmsCryptoKey_basic(kms.KeyRing.Name, cryptoKeyId),
Check: resource.TestMatchResourceAttr("data.google_kms_crypto_key.kms_crypto_key", "self_link", regexp.MustCompile(kms.CryptoKey.Name)),
},
},
})
}
/*
This test should run in its own project, because KMS key rings and crypto keys are not deletable
*/
func testAccDataSourceGoogleKmsCryptoKey_basic(keyRingName, cryptoKeyName string) string {
return fmt.Sprintf(`
data "google_kms_crypto_key" "kms_crypto_key" {
key_ring = "%s"
name = "%s"
}
`, keyRingName, cryptoKeyName)
}