From 970e115d166a0693542418b5a3ca7141a2b7cd0f Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Tue, 6 Apr 2021 17:44:08 +0000 Subject: [PATCH] Apigee environment group attachment (#4641) Signed-off-by: Modular Magician --- .changelog/4641.txt | 3 + google/provider.go | 5 +- google/resource_apigee_envgroup_attachment.go | 237 ++++++++++++++++++ ...igee_envgroup_attachment_generated_test.go | 164 ++++++++++++ .../apigee_envgroup_attachment.html.markdown | 156 ++++++++++++ website/google.erb | 4 + 6 files changed, 567 insertions(+), 2 deletions(-) create mode 100644 .changelog/4641.txt create mode 100644 google/resource_apigee_envgroup_attachment.go create mode 100644 google/resource_apigee_envgroup_attachment_generated_test.go create mode 100644 website/docs/r/apigee_envgroup_attachment.html.markdown diff --git a/.changelog/4641.txt b/.changelog/4641.txt new file mode 100644 index 00000000000..8d86a9834b3 --- /dev/null +++ b/.changelog/4641.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +google_apigee_envgroup_attachment +``` diff --git a/google/provider.go b/google/provider.go index e97413bb5e4..2e988aac2e8 100644 --- a/google/provider.go +++ b/google/provider.go @@ -726,9 +726,9 @@ func Provider() *schema.Provider { return provider } -// Generated resources: 188 +// Generated resources: 189 // Generated IAM resources: 81 -// Total generated resources: 269 +// Total generated resources: 270 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -755,6 +755,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_apigee_environment": resourceApigeeEnvironment(), "google_apigee_envgroup": resourceApigeeEnvgroup(), "google_apigee_instance_attachment": resourceApigeeInstanceAttachment(), + "google_apigee_envgroup_attachment": resourceApigeeEnvgroupAttachment(), "google_app_engine_domain_mapping": resourceAppEngineDomainMapping(), "google_app_engine_firewall_rule": resourceAppEngineFirewallRule(), "google_app_engine_standard_app_version": resourceAppEngineStandardAppVersion(), diff --git a/google/resource_apigee_envgroup_attachment.go b/google/resource_apigee_envgroup_attachment.go new file mode 100644 index 00000000000..b478c6461a1 --- /dev/null +++ b/google/resource_apigee_envgroup_attachment.go @@ -0,0 +1,237 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// 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" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceApigeeEnvgroupAttachment() *schema.Resource { + return &schema.Resource{ + Create: resourceApigeeEnvgroupAttachmentCreate, + Read: resourceApigeeEnvgroupAttachmentRead, + Delete: resourceApigeeEnvgroupAttachmentDelete, + + Importer: &schema.ResourceImporter{ + State: resourceApigeeEnvgroupAttachmentImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(30 * time.Minute), + Delete: schema.DefaultTimeout(30 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "envgroup_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The Apigee environment group associated with the Apigee environment, +in the format 'organizations/{{org_name}}/envgroups/{{envgroup_name}}'.`, + }, + "environment": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The resource ID of the environment.`, + }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: `The name of the newly created attachment (output parameter).`, + }, + }, + UseJSONNumber: true, + } +} + +func resourceApigeeEnvgroupAttachmentCreate(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{}) + environmentProp, err := expandApigeeEnvgroupAttachmentEnvironment(d.Get("environment"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("environment"); !isEmptyValue(reflect.ValueOf(environmentProp)) && (ok || !reflect.DeepEqual(v, environmentProp)) { + obj["environment"] = environmentProp + } + + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{envgroup_id}}/attachments") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new EnvgroupAttachment: %#v", obj) + billingProject := "" + + // 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 EnvgroupAttachment: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "{{envgroup_id}}/attachments/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + // Use the resource in the operation response to populate + // identity fields and d.Id() before read + var opRes map[string]interface{} + err = apigeeOperationWaitTimeWithResponse( + config, res, &opRes, "Creating EnvgroupAttachment", userAgent, + d.Timeout(schema.TimeoutCreate)) + if err != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create EnvgroupAttachment: %s", err) + } + + if err := d.Set("name", flattenApigeeEnvgroupAttachmentName(opRes["name"], d, config)); err != nil { + return err + } + + // This may have caused the ID to update - update it if so. + id, err = replaceVars(d, config, "{{envgroup_id}}/attachments/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating EnvgroupAttachment %q: %#v", d.Id(), res) + + return resourceApigeeEnvgroupAttachmentRead(d, meta) +} + +func resourceApigeeEnvgroupAttachmentRead(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, "{{ApigeeBasePath}}{{envgroup_id}}/attachments/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + // 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("ApigeeEnvgroupAttachment %q", d.Id())) + } + + if err := d.Set("environment", flattenApigeeEnvgroupAttachmentEnvironment(res["environment"], d, config)); err != nil { + return fmt.Errorf("Error reading EnvgroupAttachment: %s", err) + } + if err := d.Set("name", flattenApigeeEnvgroupAttachmentName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading EnvgroupAttachment: %s", err) + } + + return nil +} + +func resourceApigeeEnvgroupAttachmentDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + billingProject := "" + + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{envgroup_id}}/attachments/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting EnvgroupAttachment %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, "EnvgroupAttachment") + } + + err = apigeeOperationWaitTime( + config, res, "Deleting EnvgroupAttachment", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting EnvgroupAttachment %q: %#v", d.Id(), res) + return nil +} + +func resourceApigeeEnvgroupAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + + // current import_formats cannot import fields with forward slashes in their value + if err := parseImportId([]string{ + "(?P.+)/attachments/(?P.+)", + "(?P.+)/(?P.+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{envgroup_id}}/attachments/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenApigeeEnvgroupAttachmentEnvironment(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenApigeeEnvgroupAttachmentName(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func expandApigeeEnvgroupAttachmentEnvironment(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google/resource_apigee_envgroup_attachment_generated_test.go b/google/resource_apigee_envgroup_attachment_generated_test.go new file mode 100644 index 00000000000..98d2496ffdc --- /dev/null +++ b/google/resource_apigee_envgroup_attachment_generated_test.go @@ -0,0 +1,164 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// 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 TestAccApigeeEnvgroupAttachment_apigeeEnvironmentGroupAttachmentBasicTestExample(t *testing.T) { + skipIfVcr(t) + t.Parallel() + + context := map[string]interface{}{ + "org_id": getTestOrgFromEnv(t), + "billing_account": getTestBillingAccountFromEnv(t), + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + CheckDestroy: testAccCheckApigeeEnvgroupAttachmentDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccApigeeEnvgroupAttachment_apigeeEnvironmentGroupAttachmentBasicTestExample(context), + }, + { + ResourceName: "google_apigee_envgroup_attachment.apigee_environment_group_attachment", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"envgroup_id"}, + }, + }, + }) +} + +func testAccApigeeEnvgroupAttachment_apigeeEnvironmentGroupAttachmentBasicTestExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_project" "project" { + project_id = "tf-test%{random_suffix}" + name = "tf-test%{random_suffix}" + org_id = "%{org_id}" + billing_account = "%{billing_account}" +} + +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" +} + +resource "google_project_service" "servicenetworking" { + project = google_project.project.project_id + service = "servicenetworking.googleapis.com" +} + +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 + depends_on = [ + google_service_networking_connection.apigee_vpc_connection, + google_project_service.apigee, + ] +} + +resource "google_apigee_envgroup" "apigee_envgroup" { + org_id = google_apigee_organization.apigee_org.id + name = "tf-test%{random_suffix}" + hostnames = ["abc.foo.com"] +} + +resource "google_apigee_environment" "apigee_env" { + org_id = google_apigee_organization.apigee_org.id + name = "tf-test%{random_suffix}" + description = "Apigee Environment" + display_name = "tf-test%{random_suffix}" +} + +resource "google_apigee_envgroup_attachment" "apigee_environment_group_attachment" { + envgroup_id = google_apigee_envgroup.apigee_envgroup.id + environment = google_apigee_environment.apigee_env.name +} +`, context) +} + +func testAccCheckApigeeEnvgroupAttachmentDestroyProducer(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_apigee_envgroup_attachment" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{ApigeeBasePath}}{{envgroup_id}}/attachments/{{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("ApigeeEnvgroupAttachment still exists at %s", url) + } + } + + return nil + } +} diff --git a/website/docs/r/apigee_envgroup_attachment.html.markdown b/website/docs/r/apigee_envgroup_attachment.html.markdown new file mode 100644 index 00000000000..02cab3fd258 --- /dev/null +++ b/website/docs/r/apigee_envgroup_attachment.html.markdown @@ -0,0 +1,156 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# 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" +layout: "google" +page_title: "Google: google_apigee_envgroup_attachment" +sidebar_current: "docs-google-apigee-envgroup-attachment" +description: |- + An `Environment Group attachment` in Apigee. +--- + +# google\_apigee\_envgroup\_attachment + +An `Environment Group attachment` in Apigee. + + +To get more information about EnvgroupAttachment, see: + +* [API documentation](https://cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.envgroups.attachments/create) +* How-to Guides + * [Creating an environment](https://cloud.google.com/apigee/docs/api-platform/get-started/create-environment) + +## Example Usage - Apigee Environment Group Attachment Basic + + +```hcl +resource "google_project" "project" { + project_id = "tf-test%{random_suffix}" + name = "tf-test%{random_suffix}" + org_id = "" + billing_account = "" +} + +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" +} + +resource "google_project_service" "servicenetworking" { + project = google_project.project.project_id + service = "servicenetworking.googleapis.com" +} + +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 + depends_on = [ + google_service_networking_connection.apigee_vpc_connection, + google_project_service.apigee, + ] +} + +resource "google_apigee_envgroup" "apigee_envgroup" { + org_id = google_apigee_organization.apigee_org.id + name = "tf-test%{random_suffix}" + hostnames = ["abc.foo.com"] +} + +resource "google_apigee_environment" "apigee_env" { + org_id = google_apigee_organization.apigee_org.id + name = "tf-test%{random_suffix}" + description = "Apigee Environment" + display_name = "tf-test%{random_suffix}" +} + +resource "google_apigee_envgroup_attachment" "" { + envgroup_id = google_apigee_envgroup.apigee_envgroup.name + environment = google_apigee_environment.apigee_env.name +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `environment` - + (Required) + The resource ID of the environment. + +* `envgroup_id` - + (Required) + The Apigee environment group associated with the Apigee environment, + in the format `organizations/{{org_name}}/envgroups/{{envgroup_name}}`. + + +- - - + + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `{{envgroup_id}}/attachments/{{name}}` + +* `name` - + The name of the newly created attachment (output parameter). + + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 30 minutes. +- `delete` - Default is 30 minutes. + +## Import + + +EnvgroupAttachment can be imported using any of these accepted formats: + +``` +$ terraform import google_apigee_envgroup_attachment.default {{envgroup_id}}/attachments/{{name}} +$ terraform import google_apigee_envgroup_attachment.default {{envgroup_id}}/{{name}} +``` diff --git a/website/google.erb b/website/google.erb index 8aea212a8bf..360445ce1ac 100644 --- a/website/google.erb +++ b/website/google.erb @@ -174,6 +174,10 @@ google_apigee_envgroup +
  • + google_apigee_envgroup_attachment +
  • +
  • google_apigee_environment