-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new resource Quota Adjuster Settings Manager for Cloud Quotas bet…
…a (#12359) (#9005) [upstream:94e30b37c66614a95e70a89ba03e6a45b742c5d6] Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
a81f64c
commit 599c665
Showing
8 changed files
with
486 additions
and
4 deletions.
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,3 @@ | ||
```release-note:new-resource | ||
`google_cloud_quotas_quota_adjuster_settings` | ||
``` |
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
274 changes: 274 additions & 0 deletions
274
google-beta/services/cloudquotas/resource_cloud_quotas_quota_adjuster_settings.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,274 @@ | ||
// 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 cloudquotas | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify" | ||
) | ||
|
||
func ResourceCloudQuotasQuotaAdjusterSettings() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudQuotasQuotaAdjusterSettingsCreate, | ||
Read: resourceCloudQuotasQuotaAdjusterSettingsRead, | ||
Update: resourceCloudQuotasQuotaAdjusterSettingsUpdate, | ||
Delete: resourceCloudQuotasQuotaAdjusterSettingsDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: resourceCloudQuotasQuotaAdjusterSettingsImport, | ||
}, | ||
|
||
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{ | ||
"enablement": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: verify.ValidateEnum([]string{"ENABLED", "DISABLED"}), | ||
Description: `Required. The configured value of the enablement at the given resource. Possible values: ["ENABLED", "DISABLED"]`, | ||
}, | ||
"parent": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: verify.ValidateRegexp(`^(projects|folders|organizations)/([^/]+)$`), | ||
Description: `The parent of the quota preference. Allowed parents are "projects/[project-id / number]" or "folders/[folder-id / number]" or "organizations/[org-id / number]".`, | ||
}, | ||
"effective_container": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Fields to capture the hierarchy enablement. | ||
The container (org/folder/project) that determines if the quota adjuster is set for this project/folder/org. We use the nearest-ancestor to determine the effective container. | ||
The nearest ancestor (including this container) with 'enabled' set (either true or false) will be returned.`, | ||
}, | ||
"effective_enablement": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Based on the effective container's setting above, determines Whether this container has the quota adjuster enabled.`, | ||
}, | ||
}, | ||
UseJSONNumber: true, | ||
} | ||
} | ||
|
||
func resourceCloudQuotasQuotaAdjusterSettingsCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
obj := make(map[string]interface{}) | ||
enablementProp, err := expandCloudQuotasQuotaAdjusterSettingsEnablement(d.Get("enablement"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("enablement"); !tpgresource.IsEmptyValue(reflect.ValueOf(enablementProp)) && (ok || !reflect.DeepEqual(v, enablementProp)) { | ||
obj["enablement"] = enablementProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/quotaAdjusterSettings") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Creating new QuotaAdjusterSettings: %#v", obj) | ||
billingProject := "" | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
headers := make(http.Header) | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "PATCH", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutCreate), | ||
Headers: headers, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error creating QuotaAdjusterSettings: %s", err) | ||
} | ||
|
||
// Store the ID now | ||
id, err := tpgresource.ReplaceVars(d, config, "{{parent}}/locations/global/quotaAdjusterSettings") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
log.Printf("[DEBUG] Finished creating QuotaAdjusterSettings %q: %#v", d.Id(), res) | ||
|
||
return resourceCloudQuotasQuotaAdjusterSettingsRead(d, meta) | ||
} | ||
|
||
func resourceCloudQuotasQuotaAdjusterSettingsRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/quotaAdjusterSettings") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
headers := make(http.Header) | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "GET", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Headers: headers, | ||
}) | ||
if err != nil { | ||
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("CloudQuotasQuotaAdjusterSettings %q", d.Id())) | ||
} | ||
|
||
if err := d.Set("enablement", flattenCloudQuotasQuotaAdjusterSettingsEnablement(res["enablement"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading QuotaAdjusterSettings: %s", err) | ||
} | ||
if err := d.Set("effective_container", flattenCloudQuotasQuotaAdjusterSettingsEffectiveContainer(res["effectiveContainer"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading QuotaAdjusterSettings: %s", err) | ||
} | ||
if err := d.Set("effective_enablement", flattenCloudQuotasQuotaAdjusterSettingsEffectiveEnablement(res["effectiveEnablement"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading QuotaAdjusterSettings: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudQuotasQuotaAdjusterSettingsUpdate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
obj := make(map[string]interface{}) | ||
enablementProp, err := expandCloudQuotasQuotaAdjusterSettingsEnablement(d.Get("enablement"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("enablement"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, enablementProp)) { | ||
obj["enablement"] = enablementProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/quotaAdjusterSettings") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Updating QuotaAdjusterSettings %q: %#v", d.Id(), obj) | ||
headers := make(http.Header) | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "PATCH", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
Headers: headers, | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error updating QuotaAdjusterSettings %q: %s", d.Id(), err) | ||
} else { | ||
log.Printf("[DEBUG] Finished updating QuotaAdjusterSettings %q: %#v", d.Id(), res) | ||
} | ||
|
||
return resourceCloudQuotasQuotaAdjusterSettingsRead(d, meta) | ||
} | ||
|
||
func resourceCloudQuotasQuotaAdjusterSettingsDelete(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[WARNING] CloudQuotas QuotaAdjusterSettings resources"+ | ||
" cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+ | ||
" state, but will still be present on Google Cloud.", d.Id()) | ||
d.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudQuotasQuotaAdjusterSettingsImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
config := meta.(*transport_tpg.Config) | ||
if err := tpgresource.ParseImportId([]string{ | ||
"^(?P<parent>.+)/locations/global/quotaAdjusterSettings$", | ||
}, d, config); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Replace import id for the resource id | ||
id, err := tpgresource.ReplaceVars(d, config, "{{parent}}/locations/global/quotaAdjusterSettings") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func flattenCloudQuotasQuotaAdjusterSettingsEnablement(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenCloudQuotasQuotaAdjusterSettingsEffectiveContainer(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenCloudQuotasQuotaAdjusterSettingsEffectiveEnablement(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func expandCloudQuotasQuotaAdjusterSettingsEnablement(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} |
5 changes: 5 additions & 0 deletions
5
...ta/services/cloudquotas/resource_cloud_quotas_quota_adjuster_settings_generated_meta.yaml
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,5 @@ | ||
resource: 'google_cloud_quotas_quota_adjuster_settings' | ||
generation_type: 'mmv1' | ||
api_service_name: 'cloudquotas.googleapis.com' | ||
api_version: 'v1beta' | ||
api_resource_type_kind: 'QuotaAdjusterSettings' |
91 changes: 91 additions & 0 deletions
91
google-beta/services/cloudquotas/resource_cloud_quotas_quota_adjuster_settings_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,91 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package cloudquotas_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
|
||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" | ||
) | ||
|
||
func TestAccCloudQuotasQuotaAdjusterSettings_cloudQuotasQuotaAdjusterSettingsUpdate(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"enablement": "ENABLED", | ||
"org_id": envvar.GetTestOrgFromEnv(t), | ||
"billing_account": envvar.GetTestBillingAccountFromEnv(t), | ||
"updated_enablement": "DISABLED", | ||
"random_suffix": acctest.RandString(t, 10), | ||
} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCloudQuotasQuotaAdjusterSettings_basic(context), | ||
}, | ||
{ | ||
ResourceName: "google_cloud_quotas_quota_adjuster_settings.adjuster_settings", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"parent"}, | ||
}, | ||
{ | ||
Config: testAccCloudQuotasQuotaAdjusterSettings_updateEnabelement(context), | ||
}, | ||
{ | ||
ResourceName: "google_cloud_quotas_quota_adjuster_settings.adjuster_settings", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"parent"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCloudQuotasQuotaAdjusterSettings_basic(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
data "google_project" "project" { | ||
provider = google-beta | ||
} | ||
resource "google_project_service" "cloudquotas" { | ||
provider = google-beta | ||
project = data.google_project.project.project_id | ||
service = "cloudquotas.googleapis.com" | ||
} | ||
resource "google_cloud_quotas_quota_adjuster_settings" "adjuster_settings" { | ||
provider = google-beta | ||
parent = "projects/${data.google_project.project.number}" | ||
enablement = "%{enablement}" | ||
depends_on = [google_project_service.cloudquotas] | ||
} | ||
`, context) | ||
} | ||
|
||
func testAccCloudQuotasQuotaAdjusterSettings_updateEnabelement(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
data "google_project" "project" { | ||
provider = google-beta | ||
} | ||
resource "google_project_service" "cloudquotas" { | ||
provider = google-beta | ||
project = data.google_project.project.project_id | ||
service = "cloudquotas.googleapis.com" | ||
} | ||
resource "google_cloud_quotas_quota_adjuster_settings" "adjuster_settings" { | ||
provider = google-beta | ||
parent = "projects/${data.google_project.project.number}" | ||
enablement = "%{updated_enablement}" | ||
depends_on = [google_project_service.cloudquotas] | ||
} | ||
`, context) | ||
} |
Oops, something went wrong.