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

Add new resource Quota Adjuster Settings Manager for Cloud Quotas beta #9005

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/12359.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
`google_cloud_quotas_quota_adjuster_settings`
```
5 changes: 3 additions & 2 deletions google-beta/provider/provider_mmv1_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{
}

// Resources
// Generated resources: 576
// Generated resources: 577
// Generated IAM resources: 294
// Total generated resources: 870
// Total generated resources: 871
var generatedResources = map[string]*schema.Resource{
"google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(),
"google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(),
Expand Down Expand Up @@ -682,6 +682,7 @@ var generatedResources = map[string]*schema.Resource{
"google_cloud_identity_group": cloudidentity.ResourceCloudIdentityGroup(),
"google_cloud_identity_group_membership": cloudidentity.ResourceCloudIdentityGroupMembership(),
"google_cloud_ids_endpoint": cloudids.ResourceCloudIdsEndpoint(),
"google_cloud_quotas_quota_adjuster_settings": cloudquotas.ResourceCloudQuotasQuotaAdjusterSettings(),
"google_cloud_quotas_quota_preference": cloudquotas.ResourceCloudQuotasQuotaPreference(),
"google_cloud_run_domain_mapping": cloudrun.ResourceCloudRunDomainMapping(),
"google_cloud_run_service": cloudrun.ResourceCloudRunService(),
Expand Down
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
}
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'
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)
}
Loading
Loading