Skip to content
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

Conversation

vishen
Copy link
Contributor

@vishen vishen commented May 24, 2018

Added datasources for; service account and service account key. Both datasources should be somewhat similar to their resource counterparts, but they accept a much more flexible account_id and service_account_id.

This is my first time with acceptance tests (or a datasource), so please let me know if I need to add more, or have done something weird :)

Added datasources for; service account and service account key.
Both datasources should be somewhat similar to their resource
counterparts, but they accept a much more flexible 'account_id'
and 'service_account_id'.
@nat-henderson nat-henderson self-requested a review May 25, 2018 00:18
Read: dataSourceGoogleServiceAccountRead,

Schema: map[string]*schema.Schema{
// Required
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments here aren't necessary since they duplicate the code below, and it's generally good practice not to include comments that duplicate code. Thanks for putting the schema elements in our preferred order, though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! I was trying to keep it similar to other datasource code, but I will remove it :)

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])
Copy link
Contributor

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 if account_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 a DiffSuppressFunc), which we try to avoid. I'm 50/50 on this - what do you think?

Copy link
Contributor Author

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 the account_id the same way) - but happy to remove it if it is likely to cause confusion?

Copy link
Contributor

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.

Copy link
Contributor Author

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?

Read: dataSourceGoogleServiceAccountKeyRead,

Schema: map[string]*schema.Schema{
// Required
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment - probably best to remove duplicate comments.

serviceAccountID := d.Get("service_account_id").(string)

// If the service account id isn't already the fully qualified name
if !strings.HasPrefix(serviceAccountID, "projects/") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much of this can be shared? Possibly even with the resources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I can share it with the resource google_sevice_account_key, but I don't know if I can do the same with google_service_account as it has vadliateRFC1035Name(6, 30) in the schema validation for account_id which doesn't allow '@' or /

Config: testAccDatasourceGoogleServiceAccountKey(account),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleServiceAccountKeyExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "name"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to test that they're set to the right values, also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service account key endpoint returns the name as projects/PROJECT-ID/serviceAccounts/SA-NAME@PROJECT-ID.iam.gserviceaccount.com/keys/<key_id> - should I check that it starts with the expected service account?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, right now I think this just checks that the resource has any attribute as name - if you could check that it's the value you expect somehow, that would help.

Cleaned up some CR comments. 'datasource google_service_account'
now behaves more similar to 'resource google_service_account' in
regards to how they both accept ONLY the service account id, so
there is no weird diffs or confusing behaviour.

Moved the service account FQN into a function that
'resource service_account_key' now uses as well. Test that
'datasource google_service_account_key' returns the name that starts with
the service account name. And removed now pointless tests that were
testing features that were incompatible with the service account
resource.
@nat-henderson
Copy link
Contributor

Okay, that looks all right to me. I'll run the tests!

Copy link
Contributor

@nat-henderson nat-henderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests pass - merging.

@nat-henderson
Copy link
Contributor

Hm. I re-ran the tests (forgot about the resource tests) and now they're crashing with a tooling error. Let me see what's going on there!

@nat-henderson
Copy link
Contributor

Eh, you know what, I ran them locally and I'm sure it works. Merging!

@nat-henderson nat-henderson merged commit 8f31fec into hashicorp:master Jun 1, 2018
@vishen vishen deleted the datasource_service_account_and_key branch June 1, 2018 07:38
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
@ghost
Copy link

ghost commented Nov 18, 2018

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants