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