github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_route.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/arm/network"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceArmRoute() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceArmRouteCreate,
    14  		Read:   resourceArmRouteRead,
    15  		Update: resourceArmRouteCreate,
    16  		Delete: resourceArmRouteDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": {
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"resource_group_name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"route_table_name": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"address_prefix": {
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  			},
    41  
    42  			"next_hop_type": {
    43  				Type:         schema.TypeString,
    44  				Required:     true,
    45  				ValidateFunc: validateRouteTableNextHopType,
    46  			},
    47  
    48  			"next_hop_in_ip_address": {
    49  				Type:     schema.TypeString,
    50  				Optional: true,
    51  				Computed: true,
    52  			},
    53  		},
    54  	}
    55  }
    56  
    57  func resourceArmRouteCreate(d *schema.ResourceData, meta interface{}) error {
    58  	client := meta.(*ArmClient)
    59  	routesClient := client.routesClient
    60  
    61  	name := d.Get("name").(string)
    62  	rtName := d.Get("route_table_name").(string)
    63  	resGroup := d.Get("resource_group_name").(string)
    64  
    65  	addressPrefix := d.Get("address_prefix").(string)
    66  	nextHopType := d.Get("next_hop_type").(string)
    67  
    68  	armMutexKV.Lock(rtName)
    69  	defer armMutexKV.Unlock(rtName)
    70  
    71  	properties := network.RoutePropertiesFormat{
    72  		AddressPrefix: &addressPrefix,
    73  		NextHopType:   network.RouteNextHopType(nextHopType),
    74  	}
    75  
    76  	if v, ok := d.GetOk("next_hop_in_ip_address"); ok {
    77  		nextHopInIpAddress := v.(string)
    78  		properties.NextHopIPAddress = &nextHopInIpAddress
    79  	}
    80  
    81  	route := network.Route{
    82  		Name:       &name,
    83  		Properties: &properties,
    84  	}
    85  
    86  	_, err := routesClient.CreateOrUpdate(resGroup, rtName, name, route, make(chan struct{}))
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	read, err := routesClient.Get(resGroup, rtName, name)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	if read.ID == nil {
    96  		return fmt.Errorf("Cannot read Route %s/%s (resource group %s) ID", rtName, name, resGroup)
    97  	}
    98  	d.SetId(*read.ID)
    99  
   100  	return resourceArmRouteRead(d, meta)
   101  }
   102  
   103  func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error {
   104  	routesClient := meta.(*ArmClient).routesClient
   105  
   106  	id, err := parseAzureResourceID(d.Id())
   107  	if err != nil {
   108  		return err
   109  	}
   110  	resGroup := id.ResourceGroup
   111  	rtName := id.Path["routeTables"]
   112  	routeName := id.Path["routes"]
   113  
   114  	resp, err := routesClient.Get(resGroup, rtName, routeName)
   115  	if err != nil {
   116  		return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err)
   117  	}
   118  	if resp.StatusCode == http.StatusNotFound {
   119  		d.SetId("")
   120  		return nil
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  func resourceArmRouteDelete(d *schema.ResourceData, meta interface{}) error {
   127  	client := meta.(*ArmClient)
   128  	routesClient := client.routesClient
   129  
   130  	id, err := parseAzureResourceID(d.Id())
   131  	if err != nil {
   132  		return err
   133  	}
   134  	resGroup := id.ResourceGroup
   135  	rtName := id.Path["routeTables"]
   136  	routeName := id.Path["routes"]
   137  
   138  	armMutexKV.Lock(rtName)
   139  	defer armMutexKV.Unlock(rtName)
   140  
   141  	_, err = routesClient.Delete(resGroup, rtName, routeName, make(chan struct{}))
   142  
   143  	return err
   144  }