Skip to content

Commit

Permalink
don't call full read when updating billing acct (#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow authored Jul 19, 2018
1 parent cacdaec commit 080f6fe
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
20 changes: 9 additions & 11 deletions google/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,12 @@ func forceDeleteComputeNetwork(projectId, networkName string, config *Config) er
func updateProjectBillingAccount(d *schema.ResourceData, config *Config) error {
pid := d.Id()
name := d.Get("billing_account").(string)
ba := cloudbilling.ProjectBillingInfo{}
ba := &cloudbilling.ProjectBillingInfo{}
// If we're unlinking an existing billing account, an empty request does that, not an empty-string billing account.
if name != "" {
ba.BillingAccountName = "billingAccounts/" + name
}
_, err := config.clientBilling.Projects.UpdateBillingInfo(prefixedProject(pid), &ba).Do()
_, err := config.clientBilling.Projects.UpdateBillingInfo(prefixedProject(pid), ba).Do()
if err != nil {
d.Set("billing_account", "")
if _err, ok := err.(*googleapi.Error); ok {
Expand All @@ -602,20 +602,18 @@ func updateProjectBillingAccount(d *schema.ResourceData, config *Config) error {
return fmt.Errorf("Error setting billing account %q for project %q: %v", name, prefixedProject(pid), err)
}
for retries := 0; retries < 3; retries++ {
err = resourceGoogleProjectRead(d, config)
ba, err = config.clientBilling.Projects.GetBillingInfo(prefixedProject(pid)).Do()
if err != nil {
return err
}
if d.Get("billing_account").(string) == name {
break
baName := strings.TrimPrefix(ba.BillingAccountName, "billingAccounts/")
if baName == name {
return nil
}
time.Sleep(3)
time.Sleep(3 * time.Second)
}
if d.Get("billing_account").(string) != name {
return fmt.Errorf("Timed out waiting for billing account to return correct value. Waiting for %s, got %s.",
d.Get("billding_account").(string), name)
}
return nil
return fmt.Errorf("Timed out waiting for billing account to return correct value. Waiting for %s, got %s.",
name, strings.TrimPrefix(ba.BillingAccountName, "billingAccounts/"))
}

func expandAppEngineApp(d *schema.ResourceData) (*appengine.Application, error) {
Expand Down
46 changes: 46 additions & 0 deletions google/resource_google_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,35 @@ func TestAccProject_appEngineBasic(t *testing.T) {
})
}

func TestAccProject_appEngineBasicWithBilling(t *testing.T) {
t.Parallel()

org := getTestOrgFromEnv(t)
pid := acctest.RandomWithPrefix("tf-test")
billingId := getTestBillingAccountFromEnv(t)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccProject_appEngineBasicWithBilling(pid, org, billingId),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("google_project.acceptance", "app_engine.0.name"),
resource.TestCheckResourceAttrSet("google_project.acceptance", "app_engine.0.url_dispatch_rule.#"),
resource.TestCheckResourceAttrSet("google_project.acceptance", "app_engine.0.code_bucket"),
resource.TestCheckResourceAttrSet("google_project.acceptance", "app_engine.0.default_hostname"),
resource.TestCheckResourceAttrSet("google_project.acceptance", "app_engine.0.default_bucket"),
),
},
resource.TestStep{
ResourceName: "google_project.acceptance",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccProject_appEngineUpdate(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -472,6 +501,23 @@ resource "google_project" "acceptance" {
}`, pid, pid, org)
}

func testAccProject_appEngineBasicWithBilling(pid, org, billing string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
project_id = "%s"
name = "%s"
org_id = "%s"
billing_account = "%s"
app_engine {
auth_domain = "hashicorptest.com"
location_id = "us-central"
serving_status = "SERVING"
}
}`, pid, pid, org, billing)
}

func testAccProject_appEngineUpdate(pid, org string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
Expand Down

0 comments on commit 080f6fe

Please sign in to comment.