diff --git a/.changelog/12616.txt b/.changelog/12616.txt new file mode 100644 index 0000000000..74b09dbf11 --- /dev/null +++ b/.changelog/12616.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +`google_apigee_environment_addons_config` +``` \ No newline at end of file diff --git a/google-beta/provider/provider_mmv1_resources.go b/google-beta/provider/provider_mmv1_resources.go index e7d2f77b3e..b8ef8c5c52 100644 --- a/google-beta/provider/provider_mmv1_resources.go +++ b/google-beta/provider/provider_mmv1_resources.go @@ -513,9 +513,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{ } // Resources -// Generated resources: 578 +// Generated resources: 579 // Generated IAM resources: 294 -// Total generated resources: 872 +// Total generated resources: 873 var generatedResources = map[string]*schema.Resource{ "google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(), "google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(), @@ -570,6 +570,7 @@ var generatedResources = map[string]*schema.Resource{ "google_apigee_environment_iam_binding": tpgiamresource.ResourceIamBinding(apigee.ApigeeEnvironmentIamSchema, apigee.ApigeeEnvironmentIamUpdaterProducer, apigee.ApigeeEnvironmentIdParseFunc), "google_apigee_environment_iam_member": tpgiamresource.ResourceIamMember(apigee.ApigeeEnvironmentIamSchema, apigee.ApigeeEnvironmentIamUpdaterProducer, apigee.ApigeeEnvironmentIdParseFunc), "google_apigee_environment_iam_policy": tpgiamresource.ResourceIamPolicy(apigee.ApigeeEnvironmentIamSchema, apigee.ApigeeEnvironmentIamUpdaterProducer, apigee.ApigeeEnvironmentIdParseFunc), + "google_apigee_environment_addons_config": apigee.ResourceApigeeEnvironmentAddonsConfig(), "google_apigee_environment_keyvaluemaps": apigee.ResourceApigeeEnvironmentKeyvaluemaps(), "google_apigee_environment_keyvaluemaps_entries": apigee.ResourceApigeeEnvironmentKeyvaluemapsEntries(), "google_apigee_instance": apigee.ResourceApigeeInstance(), diff --git a/google-beta/services/apigee/resource_apigee_environment_addons_config.go b/google-beta/services/apigee/resource_apigee_environment_addons_config.go new file mode 100644 index 0000000000..6725756183 --- /dev/null +++ b/google-beta/services/apigee/resource_apigee_environment_addons_config.go @@ -0,0 +1,280 @@ +// 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 apigee + +import ( + "fmt" + "log" + "net/http" + "reflect" + "strings" + "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" +) + +func ResourceApigeeEnvironmentAddonsConfig() *schema.Resource { + return &schema.Resource{ + Create: resourceApigeeEnvironmentAddonsConfigCreate, + Read: resourceApigeeEnvironmentAddonsConfigRead, + Update: resourceApigeeEnvironmentAddonsConfigUpdate, + Delete: resourceApigeeEnvironmentAddonsConfigDelete, + + Importer: &schema.ResourceImporter{ + State: resourceApigeeEnvironmentAddonsConfigImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(0 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "env_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The Apigee environment group associated with the Apigee environment, +in the format 'organizations/{{org_name}}/environments/{{env_name}}'.`, + }, + "analytics_enabled": { + Type: schema.TypeBool, + Optional: true, + Description: `Flag to enable/disable Analytics.`, + }, + }, + UseJSONNumber: true, + } +} + +func resourceApigeeEnvironmentAddonsConfigCreate(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{}) + analyticsEnabledProp, err := expandApigeeEnvironmentAddonsConfigAnalyticsEnabled(d.Get("analytics_enabled"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("analytics_enabled"); !tpgresource.IsEmptyValue(reflect.ValueOf(analyticsEnabledProp)) && (ok || !reflect.DeepEqual(v, analyticsEnabledProp)) { + obj["analyticsEnabled"] = analyticsEnabledProp + } + + url, err := tpgresource.ReplaceVars(d, config, "{{ApigeeBasePath}}{{env_id}}/addonsConfig:setAddonEnablement") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new EnvironmentAddonsConfig: %#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: "POST", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutCreate), + Headers: headers, + }) + if err != nil { + return fmt.Errorf("Error creating EnvironmentAddonsConfig: %s", err) + } + + // Store the ID now + id, err := tpgresource.ReplaceVars(d, config, "{{env_id}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + err = ApigeeOperationWaitTime( + config, res, "Creating EnvironmentAddonsConfig", userAgent, + d.Timeout(schema.TimeoutCreate)) + + if err != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create EnvironmentAddonsConfig: %s", err) + } + + log.Printf("[DEBUG] Finished creating EnvironmentAddonsConfig %q: %#v", d.Id(), res) + + return resourceApigeeEnvironmentAddonsConfigRead(d, meta) +} + +func resourceApigeeEnvironmentAddonsConfigRead(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, "{{ApigeeBasePath}}{{env_id}}/addonsConfig") + 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("ApigeeEnvironmentAddonsConfig %q", d.Id())) + } + + res, err = resourceApigeeEnvironmentAddonsConfigDecoder(d, meta, res) + if err != nil { + return err + } + + if res == nil { + // Decoding the object has resulted in it being gone. It may be marked deleted + log.Printf("[DEBUG] Removing ApigeeEnvironmentAddonsConfig because it no longer exists.") + d.SetId("") + return nil + } + + if err := d.Set("analytics_enabled", flattenApigeeEnvironmentAddonsConfigAnalyticsEnabled(res["analyticsEnabled"], d, config)); err != nil { + return fmt.Errorf("Error reading EnvironmentAddonsConfig: %s", err) + } + + return nil +} + +func resourceApigeeEnvironmentAddonsConfigUpdate(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{}) + analyticsEnabledProp, err := expandApigeeEnvironmentAddonsConfigAnalyticsEnabled(d.Get("analytics_enabled"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("analytics_enabled"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, analyticsEnabledProp)) { + obj["analyticsEnabled"] = analyticsEnabledProp + } + + url, err := tpgresource.ReplaceVars(d, config, "{{ApigeeBasePath}}{{env_id}}/addonsConfig:setAddonEnablement") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating EnvironmentAddonsConfig %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: "POST", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), + Headers: headers, + }) + + if err != nil { + return fmt.Errorf("Error updating EnvironmentAddonsConfig %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating EnvironmentAddonsConfig %q: %#v", d.Id(), res) + } + + err = ApigeeOperationWaitTime( + config, res, "Updating EnvironmentAddonsConfig", userAgent, + d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return err + } + + return resourceApigeeEnvironmentAddonsConfigRead(d, meta) +} + +func resourceApigeeEnvironmentAddonsConfigDelete(d *schema.ResourceData, meta interface{}) error { + log.Printf("[WARNING] Apigee EnvironmentAddonsConfig 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 resourceApigeeEnvironmentAddonsConfigImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*transport_tpg.Config) + + // current import_formats cannot import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + id := d.Get("env_id").(string) + nameParts := strings.Split(id, "/") + if len(nameParts) != 4 { + return nil, fmt.Errorf("env is expected to have shape organizations/{{org_id}}/environments/{{env}}, got %s instead", id) + } + d.SetId(id) + return []*schema.ResourceData{d}, nil +} + +func flattenApigeeEnvironmentAddonsConfigAnalyticsEnabled(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func expandApigeeEnvironmentAddonsConfigAnalyticsEnabled(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func resourceApigeeEnvironmentAddonsConfigDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) { + res["analyticsEnabled"] = res["analyticsConfig"].(map[string]interface{})["enabled"] + return res, nil +} diff --git a/google-beta/services/apigee/resource_apigee_environment_addons_config_generated_meta.yaml b/google-beta/services/apigee/resource_apigee_environment_addons_config_generated_meta.yaml new file mode 100644 index 0000000000..586d720bc3 --- /dev/null +++ b/google-beta/services/apigee/resource_apigee_environment_addons_config_generated_meta.yaml @@ -0,0 +1,5 @@ +resource: 'google_apigee_environment_addons_config' +generation_type: 'mmv1' +api_service_name: 'apigee.googleapis.com' +api_version: 'v1' +api_resource_type_kind: 'AddonsConfig' diff --git a/google-beta/services/apigee/resource_apigee_environment_addons_config_generated_test.go b/google-beta/services/apigee/resource_apigee_environment_addons_config_generated_test.go new file mode 100644 index 0000000000..63e2edc2c3 --- /dev/null +++ b/google-beta/services/apigee/resource_apigee_environment_addons_config_generated_test.go @@ -0,0 +1,128 @@ +// 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 apigee_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 TestAccApigeeEnvironmentAddonsConfig_apigeeEnvAddonsAnalyticsTestExample(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), + Steps: []resource.TestStep{ + { + Config: testAccApigeeEnvironmentAddonsConfig_apigeeEnvAddonsAnalyticsTestExample(context), + }, + { + ResourceName: "google_apigee_environment_addons_config.apigee_org_addons", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"env_id"}, + }, + }, + }) +} + +func testAccApigeeEnvironmentAddonsConfig_apigeeEnvAddonsAnalyticsTestExample(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}" + billing_account = "%{billing_account}" + deletion_policy = "DELETE" +} + +resource "google_project_service" "apigee" { + project = google_project.project.project_id + service = "apigee.googleapis.com" +} + +resource "google_project_service" "compute" { + project = google_project.project.project_id + service = "compute.googleapis.com" + depends_on = [google_project_service.servicenetworking ] +} + +resource "google_project_service" "servicenetworking" { + project = google_project.project.project_id + service = "servicenetworking.googleapis.com" + depends_on = [google_project_service.apigee] +} + +resource "google_compute_network" "apigee_network" { + name = "apigee-network" + project = google_project.project.project_id + depends_on = [google_project_service.compute] +} + +resource "google_compute_global_address" "apigee_range" { + name = "apigee-range" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 16 + network = google_compute_network.apigee_network.id + project = google_project.project.project_id +} + +resource "google_service_networking_connection" "apigee_vpc_connection" { + network = google_compute_network.apigee_network.id + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.apigee_range.name] + depends_on = [google_project_service.servicenetworking] +} + +resource "google_apigee_organization" "apigee_org" { + analytics_region = "us-central1" + project_id = google_project.project.project_id + authorized_network = google_compute_network.apigee_network.id + billing_type = "PAYG" + depends_on = [ + google_service_networking_connection.apigee_vpc_connection, + google_project_service.apigee + ] +} + +resource "google_apigee_environment" "apigee_org_addons" { + org_id = google_apigee_organization.apigee_org.id + name = "tf-test%{random_suffix}" + type = "COMPREHENSIVE" + description = "Apigee Environment" + display_name = "environment-1" +} + +resource "google_apigee_environment_addons_config" "apigee_org_addons" { + env_id = google_apigee_environment.apigee_org_addons.id + analytics_enabled = true +} +`, context) +} diff --git a/website/docs/r/apigee_environment_addons_config.html.markdown b/website/docs/r/apigee_environment_addons_config.html.markdown new file mode 100644 index 0000000000..519325dead --- /dev/null +++ b/website/docs/r/apigee_environment_addons_config.html.markdown @@ -0,0 +1,112 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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: "Apigee" +description: |- + Enable/Disable add-ons for an Apigee environment. +--- + +# google_apigee_environment_addons_config + +Enable/Disable add-ons for an Apigee environment. + + +To get more information about EnvironmentAddonsConfig, see: + +* [API documentation](https://cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.environments.addonsConfig/setAddonEnablement) +* How-to Guides + * [Enable Analytics Add-On](https://cloud.google.com/apigee/docs/api-platform/reference/manage-analytics-add-on) + +## Example Usage - Apigee Env Addons Enable Analytics + + +```hcl +resource "google_apigee_organization" "apigee_org" { + analytics_region = "us-central1" + project_id = google_project.project.project_id + authorized_network = google_compute_network.apigee_network.id + billing_type = "PAYG" +} + +resource "google_apigee_environment" "" { + org_id = google_apigee_organization.apigee_org.id + name = "tf-test%{random_suffix}" + type = "COMPREHENSIVE" + description = "Apigee Environment" + display_name = "environment-1" +} + +resource "google_apigee_environment_addons_config" "" { + env_id = google_apigee_environment..id + analytics_enabled = true +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `env_id` - + (Required) + The Apigee environment group associated with the Apigee environment, + in the format `organizations/{{org_name}}/environments/{{env_name}}`. + + +- - - + + +* `analytics_enabled` - + (Optional) + Flag to enable/disable Analytics. + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `{{env_id}}` + + +## Timeouts + +This resource provides the following +[Timeouts](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts) configuration options: + +- `create` - Default is 5 minutes. +- `update` - Default is 5 minutes. +- `delete` - Default is 0 minutes. + +## Import + + +EnvironmentAddonsConfig can be imported using any of these accepted formats: + +* `{{env_id}}` + + +In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import EnvironmentAddonsConfig using one of the formats above. For example: + +```tf +import { + id = "{{env_id}}" + to = google_apigee_environment_addons_config.default +} +``` + +When using the [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import), EnvironmentAddonsConfig can be imported using one of the formats above. For example: + +``` +$ terraform import google_apigee_environment_addons_config.default {{env_id}} +```