From fcea0cbde4fd8324ee6201ed6b941003887d1665 Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 1 Oct 2021 12:11:19 -0700 Subject: [PATCH] Manage services peered-dns-domains via terraform (#5158) (#10229) Signed-off-by: Modular Magician --- .changelog/5158.txt | 6 + go.mod | 1 + go.sum | 11 + ...le_service_networking_peered_dns_domain.go | 37 +++ ...rvice_networking_peered_dns_domain_test.go | 117 ++++++++ google/provider.go | 2 + ...le_service_networking_peered_dns_domain.go | 255 ++++++++++++++++++ ...rvice_networking_peered_dns_domain_test.go | 96 +++++++ ...networking_peered_dns_domain.html.markdown | 77 ++++++ website/google.erb | 4 + 10 files changed, 606 insertions(+) create mode 100644 .changelog/5158.txt create mode 100644 google/data_source_google_service_networking_peered_dns_domain.go create mode 100644 google/data_source_google_service_networking_peered_dns_domain_test.go create mode 100644 google/resource_google_service_networking_peered_dns_domain.go create mode 100644 google/resource_google_service_networking_peered_dns_domain_test.go create mode 100644 website/docs/r/google_service_networking_peered_dns_domain.html.markdown diff --git a/.changelog/5158.txt b/.changelog/5158.txt new file mode 100644 index 00000000000..36e9fce1e6b --- /dev/null +++ b/.changelog/5158.txt @@ -0,0 +1,6 @@ +```release-note:new-resource +`google_service_networking_peered_dns_domain` +``` +```release-note:new-datasource +`google_service_networking_peered_dns_domain` +``` diff --git a/go.mod b/go.mod index 09848d69bdc..c3003a5b080 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-version v1.3.0 github.com/hashicorp/hcl/v2 v2.6.0 // indirect + github.com/hashicorp/terraform-json v0.12.0 // indirect github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0 github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/hashstructure v1.1.0 diff --git a/go.sum b/go.sum index 64ec8078e7b..1e7268df5bf 100644 --- a/go.sum +++ b/go.sum @@ -466,6 +466,8 @@ github.com/hashicorp/terraform-exec v0.13.0 h1:1Pth+pdWJAufJuWWjaVOVNEkoRTOjGn3h github.com/hashicorp/terraform-exec v0.13.0/go.mod h1:SGhto91bVRlgXQWcJ5znSz+29UZIa8kpBbkGwQ+g9E8= github.com/hashicorp/terraform-json v0.8.0 h1:XObQ3PgqU52YLQKEaJ08QtUshAfN3yu4u8ebSW0vztc= github.com/hashicorp/terraform-json v0.8.0/go.mod h1:3defM4kkMfttwiE7VakJDwCd4R+umhSQnvJwORXbprE= +github.com/hashicorp/terraform-json v0.12.0 h1:8czPgEEWWPROStjkWPUnTQDXmpmZPlkQAwYYLETaTvw= +github.com/hashicorp/terraform-json v0.12.0/go.mod h1:pmbq9o4EuL43db5+0ogX10Yofv1nozM+wskr/bGFJpI= github.com/hashicorp/terraform-plugin-go v0.1.0/go.mod h1:10V6F3taeDWVAoLlkmArKttR3IULlRWFAGtQIQTIDr4= github.com/hashicorp/terraform-plugin-go v0.2.1 h1:EW/R8bB2Zbkjmugzsy1d27yS8/0454b3MtYHkzOknqA= github.com/hashicorp/terraform-plugin-go v0.2.1/go.mod h1:10V6F3taeDWVAoLlkmArKttR3IULlRWFAGtQIQTIDr4= @@ -599,6 +601,8 @@ github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceT github.com/mitchellh/cli v1.1.1/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -620,6 +624,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE= github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= @@ -733,6 +739,7 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/sanposhiho/wastedassign v1.0.0 h1:dB+7OV0iJ5b0SpGwKjKlPCr8GDZJX6Ylm3YG+66xGpc= github.com/sanposhiho/wastedassign v1.0.0/go.mod h1:LGpq5Hsv74QaqM47WtIsRSF/ik9kqk07kchgv66tLVE= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4= github.com/securego/gosec/v2 v2.7.0 h1:mOhJv5w6UyNLpSssQOQCc7eGkKLuicAxvf66Ey/X4xk= github.com/securego/gosec/v2 v2.7.0/go.mod h1:xNbGArrGUspJLuz3LS5XCY1EBW/0vABAl/LWfSklmiM= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= @@ -832,6 +839,8 @@ github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8/go.mod h1:dniwbG github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= +github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4= +github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= @@ -1094,6 +1103,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/google/data_source_google_service_networking_peered_dns_domain.go b/google/data_source_google_service_networking_peered_dns_domain.go new file mode 100644 index 00000000000..4ac4f2c316e --- /dev/null +++ b/google/data_source_google_service_networking_peered_dns_domain.go @@ -0,0 +1,37 @@ +package google + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleServiceNetworkingPeeredDNSDomain() *schema.Resource { + return &schema.Resource{ + Read: resourceGoogleServiceNetworkingPeeredDNSDomainRead, + Schema: map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "network": { + Type: schema.TypeString, + Required: true, + }, + "service": { + Type: schema.TypeString, + Required: true, + }, + "dns_suffix": { + Type: schema.TypeString, + Computed: true, + }, + "parent": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} diff --git a/google/data_source_google_service_networking_peered_dns_domain_test.go b/google/data_source_google_service_networking_peered_dns_domain_test.go new file mode 100644 index 00000000000..8d68ec6eb6d --- /dev/null +++ b/google/data_source_google_service_networking_peered_dns_domain_test.go @@ -0,0 +1,117 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDatasourceGoogleServiceNetworkingPeeredDnsDomain_basic(t *testing.T) { + t.Parallel() + org := getTestOrgFromEnv(t) + billingId := getTestBillingAccountFromEnv(t) + + project := fmt.Sprintf("tf-test-%d", randInt(t)) + + resourceName := "data.google_service_networking_peered_dns_domain.acceptance" + name := fmt.Sprintf("test-name-%d", randInt(t)) + network := "test-network" + service := "servicenetworking.googleapis.com" + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleServiceNetworkingPeeredDnsDomain_basic(project, org, billingId, name, network, service), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "name"), + resource.TestCheckResourceAttrSet(resourceName, "network"), + resource.TestCheckResourceAttrSet(resourceName, "dns_suffix"), + resource.TestCheckResourceAttrSet(resourceName, "service"), + ), + }, + }, + }) +} + +func testAccCheckGoogleServiceNetworkingPeeredDnsDomain_basic(project, org, billing, name, network, service string) string { + return fmt.Sprintf(` +resource "google_project" "host" { + project_id = "%s" + name = "%s" + org_id = "%s" + billing_account = "%s" +} + +resource "google_project_service" "host-compute" { + project = google_project.host.project_id + service = "compute.googleapis.com" +} + +resource "google_project_service" "host" { + project = google_project.host.project_id + service = "%s" +} + +resource "google_compute_network" "test" { + name = "test-network" + project = google_project.host.project_id + routing_mode = "GLOBAL" + depends_on = [google_project_service.host-compute] +} + +resource "google_compute_global_address" "host-private-access" { + name = "private-ip-alloc-host" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 24 + address = "192.168.255.0" + network = google_compute_network.test.name + project = google_project.host.project_id + + depends_on = [ + google_project_service.host-compute, + google_project_service.host, + google_compute_network.test, + ] +} + +resource "google_service_networking_connection" "host-private-access" { + network = google_compute_network.test.self_link + service = "%s" + reserved_peering_ranges = [google_compute_global_address.host-private-access.name] + + depends_on = [ + google_project_service.host, + google_compute_network.test, + google_compute_global_address.host-private-access, + ] +} + +resource "google_service_networking_peered_dns_domain" "acceptance" { + name = "%s" + project = google_project.host.number + network = google_compute_network.test.name + dns_suffix = "example.com." + service = "%s" + + depends_on = [ + google_compute_network.test, + google_service_networking_connection.host-private-access, + ] +} + +data "google_service_networking_peered_dns_domain" "acceptance" { + project = google_project.host.number + name = "%s" + network = google_compute_network.test.name + service = "%s" + + depends_on = [ + google_service_networking_peered_dns_domain.acceptance, + ] +} +`, project, project, org, billing, service, service, name, service, name, service) +} diff --git a/google/provider.go b/google/provider.go index 75539ef26fd..c62c16d2afc 100644 --- a/google/provider.go +++ b/google/provider.go @@ -793,6 +793,7 @@ func Provider() *schema.Provider { "google_sql_ca_certs": dataSourceGoogleSQLCaCerts(), "google_sql_backup_run": dataSourceSqlBackupRun(), "google_sql_database_instance": dataSourceSqlDatabaseInstance(), + "google_service_networking_peered_dns_domain": dataSourceGoogleServiceNetworkingPeeredDNSDomain(), "google_storage_bucket": dataSourceGoogleStorageBucket(), "google_storage_bucket_object": dataSourceGoogleStorageBucketObject(), "google_storage_bucket_object_content": dataSourceGoogleStorageBucketObjectContent(), @@ -1192,6 +1193,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_runtimeconfig_variable": resourceRuntimeconfigVariable(), "google_service_account": resourceGoogleServiceAccount(), "google_service_account_key": resourceGoogleServiceAccountKey(), + "google_service_networking_peered_dns_domain": resourceGoogleServiceNetworkingPeeredDNSDomain(), "google_storage_bucket": resourceStorageBucket(), "google_storage_bucket_acl": resourceStorageBucketAcl(), "google_storage_bucket_object": resourceStorageBucketObject(), diff --git a/google/resource_google_service_networking_peered_dns_domain.go b/google/resource_google_service_networking_peered_dns_domain.go new file mode 100644 index 00000000000..03ad0dc887d --- /dev/null +++ b/google/resource_google_service_networking_peered_dns_domain.go @@ -0,0 +1,255 @@ +package google + +import ( + "fmt" + "log" + "strconv" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "google.golang.org/api/servicenetworking/v1" +) + +func resourceGoogleServiceNetworkingPeeredDNSDomain() *schema.Resource { + return &schema.Resource{ + Create: resourceGoogleServiceNetworkingPeeredDNSDomainCreate, + Read: resourceGoogleServiceNetworkingPeeredDNSDomainRead, + Delete: resourceGoogleServiceNetworkingPeeredDNSDomainDelete, + + Importer: &schema.ResourceImporter{ + State: resourceGoogleServiceNetworkingPeeredDNSDomainImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(20 * time.Minute), + Read: schema.DefaultTimeout(10 * time.Minute), + Delete: schema.DefaultTimeout(20 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + Description: `The ID of the project that the service account will be created in. Defaults to the provider project configuration.`, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Name of the peered DNS domain.", + }, + "dns_suffix": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The DNS domain name suffix of the peered DNS domain.", + }, + "service": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: "servicenetworking.googleapis.com", + Description: "The name of the service to create a peered DNS domain for, e.g. servicenetworking.googleapis.com", + }, + "network": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Network in the consumer project to peer with.", + }, + "parent": { + Type: schema.TypeString, + Computed: true, + }, + }, + UseJSONNumber: true, + } +} + +func resourceGoogleServiceNetworkingPeeredDNSDomainImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "/") + if len(parts) != 9 { + return nil, fmt.Errorf("Invalid google_project_service_peered_dns_domain id format for import, expecting `services/{service}/projects/{project}/global/networks/{network}/peeredDnsDomains/{name}`, found %s", d.Id()) + } + if err := d.Set("service", parts[1]); err != nil { + return nil, fmt.Errorf("Error setting service: %s", err) + } + if err := d.Set("project", parts[3]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("network", parts[6]); err != nil { + return nil, fmt.Errorf("Error setting network: %s", err) + } + if err := d.Set("name", parts[8]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + return []*schema.ResourceData{d}, nil +} + +func resourceGoogleServiceNetworkingPeeredDNSDomainCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + projectNumber, err := getProjectNumber(d, config, project, userAgent) + if err != nil { + return err + } + + service := d.Get("service").(string) + network := d.Get("network").(string) + parent := fmt.Sprintf("services/%s/projects/%s/global/networks/%s", service, projectNumber, network) + + name := d.Get("name").(string) + dnsSuffix := d.Get("dns_suffix").(string) + r := &servicenetworking.PeeredDnsDomain{ + DnsSuffix: dnsSuffix, + Name: name, + } + + apiService := config.NewServiceNetworkingClient(userAgent) + peeredDnsDomainsService := servicenetworking.NewServicesProjectsGlobalNetworksPeeredDnsDomainsService(apiService) + createCall := peeredDnsDomainsService.Create(parent, r) + if config.UserProjectOverride { + createCall.Header().Add("X-Goog-User-Project", project) + } + op, err := createCall.Do() + if err != nil { + return err + } + + if err := serviceNetworkingOperationWaitTime(config, op, "Create Service Networking Peered DNS Domain", userAgent, project, d.Timeout(schema.TimeoutCreate)); err != nil { + return err + } + + if err := d.Set("parent", parent); err != nil { + return fmt.Errorf("Error setting parent: %s", err) + } + id := fmt.Sprintf("%s/peeredDnsDomains/%s", parent, name) + d.SetId(id) + return resourceGoogleServiceNetworkingPeeredDNSDomainRead(d, meta) +} + +func resourceGoogleServiceNetworkingPeeredDNSDomainRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + projectNumber, err := getProjectNumber(d, config, project, userAgent) + if err != nil { + return err + } + + service := d.Get("service").(string) + network := d.Get("network").(string) + parent := fmt.Sprintf("services/%s/projects/%s/global/networks/%s", service, projectNumber, network) + + apiService := config.NewServiceNetworkingClient(userAgent) + peeredDnsDomainsService := servicenetworking.NewServicesProjectsGlobalNetworksPeeredDnsDomainsService(apiService) + readCall := peeredDnsDomainsService.List(parent) + if config.UserProjectOverride { + readCall.Header().Add("X-Goog-User-Project", project) + } + response, err := readCall.Do() + if err != nil { + return err + } + + name := d.Get("name").(string) + id := fmt.Sprintf("%s/peeredDnsDomains/%s", parent, name) + d.SetId(id) + + var peeredDnsDomain *servicenetworking.PeeredDnsDomain + for _, c := range response.PeeredDnsDomains { + if c.Name == name { + peeredDnsDomain = c + break + } + } + + if peeredDnsDomain == nil { + d.SetId("") + log.Printf("[WARNING] Failed to find Service Peered DNS Domain, service: %s, project: %s, network: %s, name: %s", service, project, network, name) + return nil + } + + if err := d.Set("network", network); err != nil { + return fmt.Errorf("Error setting network: %s", err) + } + if err := d.Set("name", peeredDnsDomain.Name); err != nil { + return fmt.Errorf("Error setting name: %s", err) + } + if err := d.Set("dns_suffix", peeredDnsDomain.DnsSuffix); err != nil { + return fmt.Errorf("Error setting peering: %s", err) + } + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("parent", parent); err != nil { + return fmt.Errorf("Error setting parent: %s", err) + } + + return nil +} + +func resourceGoogleServiceNetworkingPeeredDNSDomainDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + name := d.Get("name").(string) + apiService := config.NewServiceNetworkingClient(userAgent) + peeredDnsDomainsService := servicenetworking.NewServicesProjectsGlobalNetworksPeeredDnsDomainsService(apiService) + + if err := retryTimeDuration(func() error { + _, delErr := peeredDnsDomainsService.Delete(d.Id()).Do() + return delErr + }, d.Timeout(schema.TimeoutDelete)); err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("Peered DNS domain %s", name)) + } + + d.SetId("") + return nil +} + +// NOTE(deviavir): An out of band aspect of this API is that it uses a unique formatting of network +// different from the standard self_link URI. It requires a call to the resource manager to get the project +// number for the current project. +func getProjectNumber(d *schema.ResourceData, config *Config, project, userAgent string) (string, error) { + log.Printf("[DEBUG] Retrieving project number by doing a GET with the project id, as required by service networking") + // err == nil indicates that the billing_project value was found + billingProject := project + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + getProjectCall := config.NewResourceManagerClient(userAgent).Projects.Get(project) + if config.UserProjectOverride { + getProjectCall.Header().Add("X-Goog-User-Project", billingProject) + } + projectCall, err := getProjectCall.Do() + if err != nil { + // note: returning a wrapped error is part of this method's contract! + // https://blog.golang.org/go1.13-errors + return "", fmt.Errorf("Failed to retrieve project, project: %s, err: %w", project, err) + } + + return strconv.FormatInt(projectCall.ProjectNumber, 10), nil +} diff --git a/google/resource_google_service_networking_peered_dns_domain_test.go b/google/resource_google_service_networking_peered_dns_domain_test.go new file mode 100644 index 00000000000..71db77d3443 --- /dev/null +++ b/google/resource_google_service_networking_peered_dns_domain_test.go @@ -0,0 +1,96 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccServiceNetworkingPeeredDNSDomain_basic(t *testing.T) { + t.Parallel() + org := getTestOrgFromEnv(t) + billingId := getTestBillingAccountFromEnv(t) + + project := fmt.Sprintf("tf-test-%d", randInt(t)) + name := fmt.Sprintf("test-name-%d", randInt(t)) + service := "servicenetworking.googleapis.com" + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccServiceNetworkingPeeredDNSDomain_basic(project, org, billingId, name, service), + }, + }, + }) +} + +func testAccServiceNetworkingPeeredDNSDomain_basic(project, org, billing, name, service string) string { + return fmt.Sprintf(` +resource "google_project" "host" { + project_id = "%s" + name = "%s" + org_id = "%s" + billing_account = "%s" +} + +resource "google_project_service" "host-compute" { + project = google_project.host.project_id + service = "compute.googleapis.com" +} + +resource "google_project_service" "host" { + project = google_project.host.project_id + service = "%s" +} + +resource "google_compute_network" "test" { + name = "test-network" + project = google_project.host.project_id + routing_mode = "GLOBAL" + depends_on = [google_project_service.host-compute] +} + +resource "google_compute_global_address" "host-private-access" { + name = "private-ip-alloc-host" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 24 + address = "192.168.255.0" + network = "test-network" + project = google_project.host.project_id + + depends_on = [ + google_project_service.host-compute, + google_project_service.host, + google_compute_network.test, + ] +} + +resource "google_service_networking_connection" "host-private-access" { + network = google_compute_network.test.id + service = "%s" + reserved_peering_ranges = [google_compute_global_address.host-private-access.name] + + depends_on = [ + google_project_service.host, + google_compute_network.test, + google_compute_global_address.host-private-access, + ] +} + +resource "google_service_networking_peered_dns_domain" "test" { + name = "%s" + project = google_project.host.number + network = "test-network" + dns_suffix = "example.com." + service = "%s" + depends_on = [ + google_compute_network.test, + google_service_networking_connection.host-private-access, + ] +} +`, project, project, org, billing, service, service, name, service) +} diff --git a/website/docs/r/google_service_networking_peered_dns_domain.html.markdown b/website/docs/r/google_service_networking_peered_dns_domain.html.markdown new file mode 100644 index 00000000000..98554e5c43b --- /dev/null +++ b/website/docs/r/google_service_networking_peered_dns_domain.html.markdown @@ -0,0 +1,77 @@ +--- +subcategory: "Cloud Platform" +layout: "google" +page_title: "Google: google_service_networking_peered_dns_domain" +sidebar_current: "docs-google-project-service-peered-dns-domain-x" +description: |- + Allows management of a single peered DNS domain on a project. +--- + +# google\_project\_service\_peered\_dns\_domain + +Allows management of a single peered DNS domain for an existing Google Cloud Platform project. + +When using Google Cloud DNS to manage internal DNS, create peered DNS domains to make your DNS available to services like Google Cloud Build. + +## Example Usage + +```hcl +resource "google_service_networking_peered_dns_domain" "name" { + project = 10000000 + name = "example-com" + network = "default" + dns_suffix = "example.com." + service = "peering-service" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `project` - (Optional) The producer project number. If not provided, the provider project is used. + +* `name` - (Required) Internal name used for the peered DNS domain. + +* `network` - (Required) The network in the consumer project. + +* `dns_suffix` - (Required) The DNS domain suffix of the peered DNS domain. Make sure to suffix with a `.` (dot). + +* `service` - (Optional) Private service connection between service and consumer network, defaults to `servicenetworking.googleapis.com` + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `services/{{service}}/projects/{{project}}/global/networks/{{network}}/peeredDnsDomains/{{name}}` + +* `parent` - an identifier for the resource with format `services/{{service}}/projects/{{project}}/global/networks/{{network}}` + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 20 minutes. +- `read` - Default is 10 minutes. +- `update` - Default is 20 minutes. +- `delete` - Default is 20 minutes. + +## Import + +Project peered DNS domains can be imported using the `service`, `project`, `network` and `name`, e.g. + +``` +$ terraform import google_service_networking_peered_dns_domain.my_domain services/{service}/projects/{project}/global/networks/{network}/peeredDnsDomains/{name} +``` + +Where: + +- `service` is the service connection, defaults to `servicenetworking.googleapis.com`. +- `project` is the producer project name. +- `network` is the consumer network name. +- `name` is the name of your peered DNS domain. + +## User Project Overrides + +This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/guides/provider_reference.html#user_project_override). diff --git a/website/google.erb b/website/google.erb index 5f7aecc99cb..b79256fda2f 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1203,6 +1203,10 @@ google_service_account_key +
  • + google_service_networking_peered_dns_domain +
  • +
  • google_project_service_identity