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

Service Directory Cloud DNS integration #1976

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/3398.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
dns: Added `service_directory_config` field to`google_dns_managed_zone` (Beta only)
```
128 changes: 128 additions & 0 deletions google-beta/resource_dns_managed_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"reflect"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
Expand Down Expand Up @@ -243,6 +244,36 @@ blocks in an update and then apply another update adding all of them back simult
lookup queries using automatically configured records for VPC resources. This only applies
to networks listed under 'private_visibility_config'.`,
},
"service_directory_config": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The presence of this field indicates that this zone is backed by Service Directory. The value of this field contains information related to the namespace associated with the zone.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeList,
Required: true,
Description: `The namespace associated with the zone.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace_url": {
Type: schema.TypeString,
Required: true,
Description: `The fully qualified or partial URL of the service directory namespace that should be
associated with the zone. This should be formatted like
'https://servicedirectory.googleapis.com/v1/projects/{project}/locations/{location}/namespaces/{namespace_id}'
or simply 'projects/{project}/locations/{location}/namespaces/{namespace_id}'
Ignored for 'public' visibility zones.`,
},
},
},
},
},
},
},
"visibility": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -372,6 +403,12 @@ func resourceDNSManagedZoneCreate(d *schema.ResourceData, meta interface{}) erro
} else if v, ok := d.GetOkExists("reverse_lookup"); !isEmptyValue(reflect.ValueOf(reverseLookupConfigProp)) && (ok || !reflect.DeepEqual(v, reverseLookupConfigProp)) {
obj["reverseLookupConfig"] = reverseLookupConfigProp
}
serviceDirectoryConfigProp, err := expandDNSManagedZoneServiceDirectoryConfig(d.Get("service_directory_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("service_directory_config"); !isEmptyValue(reflect.ValueOf(serviceDirectoryConfigProp)) && (ok || !reflect.DeepEqual(v, serviceDirectoryConfigProp)) {
obj["serviceDirectoryConfig"] = serviceDirectoryConfigProp
}

url, err := replaceVars(d, config, "{{DNSBasePath}}projects/{{project}}/managedZones")
if err != nil {
Expand Down Expand Up @@ -454,6 +491,9 @@ func resourceDNSManagedZoneRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("reverse_lookup", flattenDNSManagedZoneReverseLookup(res["reverseLookupConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}
if err := d.Set("service_directory_config", flattenDNSManagedZoneServiceDirectoryConfig(res["serviceDirectoryConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}

return nil
}
Expand Down Expand Up @@ -801,6 +841,43 @@ func flattenDNSManagedZoneReverseLookup(v interface{}, d *schema.ResourceData, c
return v != nil
}

func flattenDNSManagedZoneServiceDirectoryConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["namespace"] =
flattenDNSManagedZoneServiceDirectoryConfigNamespace(original["namespace"], d, config)
return []interface{}{transformed}
}
func flattenDNSManagedZoneServiceDirectoryConfigNamespace(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["namespace_url"] =
flattenDNSManagedZoneServiceDirectoryConfigNamespaceNamespaceUrl(original["namespaceUrl"], d, config)
return []interface{}{transformed}
}
func flattenDNSManagedZoneServiceDirectoryConfigNamespaceNamespaceUrl(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
relative, err := getRelativePath(v.(string))
if err != nil {
return v
}
return relative
}

func expandDNSManagedZoneDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1091,3 +1168,54 @@ func expandDNSManagedZoneReverseLookup(v interface{}, d TerraformResourceData, c

return struct{}{}, nil
}

func expandDNSManagedZoneServiceDirectoryConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedNamespace, err := expandDNSManagedZoneServiceDirectoryConfigNamespace(original["namespace"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedNamespace); val.IsValid() && !isEmptyValue(val) {
transformed["namespace"] = transformedNamespace
}

return transformed, nil
}

func expandDNSManagedZoneServiceDirectoryConfigNamespace(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedNamespaceUrl, err := expandDNSManagedZoneServiceDirectoryConfigNamespaceNamespaceUrl(original["namespace_url"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedNamespaceUrl); val.IsValid() && !isEmptyValue(val) {
transformed["namespaceUrl"] = transformedNamespaceUrl
}

return transformed, nil
}

func expandDNSManagedZoneServiceDirectoryConfigNamespaceNamespaceUrl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
if v == nil || v.(string) == "" {
return "", nil
} else if strings.HasPrefix(v.(string), "https://") {
return v, nil
}
url, err := replaceVars(d, config, "{{ServiceDirectoryBasePath}}"+v.(string))
if err != nil {
return "", err
}
return url, nil
}
53 changes: 53 additions & 0 deletions google-beta/resource_dns_managed_zone_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,59 @@ provider "google-beta" {
`, context)
}

func TestAccDNSManagedZone_dnsManagedZoneServiceDirectoryExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckDNSManagedZoneDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDNSManagedZone_dnsManagedZoneServiceDirectoryExample(context),
},
},
})
}

func testAccDNSManagedZone_dnsManagedZoneServiceDirectoryExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_dns_managed_zone" "sd-zone" {
provider = google-beta

name = "tf-test-peering-zone%{random_suffix}"
dns_name = "services.example.com."
description = "Example private DNS Service Directory zone"

visibility = "private"

service_directory_config {
namespace {
namespace_url = google_service_directory_namespace.example.id
}
}
}

resource "google_service_directory_namespace" "example" {
provider = google-beta

namespace_id = "example"
location = "us-central1"
}

resource "google_compute_network" "network" {
provider = google-beta

name = "network%{random_suffix}"
auto_create_subnetworks = false
}
`, context)
}

func testAccCheckDNSManagedZoneDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
60 changes: 60 additions & 0 deletions website/docs/r/dns_managed_zone.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,45 @@ provider "google-beta" {
zone = "us-central1-a"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgit.luolix.top%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=dns_managed_zone_service_directory&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Dns Managed Zone Service Directory


```hcl
resource "google_dns_managed_zone" "sd-zone" {
provider = google-beta

name = "peering-zone"
dns_name = "services.example.com."
description = "Example private DNS Service Directory zone"

visibility = "private"

service_directory_config {
namespace {
namespace_url = google_service_directory_namespace.example.id
}
}
}

resource "google_service_directory_namespace" "example" {
provider = google-beta

namespace_id = "example"
location = "us-central1"
}

resource "google_compute_network" "network" {
provider = google-beta

name = "network"
auto_create_subnetworks = false
}
```

## Argument Reference

Expand Down Expand Up @@ -248,6 +287,10 @@ The following arguments are supported:
lookup queries using automatically configured records for VPC resources. This only applies
to networks listed under `private_visibility_config`.

* `service_directory_config` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
The presence of this field indicates that this zone is backed by Service Directory. The value of this field contains information related to the namespace associated with the zone. Structure is documented below.

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

Expand Down Expand Up @@ -353,6 +396,23 @@ The `target_network` block supports:
This should be formatted like
`https://www.googleapis.com/compute/v1/projects/{project}/global/networks/{network}`

The `service_directory_config` block supports:

* `namespace` -
(Required)
The namespace associated with the zone. Structure is documented below.


The `namespace` block supports:

* `namespace_url` -
(Required)
The fully qualified or partial URL of the service directory namespace that should be
associated with the zone. This should be formatted like
`https://servicedirectory.googleapis.com/v1/projects/{project}/locations/{location}/namespaces/{namespace_id}`
or simply `projects/{project}/locations/{location}/namespaces/{namespace_id}`
Ignored for `public` visibility zones.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down