-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datasource: added service account and service account key #1535
Merged
nat-henderson
merged 2 commits into
hashicorp:master
from
vishen:datasource_service_account_and_key
Jun 1, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceGoogleServiceAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleServiceAccountRead, | ||
Schema: map[string]*schema.Schema{ | ||
"account_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateRFC1035Name(6, 30), | ||
}, | ||
"project": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"email": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"unique_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"display_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleServiceAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
// Get the project from the resource or fallback to the project | ||
// in the provider configuration | ||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the service account as a fully qualified name | ||
serviceAccountName := serviceAccountFQN(d.Get("account_id").(string), project) | ||
|
||
sa, err := config.clientIAM.Projects.ServiceAccounts.Get(serviceAccountName).Do() | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Service Account %q", serviceAccountName)) | ||
} | ||
|
||
d.SetId(sa.Name) | ||
d.Set("email", sa.Email) | ||
d.Set("unique_id", sa.UniqueId) | ||
d.Set("project", sa.ProjectId) | ||
d.Set("account_id", strings.Split(sa.Email, "@")[0]) | ||
d.Set("name", sa.Name) | ||
d.Set("display_name", sa.DisplayName) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func dataSourceGoogleServiceAccountKey() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleServiceAccountKeyRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"service_account_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"public_key_type": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Default: "TYPE_X509_PEM_FILE", | ||
Optional: true, | ||
ValidateFunc: validation.StringInSlice([]string{"TYPE_NONE", "TYPE_X509_PEM_FILE", "TYPE_RAW_PUBLIC_KEY"}, false), | ||
}, | ||
"project": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
// Computed | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"key_algorithm": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleServiceAccountKeyRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
// Get the project from the resource or fallback to the project | ||
// in the provider configuration | ||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the service account as the fully qualified name | ||
serviceAccountName := serviceAccountFQN(d.Get("service_account_id").(string), project) | ||
|
||
publicKeyType := d.Get("public_key_type").(string) | ||
|
||
// Confirm the service account key exists | ||
sak, err := config.clientIAM.Projects.ServiceAccounts.Keys.Get(serviceAccountName).PublicKeyType(publicKeyType).Do() | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Service Account Key %q", serviceAccountName)) | ||
} | ||
|
||
d.SetId(sak.Name) | ||
|
||
d.Set("name", sak.Name) | ||
d.Set("key_algorithm", sak.KeyAlgorithm) | ||
d.Set("public_key", sak.PublicKeyData) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
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{ | ||
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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" { | ||
service_account_id = "${google_service_account_key.acceptance.id}" | ||
}`, account) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDatasourceGoogleServiceAccount_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
resourceName := "data.google_service_account.acceptance" | ||
account := acctest.RandomWithPrefix("tf-test") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckGoogleServiceAccount_basic(account), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGoogleServiceAccountExists(resourceName), | ||
resource.TestCheckResourceAttr( | ||
resourceName, "id", fmt.Sprintf("projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com", getTestProjectFromEnv(), account, getTestProjectFromEnv())), | ||
resource.TestCheckResourceAttrSet(resourceName, "email"), | ||
resource.TestCheckResourceAttrSet(resourceName, "unique_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "name"), | ||
resource.TestCheckResourceAttrSet(resourceName, "display_name"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGoogleServiceAccount_basic(account string) string { | ||
return fmt.Sprintf(` | ||
resource "google_service_account" "acceptance" { | ||
account_id = "%s" | ||
display_name = "Testing Account" | ||
} | ||
|
||
data "google_service_account" "acceptance" { | ||
account_id = "${google_service_account.acceptance.account_id}" | ||
} | ||
`, account) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not ideal to set
account_id
- users will be a little surprised ifaccount_id
changes from the value they set it to, on the off chance they choose to interpolate it. In a resource this would be a permanent diff (unless we had aDiffSuppressFunc
), which we try to avoid. I'm 50/50 on this - what do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree, it is a little confusing. I was trying to keep the data set to be the same as the resource
google_service_account
(which sets theaccount_id
the same way) - but happy to remove it if it is likely to cause confusion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. That must have a
DiffSuppressFunc
, then? I suppose it's better to maintain consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I was misusing the service account id for this, the service account resource only accepts a service account id, so I have changed the datasource to mimic this; as such the account_id should always be the same. The resource for service account still has this line, so I left it in, but if it is confusing I can remove it?