From 37b9605cc8e787a5fc1dddfa18fd2c14c635539d Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 3 Mar 2023 00:09:49 +0000 Subject: [PATCH] Adding support for big query capacity commitments (#7364) Co-authored-by: drfaust92 Signed-off-by: Modular Magician --- .changelog/7364.txt | 3 + google-beta/provider.go | 5 +- .../resource_bigquery_capacity_commitment.go | 407 ++++++++++++++++++ ...uery_capacity_commitment_generated_test.go | 103 +++++ ...gquery_capacity_commitment_sweeper_test.go | 124 ++++++ ...bigquery_capacity_commitment.html.markdown | 126 ++++++ 6 files changed, 766 insertions(+), 2 deletions(-) create mode 100644 .changelog/7364.txt create mode 100644 google-beta/resource_bigquery_capacity_commitment.go create mode 100644 google-beta/resource_bigquery_capacity_commitment_generated_test.go create mode 100644 google-beta/resource_bigquery_capacity_commitment_sweeper_test.go create mode 100644 website/docs/r/bigquery_capacity_commitment.html.markdown diff --git a/.changelog/7364.txt b/.changelog/7364.txt new file mode 100644 index 0000000000..23ef9ab568 --- /dev/null +++ b/.changelog/7364.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +`google_bigquery_capacity_commitment` +``` diff --git a/google-beta/provider.go b/google-beta/provider.go index d453dd17c6..3c38afa0f8 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -1108,9 +1108,9 @@ func Provider() *schema.Provider { return provider } -// Generated resources: 308 +// Generated resources: 309 // Generated IAM resources: 204 -// Total generated resources: 512 +// Total generated resources: 513 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -1204,6 +1204,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_bigquery_datapolicy_data_policy_iam_member": ResourceIamMember(BigqueryDatapolicyDataPolicyIamSchema, BigqueryDatapolicyDataPolicyIamUpdaterProducer, BigqueryDatapolicyDataPolicyIdParseFunc), "google_bigquery_datapolicy_data_policy_iam_policy": ResourceIamPolicy(BigqueryDatapolicyDataPolicyIamSchema, BigqueryDatapolicyDataPolicyIamUpdaterProducer, BigqueryDatapolicyDataPolicyIdParseFunc), "google_bigquery_data_transfer_config": ResourceBigqueryDataTransferConfig(), + "google_bigquery_capacity_commitment": ResourceBigqueryReservationCapacityCommitment(), "google_bigquery_reservation": ResourceBigqueryReservationReservation(), "google_bigtable_app_profile": ResourceBigtableAppProfile(), "google_billing_budget": ResourceBillingBudget(), diff --git a/google-beta/resource_bigquery_capacity_commitment.go b/google-beta/resource_bigquery_capacity_commitment.go new file mode 100644 index 0000000000..524463f147 --- /dev/null +++ b/google-beta/resource_bigquery_capacity_commitment.go @@ -0,0 +1,407 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "log" + "reflect" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func ResourceBigqueryReservationCapacityCommitment() *schema.Resource { + return &schema.Resource{ + Create: resourceBigqueryReservationCapacityCommitmentCreate, + Read: resourceBigqueryReservationCapacityCommitmentRead, + Update: resourceBigqueryReservationCapacityCommitmentUpdate, + Delete: resourceBigqueryReservationCapacityCommitmentDelete, + + Importer: &schema.ResourceImporter{ + State: resourceBigqueryReservationCapacityCommitmentImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(20 * time.Minute), + Update: schema.DefaultTimeout(20 * time.Minute), + Delete: schema.DefaultTimeout(20 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "plan": { + Type: schema.TypeString, + Required: true, + Description: `Capacity commitment plan. Valid values are FLEX, TRIAL, MONTHLY, ANNUAL`, + }, + "slot_count": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + Description: `Number of slots in this commitment.`, + }, + "capacity_commitment_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: `The optional capacity commitment ID. Capacity commitment name will be generated automatically if this field is +empty. This field must only contain lower case alphanumeric characters or dashes. The first and last character +cannot be a dash. Max length is 64 characters. NOTE: this ID won't be kept if the capacity commitment is split +or merged.`, + }, + "enforce_single_admin_project_per_org": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: `If true, fail the request if another project in the organization has a capacity commitment.`, + }, + "location": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: `The geographic location where the transfer config should reside. +Examples: US, EU, asia-northeast1. The default value is US.`, + Default: "US", + }, + "renewal_plan": { + Type: schema.TypeString, + Optional: true, + Description: `The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable for ANNUAL and TRIAL commitments.`, + }, + "commitment_end_time": { + Type: schema.TypeString, + Computed: true, + Description: `The start of the current commitment period. It is applicable only for ACTIVE capacity commitments.`, + }, + "commitment_start_time": { + Type: schema.TypeString, + Computed: true, + Description: `The start of the current commitment period. It is applicable only for ACTIVE capacity commitments.`, + }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: `The resource name of the capacity commitment, e.g., projects/myproject/locations/US/capacityCommitments/123`, + }, + "state": { + Type: schema.TypeString, + Computed: true, + Description: `State of the commitment`, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + UseJSONNumber: true, + } +} + +func resourceBigqueryReservationCapacityCommitmentCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + slotCountProp, err := expandBigqueryReservationCapacityCommitmentSlotCount(d.Get("slot_count"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("slot_count"); !isEmptyValue(reflect.ValueOf(slotCountProp)) && (ok || !reflect.DeepEqual(v, slotCountProp)) { + obj["slotCount"] = slotCountProp + } + planProp, err := expandBigqueryReservationCapacityCommitmentPlan(d.Get("plan"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("plan"); !isEmptyValue(reflect.ValueOf(planProp)) && (ok || !reflect.DeepEqual(v, planProp)) { + obj["plan"] = planProp + } + renewalPlanProp, err := expandBigqueryReservationCapacityCommitmentRenewalPlan(d.Get("renewal_plan"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("renewal_plan"); !isEmptyValue(reflect.ValueOf(renewalPlanProp)) && (ok || !reflect.DeepEqual(v, renewalPlanProp)) { + obj["renewalPlan"] = renewalPlanProp + } + + url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}projects/{{project}}/locations/{{location}}/capacityCommitments?capacityCommitmentId={{capacity_commitment_id}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new CapacityCommitment: %#v", obj) + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for CapacityCommitment: %s", err) + } + billingProject = project + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := SendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) + if err != nil { + return fmt.Errorf("Error creating CapacityCommitment: %s", err) + } + if err := d.Set("name", flattenBigqueryReservationCapacityCommitmentName(res["name"], d, config)); err != nil { + return fmt.Errorf(`Error setting computed identity field "name": %s`, err) + } + + // Store the ID now + id, err := replaceVars(d, config, "{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating CapacityCommitment %q: %#v", d.Id(), res) + + return resourceBigqueryReservationCapacityCommitmentRead(d, meta) +} + +func resourceBigqueryReservationCapacityCommitmentRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for CapacityCommitment: %s", err) + } + billingProject = project + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := SendRequest(config, "GET", billingProject, url, userAgent, nil) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("BigqueryReservationCapacityCommitment %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + + if err := d.Set("name", flattenBigqueryReservationCapacityCommitmentName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + if err := d.Set("slot_count", flattenBigqueryReservationCapacityCommitmentSlotCount(res["slotCount"], d, config)); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + if err := d.Set("plan", flattenBigqueryReservationCapacityCommitmentPlan(res["plan"], d, config)); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + if err := d.Set("state", flattenBigqueryReservationCapacityCommitmentState(res["state"], d, config)); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + if err := d.Set("commitment_start_time", flattenBigqueryReservationCapacityCommitmentCommitmentStartTime(res["commitmentStartTime"], d, config)); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + if err := d.Set("commitment_end_time", flattenBigqueryReservationCapacityCommitmentCommitmentEndTime(res["commitmentEndTime"], d, config)); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + if err := d.Set("renewal_plan", flattenBigqueryReservationCapacityCommitmentRenewalPlan(res["renewalPlan"], d, config)); err != nil { + return fmt.Errorf("Error reading CapacityCommitment: %s", err) + } + + return nil +} + +func resourceBigqueryReservationCapacityCommitmentUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for CapacityCommitment: %s", err) + } + billingProject = project + + obj := make(map[string]interface{}) + planProp, err := expandBigqueryReservationCapacityCommitmentPlan(d.Get("plan"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("plan"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, planProp)) { + obj["plan"] = planProp + } + renewalPlanProp, err := expandBigqueryReservationCapacityCommitmentRenewalPlan(d.Get("renewal_plan"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("renewal_plan"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, renewalPlanProp)) { + obj["renewalPlan"] = renewalPlanProp + } + + url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating CapacityCommitment %q: %#v", d.Id(), obj) + updateMask := []string{} + + if d.HasChange("plan") { + updateMask = append(updateMask, "plan") + } + + if d.HasChange("renewal_plan") { + updateMask = append(updateMask, "renewalPlan") + } + // updateMask is a URL parameter but not present in the schema, so replaceVars + // won't set it + url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) + if err != nil { + return err + } + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := SendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return fmt.Errorf("Error updating CapacityCommitment %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating CapacityCommitment %q: %#v", d.Id(), res) + } + + return resourceBigqueryReservationCapacityCommitmentRead(d, meta) +} + +func resourceBigqueryReservationCapacityCommitmentDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for CapacityCommitment: %s", err) + } + billingProject = project + + url, err := replaceVars(d, config, "{{BigqueryReservationBasePath}}{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting CapacityCommitment %q", d.Id()) + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := SendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete)) + if err != nil { + return handleNotFoundError(err, d, "CapacityCommitment") + } + + log.Printf("[DEBUG] Finished deleting CapacityCommitment %q: %#v", d.Id(), res) + return nil +} + +func resourceBigqueryReservationCapacityCommitmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + + config := meta.(*Config) + + // current import_formats can't import fields with forward slashes in their value + if err := parseImportId([]string{"(?P[^ ]+) (?P[^ ]+)", "(?P[^ ]+)"}, d, config); err != nil { + return nil, err + } + + return []*schema.ResourceData{d}, nil +} + +func flattenBigqueryReservationCapacityCommitmentName(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenBigqueryReservationCapacityCommitmentSlotCount(v interface{}, d *schema.ResourceData, config *Config) interface{} { + // Handles the string fixed64 format + if strVal, ok := v.(string); ok { + if intVal, err := StringToFixed64(strVal); err == nil { + return intVal + } + } + + // number values are represented as float64 + if floatVal, ok := v.(float64); ok { + intVal := int(floatVal) + return intVal + } + + return v // let terraform core handle it otherwise +} + +func flattenBigqueryReservationCapacityCommitmentPlan(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenBigqueryReservationCapacityCommitmentState(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenBigqueryReservationCapacityCommitmentCommitmentStartTime(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenBigqueryReservationCapacityCommitmentCommitmentEndTime(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenBigqueryReservationCapacityCommitmentRenewalPlan(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func expandBigqueryReservationCapacityCommitmentSlotCount(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandBigqueryReservationCapacityCommitmentPlan(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandBigqueryReservationCapacityCommitmentRenewalPlan(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google-beta/resource_bigquery_capacity_commitment_generated_test.go b/google-beta/resource_bigquery_capacity_commitment_generated_test.go new file mode 100644 index 0000000000..23fcbc9bb9 --- /dev/null +++ b/google-beta/resource_bigquery_capacity_commitment_generated_test.go @@ -0,0 +1,103 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" +) + +func TestAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + "time": {}, + }, + CheckDestroy: testAccCheckBigqueryReservationCapacityCommitmentDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentBasicExample(context), + }, + { + ResourceName: "google_bigquery_capacity_commitment.commitment", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"capacity_commitment_id", "location", "enforce_single_admin_project_per_org"}, + }, + }, + }) +} + +func testAccBigqueryReservationCapacityCommitment_bigqueryReservationCapacityCommitmentBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_bigquery_capacity_commitment" "commitment" { + location = "us-west1" + slot_count = 100 + plan = "FLEX" +} + +resource "time_sleep" "wait_61_seconds" { + depends_on = [google_bigquery_capacity_commitment.commitment] + + # Only needed for CI tests to be able to tear down the commitment once it's expired + create_duration = "61s" +} +`, context) +} + +func testAccCheckBigqueryReservationCapacityCommitmentDestroyProducer(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_bigquery_capacity_commitment" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{BigqueryReservationBasePath}}{{name}}") + if err != nil { + return err + } + + billingProject := "" + + if config.BillingProject != "" { + billingProject = config.BillingProject + } + + _, err = SendRequest(config, "GET", billingProject, url, config.UserAgent, nil) + if err == nil { + return fmt.Errorf("BigqueryReservationCapacityCommitment still exists at %s", url) + } + } + + return nil + } +} diff --git a/google-beta/resource_bigquery_capacity_commitment_sweeper_test.go b/google-beta/resource_bigquery_capacity_commitment_sweeper_test.go new file mode 100644 index 0000000000..a32eb73f1d --- /dev/null +++ b/google-beta/resource_bigquery_capacity_commitment_sweeper_test.go @@ -0,0 +1,124 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func init() { + resource.AddTestSweepers("BigqueryReservationCapacityCommitment", &resource.Sweeper{ + Name: "BigqueryReservationCapacityCommitment", + F: testSweepBigqueryReservationCapacityCommitment, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepBigqueryReservationCapacityCommitment(region string) error { + resourceName := "BigqueryReservationCapacityCommitment" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sharedConfigForRegion(region) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) + return err + } + + err = config.LoadAndValidate(context.Background()) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) + return err + } + + t := &testing.T{} + billingId := getTestBillingAccountFromEnv(t) + + // Setup variables to replace in list template + d := &ResourceDataMock{ + FieldsInSchema: map[string]interface{}{ + "project": config.Project, + "region": region, + "location": region, + "zone": "-", + "billing_account": billingId, + }, + } + + listTemplate := strings.Split("https://bigqueryreservation.googleapis.com/v1/projects/{{project}}/locations/{{location}}/capacityCommitments", "?")[0] + listUrl, err := replaceVars(d, config, listTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) + return nil + } + + res, err := SendRequest(config, "GET", config.Project, listUrl, config.UserAgent, nil) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) + return nil + } + + resourceList, ok := res["capacityCommitments"] + if !ok { + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") + return nil + } + + rl := resourceList.([]interface{}) + + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) + // Keep count of items that aren't sweepable for logging. + nonPrefixCount := 0 + for _, ri := range rl { + obj := ri.(map[string]interface{}) + if obj["name"] == nil { + log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) + return nil + } + + name := GetResourceNameFromSelfLink(obj["name"].(string)) + // Skip resources that shouldn't be sweeped + if !isSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://bigqueryreservation.googleapis.com/v1/{{name}}" + deleteUrl, err := replaceVars(d, config, deleteTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) + return nil + } + deleteUrl = deleteUrl + name + + // Don't wait on operations as we may have a lot to delete + _, err = SendRequest(config, "DELETE", config.Project, deleteUrl, config.UserAgent, nil) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) + } else { + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) + } + } + + if nonPrefixCount > 0 { + log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) + } + + return nil +} diff --git a/website/docs/r/bigquery_capacity_commitment.html.markdown b/website/docs/r/bigquery_capacity_commitment.html.markdown new file mode 100644 index 0000000000..dfdcd63aaa --- /dev/null +++ b/website/docs/r/bigquery_capacity_commitment.html.markdown @@ -0,0 +1,126 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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. +# +# ---------------------------------------------------------------------------- +subcategory: "BigQuery Reservation" +description: |- + Capacity commitment is a way to purchase compute capacity for BigQuery jobs (in the form of slots) with some committed period of usage. +--- + +# google\_bigquery\_capacity\_commitment + +Capacity commitment is a way to purchase compute capacity for BigQuery jobs (in the form of slots) with some committed period of usage. Annual commitments renew by default. Commitments can be removed after their commitment end time passes. + +In order to remove annual commitment, its plan needs to be changed to monthly or flex first. + + +To get more information about CapacityCommitment, see: + +* [API documentation](https://cloud.google.com/bigquery/docs/reference/reservations/rest/v1/projects.locations.capacityCommitments) +* How-to Guides + * [Introduction to Reservations](https://cloud.google.com/bigquery/docs/reservations-intro) + +## Example Usage - Bigquery Reservation Capacity Commitment Docs + + +```hcl +resource "google_bigquery_capacity_commitment" "example" { + capacity_commitment_id = "example-commitment" + + location = "us-west1" + slot_count = 100 + plan = "FLEX" +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `slot_count` - + (Required) + Number of slots in this commitment. + +* `plan` - + (Required) + Capacity commitment plan. Valid values are FLEX, TRIAL, MONTHLY, ANNUAL + + +- - - + + +* `renewal_plan` - + (Optional) + The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable for ANNUAL and TRIAL commitments. + +* `capacity_commitment_id` - + (Optional) + The optional capacity commitment ID. Capacity commitment name will be generated automatically if this field is + empty. This field must only contain lower case alphanumeric characters or dashes. The first and last character + cannot be a dash. Max length is 64 characters. NOTE: this ID won't be kept if the capacity commitment is split + or merged. + +* `location` - + (Optional) + The geographic location where the transfer config should reside. + Examples: US, EU, asia-northeast1. The default value is US. + +* `enforce_single_admin_project_per_org` - + (Optional) + If true, fail the request if another project in the organization has a capacity commitment. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `{{name}}` + +* `name` - + The resource name of the capacity commitment, e.g., projects/myproject/locations/US/capacityCommitments/123 + +* `state` - + State of the commitment + +* `commitment_start_time` - + The start of the current commitment period. It is applicable only for ACTIVE capacity commitments. + +* `commitment_end_time` - + The start of the current commitment period. It is applicable only for ACTIVE capacity commitments. + + +## Timeouts + +This resource provides the following +[Timeouts](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts) configuration options: + +- `create` - Default is 20 minutes. +- `update` - Default is 20 minutes. +- `delete` - Default is 20 minutes. + +## Import + + +CapacityCommitment can be imported using any of these accepted formats: + +``` +$ terraform import google_bigquery_capacity_commitment.default {{name}} +``` + +## User Project Overrides + +This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override).