github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  			"tags": tagsSchema(),
    55  		},
    56  	}
    57  }
    58  
    59  func resourceArmDnsZoneCreate(d *schema.ResourceData, meta interface{}) error {
    60  	client := meta.(*ArmClient)
    61  	rivieraClient := client.rivieraClient
    62  
    63  	tags := d.Get("tags").(map[string]interface{})
    64  	expandedTags := expandTags(tags)
    65  
    66  	createRequest := rivieraClient.NewRequest()
    67  	createRequest.Command = &dns.CreateDNSZone{
    68  		Name:              d.Get("name").(string),
    69  		Location:          "global",
    70  		ResourceGroupName: d.Get("resource_group_name").(string),
    71  		Tags:              *expandedTags,
    72  	}
    73  
    74  	createResponse, err := createRequest.Execute()
    75  	if err != nil {
    76  		return fmt.Errorf("Error creating DNS Zone: %s", err)
    77  	}
    78  	if !createResponse.IsSuccessful() {
    79  		return fmt.Errorf("Error creating DNS Zone: %s", createResponse.Error)
    80  	}
    81  
    82  	readRequest := rivieraClient.NewRequest()
    83  	readRequest.Command = &dns.GetDNSZone{
    84  		Name:              d.Get("name").(string),
    85  		ResourceGroupName: d.Get("resource_group_name").(string),
    86  	}
    87  
    88  	readResponse, err := readRequest.Execute()
    89  	if err != nil {
    90  		return fmt.Errorf("Error reading DNS Zone: %s", err)
    91  	}
    92  	if !readResponse.IsSuccessful() {
    93  		return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)
    94  	}
    95  
    96  	resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)
    97  	d.SetId(*resp.ID)
    98  
    99  	return resourceArmDnsZoneRead(d, meta)
   100  }
   101  
   102  func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
   103  	client := meta.(*ArmClient)
   104  	rivieraClient := client.rivieraClient
   105  
   106  	id, err := parseAzureResourceID(d.Id())
   107  	if err != nil {
   108  		return err
   109  	}
   110  	resGroup := id.ResourceGroup
   111  
   112  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   113  	readRequest.Command = &dns.GetDNSZone{}
   114  
   115  	readResponse, err := readRequest.Execute()
   116  	if err != nil {
   117  		return fmt.Errorf("Error reading DNS Zone: %s", err)
   118  	}
   119  	if !readResponse.IsSuccessful() {
   120  		log.Printf("[INFO] Error reading DNS Zone %q - removing from state", d.Id())
   121  		d.SetId("")
   122  		return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)
   123  	}
   124  
   125  	resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)
   126  
   127  	d.Set("resource_group_name", resGroup)
   128  	d.Set("number_of_record_sets", resp.NumberOfRecordSets)
   129  	d.Set("max_number_of_record_sets", resp.MaxNumberOfRecordSets)
   130  	d.Set("name", resp.Name)
   131  
   132  	nameServers := make([]string, 0, len(resp.NameServers))
   133  	for _, ns := range resp.NameServers {
   134  		nameServers = append(nameServers, *ns)
   135  	}
   136  	if err := d.Set("name_servers", nameServers); err != nil {
   137  		return err
   138  	}
   139  
   140  	flattenAndSetTags(d, resp.Tags)
   141  
   142  	return nil
   143  }
   144  
   145  func resourceArmDnsZoneDelete(d *schema.ResourceData, meta interface{}) error {
   146  	client := meta.(*ArmClient)
   147  	rivieraClient := client.rivieraClient
   148  
   149  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   150  	deleteRequest.Command = &dns.DeleteDNSZone{}
   151  
   152  	deleteResponse, err := deleteRequest.Execute()
   153  	if err != nil {
   154  		return fmt.Errorf("Error deleting DNS Zone: %s", err)
   155  	}
   156  	if !deleteResponse.IsSuccessful() {
   157  		return fmt.Errorf("Error deleting DNS Zone: %s", deleteResponse.Error)
   158  	}
   159  
   160  	return nil
   161  }