github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/azure/resource_azure_dns_server.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/management/virtualnetwork"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  // resourceAzureDnsServer returns the *schema.Resource associated
    12  // to an Azure hosted service.
    13  func resourceAzureDnsServer() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAzureDnsServerCreate,
    16  		Read:   resourceAzureDnsServerRead,
    17  		Update: resourceAzureDnsServerUpdate,
    18  		Exists: resourceAzureDnsServerExists,
    19  		Delete: resourceAzureDnsServerDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:        schema.TypeString,
    24  				ForceNew:    true,
    25  				Required:    true,
    26  				Description: parameterDescriptions["name"],
    27  			},
    28  			"dns_address": &schema.Schema{
    29  				Type:        schema.TypeString,
    30  				Required:    true,
    31  				Description: parameterDescriptions["dns_address"],
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  // resourceAzureDnsServerCreate does all the necessary API calls
    38  // to create a new DNS server definition on Azure.
    39  func resourceAzureDnsServerCreate(d *schema.ResourceData, meta interface{}) error {
    40  	azureClient := meta.(*Client)
    41  	mgmtClient := azureClient.mgmtClient
    42  	vnetClient := azureClient.vnetClient
    43  
    44  	log.Println("[INFO] Fetching current network configuration from Azure.")
    45  	azureClient.mutex.Lock()
    46  	defer azureClient.mutex.Unlock()
    47  	netConf, err := vnetClient.GetVirtualNetworkConfiguration()
    48  	if err != nil {
    49  		return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err)
    50  	}
    51  
    52  	log.Println("[DEBUG] Adding new DNS server definition to Azure.")
    53  	name := d.Get("name").(string)
    54  	address := d.Get("dns_address").(string)
    55  	netConf.Configuration.DNS.DNSServers = append(
    56  		netConf.Configuration.DNS.DNSServers,
    57  		virtualnetwork.DNSServer{
    58  			Name:      name,
    59  			IPAddress: address,
    60  		})
    61  
    62  	// send the configuration back to Azure:
    63  	log.Println("[INFO] Sending updated network configuration back to Azure.")
    64  	reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf)
    65  	if err != nil {
    66  		return fmt.Errorf("Failed issuing update to network configuration: %s", err)
    67  	}
    68  	err = mgmtClient.WaitForOperation(reqID, nil)
    69  	if err != nil {
    70  		return fmt.Errorf("Error setting network configuration: %s", err)
    71  	}
    72  
    73  	d.SetId(name)
    74  	return nil
    75  }
    76  
    77  // resourceAzureDnsServerRead does all the necessary API calls to read
    78  // the state of the DNS server off Azure.
    79  func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error {
    80  	vnetClient := meta.(*Client).vnetClient
    81  
    82  	log.Println("[INFO] Fetching current network configuration from Azure.")
    83  	netConf, err := vnetClient.GetVirtualNetworkConfiguration()
    84  	if err != nil {
    85  		return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err)
    86  	}
    87  
    88  	var found bool
    89  	name := d.Get("name").(string)
    90  
    91  	// search for our DNS and update it if the IP has been changed:
    92  	for _, dns := range netConf.Configuration.DNS.DNSServers {
    93  		if dns.Name == name {
    94  			found = true
    95  			d.Set("dns_address", dns.IPAddress)
    96  			break
    97  		}
    98  	}
    99  
   100  	// remove the resource from the state if it has been deleted in the meantime:
   101  	if !found {
   102  		d.SetId("")
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  // resourceAzureDnsServerUpdate does all the necessary API calls
   109  // to update the DNS definition on Azure.
   110  func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) error {
   111  	azureClient := meta.(*Client)
   112  	mgmtClient := azureClient.mgmtClient
   113  	vnetClient := azureClient.vnetClient
   114  
   115  	var found bool
   116  	name := d.Get("name").(string)
   117  
   118  	if d.HasChange("dns_address") {
   119  		log.Println("[DEBUG] DNS server address has changes; updating it on Azure.")
   120  		log.Println("[INFO] Fetching current network configuration from Azure.")
   121  		azureClient.mutex.Lock()
   122  		defer azureClient.mutex.Unlock()
   123  		netConf, err := vnetClient.GetVirtualNetworkConfiguration()
   124  		if err != nil {
   125  			return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err)
   126  		}
   127  
   128  		// search for our DNS and update its address value:
   129  		for i, dns := range netConf.Configuration.DNS.DNSServers {
   130  			if dns.Name == name {
   131  				found = true
   132  				netConf.Configuration.DNS.DNSServers[i].IPAddress = d.Get("dns_address").(string)
   133  				break
   134  			}
   135  		}
   136  
   137  		// if the config has changes, send the configuration back to Azure:
   138  		if found {
   139  			log.Println("[INFO] Sending updated network configuration back to Azure.")
   140  			reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf)
   141  			if err != nil {
   142  				return fmt.Errorf("Failed issuing update to network configuration: %s", err)
   143  			}
   144  			err = mgmtClient.WaitForOperation(reqID, nil)
   145  			if err != nil {
   146  				return fmt.Errorf("Error setting network configuration: %s", err)
   147  			}
   148  
   149  			return nil
   150  		}
   151  	}
   152  
   153  	// remove the resource from the state if it has been deleted in the meantime:
   154  	if !found {
   155  		d.SetId("")
   156  	}
   157  
   158  	return nil
   159  }
   160  
   161  // resourceAzureDnsServerExists does all the necessary API calls to
   162  // check if the DNS server definition alredy exists on Azure.
   163  func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   164  	azureClient := meta.(*Client)
   165  	vnetClient := azureClient.vnetClient
   166  
   167  	log.Println("[INFO] Fetching current network configuration from Azure.")
   168  	netConf, err := vnetClient.GetVirtualNetworkConfiguration()
   169  	if err != nil {
   170  		return false, fmt.Errorf("Failed to get the current network configuration from Azure: %s", err)
   171  	}
   172  
   173  	name := d.Get("name").(string)
   174  
   175  	// search for the DNS server's definition:
   176  	for _, dns := range netConf.Configuration.DNS.DNSServers {
   177  		if dns.Name == name {
   178  			return true, nil
   179  		}
   180  	}
   181  
   182  	// if we reached this point; the resource must have been deleted; and we must untrack it:
   183  	d.SetId("")
   184  	return false, nil
   185  }
   186  
   187  // resourceAzureDnsServerDelete does all the necessary API calls
   188  // to delete the DNS server definition from Azure.
   189  func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) error {
   190  	azureClient := meta.(*Client)
   191  	mgmtClient := azureClient.mgmtClient
   192  	vnetClient := azureClient.vnetClient
   193  
   194  	log.Println("[INFO] Fetching current network configuration from Azure.")
   195  	azureClient.mutex.Lock()
   196  	defer azureClient.mutex.Unlock()
   197  	netConf, err := vnetClient.GetVirtualNetworkConfiguration()
   198  	if err != nil {
   199  		return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err)
   200  	}
   201  
   202  	name := d.Get("name").(string)
   203  
   204  	// search for the DNS server's definition and remove it:
   205  	var found bool
   206  	for i, dns := range netConf.Configuration.DNS.DNSServers {
   207  		if dns.Name == name {
   208  			found = true
   209  			netConf.Configuration.DNS.DNSServers = append(
   210  				netConf.Configuration.DNS.DNSServers[:i],
   211  				netConf.Configuration.DNS.DNSServers[i+1:]...,
   212  			)
   213  			break
   214  		}
   215  	}
   216  
   217  	// if not found; don't bother re-sending the natwork config:
   218  	if !found {
   219  		return nil
   220  	}
   221  
   222  	// send the configuration back to Azure:
   223  	log.Println("[INFO] Sending updated network configuration back to Azure.")
   224  	reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf)
   225  	if err != nil {
   226  		return fmt.Errorf("Failed issuing update to network configuration: %s", err)
   227  	}
   228  	err = mgmtClient.WaitForOperation(reqID, nil)
   229  	if err != nil {
   230  		return fmt.Errorf("Error setting network configuration: %s", err)
   231  	}
   232  
   233  	return nil
   234  }