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

Make google_service_account resource importable #606

Merged
merged 7 commits into from
Oct 25, 2017

Conversation

pdecat
Copy link
Contributor

@pdecat pdecat commented Oct 18, 2017

No description provided.

t.Parallel()

resourceName := "google_service_account.acceptance"
project := os.Getenv("GOOGLE_PROJECT")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why you decided to set the project id specifically in this test rather than letting the test read it from the environment? If so mind adding a comment to explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, project is indeed an optional parameter so it should work with or without it.

Removing it from the import test raises an issue so I guess we should keep both test cases:

TF_ACC=1 go test ./google/ -v -run=TestAccGoogleServiceAccount_import.* -timeout 120m                                                                                                     
=== RUN   TestAccGoogleServiceAccount_importBasic                                                                                                                                         
=== RUN   TestAccGoogleServiceAccount_importWithProject                                                                                                                                   
--- FAIL: TestAccGoogleServiceAccount_importBasic (2.66s)                                                                                                                                 
        testing.go:435: Step 0 error: After applying this step, the plan was not empty:                                                                                                   
                                                                                                                                                                                          
                DIFF:                                                                                                                                                                     
                                                                                                                                                                                          
                DESTROY/CREATE: google_service_account.acceptance                                                                                                                         
                  account_id:   "terraform-1nd79xoges" => "terraform-1nd79xoges"                                                                                                          
                  display_name: "terraform-1nd79xoges" => "terraform-1nd79xoges"                                                                                                          
                  email:        "terraform-1nd79xoges@myproject.iam.gserviceaccount.com" => "<computed>"                                                                
                  name:         "projects/myproject/serviceAccounts/terraform-1nd79xoges@myproject.iam.gserviceaccount.com" => "<computed>"
                  project:      "myproject" => "" (forces new resource)
                  unique_id:    "113399152587722893408" => "<computed>"

                STATE:

                google_service_account.acceptance:
                  ID = projects/myproject/serviceAccounts/terraform-1nd79xoges@myproject.iam.gserviceaccount.com
                  account_id = terraform-1nd79xoges
                  display_name = terraform-1nd79xoges
                  email = terraform-1nd79xoges@myproject.iam.gserviceaccount.com
                  name = projects/myproject/serviceAccounts/terraform-1nd79xoges@myproject.iam.gserviceaccount.com
                  project = myproject
                  unique_id = 113399152587722893408
--- PASS: TestAccGoogleServiceAccount_importWithProject (3.91s)
FAIL
exit status 1
FAIL    github.com/terraform-providers/terraform-provider-google/google 3.920s

No sure why yet.
Once fixed, I'll add a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding Computed: True to the google_service_account's project field ensures the project id is always stored in the state and resolves the issue. Both test cases now pass.

I've also added an extra step and some assertions to the TestAccGoogleServiceAccount_basic test case to assert this.

Copy link
Contributor

@danawillow danawillow left a comment

Choose a reason for hiding this comment

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

Just one quick comment, otherwise 👍

… id is always stored in the state, defined in configuration or not. Add corresponding test cases

resourceName := "google_service_account.acceptance"
sa_name := "terraform-" + acctest.RandString(10)
conf := testAccGoogleServiceAccount_import(sa_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: s/sa_name/saName/

Also nit: this is (and other variables defined here are) only used in one place, so you could pretty easily inline them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

func testAccGoogleServiceAccountWithProject(project, account, name string) string {
t := `resource "google_service_account" "acceptance" {
project = "%v"
account_id = "%v"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Damn tabs!

"google_service_account.acceptance", "project", project),
),
},
// The third step adds the default project to the 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.

Hmm, is this really what you want? Wouldn't this test just update the config to what it already is and then not actually do anything? I guess that's not a huge deal but if that's what you're going for it might be good to explain it in a comment. Otherwise maybe it deserves to be a separate TestCase, rather than a TestStep.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, what I am trying to assert here is that adding the project field to an existing configuration with the same value as the default does not recreate the service account, which was not the case without the Computed: true.

But I realize that this test should rather assert that the service account's unique id does not change between step 2 and 3.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test case and comment updated, but not sure if this is the best way to compare state values between steps.

@danawillow, what do you think?

@danawillow
Copy link
Contributor

Thanks @pdecat!

@danawillow danawillow merged commit 3d1e110 into hashicorp:master Oct 25, 2017
@pdecat pdecat deleted the f-import-service-account branch March 7, 2018 08:59
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
* Make google_service_account resource importable

* Add google_service_account testcase with default project

* Mark google_service_account.project as computed to ensure the project id is always stored in the state, defined in configuration or not. Add corresponding test cases

* Inline variables with single usage

* Replace tabs with spaces in configuration strings

* Ensure service account is not recreated when the default project is explicitely added to the configuration

* camelcase
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
Signed-off-by: Modular Magician <magic-modules@google.com>
@ghost
Copy link

ghost commented Mar 29, 2020

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 Mar 29, 2020
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