github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			},
    33  
    34  			"number_of_record_sets": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  				Computed: true,
    38  			},
    39  
    40  			"max_number_of_record_sets": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  				Computed: true,
    44  			},
    45  
    46  			"name_servers": &schema.Schema{
    47  				Type:     schema.TypeSet,
    48  				Computed: true,
    49  				Elem:     &schema.Schema{Type: schema.TypeString},
    50  				Set:      schema.HashString,
    51  			},
    52  		},
    53  	}
    54  }
    55  
    56  func resourceArmDnsZoneCreate(d *schema.ResourceData, meta interface{}) error {
    57  	client := meta.(*ArmClient)
    58  	rivieraClient := client.rivieraClient
    59  
    60  	createRequest := rivieraClient.NewRequest()
    61  	createRequest.Command = &dns.CreateDNSZone{
    62  		Name:              d.Get("name").(string),
    63  		Location:          "global",
    64  		ResourceGroupName: d.Get("resource_group_name").(string),
    65  	}
    66  
    67  	createResponse, err := createRequest.Execute()
    68  	if err != nil {
    69  		return fmt.Errorf("Error creating DNS Zone: %s", err)
    70  	}
    71  	if !createResponse.IsSuccessful() {
    72  		return fmt.Errorf("Error creating DNS Zone: %s", createResponse.Error)
    73  	}
    74  
    75  	readRequest := rivieraClient.NewRequest()
    76  	readRequest.Command = &dns.GetDNSZone{
    77  		Name:              d.Get("name").(string),
    78  		ResourceGroupName: d.Get("resource_group_name").(string),
    79  	}
    80  
    81  	readResponse, err := readRequest.Execute()
    82  	if err != nil {
    83  		return fmt.Errorf("Error reading DNS Zone: %s", err)
    84  	}
    85  	if !readResponse.IsSuccessful() {
    86  		return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)
    87  	}
    88  
    89  	resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)
    90  	d.SetId(*resp.ID)
    91  
    92  	return resourceArmDnsZoneRead(d, meta)
    93  }
    94  
    95  func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
    96  	client := meta.(*ArmClient)
    97  	rivieraClient := client.rivieraClient
    98  
    99  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   100  	readRequest.Command = &dns.GetDNSZone{}
   101  
   102  	readResponse, err := readRequest.Execute()
   103  	if err != nil {
   104  		return fmt.Errorf("Error reading DNS Zone: %s", err)
   105  	}
   106  	if !readResponse.IsSuccessful() {
   107  		log.Printf("[INFO] Error reading DNS Zone %q - removing from state", d.Id())
   108  		d.SetId("")
   109  		return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)
   110  	}
   111  
   112  	resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)
   113  
   114  	d.Set("number_of_record_sets", resp.NumberOfRecordSets)
   115  	d.Set("max_number_of_record_sets", resp.MaxNumberOfRecordSets)
   116  	d.Set("name", resp.Name)
   117  
   118  	nameServers := make([]string, 0, len(resp.NameServers))
   119  	for _, ns := range resp.NameServers {
   120  		nameServers = append(nameServers, *ns)
   121  	}
   122  	if err := d.Set("name_servers", nameServers); err != nil {
   123  		return err
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  func resourceArmDnsZoneDelete(d *schema.ResourceData, meta interface{}) error {
   130  	client := meta.(*ArmClient)
   131  	rivieraClient := client.rivieraClient
   132  
   133  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   134  	deleteRequest.Command = &dns.DeleteDNSZone{}
   135  
   136  	deleteResponse, err := deleteRequest.Execute()
   137  	if err != nil {
   138  		return fmt.Errorf("Error deleting DNS Zone: %s", err)
   139  	}
   140  	if !deleteResponse.IsSuccessful() {
   141  		return fmt.Errorf("Error deleting DNS Zone: %s", deleteResponse.Error)
   142  	}
   143  
   144  	return nil
   145  }