github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 _, error := routesClient.CreateOrUpdate(resGroup, rtName, name, route, make(chan struct{})) 94 err := <-error 95 if err != nil { 96 return err 97 } 98 99 read, err := routesClient.Get(resGroup, rtName, name) 100 if err != nil { 101 return err 102 } 103 if read.ID == nil { 104 return fmt.Errorf("Cannot read Route %s/%s (resource group %s) ID", rtName, name, resGroup) 105 } 106 d.SetId(*read.ID) 107 108 return resourceArmRouteRead(d, meta) 109 } 110 111 func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error { 112 routesClient := meta.(*ArmClient).routesClient 113 114 id, err := parseAzureResourceID(d.Id()) 115 if err != nil { 116 return err 117 } 118 resGroup := id.ResourceGroup 119 rtName := id.Path["routeTables"] 120 routeName := id.Path["routes"] 121 122 resp, err := routesClient.Get(resGroup, rtName, routeName) 123 if err != nil { 124 if resp.StatusCode == http.StatusNotFound { 125 d.SetId("") 126 return nil 127 } 128 return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err) 129 } 130 131 d.Set("name", routeName) 132 d.Set("resource_group_name", resGroup) 133 d.Set("route_table_name", rtName) 134 d.Set("address_prefix", resp.RoutePropertiesFormat.AddressPrefix) 135 d.Set("next_hop_type", string(resp.RoutePropertiesFormat.NextHopType)) 136 137 if resp.RoutePropertiesFormat.NextHopIPAddress != nil { 138 d.Set("next_hop_in_ip_address", resp.RoutePropertiesFormat.NextHopIPAddress) 139 } 140 141 return nil 142 } 143 144 func resourceArmRouteDelete(d *schema.ResourceData, meta interface{}) error { 145 client := meta.(*ArmClient) 146 routesClient := client.routesClient 147 148 id, err := parseAzureResourceID(d.Id()) 149 if err != nil { 150 return err 151 } 152 resGroup := id.ResourceGroup 153 rtName := id.Path["routeTables"] 154 routeName := id.Path["routes"] 155 156 armMutexKV.Lock(rtName) 157 defer armMutexKV.Unlock(rtName) 158 159 _, error := routesClient.Delete(resGroup, rtName, routeName, make(chan struct{})) 160 err = <-error 161 162 return err 163 }