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

Add field cloud_logging_config to resource google_dns_managed_zone #12675

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6605.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
dns: Added general field `cloud_logging_config` to `google_dns_managed_zone`
```
71 changes: 71 additions & 0 deletions google/resource_dns_managed_zone.go
Expand Up @@ -57,6 +57,22 @@ func resourceDNSManagedZone() *schema.Resource {
Description: `User assigned name for this resource.
Must be unique within the project.`,
},
"cloud_logging_config": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `Cloud logging configuration`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_logging": {
Type: schema.TypeBool,
Required: true,
Description: `If set, enable query logging for this ManagedZone. False by default, making logging opt-in.`,
},
},
},
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -378,6 +394,12 @@ func resourceDNSManagedZoneCreate(d *schema.ResourceData, meta interface{}) erro
} else if v, ok := d.GetOkExists("peering_config"); !isEmptyValue(reflect.ValueOf(peeringConfigProp)) && (ok || !reflect.DeepEqual(v, peeringConfigProp)) {
obj["peeringConfig"] = peeringConfigProp
}
cloudLoggingConfigProp, err := expandDNSManagedZoneCloudLoggingConfig(d.Get("cloud_logging_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("cloud_logging_config"); !isEmptyValue(reflect.ValueOf(cloudLoggingConfigProp)) && (ok || !reflect.DeepEqual(v, cloudLoggingConfigProp)) {
obj["cloudLoggingConfig"] = cloudLoggingConfigProp
}

url, err := replaceVars(d, config, "{{DNSBasePath}}projects/{{project}}/managedZones")
if err != nil {
Expand Down Expand Up @@ -491,6 +513,9 @@ func resourceDNSManagedZoneRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("peering_config", flattenDNSManagedZonePeeringConfig(res["peeringConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}
if err := d.Set("cloud_logging_config", flattenDNSManagedZoneCloudLoggingConfig(res["cloudLoggingConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading ManagedZone: %s", err)
}

return nil
}
Expand Down Expand Up @@ -565,6 +590,12 @@ func resourceDNSManagedZoneUpdate(d *schema.ResourceData, meta interface{}) erro
} else if v, ok := d.GetOkExists("peering_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, peeringConfigProp)) {
obj["peeringConfig"] = peeringConfigProp
}
cloudLoggingConfigProp, err := expandDNSManagedZoneCloudLoggingConfig(d.Get("cloud_logging_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("cloud_logging_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, cloudLoggingConfigProp)) {
obj["cloudLoggingConfig"] = cloudLoggingConfigProp
}

obj, err = resourceDNSManagedZoneUpdateEncoder(d, meta, obj)
if err != nil {
Expand Down Expand Up @@ -980,6 +1011,23 @@ func flattenDNSManagedZonePeeringConfigTargetNetworkNetworkUrl(v interface{}, d
return v
}

func flattenDNSManagedZoneCloudLoggingConfig(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["enable_logging"] =
flattenDNSManagedZoneCloudLoggingConfigEnableLogging(original["enableLogging"], d, config)
return []interface{}{transformed}
}
func flattenDNSManagedZoneCloudLoggingConfigEnableLogging(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandDNSManagedZoneDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1285,6 +1333,29 @@ func expandDNSManagedZonePeeringConfigTargetNetworkNetworkUrl(v interface{}, d T
return ConvertSelfLinkToV1(url), nil
}

func expandDNSManagedZoneCloudLoggingConfig(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{})

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

return transformed, nil
}

func expandDNSManagedZoneCloudLoggingConfigEnableLogging(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func resourceDNSManagedZoneUpdateEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
// The upstream update method (https://cloud.google.com/dns/docs/reference/v1/managedZones/update)
// requires the full ManagedZones object, therefore, we need to keep some input only values in the struct
Expand Down
41 changes: 41 additions & 0 deletions google/resource_dns_managed_zone_generated_test.go
Expand Up @@ -311,6 +311,47 @@ resource "google_compute_network" "network-target" {
`, context)
}

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

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

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSManagedZoneDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDNSManagedZone_dnsManagedZoneCloudLoggingExample(context),
},
{
ResourceName: "google_dns_managed_zone.cloud-logging-enabled-zone",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccDNSManagedZone_dnsManagedZoneCloudLoggingExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_dns_managed_zone" "cloud-logging-enabled-zone" {
name = "tf-test-cloud-logging-enabled-zone%{random_suffix}"
dns_name = "services.example.com."
description = "Example cloud logging enabled DNS zone"
labels = {
foo = "bar"
}

cloud_logging_config {
enable_logging = true
}
}
`, 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
68 changes: 68 additions & 0 deletions google/resource_dns_managed_zone_test.go
Expand Up @@ -151,6 +151,44 @@ func TestAccDNSManagedZone_privateForwardingUpdate(t *testing.T) {
})
}

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

zoneSuffix := randString(t, 10)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSManagedZoneDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDnsManagedZone_cloudLoggingConfig_basic(zoneSuffix),
},
{
ResourceName: "google_dns_managed_zone.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDnsManagedZone_cloudLoggingConfig_update(zoneSuffix, true),
},
{
ResourceName: "google_dns_managed_zone.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDnsManagedZone_cloudLoggingConfig_update(zoneSuffix, false),
},
{
ResourceName: "google_dns_managed_zone.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccDNSManagedZone_forceDestroy(t *testing.T) {
//t.Parallel()

Expand Down Expand Up @@ -379,6 +417,36 @@ resource "google_compute_network" "network-1" {
`, suffix, first_nameserver, first_forwarding_path, second_nameserver, second_forwarding_path, suffix)
}

func testAccDnsManagedZone_cloudLoggingConfig_basic(suffix string) string {
return fmt.Sprintf(`
resource "google_dns_managed_zone" "foobar" {
name = "mzone-test-%s"
dns_name = "tf-acctest-%s.hashicorptest.com."
description = "Example DNS zone"
labels = {
foo = "bar"
}
}
`, suffix, suffix)
}

func testAccDnsManagedZone_cloudLoggingConfig_update(suffix string, enableCloudLogging bool) string {
return fmt.Sprintf(`
resource "google_dns_managed_zone" "foobar" {
name = "mzone-test-%s"
dns_name = "tf-acctest-%s.hashicorptest.com."
description = "Example DNS zone"
labels = {
foo = "bar"
}

cloud_logging_config {
enable_logging = %t
}
}
`, suffix, suffix, enableCloudLogging)
}

func TestDnsManagedZoneImport_parseImportId(t *testing.T) {
zoneRegexes := []string{
"projects/(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)",
Expand Down
33 changes: 33 additions & 0 deletions website/docs/r/dns_managed_zone.html.markdown
Expand Up @@ -214,6 +214,28 @@ resource "google_compute_network" "network" {
auto_create_subnetworks = false
}
```
<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%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=dns_managed_zone_cloud_logging&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 Cloud Logging


```hcl
resource "google_dns_managed_zone" "cloud-logging-enabled-zone" {
name = "cloud-logging-enabled-zone"
dns_name = "services.example.com."
description = "Example cloud logging enabled DNS zone"
labels = {
foo = "bar"
}

cloud_logging_config {
enable_logging = true
}
}
```

## Argument Reference

Expand Down Expand Up @@ -283,6 +305,11 @@ The following arguments are supported:
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](#nested_service_directory_config).

* `cloud_logging_config` -
(Optional)
Cloud logging configuration
Structure is [documented below](#nested_cloud_logging_config).

* `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 @@ -416,6 +443,12 @@ The following arguments are supported:
or simply `projects/{project}/locations/{location}/namespaces/{namespace_id}`
Ignored for `public` visibility zones.

<a name="nested_cloud_logging_config"></a>The `cloud_logging_config` block supports:

* `enable_logging` -
(Required)
If set, enable query logging for this ManagedZone. False by default, making logging opt-in.

## Attributes Reference

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