|
| 1 | +package azurerm |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Azure/azure-sdk-for-go/arm/network" |
| 10 | + "github.com/hashicorp/terraform/helper/resource" |
| 11 | + "github.com/hashicorp/terraform/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceArmRoute() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceArmRouteCreate, |
| 17 | + Read: resourceArmRouteRead, |
| 18 | + Update: resourceArmRouteCreate, |
| 19 | + Delete: resourceArmRouteDelete, |
| 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 | + }, |
| 33 | + |
| 34 | + "route_table_name": &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + |
| 40 | + "address_prefix": &schema.Schema{ |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + }, |
| 44 | + |
| 45 | + "next_hop_type": &schema.Schema{ |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + ValidateFunc: validateRouteTableNextHopType, |
| 49 | + }, |
| 50 | + |
| 51 | + "next_hop_in_ip_address": &schema.Schema{ |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func resourceArmRouteCreate(d *schema.ResourceData, meta interface{}) error { |
| 61 | + client := meta.(*ArmClient) |
| 62 | + routesClient := client.routesClient |
| 63 | + |
| 64 | + name := d.Get("name").(string) |
| 65 | + rtName := d.Get("route_table_name").(string) |
| 66 | + resGroup := d.Get("resource_group_name").(string) |
| 67 | + |
| 68 | + addressPrefix := d.Get("address_prefix").(string) |
| 69 | + nextHopType := d.Get("next_hop_type").(string) |
| 70 | + |
| 71 | + armMutexKV.Lock(rtName) |
| 72 | + defer armMutexKV.Unlock(rtName) |
| 73 | + |
| 74 | + properties := network.RoutePropertiesFormat{ |
| 75 | + AddressPrefix: &addressPrefix, |
| 76 | + NextHopType: network.RouteNextHopType(nextHopType), |
| 77 | + } |
| 78 | + |
| 79 | + if v, ok := d.GetOk("next_hop_in_ip_address"); ok { |
| 80 | + nextHopInIpAddress := v.(string) |
| 81 | + properties.NextHopIPAddress = &nextHopInIpAddress |
| 82 | + } |
| 83 | + |
| 84 | + route := network.Route{ |
| 85 | + Name: &name, |
| 86 | + Properties: &properties, |
| 87 | + } |
| 88 | + |
| 89 | + resp, err := routesClient.CreateOrUpdate(resGroup, rtName, name, route) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + d.SetId(*resp.ID) |
| 94 | + |
| 95 | + log.Printf("[DEBUG] Waiting for Route (%s) to become available", name) |
| 96 | + stateConf := &resource.StateChangeConf{ |
| 97 | + Pending: []string{"Accepted", "Updating"}, |
| 98 | + Target: "Succeeded", |
| 99 | + Refresh: routeStateRefreshFunc(client, resGroup, rtName, name), |
| 100 | + Timeout: 10 * time.Minute, |
| 101 | + } |
| 102 | + if _, err := stateConf.WaitForState(); err != nil { |
| 103 | + return fmt.Errorf("Error waiting for Route (%s) to become available: %s", name, err) |
| 104 | + } |
| 105 | + |
| 106 | + return resourceArmRouteRead(d, meta) |
| 107 | +} |
| 108 | + |
| 109 | +func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + routesClient := meta.(*ArmClient).routesClient |
| 111 | + |
| 112 | + id, err := parseAzureResourceID(d.Id()) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + resGroup := id.ResourceGroup |
| 117 | + rtName := id.Path["routeTables"] |
| 118 | + routeName := id.Path["routes"] |
| 119 | + |
| 120 | + resp, err := routesClient.Get(resGroup, rtName, routeName) |
| 121 | + if resp.StatusCode == http.StatusNotFound { |
| 122 | + d.SetId("") |
| 123 | + return nil |
| 124 | + } |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err) |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func resourceArmRouteDelete(d *schema.ResourceData, meta interface{}) error { |
| 133 | + client := meta.(*ArmClient) |
| 134 | + routesClient := client.routesClient |
| 135 | + |
| 136 | + id, err := parseAzureResourceID(d.Id()) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + resGroup := id.ResourceGroup |
| 141 | + rtName := id.Path["routeTables"] |
| 142 | + routeName := id.Path["routes"] |
| 143 | + |
| 144 | + armMutexKV.Lock(rtName) |
| 145 | + defer armMutexKV.Unlock(rtName) |
| 146 | + |
| 147 | + _, err = routesClient.Delete(resGroup, rtName, routeName) |
| 148 | + |
| 149 | + return err |
| 150 | +} |
| 151 | + |
| 152 | +func routeStateRefreshFunc(client *ArmClient, resourceGroupName string, routeTableName string, routeName string) resource.StateRefreshFunc { |
| 153 | + return func() (interface{}, string, error) { |
| 154 | + res, err := client.routesClient.Get(resourceGroupName, routeTableName, routeName) |
| 155 | + if err != nil { |
| 156 | + return nil, "", fmt.Errorf("Error issuing read request in routeStateRefreshFunc to Azure ARM for route '%s' (RG: '%s') (NSG: '%s'): %s", routeName, resourceGroupName, routeTableName, err) |
| 157 | + } |
| 158 | + |
| 159 | + return res, *res.Properties.ProvisioningState, nil |
| 160 | + } |
| 161 | +} |
0 commit comments