github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"dns_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  			},
    36  
    37  			"name_servers": &schema.Schema{
    38  				Type:     schema.TypeList,
    39  				Computed: true,
    40  				Elem: &schema.Schema{
    41  					Type: schema.TypeString,
    42  				},
    43  			},
    44  
    45  			// Google Cloud DNS ManagedZone resources do not have a SelfLink attribute.
    46  		},
    47  	}
    48  }
    49  
    50  func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) error {
    51  	config := meta.(*Config)
    52  
    53  	// Build the parameter
    54  	zone := &dns.ManagedZone{
    55  		Name:    d.Get("name").(string),
    56  		DnsName: d.Get("dns_name").(string),
    57  	}
    58  	// Optional things
    59  	if v, ok := d.GetOk("description"); ok {
    60  		zone.Description = v.(string)
    61  	}
    62  	if v, ok := d.GetOk("dns_name"); ok {
    63  		zone.DnsName = v.(string)
    64  	}
    65  
    66  	log.Printf("[DEBUG] DNS ManagedZone create request: %#v", zone)
    67  	zone, err := config.clientDns.ManagedZones.Create(config.Project, zone).Do()
    68  	if err != nil {
    69  		return fmt.Errorf("Error creating DNS ManagedZone: %s", err)
    70  	}
    71  
    72  	d.SetId(zone.Name)
    73  
    74  	return resourceDnsManagedZoneRead(d, meta)
    75  }
    76  
    77  func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error {
    78  	config := meta.(*Config)
    79  
    80  	zone, err := config.clientDns.ManagedZones.Get(
    81  		config.Project, d.Id()).Do()
    82  	if err != nil {
    83  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
    84  			// The resource doesn't exist anymore
    85  			d.SetId("")
    86  
    87  			return nil
    88  		}
    89  
    90  		return fmt.Errorf("Error reading DNS ManagedZone: %#v", err)
    91  	}
    92  
    93  	d.Set("name_servers", zone.NameServers)
    94  
    95  	return nil
    96  }
    97  
    98  func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) error {
    99  	config := meta.(*Config)
   100  
   101  	err := config.clientDns.ManagedZones.Delete(config.Project, d.Id()).Do()
   102  	if err != nil {
   103  		return fmt.Errorf("Error deleting DNS ManagedZone: %s", err)
   104  	}
   105  
   106  	d.SetId("")
   107  	return nil
   108  }