-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8316c3d
Add new resource to set project billing info
juliocc fc790dc
Add data source to extract project billing info
juliocc 5d218c5
Add basic test/example to google_billing_project_info
juliocc 86efe40
Add data source test
juliocc 752717d
Add documentation info
juliocc 003bec4
Add data source documentation
juliocc 4ef10ac
Remove google_billing_project_info data source
juliocc f33393b
Add handwritten test for google_billing_project_info
juliocc a414995
Update test logic for google_billing_project_info
juliocc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
27 changes: 27 additions & 0 deletions
27
mmv1/templates/terraform/custom_check_destroy/billing_project_info.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
2
mmv1/templates/terraform/decoders/billing_project_info.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
8
mmv1/templates/terraform/encoders/billing_project_info.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
mmv1/templates/terraform/examples/billing_project_info_basic.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] %>" | ||
} |
69 changes: 69 additions & 0 deletions
69
mmv1/third_party/terraform/tests/resource_google_billing_project_info_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done