mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-06 18:51:13 +00:00
7e82a982e4
* fix service account key data source name * switch id to name * update docs * doc format * fixes for validation and tests * last fixes for service account key data source
133 lines
3.9 KiB
Go
133 lines
3.9 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"strings"
|
|
)
|
|
|
|
func TestAccDatasourceGoogleServiceAccountKey_basic(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
resourceName := "data.google_service_account_key.acceptance"
|
|
account := acctest.RandomWithPrefix("tf-test")
|
|
serviceAccountName := fmt.Sprintf(
|
|
"projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com",
|
|
getTestProjectFromEnv(),
|
|
account,
|
|
getTestProjectFromEnv(),
|
|
)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccDatasourceGoogleServiceAccountKey(account),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleServiceAccountKeyExists(resourceName),
|
|
// Check that the 'name' starts with the service account name
|
|
resource.TestMatchResourceAttr(resourceName, "name", regexp.MustCompile(serviceAccountName)),
|
|
resource.TestCheckResourceAttrSet(resourceName, "key_algorithm"),
|
|
resource.TestCheckResourceAttrSet(resourceName, "public_key"),
|
|
),
|
|
},
|
|
{
|
|
Config: testAccDatasourceGoogleServiceAccountKey_deprecated(account),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleServiceAccountKeyExists(resourceName),
|
|
// Check that the 'name' starts with the service account name
|
|
resource.TestMatchResourceAttr(resourceName, "name", regexp.MustCompile(serviceAccountName)),
|
|
resource.TestCheckResourceAttrSet(resourceName, "key_algorithm"),
|
|
resource.TestCheckResourceAttrSet(resourceName, "public_key"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDatasourceGoogleServiceAccountKey_errors(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
account := acctest.RandomWithPrefix("tf-test")
|
|
serviceAccountName := fmt.Sprintf(
|
|
"projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com",
|
|
getTestProjectFromEnv(),
|
|
account,
|
|
getTestProjectFromEnv(),
|
|
)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccDatasourceGoogleServiceAccountKey_error(
|
|
account,
|
|
`name = "${google_service_account.acceptance.name}"`),
|
|
ExpectError: regexp.MustCompile(
|
|
fmt.Sprintf("invalid key name %q", serviceAccountName)),
|
|
},
|
|
{
|
|
Config: testAccDatasourceGoogleServiceAccountKey_error(
|
|
account,
|
|
`service_account_id = "${google_service_account.acceptance.id}"`),
|
|
ExpectError: regexp.MustCompile(
|
|
fmt.Sprintf("invalid key name %q", serviceAccountName)),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccDatasourceGoogleServiceAccountKey(account string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_service_account" "acceptance" {
|
|
account_id = "%s"
|
|
}
|
|
|
|
resource "google_service_account_key" "acceptance" {
|
|
service_account_id = "${google_service_account.acceptance.name}"
|
|
public_key_type = "TYPE_X509_PEM_FILE"
|
|
}
|
|
|
|
data "google_service_account_key" "acceptance" {
|
|
name = "${google_service_account_key.acceptance.name}"
|
|
}`, account)
|
|
}
|
|
|
|
func testAccDatasourceGoogleServiceAccountKey_deprecated(account string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_service_account" "acceptance" {
|
|
account_id = "%s"
|
|
}
|
|
|
|
resource "google_service_account_key" "acceptance" {
|
|
service_account_id = "${google_service_account.acceptance.name}"
|
|
public_key_type = "TYPE_X509_PEM_FILE"
|
|
}
|
|
|
|
data "google_service_account_key" "acceptance" {
|
|
service_account_id = "${google_service_account_key.acceptance.name}"
|
|
}`, account)
|
|
}
|
|
|
|
func testAccDatasourceGoogleServiceAccountKey_error(account string, incorrectDataFields ...string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_service_account" "acceptance" {
|
|
account_id = "%s"
|
|
}
|
|
|
|
resource "google_service_account_key" "acceptance" {
|
|
service_account_id = "${google_service_account.acceptance.name}"
|
|
public_key_type = "TYPE_X509_PEM_FILE"
|
|
}
|
|
|
|
data "google_service_account_key" "acceptance" {
|
|
%s
|
|
}`, account, strings.Join(incorrectDataFields, "\n\t"))
|
|
}
|