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

Google project precheck #3145

Merged
merged 7 commits into from
Feb 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions third_party/terraform/resources/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func resourceGoogleProject() *schema.Resource {
func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

if err := resourceGoogleProjectCheckPreRequisites(config, d); err != nil {
return fmt.Errorf("failed pre-requisites: %v", err)
}

var pid string
var err error
pid = d.Get("project_id").(string)
Expand Down Expand Up @@ -173,6 +177,26 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceGoogleProjectCheckPreRequisites(config *Config, d *schema.ResourceData) error {
ib, ok := d.GetOk("billing_account")
if !ok {
return nil
}
ba := "billingAccounts/" + ib.(string)
const perm = "billing.resourceAssociations.create"
req := &cloudbilling.TestIamPermissionsRequest{
Permissions: []string{perm},
}
resp, err := config.clientBilling.BillingAccounts.TestIamPermissions(ba, req).Do()
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Was there a consensus on whether this request failing should actually fail the apply?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed my mind and decided it's fine. I'd love to make this a check for the correct API being enabled but I'm not sure it's possible at this point. I added something to that effect in hashicorp/terraform-provider-google#5671 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, thanks!

return fmt.Errorf("failed to check permissions on billing account %q: %v", ba, err)
}
if !stringInSlice(resp.Permissions, perm) {
return fmt.Errorf("missing permission on %q: %v", ba, perm)
}
return nil
}

func resourceGoogleProjectRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
parts := strings.Split(d.Id(), "/")
Expand Down