From b71e02b8398e16727332268579a05f578bf52e0e Mon Sep 17 00:00:00 2001 From: Roberto Jung Drebes Date: Fri, 16 Nov 2018 21:06:48 +0000 Subject: [PATCH] Start autogenerating resource_dns_managed_zone --- google-beta/provider.go | 2 +- google-beta/provider_dns_gen.go | 21 ++ google-beta/resource_dns_managed_zone.go | 235 ++++++++++++------ ...esource_dns_managed_zone_generated_test.go | 84 +++++++ google-beta/resource_dns_managed_zone_test.go | 41 --- website/docs/r/dns_managed_zone.html.markdown | 107 ++++++++ website/docs/r/dns_managed_zone.markdown | 63 ----- 7 files changed, 378 insertions(+), 175 deletions(-) create mode 100644 google-beta/provider_dns_gen.go create mode 100644 google-beta/resource_dns_managed_zone_generated_test.go create mode 100644 website/docs/r/dns_managed_zone.html.markdown delete mode 100644 website/docs/r/dns_managed_zone.markdown diff --git a/google-beta/provider.go b/google-beta/provider.go index f2f011e708..a478776b12 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -107,6 +107,7 @@ func Provider() terraform.ResourceProvider { GeneratedAccessContextManagerResourcesMap, // end beta-only products GeneratedComputeResourcesMap, + GeneratedDnsResourcesMap, GeneratedRedisResourcesMap, GeneratedResourceManagerResourcesMap, GeneratedStorageResourcesMap, @@ -168,7 +169,6 @@ func Provider() terraform.ResourceProvider { "google_dataflow_job": resourceDataflowJob(), "google_dataproc_cluster": resourceDataprocCluster(), "google_dataproc_job": resourceDataprocJob(), - "google_dns_managed_zone": resourceDnsManagedZone(), "google_dns_record_set": resourceDnsRecordSet(), "google_endpoints_service": resourceEndpointsService(), "google_folder": resourceGoogleFolder(), diff --git a/google-beta/provider_dns_gen.go b/google-beta/provider_dns_gen.go new file mode 100644 index 0000000000..8ec5b11f36 --- /dev/null +++ b/google-beta/provider_dns_gen.go @@ -0,0 +1,21 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 "github.com/hashicorp/terraform/helper/schema" + +var GeneratedDnsResourcesMap = map[string]*schema.Resource{ + "google_dns_managed_zone": resourceDnsManagedZone(), +} diff --git a/google-beta/resource_dns_managed_zone.go b/google-beta/resource_dns_managed_zone.go index 175facd837..ffc07234f7 100644 --- a/google-beta/resource_dns_managed_zone.go +++ b/google-beta/resource_dns_managed_zone.go @@ -1,11 +1,25 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/dns/v1" ) func resourceDnsManagedZone() *schema.Resource { @@ -14,50 +28,45 @@ func resourceDnsManagedZone() *schema.Resource { Read: resourceDnsManagedZoneRead, Update: resourceDnsManagedZoneUpdate, Delete: resourceDnsManagedZoneDelete, + Importer: &schema.ResourceImporter{ State: resourceDnsManagedZoneImport, }, + Schema: map[string]*schema.Schema{ - "dns_name": &schema.Schema{ + "dns_name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - - "description": &schema.Schema{ + "description": { Type: schema.TypeString, Optional: true, Default: "Managed by Terraform", }, - - "name_servers": &schema.Schema{ + "labels": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "name_servers": { Type: schema.TypeList, Computed: true, Elem: &schema.Schema{ Type: schema.TypeString, }, }, - - // Google Cloud DNS ManagedZone resources do not have a SelfLink attribute. - - "project": &schema.Schema{ + "project": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: true, }, - - "labels": &schema.Schema{ - Type: schema.TypeMap, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, }, } } @@ -65,30 +74,51 @@ func resourceDnsManagedZone() *schema.Resource { func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - project, err := getProject(d, config) + obj := make(map[string]interface{}) + descriptionProp, err := expandDnsManagedZoneDescription(d.Get("description"), d, config) if err != nil { return err + } else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp } - - // Build the parameter - zone := &dns.ManagedZone{ - Name: d.Get("name").(string), - DnsName: d.Get("dns_name").(string), - Description: d.Get("description").(string), + dnsNameProp, err := expandDnsManagedZoneDnsName(d.Get("dns_name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("dns_name"); !isEmptyValue(reflect.ValueOf(dnsNameProp)) && (ok || !reflect.DeepEqual(v, dnsNameProp)) { + obj["dnsName"] = dnsNameProp + } + nameProp, err := expandDnsManagedZoneName(d.Get("name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["name"] = nameProp + } + labelsProp, err := expandDnsManagedZoneLabels(d.Get("labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp } - if _, ok := d.GetOk("labels"); ok { - zone.Labels = expandLabels(d) + url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones") + if err != nil { + return err } - log.Printf("[DEBUG] DNS ManagedZone create request: %#v", zone) + log.Printf("[DEBUG] Creating new ManagedZone: %#v", obj) + res, err := sendRequest(config, "POST", url, obj) + if err != nil { + return fmt.Errorf("Error creating ManagedZone: %s", err) + } - zone, err = config.clientDns.ManagedZones.Create(project, zone).Do() + // Store the ID now + id, err := replaceVars(d, config, "{{name}}") if err != nil { - return fmt.Errorf("Error creating DNS ManagedZone: %s", err) + return fmt.Errorf("Error constructing id: %s", err) } + d.SetId(id) - d.SetId(zone.Name) + log.Printf("[DEBUG] Finished creating ManagedZone %q: %#v", d.Id(), res) return resourceDnsManagedZoneRead(d, meta) } @@ -96,80 +126,102 @@ func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) erro func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - project, err := getProject(d, config) + url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones/{{name}}") if err != nil { return err } - zone, err := config.clientDns.ManagedZones.Get( - project, d.Id()).Do() + res, err := sendRequest(config, "GET", url, nil) if err != nil { - return handleNotFoundError(err, d, fmt.Sprintf("DNS Managed Zone %q", d.Get("name").(string))) + return handleNotFoundError(err, d, fmt.Sprintf("DnsManagedZone %q", d.Id())) } - d.Set("name_servers", zone.NameServers) - d.Set("name", zone.Name) - d.Set("dns_name", zone.DnsName) - d.Set("description", zone.Description) - d.Set("project", project) - d.Set("labels", zone.Labels) - - return nil -} - -func resourceDnsManagedZoneUpdate(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - + if err := d.Set("description", flattenDnsManagedZoneDescription(res["description"])); err != nil { + return fmt.Errorf("Error reading ManagedZone: %s", err) + } + if err := d.Set("dns_name", flattenDnsManagedZoneDnsName(res["dnsName"])); err != nil { + return fmt.Errorf("Error reading ManagedZone: %s", err) + } + if err := d.Set("name", flattenDnsManagedZoneName(res["name"])); err != nil { + return fmt.Errorf("Error reading ManagedZone: %s", err) + } + if err := d.Set("name_servers", flattenDnsManagedZoneNameServers(res["nameServers"])); err != nil { + return fmt.Errorf("Error reading ManagedZone: %s", err) + } + if err := d.Set("labels", flattenDnsManagedZoneLabels(res["labels"])); err != nil { + return fmt.Errorf("Error reading ManagedZone: %s", err) + } project, err := getProject(d, config) if err != nil { return err } - - zone := &dns.ManagedZone{ - Name: d.Get("name").(string), - DnsName: d.Get("dns_name").(string), - Description: d.Get("description").(string), + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading ManagedZone: %s", err) } - if _, ok := d.GetOk("labels"); ok { - zone.Labels = expandLabels(d) - } + return nil +} - op, err := config.clientDns.ManagedZones.Patch(project, d.Id(), zone).Do() - if err != nil { - return err - } +func resourceDnsManagedZoneUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) - err = dnsOperationWait(config.clientDns, op, project, "Updating DNS Managed Zone") - if err != nil { - return err + d.Partial(true) + + if d.HasChange("description") || d.HasChange("labels") { + obj := make(map[string]interface{}) + descriptionProp, err := expandDnsManagedZoneDescription(d.Get("description"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp + } + labelsProp, err := expandDnsManagedZoneLabels(d.Get("labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp + } + + url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones/{{name}}") + if err != nil { + return err + } + _, err = sendRequest(config, "PATCH", url, obj) + if err != nil { + return fmt.Errorf("Error updating ManagedZone %q: %s", d.Id(), err) + } + + d.SetPartial("description") + d.SetPartial("labels") } + d.Partial(false) + return resourceDnsManagedZoneRead(d, meta) } func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - project, err := getProject(d, config) + url, err := replaceVars(d, config, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones/{{name}}") if err != nil { return err } - err = config.clientDns.ManagedZones.Delete(project, d.Id()).Do() + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting ManagedZone %q", d.Id()) + res, err := sendRequest(config, "DELETE", url, obj) if err != nil { - return fmt.Errorf("Error deleting DNS ManagedZone: %s", err) + return handleNotFoundError(err, d, "ManagedZone") } - d.SetId("") + log.Printf("[DEBUG] Finished deleting ManagedZone %q: %#v", d.Id(), res) return nil } func resourceDnsManagedZoneImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { config := meta.(*Config) - parseImportId([]string{"projects/(?P[^/]+)/managedZones/(?P[^/]+)", - "(?P[^/]+)/managedZones/(?P[^/]+)", - "(?P[^/]+)"}, d, config) + parseImportId([]string{"projects/(?P[^/]+)/managedZones/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config) // Replace import id for the resource id id, err := replaceVars(d, config, "{{name}}") @@ -180,3 +232,46 @@ func resourceDnsManagedZoneImport(d *schema.ResourceData, meta interface{}) ([]* return []*schema.ResourceData{d}, nil } + +func flattenDnsManagedZoneDescription(v interface{}) interface{} { + return v +} + +func flattenDnsManagedZoneDnsName(v interface{}) interface{} { + return v +} + +func flattenDnsManagedZoneName(v interface{}) interface{} { + return v +} + +func flattenDnsManagedZoneNameServers(v interface{}) interface{} { + return v +} + +func flattenDnsManagedZoneLabels(v interface{}) interface{} { + return v +} + +func expandDnsManagedZoneDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandDnsManagedZoneDnsName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandDnsManagedZoneName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandDnsManagedZoneLabels(v interface{}, d *schema.ResourceData, config *Config) (map[string]string, error) { + if v == nil { + return map[string]string{}, nil + } + m := make(map[string]string) + for k, val := range v.(map[string]interface{}) { + m[k] = val.(string) + } + return m, nil +} diff --git a/google-beta/resource_dns_managed_zone_generated_test.go b/google-beta/resource_dns_managed_zone_generated_test.go new file mode 100644 index 0000000000..de9a846c15 --- /dev/null +++ b/google-beta/resource_dns_managed_zone_generated_test.go @@ -0,0 +1,84 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDnsManagedZone_dnsManagedZoneBasicExample(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDnsManagedZoneDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDnsManagedZone_dnsManagedZoneBasicExample(acctest.RandString(10)), + }, + { + ResourceName: "google_dns_managed_zone.example-zone", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccDnsManagedZone_dnsManagedZoneBasicExample(val string) string { + return fmt.Sprintf(` +resource "google_dns_managed_zone" "example-zone" { + name = "example-zone" + dns_name = "example-${random_id.rnd.hex}.com." + description = "Example DNS zone" + labels = { + foo = "bar" + } +} + +resource "random_id" "rnd" { + byte_length = 4 +} +`, + ) +} + +func testAccCheckDnsManagedZoneDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_dns_managed_zone" { + continue + } + + config := testAccProvider.Meta().(*Config) + + url, err := replaceVarsForTest(rs, "https://www.googleapis.com/dns/v1/projects/{{project}}/managedZones/{{name}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", url, nil) + if err == nil { + return fmt.Errorf("DnsManagedZone still exists at %s", url) + } + } + + return nil +} diff --git a/google-beta/resource_dns_managed_zone_test.go b/google-beta/resource_dns_managed_zone_test.go index 372f2221b3..8d27ed580c 100644 --- a/google-beta/resource_dns_managed_zone_test.go +++ b/google-beta/resource_dns_managed_zone_test.go @@ -6,31 +6,8 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" ) -func TestAccDnsManagedZone_basic(t *testing.T) { - t.Parallel() - - zoneSuffix := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckDnsManagedZoneDestroy, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccDnsManagedZone_basic(zoneSuffix, "description1"), - }, - resource.TestStep{ - ResourceName: "google_dns_managed_zone.foobar", - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - func TestAccDnsManagedZone_update(t *testing.T) { t.Parallel() @@ -61,24 +38,6 @@ func TestAccDnsManagedZone_update(t *testing.T) { }) } -func testAccCheckDnsManagedZoneDestroy(s *terraform.State) error { - config := testAccProvider.Meta().(*Config) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "google_dns_zone" { - continue - } - - _, err := config.clientDns.ManagedZones.Get( - config.Project, rs.Primary.ID).Do() - if err == nil { - return fmt.Errorf("DNS ManagedZone still exists") - } - } - - return nil -} - func testAccDnsManagedZone_basic(suffix, description string) string { return fmt.Sprintf(` resource "google_dns_managed_zone" "foobar" { diff --git a/website/docs/r/dns_managed_zone.html.markdown b/website/docs/r/dns_managed_zone.html.markdown new file mode 100644 index 0000000000..d1585a3566 --- /dev/null +++ b/website/docs/r/dns_managed_zone.html.markdown @@ -0,0 +1,107 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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. +# +# ---------------------------------------------------------------------------- +layout: "google" +page_title: "Google: google_dns_managed_zone" +sidebar_current: "docs-google-dns-managed-zone" +description: |- + A zone is a subtree of the DNS namespace under one administrative + responsibility. +--- + +# google\_dns\_managed\_zone + +A zone is a subtree of the DNS namespace under one administrative +responsibility. A ManagedZone is a resource that represents a DNS zone +hosted by the Cloud DNS service. + + +To get more information about ManagedZone, see: + +* [API documentation](https://cloud.google.com/dns/api/v1/managedZones) +* How-to Guides + * [Managing Zones](https://cloud.google.com/dns/zones/) + + +## Example Usage - Dns Managed Zone Basic + + +```hcl +resource "google_dns_managed_zone" "example-zone" { + name = "example-zone" + dns_name = "example-${random_id.rnd.hex}.com." + description = "Example DNS zone" + labels = { + foo = "bar" + } +} + +resource "random_id" "rnd" { + byte_length = 4 +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `dns_name` - + (Required) + The DNS name of this managed zone, for instance "example.com.". + +* `name` - + (Required) + User assigned name for this resource. + Must be unique within the project. + + +- - - + + +* `description` - + (Optional) + A textual description field. Defaults to 'Managed by Terraform'. + +* `labels` - + (Optional) + A set of key/value label pairs to assign to this ManagedZone. +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + + +* `name_servers` - + Delegate your managed_zone to these virtual name servers; + defined by the server + + + +## Import + +ManagedZone can be imported using any of these accepted formats: + +``` +$ terraform import google_dns_managed_zone.default projects/{{project}}/managedZones/{{name}} +$ terraform import google_dns_managed_zone.default {{project}}/{{name}} +$ terraform import google_dns_managed_zone.default {{name}} +``` diff --git a/website/docs/r/dns_managed_zone.markdown b/website/docs/r/dns_managed_zone.markdown deleted file mode 100644 index e5b89dbb61..0000000000 --- a/website/docs/r/dns_managed_zone.markdown +++ /dev/null @@ -1,63 +0,0 @@ ---- -layout: "google" -page_title: "Google: google_dns_managed_zone" -sidebar_current: "docs-google-dns-managed-zone" -description: |- - Manages a zone within Google Cloud DNS. ---- - -# google\_dns\_managed_zone - -Manages a zone within Google Cloud DNS. For more information see [the official documentation](https://cloud.google.com/dns/zones/) and -[API](https://cloud.google.com/dns/api/v1/managedZones). - -## Example Usage - -```hcl -resource "google_dns_managed_zone" "prod" { - name = "prod-zone" - dns_name = "prod.mydomain.com." - description = "Production DNS zone" - - labels = { - foo = "bar" - } -} -``` - -## Argument Reference - -The following arguments are supported: - -* `dns_name` - (Required) The fully qualified DNS name of this zone, e.g. `terraform.io.`. - -* `name` - (Required) A unique name for the resource, required by GCE. - Changing this forces a new resource to be created. - -- - - - -* `description` - (Optional) A textual description field. Defaults to 'Managed by Terraform'. - -* `project` - (Optional) The ID of the project in which the resource belongs. If it - is not provided, the provider project is used. - -* `labels` - (Optional) A set of key/value label pairs to assign to the instance. - -## Attributes Reference - -In addition to the arguments listed above, the following computed attributes are -exported: - -* `name_servers` - The list of nameservers that will be authoritative for this - domain. Use NS records to redirect from your DNS provider to these names, - thus making Google Cloud DNS authoritative for this zone. - -## Import - -Managed zones can be imported using any of these accepted formats: - -``` -$ terraform import google_dns_managed_zone.prod projects/{{project-id}}/managedZones/{{zone}} -$ terraform import google_compute_disk.default {{project-id}}/managedZones/{{zone}} -$ terraform import google_compute_disk.default {{zone}} -```