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 #6015

Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .changelog/8112.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
`google_billing_project_info`
```
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var services = mapOf(
"containerattached" to "Containerattached",
"containeraws" to "Containeraws",
"containerazure" to "Containerazure",
"corebilling" to "Corebilling",
"databasemigrationservice" to "Databasemigrationservice",
"datacatalog" to "Datacatalog",
"dataflow" to "Dataflow",
Expand Down
1 change: 1 addition & 0 deletions google-beta/config_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func configureTestBasePaths(c *transport_tpg.Config, url string) {
c.ComputeBasePath = url
c.ContainerAnalysisBasePath = url
c.ContainerAttachedBasePath = url
c.CoreBillingBasePath = url
c.DatabaseMigrationServiceBasePath = url
c.DataCatalogBasePath = url
c.DataformBasePath = url
Expand Down
1 change: 1 addition & 0 deletions google-beta/fwmodels/provider_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type ProviderModel struct {
ComputeCustomEndpoint types.String `tfsdk:"compute_custom_endpoint"`
ContainerAnalysisCustomEndpoint types.String `tfsdk:"container_analysis_custom_endpoint"`
ContainerAttachedCustomEndpoint types.String `tfsdk:"container_attached_custom_endpoint"`
CoreBillingCustomEndpoint types.String `tfsdk:"core_billing_custom_endpoint"`
DatabaseMigrationServiceCustomEndpoint types.String `tfsdk:"database_migration_service_custom_endpoint"`
DataCatalogCustomEndpoint types.String `tfsdk:"data_catalog_custom_endpoint"`
DataformCustomEndpoint types.String `tfsdk:"dataform_custom_endpoint"`
Expand Down
6 changes: 6 additions & 0 deletions google-beta/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
transport_tpg.CustomEndpointValidator(),
},
},
"core_billing_custom_endpoint": &schema.StringAttribute{
Optional: true,
Validators: []validator.String{
transport_tpg.CustomEndpointValidator(),
},
},
"database_migration_service_custom_endpoint": &schema.StringAttribute{
Optional: true,
Validators: []validator.String{
Expand Down
10 changes: 10 additions & 0 deletions google-beta/fwtransport/framework_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type FrameworkProviderConfig struct {
ComputeBasePath string
ContainerAnalysisBasePath string
ContainerAttachedBasePath string
CoreBillingBasePath string
DatabaseMigrationServiceBasePath string
DataCatalogBasePath string
DataformBasePath string
Expand Down Expand Up @@ -233,6 +234,7 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context,
p.ComputeBasePath = data.ComputeCustomEndpoint.ValueString()
p.ContainerAnalysisBasePath = data.ContainerAnalysisCustomEndpoint.ValueString()
p.ContainerAttachedBasePath = data.ContainerAttachedCustomEndpoint.ValueString()
p.CoreBillingBasePath = data.CoreBillingCustomEndpoint.ValueString()
p.DatabaseMigrationServiceBasePath = data.DatabaseMigrationServiceCustomEndpoint.ValueString()
p.DataCatalogBasePath = data.DataCatalogCustomEndpoint.ValueString()
p.DataformBasePath = data.DataformCustomEndpoint.ValueString()
Expand Down Expand Up @@ -701,6 +703,14 @@ func (p *FrameworkProviderConfig) HandleDefaults(ctx context.Context, data *fwmo
data.ContainerAttachedCustomEndpoint = types.StringValue(customEndpoint.(string))
}
}
if data.CoreBillingCustomEndpoint.IsNull() {
customEndpoint := transport_tpg.MultiEnvDefault([]string{
"GOOGLE_CORE_BILLING_CUSTOM_ENDPOINT",
}, transport_tpg.DefaultBasePaths[transport_tpg.CoreBillingBasePathKey])
if customEndpoint != nil {
data.CoreBillingCustomEndpoint = types.StringValue(customEndpoint.(string))
}
}
if data.DatabaseMigrationServiceCustomEndpoint.IsNull() {
customEndpoint := transport_tpg.MultiEnvDefault([]string{
"GOOGLE_DATABASE_MIGRATION_SERVICE_CUSTOM_ENDPOINT",
Expand Down
1 change: 1 addition & 0 deletions google-beta/gcp_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
_ "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute"
_ "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/containeranalysis"
_ "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/containerattached"
_ "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/corebilling"
_ "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/databasemigrationservice"
_ "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/datacatalog"
_ "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/dataform"
Expand Down
12 changes: 10 additions & 2 deletions google-beta/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/containeranalysis"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/containerattached"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/corebilling"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/databasemigrationservice"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/datacatalog"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/dataform"
Expand Down Expand Up @@ -405,6 +406,11 @@ func Provider() *schema.Provider {
Optional: true,
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
},
"core_billing_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
},
"database_migration_service_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1074,9 +1080,9 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
})
}

// Generated resources: 362
// Generated resources: 363
// Generated IAM resources: 234
// Total generated resources: 596
// Total generated resources: 597
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -1341,6 +1347,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_container_analysis_note_iam_policy": tpgiamresource.ResourceIamPolicy(containeranalysis.ContainerAnalysisNoteIamSchema, containeranalysis.ContainerAnalysisNoteIamUpdaterProducer, containeranalysis.ContainerAnalysisNoteIdParseFunc),
"google_container_analysis_occurrence": containeranalysis.ResourceContainerAnalysisOccurrence(),
"google_container_attached_cluster": containerattached.ResourceContainerAttachedCluster(),
"google_billing_project_info": corebilling.ResourceCoreBillingProjectInfo(),
"google_database_migration_service_connection_profile": databasemigrationservice.ResourceDatabaseMigrationServiceConnectionProfile(),
"google_data_catalog_entry": datacatalog.ResourceDataCatalogEntry(),
"google_data_catalog_entry_group": datacatalog.ResourceDataCatalogEntryGroup(),
Expand Down Expand Up @@ -1960,6 +1967,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
config.ComputeBasePath = d.Get("compute_custom_endpoint").(string)
config.ContainerAnalysisBasePath = d.Get("container_analysis_custom_endpoint").(string)
config.ContainerAttachedBasePath = d.Get("container_attached_custom_endpoint").(string)
config.CoreBillingBasePath = d.Get("core_billing_custom_endpoint").(string)
config.DatabaseMigrationServiceBasePath = d.Get("database_migration_service_custom_endpoint").(string)
config.DataCatalogBasePath = d.Get("data_catalog_custom_endpoint").(string)
config.DataformBasePath = d.Get("dataform_custom_endpoint").(string)
Expand Down
119 changes: 119 additions & 0 deletions google-beta/resource_billing_project_info_generated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import (
"fmt"
"strings"
"testing"

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

"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)

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

context := map[string]interface{}{
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
"org_id": envvar.GetTestOrgFromEnv(t),
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckCoreBillingProjectInfoDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccCoreBillingProjectInfo_billingProjectInfoBasicExample(context),
},
{
ResourceName: "google_billing_project_info.default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCoreBillingProjectInfo_billingProjectInfoBasicExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = "tf-test%{random_suffix}"
name = "tf-test%{random_suffix}"
org_id = "%{org_id}"
lifecycle {
ignore_changes = [billing_account]
}
}

resource "google_billing_project_info" "default" {
project = google_project.project.project_id
billing_account = "%{billing_account}"
}
`, context)
}

func testAccCheckCoreBillingProjectInfoDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_billing_project_info" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}

// 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)
}

return nil
}
}
71 changes: 71 additions & 0 deletions google-beta/resource_google_billing_project_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package google

import (
"fmt"
"testing"

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

"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/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)
}
Loading