github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_dns_zone.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/jen20/riviera/dns"
     9  )
    10  
    11  func resourceArmDnsZone() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceArmDnsZoneCreate,
    14  		Read:   resourceArmDnsZoneRead,
    15  		Update: resourceArmDnsZoneCreate,
    16  		Delete: resourceArmDnsZoneDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"resource_group_name": &schema.Schema{
    29  				Type:             schema.TypeString,
    30  				Required:         true,
    31  				ForceNew:         true,
    32  				DiffSuppressFunc: resourceAzurermResourceGroupNameDiffSuppress,
    33  			},
    34  
    35  			"number_of_record_sets": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				Computed: true,
    39  			},
    40  
    41  			"max_number_of_record_sets": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Optional: true,
    44  				Computed: true,
    45  			},
    46  
    47  			"name_servers": &schema.Schema{
    48  				Type:     schema.TypeSet,
    49  				Computed: true,
    50  				Elem:     &schema.Schema{Type: schema.TypeString},
    51  				Set:      schema.HashString,
    52  			},
    53  		},
    54  	}
    55  }
    56  
    57  func resourceArmDnsZoneCreate(d *schema.ResourceData, meta interface{}) error {
    58  	client := meta.(*ArmClient)
    59  	rivieraClient := client.rivieraClient
    60  
    61  	createRequest := rivieraClient.NewRequest()
    62  	createRequest.Command = &dns.CreateDNSZone{
    63  		Name:              d.Get("name").(string),
    64  		Location:          "global",
    65  		ResourceGroupName: d.Get("resource_group_name").(string),
    66  	}
    67  
    68  	createResponse, err := createRequest.Execute()
    69  	if err != nil {
    70  		return fmt.Errorf("Error creating DNS Zone: %s", err)
    71  	}
    72  	if !createResponse.IsSuccessful() {
    73  		return fmt.Errorf("Error creating DNS Zone: %s", createResponse.Error)
    74  	}
    75  
    76  	readRequest := rivieraClient.NewRequest()
    77  	readRequest.Command = &dns.GetDNSZone{
    78  		Name:              d.Get("name").(string),
    79  		ResourceGroupName: d.Get("resource_group_name").(string),
    80  	}
    81  
    82  	readResponse, err := readRequest.Execute()
    83  	if err != nil {
    84  		return fmt.Errorf("Error reading DNS Zone: %s", err)
    85  	}
    86  	if !readResponse.IsSuccessful() {
    87  		return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)
    88  	}
    89  
    90  	resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)
    91  	d.SetId(*resp.ID)
    92  
    93  	return resourceArmDnsZoneRead(d, meta)
    94  }
    95  
    96  func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
    97  	client := meta.(*ArmClient)
    98  	rivieraClient := client.rivieraClient
    99  
   100  	id, err := parseAzureResourceID(d.Id())
   101  	if err != nil {
   102  		return err
   103  	}
   104  	resGroup := id.ResourceGroup
   105  
   106  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   107  	readRequest.Command = &dns.GetDNSZone{}
   108  
   109  	readResponse, err := readRequest.Execute()
   110  	if err != nil {
   111  		return fmt.Errorf("Error reading DNS Zone: %s", err)
   112  	}
   113  	if !readResponse.IsSuccessful() {
   114  		log.Printf("[INFO] Error reading DNS Zone %q - removing from state", d.Id())
   115  		d.SetId("")
   116  		return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)
   117  	}
   118  
   119  	resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)
   120  
   121  	d.Set("resource_group_name", resGroup)
   122  	d.Set("number_of_record_sets", resp.NumberOfRecordSets)
   123  	d.Set("max_number_of_record_sets", resp.MaxNumberOfRecordSets)
   124  	d.Set("name", resp.Name)
   125  
   126  	nameServers := make([]string, 0, len(resp.NameServers))
   127  	for _, ns := range resp.NameServers {
   128  		nameServers = append(nameServers, *ns)
   129  	}
   130  	if err := d.Set("name_servers", nameServers); err != nil {
   131  		return err
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  func resourceArmDnsZoneDelete(d *schema.ResourceData, meta interface{}) error {
   138  	client := meta.(*ArmClient)
   139  	rivieraClient := client.rivieraClient
   140  
   141  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   142  	deleteRequest.Command = &dns.DeleteDNSZone{}
   143  
   144  	deleteResponse, err := deleteRequest.Execute()
   145  	if err != nil {
   146  		return fmt.Errorf("Error deleting DNS Zone: %s", err)
   147  	}
   148  	if !deleteResponse.IsSuccessful() {
   149  		return fmt.Errorf("Error deleting DNS Zone: %s", deleteResponse.Error)
   150  	}
   151  
   152  	return nil
   153  }