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

New resource to manage a project's billing account #8112

Merged
merged 9 commits into from
Aug 3, 2023
46 changes: 46 additions & 0 deletions mmv1/products/billing/ProjectInfo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2023 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

--- !ruby/object:Api::Resource
name: ProjectInfo
base_url: 'projects/{{project}}/billingInfo'
create_verb: :PUT
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this resource updatable as well? If so, can you add a handwritten update test? If not, you can add immutable: true here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is updatable.

I'll add the update test and update the PR

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

delete_verb: :PUT
description: |
Billing information for a project.
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Enable, disable, or change billing for a project': 'https://cloud.google.com/billing/docs/how-to/modify-project'
api: 'https://cloud.google.com/billing/docs/reference/rest/v1/projects'
id_format: 'projects/{{project}}/billingInfo'
custom_code: !ruby/object:Provider::Terraform::CustomCode
decoder: templates/terraform/decoders/billing_project_info.go.erb
encoder: templates/terraform/encoders/billing_project_info.go.erb
test_check_destroy: templates/terraform/custom_check_destroy/billing_project_info.go.erb
import_format:
['projects/{{project}}', '{{project}}']
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'billing_project_info_basic'
primary_resource_id: 'default'
test_env_vars:
billing_account: :BILLING_ACCT
org_id: :ORG_ID
properties:
- !ruby/object:Api::Type::String
name: billing_account
description: |
The ID of the billing account associated with the project, if
any. Set to empty string to disable billing for the project.
For example, `"012345-567890-ABCDEF"` or `""`.
required: true
30 changes: 30 additions & 0 deletions mmv1/products/billing/product.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2023 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

--- !ruby/object:Api::Product
# "Billing" is already used by the Billing Budgets product, so we're
# forced to use a fake name and to specify `legacy_name` to have all
# resources in the `billing` namespace
name: CoreBilling
display_name: Cloud Billing
legacy_name: billing
versions:
- !ruby/object:Api::Product::Version
name: ga
base_url: https://cloudbilling.googleapis.com/v1/
scopes:
- https://www.googleapis.com/auth/cloud-platform
apis_required:
- !ruby/object:Api::Product::ApiReference
name: Cloud Billing
url: https://console.cloud.google.com/apis/library/cloudbilling.googleapis.com/
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// After deleting a project, you can still query its billing account
// (it will be empty). We change the destroy check to ensure the
// project has no billing account linked after destroying the
// google_billing_project_info resource

config := GoogleProviderConfig(t)

url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{CoreBillingBasePath}}projects/{{project}}/billingInfo")
if err != nil {
return err
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
RawURL: url,
UserAgent: config.UserAgent,
})
if err != nil {
return nil
}

if ba := res["billingAccountName"]; ba == "" {
return nil
}

return fmt.Errorf("Billing account still linked at %s", url)
2 changes: 2 additions & 0 deletions mmv1/templates/terraform/decoders/billing_project_info.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
res["billing_account"] = strings.TrimPrefix(res["billingAccountName"].(string), "billingAccounts/")
return res, nil
8 changes: 8 additions & 0 deletions mmv1/templates/terraform/encoders/billing_project_info.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ba := d.Get("billing_account").(string)
if ba == "" {
obj["billingAccountName"] = ""
} else {
obj["billingAccountName"] = "billingAccounts/" + ba
}
delete(obj, "billing_account")
return obj, nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "google_project" "project" {
project_id = "tf-test%{random_suffix}"
name = "tf-test%{random_suffix}"
org_id = "<%= ctx[:test_env_vars]['org_id'] %>"
lifecycle {
ignore_changes = [billing_account]
}
}

resource "google_billing_project_info" "<%= ctx[:primary_resource_id] %>" {
project = google_project.project.project_id
billing_account = "<%= ctx[:test_env_vars]['billing_account'] %>"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
)

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

projectId := "tf-test-" + acctest.RandString(t, 10)
orgId := envvar.GetTestOrgFromEnv(t)
billingAccount := envvar.GetTestBillingAccountFromEnv(t)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckCoreBillingProjectInfoDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBillingProjectInfo_basic(projectId, orgId, billingAccount),
},
{
ResourceName: "google_billing_project_info.info",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBillingProjectInfo_basic(projectId, orgId, ""),
},
{
ResourceName: "google_billing_project_info.info",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBillingProjectInfo_basic(projectId, orgId, billingAccount),
},
{
ResourceName: "google_billing_project_info.info",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccBillingProjectInfo_basic(projectId, orgId, billingAccountId string) string {
return fmt.Sprintf(`
resource "google_project" "project" {
project_id = "%s"
name = "%[1]s"
org_id = "%s"
lifecycle {
ignore_changes = [billing_account]
}
}

resource "google_billing_project_info" "info" {
project = google_project.project.project_id
billing_account = "%s"
}
`, projectId, orgId, billingAccountId)
}