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

Support update for DNS Managed Zone #1617

Merged
merged 2 commits into from
Jun 8, 2018
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
9 changes: 9 additions & 0 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"google.golang.org/api/dataflow/v1b3"
"google.golang.org/api/dataproc/v1"
"google.golang.org/api/dns/v1"
dnsBeta "google.golang.org/api/dns/v1beta2"
"google.golang.org/api/iam/v1"
cloudlogging "google.golang.org/api/logging/v2"
"google.golang.org/api/pubsub/v1"
Expand Down Expand Up @@ -66,6 +67,7 @@ type Config struct {
clientDataproc *dataproc.Service
clientDataflow *dataflow.Service
clientDns *dns.Service
clientDnsBeta *dnsBeta.Service
clientKms *cloudkms.Service
clientLogging *cloudlogging.Service
clientPubsub *pubsub.Service
Expand Down Expand Up @@ -192,6 +194,13 @@ func (c *Config) loadAndValidate() error {
}
c.clientDns.UserAgent = userAgent

log.Printf("[INFO] Instantiating Google Cloud DNS Beta client...")
c.clientDnsBeta, err = dnsBeta.New(client)
if err != nil {
return err
}
c.clientDnsBeta.UserAgent = userAgent

log.Printf("[INFO] Instantiating Google Cloud KMS Client...")
c.clientKms, err = cloudkms.New(client)
if err != nil {
Expand Down
67 changes: 67 additions & 0 deletions google/dns_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"google.golang.org/api/dns/v1beta2"
"log"
"time"
)

type DnsOperationWaiter struct {
Service *dns.ManagedZoneOperationsService
Op *dns.Operation
Project string
}

func (w *DnsOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
var op *dns.Operation
var err error

if w.Op.ZoneContext != nil {
op, err = w.Service.Get(w.Project, w.Op.ZoneContext.NewValue.Name, w.Op.Id).Do()
} else {
return nil, "", fmt.Errorf("unsupported DNS operation %q", w.Op.Id)
}

if err != nil {
return nil, "", err
}

log.Printf("[DEBUG] Got %q when asking for operation %q", op.Status, w.Op.Id)

return op, op.Status, nil
}
}

func (w *DnsOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"done"},
Refresh: w.RefreshFunc(),
}
}

func dnsOperationWait(service *dns.Service, op *dns.Operation, project, activity string) error {
return dnsOperationWaitTime(service, op, project, activity, 4)
}

func dnsOperationWaitTime(service *dns.Service, op *dns.Operation, project, activity string, timeoutMin int) error {
w := &DnsOperationWaiter{
Service: service.ManagedZoneOperations,
Op: op,
Project: project,
}

state := w.Conf()
state.Delay = 10 * time.Second
state.Timeout = time.Duration(timeoutMin) * time.Minute
state.MinTimeout = 2 * time.Second
_, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
}

return nil
}
30 changes: 0 additions & 30 deletions google/import_dns_managed_zone_test.go

This file was deleted.

42 changes: 32 additions & 10 deletions google/resource_dns_managed_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
dnsBeta "google.golang.org/api/dns/v1beta2"
)

func resourceDnsManagedZone() *schema.Resource {
return &schema.Resource{
Create: resourceDnsManagedZoneCreate,
Read: resourceDnsManagedZoneRead,
Update: resourceDnsManagedZoneUpdate,
Delete: resourceDnsManagedZoneDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -32,7 +34,6 @@ func resourceDnsManagedZone() *schema.Resource {
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "Managed by Terraform",
},

Expand Down Expand Up @@ -66,15 +67,9 @@ func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) erro

// Build the parameter
zone := &dns.ManagedZone{
Name: d.Get("name").(string),
DnsName: d.Get("dns_name").(string),
}
// Optional things
if v, ok := d.GetOk("description"); ok {
zone.Description = v.(string)
}
if v, ok := d.GetOk("dns_name"); ok {
zone.DnsName = v.(string)
Name: d.Get("name").(string),
DnsName: d.Get("dns_name").(string),
Description: d.Get("description").(string),
}

log.Printf("[DEBUG] DNS ManagedZone create request: %#v", zone)
Expand Down Expand Up @@ -111,6 +106,33 @@ func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceDnsManagedZoneUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

zone := &dnsBeta.ManagedZone{
Name: d.Get("name").(string),
DnsName: d.Get("dns_name").(string),
Description: d.Get("description").(string),
}

op, err := config.clientDnsBeta.ManagedZones.Patch(project, d.Id(), zone).Do()
if err != nil {
return err
}

err = dnsOperationWait(config.clientDnsBeta, op, project, "Updating DNS Managed Zone")
if err != nil {
return err
}

return resourceDnsManagedZoneRead(d, meta)
}

func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand Down
78 changes: 40 additions & 38 deletions google/resource_dns_managed_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,55 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/dns/v1"
)

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

var zone dns.ManagedZone
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(),
Check: resource.ComposeTestCheckFunc(
testAccCheckDnsManagedZoneExists(
"google_dns_managed_zone.foobar", &zone),
),
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()

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,
},
resource.TestStep{
Config: testAccDnsManagedZone_basic(zoneSuffix, "description2"),
},
resource.TestStep{
ResourceName: "google_dns_managed_zone.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -49,39 +79,11 @@ func testAccCheckDnsManagedZoneDestroy(s *terraform.State) error {
return nil
}

func testAccCheckDnsManagedZoneExists(n string, zone *dns.ManagedZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)

found, err := config.clientDns.ManagedZones.Get(
config.Project, rs.Primary.ID).Do()
if err != nil {
return err
}

if found.Name != rs.Primary.ID {
return fmt.Errorf("DNS Zone not found")
}

*zone = *found

return nil
}
}

func testAccDnsManagedZone_basic() string {
func testAccDnsManagedZone_basic(suffix, description string) string {
return fmt.Sprintf(`
resource "google_dns_managed_zone" "foobar" {
name = "mzone-test-%s"
dns_name = "tf-acctest-%s.hashicorptest.com."
}`, acctest.RandString(10), acctest.RandString(10))
description = "%s"
}`, suffix, suffix, description)
}
Loading