mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
138 lines
5.0 KiB
Go
138 lines
5.0 KiB
Go
![]() |
package google
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/hashicorp/terraform/helper/acctest"
|
||
|
"github.com/hashicorp/terraform/helper/resource"
|
||
|
"github.com/hashicorp/terraform/terraform"
|
||
|
)
|
||
|
|
||
|
// Test that a service account key can be created and destroyed
|
||
|
func TestAccGoogleServiceAccountKey_basic(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
resourceName := "google_service_account_key.acceptance"
|
||
|
accountID := "a" + acctest.RandString(10)
|
||
|
displayName := "Terraform Test"
|
||
|
resource.Test(t, resource.TestCase{
|
||
|
PreCheck: func() { testAccPreCheck(t) },
|
||
|
Providers: testAccProviders,
|
||
|
Steps: []resource.TestStep{
|
||
|
resource.TestStep{
|
||
|
Config: testAccGoogleServiceAccountKey(accountID, displayName),
|
||
|
Check: resource.ComposeTestCheckFunc(
|
||
|
testAccCheckGoogleServiceAccountKeyExists(resourceName),
|
||
|
resource.TestCheckResourceAttrSet(resourceName, "public_key"),
|
||
|
resource.TestCheckResourceAttrSet(resourceName, "valid_after"),
|
||
|
resource.TestCheckResourceAttrSet(resourceName, "valid_before"),
|
||
|
resource.TestCheckResourceAttrSet(resourceName, "private_key"),
|
||
|
),
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestAccGoogleServiceAccountKey_pgp(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
resourceName := "google_service_account_key.acceptance"
|
||
|
accountID := "a" + acctest.RandString(10)
|
||
|
displayName := "Terraform Test"
|
||
|
resource.Test(t, resource.TestCase{
|
||
|
PreCheck: func() { testAccPreCheck(t) },
|
||
|
Providers: testAccProviders,
|
||
|
Steps: []resource.TestStep{
|
||
|
resource.TestStep{
|
||
|
Config: testAccGoogleServiceAccountKey_pgp(accountID, displayName, testKeyPairPubKey1),
|
||
|
Check: resource.ComposeTestCheckFunc(
|
||
|
testAccCheckGoogleServiceAccountKeyExists(resourceName),
|
||
|
resource.TestCheckResourceAttrSet(resourceName, "public_key"),
|
||
|
resource.TestCheckResourceAttrSet(resourceName, "private_key_encrypted"),
|
||
|
resource.TestCheckResourceAttrSet(resourceName, "private_key_fingerprint"),
|
||
|
),
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func testAccCheckGoogleServiceAccountKeyExists(r string) resource.TestCheckFunc {
|
||
|
return func(s *terraform.State) error {
|
||
|
|
||
|
rs, ok := s.RootModule().Resources[r]
|
||
|
if !ok {
|
||
|
return fmt.Errorf("Not found: %s", r)
|
||
|
}
|
||
|
|
||
|
if rs.Primary.ID == "" {
|
||
|
return fmt.Errorf("No ID is set")
|
||
|
}
|
||
|
config := testAccProvider.Meta().(*Config)
|
||
|
|
||
|
_, err := config.clientIAM.Projects.ServiceAccounts.Keys.Get(rs.Primary.ID).Do()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func testAccGoogleServiceAccountKey(account, name string) string {
|
||
|
return fmt.Sprintf(`
|
||
|
resource "google_service_account" "acceptance" {
|
||
|
account_id = "%s"
|
||
|
display_name = "%s"
|
||
|
}
|
||
|
|
||
|
resource "google_service_account_key" "acceptance" {
|
||
|
service_account_id = "${google_service_account.acceptance.id}"
|
||
|
public_key_type = "TYPE_X509_PEM_FILE"
|
||
|
}
|
||
|
`, account, name)
|
||
|
}
|
||
|
|
||
|
func testAccGoogleServiceAccountKey_pgp(account, name string, key string) string {
|
||
|
return fmt.Sprintf(`
|
||
|
resource "google_service_account" "acceptance" {
|
||
|
account_id = "%s"
|
||
|
display_name = "%s"
|
||
|
}
|
||
|
|
||
|
resource "google_service_account_key" "acceptance" {
|
||
|
service_account_id = "${google_service_account.acceptance.id}"
|
||
|
public_key_type = "TYPE_X509_PE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