github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/google/resource_dns_managed_zone.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"google.golang.org/api/dns/v1"
     9  	"google.golang.org/api/googleapi"
    10  )
    11  
    12  func resourceDnsManagedZone() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceDnsManagedZoneCreate,
    15  		Read:   resourceDnsManagedZoneRead,
    16  		Delete: resourceDnsManagedZoneDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"dns_name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"description": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Optional: true,
    34  				ForceNew: true,
    35  				Default:  "Managed by Terraform",
    36  			},
    37  
    38  			"name_servers": &schema.Schema{
    39  				Type:     schema.TypeList,
    40  				Computed: true,
    41  				Elem: &schema.Schema{
    42  					Type: schema.TypeString,
    43  				},
    44  			},
    45  
    46  			// Google Cloud DNS ManagedZone resources do not have a SelfLink attribute.
    47  
    48  			"project": &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Optional: true,
    51  				ForceNew: true,
    52  			},
    53  		},
    54  	}
    55  }
    56  
    57  func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) error {
    58  	config := meta.(*Config)
    59  
    60  	project, err := getProject(d, config)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	// Build the parameter
    66  	zone := &dns.ManagedZone{
    67  		Name:    d.Get("name").(string),
    68  		DnsName: d.Get("dns_name").(string),
    69  	}
    70  	// Optional things
    71  	if v, ok := d.GetOk("description"); ok {
    72  		zone.Description = v.(string)
    73  	}
    74  	if v, ok := d.GetOk("dns_name"); ok {
    75  		zone.DnsName = v.(string)
    76  	}
    77  
    78  	log.Printf("[DEBUG] DNS ManagedZone create request: %#v", zone)
    79  	zone, err = config.clientDns.ManagedZones.Create(project, zone).Do()
    80  	if err != nil {
    81  		return fmt.Errorf("Error creating DNS ManagedZone: %s", err)
    82  	}
    83  
    84  	d.SetId(zone.Name)
    85  
    86  	return resourceDnsManagedZoneRead(d, meta)
    87  }
    88  
    89  func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error {
    90  	config := meta.(*Config)
    91  
    92  	project, err := getProject(d, config)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	zone, err := config.clientDns.ManagedZones.Get(
    98  		project, d.Id()).Do()
    99  	if err != nil {
   100  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   101  			log.Printf("[WARN] Removing DNS Managed Zone %q because it's gone", d.Get("name").(string))
   102  			// The resource doesn't exist anymore
   103  			d.SetId("")
   104  
   105  			return nil
   106  		}
   107  
   108  		return fmt.Errorf("Error reading DNS ManagedZone: %#v", err)
   109  	}
   110  
   111  	d.Set("name_servers", zone.NameServers)
   112  
   113  	return nil
   114  }
   115  
   116  func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) error {
   117  	config := meta.(*Config)
   118  
   119  	project, err := getProject(d, config)
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	err = config.clientDns.ManagedZones.Delete(project, d.Id()).Do()
   125  	if err != nil {
   126  		return fmt.Errorf("Error deleting DNS ManagedZone: %s", err)
   127  	}
   128  
   129  	d.SetId("")
   130  	return nil
   131  }