terraform/builtin/providers/google/resource_dns_managed_zone.go

111 lines
2.5 KiB
Go
Raw Normal View History

2015-04-30 07:32:34 +02:00
package google
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
"google.golang.org/api/googleapi"
)
func resourceDnsManagedZone() *schema.Resource {
return &schema.Resource{
Create: resourceDnsManagedZoneCreate,
Read: resourceDnsManagedZoneRead,
Delete: resourceDnsManagedZoneDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"dns_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "Managed by Terraform",
2015-04-30 07:32:34 +02:00
},
"name_servers": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
// Google Cloud DNS ManagedZone resources do not have a SelfLink attribute.
},
}
}
func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
2015-06-24 07:31:24 +02:00
// Build the parameter
zone := &dns.ManagedZone{
Name: d.Get("name").(string),
2015-04-30 07:32:34 +02:00
DnsName: d.Get("dns_name").(string),
2015-06-24 07:31:24 +02:00
}
// 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)
}
2015-04-30 07:32:34 +02:00
log.Printf("[DEBUG] DNS ManagedZone create request: %#v", zone)
zone, err := config.clientDns.ManagedZones.Create(config.Project, zone).Do()
if err != nil {
return fmt.Errorf("Error creating DNS ManagedZone: %s", err)
}
d.SetId(zone.Name)
return resourceDnsManagedZoneRead(d, meta)
}
func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
zone, err := config.clientDns.ManagedZones.Get(
config.Project, d.Id()).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Removing DNS Managed Zone %q because it's gone", d.Get("name").(string))
2015-04-30 07:32:34 +02:00
// The resource doesn't exist anymore
d.SetId("")
return nil
}
return fmt.Errorf("Error reading DNS ManagedZone: %#v", err)
}
2015-06-24 07:31:24 +02:00
d.Set("name_servers", zone.NameServers)
2015-04-30 07:32:34 +02:00
return nil
}
func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
err := config.clientDns.ManagedZones.Delete(config.Project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting DNS ManagedZone: %s", err)
}
d.SetId("")
return nil
}