diff --git a/.changelog/5717.txt b/.changelog/5717.txt new file mode 100644 index 00000000000..2dd573b16b0 --- /dev/null +++ b/.changelog/5717.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +`google_apigee_endpoint_attachment` +``` diff --git a/google/provider.go b/google/provider.go index 5f596df70b5..8eacf84e67c 100644 --- a/google/provider.go +++ b/google/provider.go @@ -821,9 +821,9 @@ func Provider() *schema.Provider { return provider } -// Generated resources: 216 +// Generated resources: 217 // Generated IAM resources: 96 -// Total generated resources: 312 +// Total generated resources: 313 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -854,6 +854,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_apigee_envgroup": resourceApigeeEnvgroup(), "google_apigee_instance_attachment": resourceApigeeInstanceAttachment(), "google_apigee_envgroup_attachment": resourceApigeeEnvgroupAttachment(), + "google_apigee_endpoint_attachment": resourceApigeeEndpointAttachment(), "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_endpoint_attachment.go b/google/resource_apigee_endpoint_attachment.go new file mode 100644 index 00000000000..7dff464d5dc --- /dev/null +++ b/google/resource_apigee_endpoint_attachment.go @@ -0,0 +1,294 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 resourceApigeeEndpointAttachment() *schema.Resource { + return &schema.Resource{ + Create: resourceApigeeEndpointAttachmentCreate, + Read: resourceApigeeEndpointAttachmentRead, + Delete: resourceApigeeEndpointAttachmentDelete, + + Importer: &schema.ResourceImporter{ + State: resourceApigeeEndpointAttachmentImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(30 * time.Minute), + Delete: schema.DefaultTimeout(30 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "endpoint_attachment_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `ID of the endpoint attachment.`, + }, + "location": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `Location of the endpoint attachment.`, + }, + "org_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The Apigee Organization associated with the Apigee instance, +in the format 'organizations/{{org_name}}'.`, + }, + "service_attachment": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `Format: projects/*/regions/*/serviceAttachments/*`, + }, + "host": { + Type: schema.TypeString, + Computed: true, + Description: `Host that can be used in either HTTP Target Endpoint directly, or as the host in Target Server.`, + }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: `Name of the Endpoint Attachment in the following format: +organizations/{organization}/endpointAttachments/{endpointAttachment}.`, + }, + }, + UseJSONNumber: true, + } +} + +func resourceApigeeEndpointAttachmentCreate(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{}) + locationProp, err := expandApigeeEndpointAttachmentLocation(d.Get("location"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("location"); !isEmptyValue(reflect.ValueOf(locationProp)) && (ok || !reflect.DeepEqual(v, locationProp)) { + obj["location"] = locationProp + } + serviceAttachmentProp, err := expandApigeeEndpointAttachmentServiceAttachment(d.Get("service_attachment"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("service_attachment"); !isEmptyValue(reflect.ValueOf(serviceAttachmentProp)) && (ok || !reflect.DeepEqual(v, serviceAttachmentProp)) { + obj["serviceAttachment"] = serviceAttachmentProp + } + + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{org_id}}/endpointAttachments?endpointAttachmentId={{endpoint_attachment_id}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new EndpointAttachment: %#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 EndpointAttachment: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") + 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 EndpointAttachment", userAgent, + d.Timeout(schema.TimeoutCreate)) + if err != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create EndpointAttachment: %s", err) + } + + if err := d.Set("name", flattenApigeeEndpointAttachmentName(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, "{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating EndpointAttachment %q: %#v", d.Id(), res) + + return resourceApigeeEndpointAttachmentRead(d, meta) +} + +func resourceApigeeEndpointAttachmentRead(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}}{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") + 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("ApigeeEndpointAttachment %q", d.Id())) + } + + if err := d.Set("name", flattenApigeeEndpointAttachmentName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading EndpointAttachment: %s", err) + } + if err := d.Set("location", flattenApigeeEndpointAttachmentLocation(res["location"], d, config)); err != nil { + return fmt.Errorf("Error reading EndpointAttachment: %s", err) + } + if err := d.Set("host", flattenApigeeEndpointAttachmentHost(res["host"], d, config)); err != nil { + return fmt.Errorf("Error reading EndpointAttachment: %s", err) + } + if err := d.Set("service_attachment", flattenApigeeEndpointAttachmentServiceAttachment(res["serviceAttachment"], d, config)); err != nil { + return fmt.Errorf("Error reading EndpointAttachment: %s", err) + } + + return nil +} + +func resourceApigeeEndpointAttachmentDelete(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}}{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting EndpointAttachment %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, "EndpointAttachment") + } + + err = apigeeOperationWaitTime( + config, res, "Deleting EndpointAttachment", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting EndpointAttachment %q: %#v", d.Id(), res) + return nil +} + +func resourceApigeeEndpointAttachmentImport(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.+)"}, d, config); err != nil { + return nil, err + } + + nameParts := strings.Split(d.Get("name").(string), "/") + if len(nameParts) == 4 { + // `organizations/{{org_name}}/endpointAttachment/{{endpoint_attachment_id}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("endpoint_attachment_id", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting endpoint_attachment_id: %s", err) + } + } else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s", + d.Get("name"), + "organizations/{{org_name}}/environments/{{name}}") + } + + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenApigeeEndpointAttachmentName(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenApigeeEndpointAttachmentLocation(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenApigeeEndpointAttachmentHost(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenApigeeEndpointAttachmentServiceAttachment(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func expandApigeeEndpointAttachmentLocation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandApigeeEndpointAttachmentServiceAttachment(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google/resource_apigee_endpoint_attachment_generated_test.go b/google/resource_apigee_endpoint_attachment_generated_test.go new file mode 100644 index 00000000000..f786bd75039 --- /dev/null +++ b/google/resource_apigee_endpoint_attachment_generated_test.go @@ -0,0 +1,250 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 TestAccApigeeEndpointAttachment_apigeeEndpointAttachmentBasicTestExample(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, + CheckDestroy: testAccCheckApigeeEndpointAttachmentDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccApigeeEndpointAttachment_apigeeEndpointAttachmentBasicTestExample(context), + }, + { + ResourceName: "google_apigee_endpoint_attachment.apigee_endpoint_attachment", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"org_id", "endpoint_attachment_id"}, + }, + }, + }) +} + +func testAccApigeeEndpointAttachment_apigeeEndpointAttachmentBasicTestExample(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_compute_address" "psc_ilb_consumer_address" { + name = "psc-ilb-consumer-address" + region = "us-west2" + + subnetwork = "default" + address_type = "INTERNAL" + + project = google_project.project.project_id + depends_on = [google_project_service.compute] +} + +resource "google_compute_forwarding_rule" "psc_ilb_consumer" { + name = "psc-ilb-consumer-forwarding-rule" + region = "us-west2" + + target = google_compute_service_attachment.psc_ilb_service_attachment.id + load_balancing_scheme = "" # need to override EXTERNAL default when target is a service attachment + network = "default" + ip_address = google_compute_address.psc_ilb_consumer_address.id + + project = google_project.project.project_id +} + +resource "google_compute_forwarding_rule" "psc_ilb_target_service" { + name = "producer-forwarding-rule" + region = "us-west2" + + load_balancing_scheme = "INTERNAL" + backend_service = google_compute_region_backend_service.producer_service_backend.id + all_ports = true + network = google_compute_network.psc_ilb_network.name + subnetwork = google_compute_subnetwork.psc_ilb_producer_subnetwork.name + + project = google_project.project.project_id +} + +resource "google_compute_region_backend_service" "producer_service_backend" { + name = "producer-service" + region = "us-west2" + + health_checks = [google_compute_health_check.producer_service_health_check.id] + + project = google_project.project.project_id +} + +resource "google_compute_health_check" "producer_service_health_check" { + name = "producer-service-health-check" + + check_interval_sec = 1 + timeout_sec = 1 + tcp_health_check { + port = "80" + } + + project = google_project.project.project_id + depends_on = [google_project_service.compute] +} + +resource "google_compute_network" "psc_ilb_network" { + name = "psc-ilb-network" + auto_create_subnetworks = false + + project = google_project.project.project_id + depends_on = [google_project_service.compute] +} + +resource "google_compute_subnetwork" "psc_ilb_producer_subnetwork" { + name = "psc-ilb-producer-subnetwork" + region = "us-west2" + + network = google_compute_network.psc_ilb_network.id + ip_cidr_range = "10.0.0.0/16" + + project = google_project.project.project_id +} + +resource "google_compute_subnetwork" "psc_ilb_nat" { + name = "psc-ilb-nat" + region = "us-west2" + + network = google_compute_network.psc_ilb_network.id + purpose = "PRIVATE_SERVICE_CONNECT" + ip_cidr_range = "10.1.0.0/16" + + project = google_project.project.project_id +} + +resource "google_compute_service_attachment" "psc_ilb_service_attachment" { + name = "my-psc-ilb" + region = "us-west2" + description = "A service attachment configured with Terraform" + + enable_proxy_protocol = true + connection_preference = "ACCEPT_AUTOMATIC" + nat_subnets = [google_compute_subnetwork.psc_ilb_nat.id] + target_service = google_compute_forwarding_rule.psc_ilb_target_service.id + + project = google_project.project.project_id +} + +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_endpoint_attachment" "apigee_endpoint_attachment" { + org_id = google_apigee_organization.apigee_org.id + endpoint_attachment_id = "test1" + location = "us-west2" + service_attachment = google_compute_service_attachment.psc_ilb_service_attachment.id +} +`, context) +} + +func testAccCheckApigeeEndpointAttachmentDestroyProducer(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_endpoint_attachment" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{ApigeeBasePath}}{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") + 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("ApigeeEndpointAttachment still exists at %s", url) + } + } + + return nil + } +} diff --git a/website/docs/r/apigee_endpoint_attachment.html.markdown b/website/docs/r/apigee_endpoint_attachment.html.markdown new file mode 100644 index 00000000000..7180460d9fd --- /dev/null +++ b/website/docs/r/apigee_endpoint_attachment.html.markdown @@ -0,0 +1,130 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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" +layout: "google" +page_title: "Google: google_apigee_endpoint_attachment" +sidebar_current: "docs-google-apigee-endpoint-attachment" +description: |- + Apigee Endpoint Attachment. +--- + +# google\_apigee\_endpoint\_attachment + +Apigee Endpoint Attachment. + + +To get more information about EndpointAttachment, see: + +* [API documentation](https://cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.endpointAttachments/create) +* How-to Guides + * [Creating an environment](https://cloud.google.com/apigee/docs/api-platform/get-started/create-environment) + +## Example Usage - Apigee Endpoint Attachment Basic + + +```hcl +data "google_client_config" "current" {} + +resource "google_compute_network" "apigee_network" { + name = "apigee-network" +} + +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 +} + +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] +} + +resource "google_apigee_organization" "apigee_org" { + analytics_region = "us-central1" + project_id = data.google_client_config.current.project + authorized_network = google_compute_network.apigee_network.id + depends_on = [google_service_networking_connection.apigee_vpc_connection] +} + +resource "google_apigee_endpoint_attachment" "apigee_endpoint_attachment" { + org_id = google_apigee_organization.apigee_org.id + endpoint_attachment_id = "test1" + location = "{google_compute_service_attachment location}" + service_attachment = "{google_compute_service_attachment id}" +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `location` - + (Required) + Location of the endpoint attachment. + +* `service_attachment` - + (Required) + Format: projects/*/regions/*/serviceAttachments/* + +* `org_id` - + (Required) + The Apigee Organization associated with the Apigee instance, + in the format `organizations/{{org_name}}`. + +* `endpoint_attachment_id` - + (Required) + ID of the endpoint attachment. + + +- - - + + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}` + +* `name` - + Name of the Endpoint Attachment in the following format: + organizations/{organization}/endpointAttachments/{endpointAttachment}. + +* `host` - + Host that can be used in either HTTP Target Endpoint directly, or as the host in Target Server. + + +## 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 + + +EndpointAttachment can be imported using any of these accepted formats: + +``` +$ terraform import google_apigee_endpoint_attachment.default {{org_id}}/endpointAttachments/{{endpoint_attachment_id}} +$ terraform import google_apigee_endpoint_attachment.default {{org_id}}/{{endpoint_attachment_id}} +``` diff --git a/website/google.erb b/website/google.erb index d60580a3864..5950ec8608d 100644 --- a/website/google.erb +++ b/website/google.erb @@ -157,6 +157,10 @@ Resources