github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/resource_openstack_networking_router_route_v2.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 9 "github.com/gophercloud/gophercloud" 10 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" 11 ) 12 13 func resourceNetworkingRouterRouteV2() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceNetworkingRouterRouteV2Create, 16 Read: resourceNetworkingRouterRouteV2Read, 17 Delete: resourceNetworkingRouterRouteV2Delete, 18 19 Schema: map[string]*schema.Schema{ 20 "region": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""), 25 }, 26 "router_id": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 "destination_cidr": &schema.Schema{ 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 }, 36 "next_hop": &schema.Schema{ 37 Type: schema.TypeString, 38 Required: true, 39 ForceNew: true, 40 }, 41 }, 42 } 43 } 44 45 func resourceNetworkingRouterRouteV2Create(d *schema.ResourceData, meta interface{}) error { 46 47 routerId := d.Get("router_id").(string) 48 osMutexKV.Lock(routerId) 49 defer osMutexKV.Unlock(routerId) 50 51 var destCidr string = d.Get("destination_cidr").(string) 52 var nextHop string = d.Get("next_hop").(string) 53 54 config := meta.(*Config) 55 networkingClient, err := config.networkingV2Client(GetRegion(d)) 56 if err != nil { 57 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 58 } 59 60 n, err := routers.Get(networkingClient, routerId).Extract() 61 if err != nil { 62 if _, ok := err.(gophercloud.ErrDefault404); ok { 63 d.SetId("") 64 return nil 65 } 66 67 return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err) 68 } 69 70 var updateOpts routers.UpdateOpts 71 var routeExists bool = false 72 73 var rts []routers.Route = n.Routes 74 for _, r := range rts { 75 76 if r.DestinationCIDR == destCidr && r.NextHop == nextHop { 77 routeExists = true 78 break 79 } 80 } 81 82 if !routeExists { 83 84 if destCidr != "" && nextHop != "" { 85 r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop} 86 log.Printf( 87 "[INFO] Adding route %s", r) 88 rts = append(rts, r) 89 } 90 91 updateOpts.Routes = rts 92 93 log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts) 94 95 _, err = routers.Update(networkingClient, routerId, updateOpts).Extract() 96 if err != nil { 97 return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err) 98 } 99 d.SetId(fmt.Sprintf("%s-route-%s-%s", routerId, destCidr, nextHop)) 100 101 } else { 102 log.Printf("[DEBUG] Router %s has route already", routerId) 103 } 104 105 return resourceNetworkingRouterRouteV2Read(d, meta) 106 } 107 108 func resourceNetworkingRouterRouteV2Read(d *schema.ResourceData, meta interface{}) error { 109 110 routerId := d.Get("router_id").(string) 111 112 config := meta.(*Config) 113 networkingClient, err := config.networkingV2Client(GetRegion(d)) 114 if err != nil { 115 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 116 } 117 118 n, err := routers.Get(networkingClient, routerId).Extract() 119 if err != nil { 120 if _, ok := err.(gophercloud.ErrDefault404); ok { 121 d.SetId("") 122 return nil 123 } 124 125 return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err) 126 } 127 128 log.Printf("[DEBUG] Retrieved Router %s: %+v", routerId, n) 129 130 var destCidr string = d.Get("destination_cidr").(string) 131 var nextHop string = d.Get("next_hop").(string) 132 133 d.Set("next_hop", "") 134 d.Set("destination_cidr", "") 135 136 for _, r := range n.Routes { 137 138 if r.DestinationCIDR == destCidr && r.NextHop == nextHop { 139 d.Set("destination_cidr", destCidr) 140 d.Set("next_hop", nextHop) 141 break 142 } 143 } 144 145 return nil 146 } 147 148 func resourceNetworkingRouterRouteV2Delete(d *schema.ResourceData, meta interface{}) error { 149 150 routerId := d.Get("router_id").(string) 151 osMutexKV.Lock(routerId) 152 defer osMutexKV.Unlock(routerId) 153 154 config := meta.(*Config) 155 156 networkingClient, err := config.networkingV2Client(GetRegion(d)) 157 if err != nil { 158 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 159 } 160 161 n, err := routers.Get(networkingClient, routerId).Extract() 162 if err != nil { 163 if _, ok := err.(gophercloud.ErrDefault404); ok { 164 return nil 165 } 166 167 return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err) 168 } 169 170 var updateOpts routers.UpdateOpts 171 172 var destCidr string = d.Get("destination_cidr").(string) 173 var nextHop string = d.Get("next_hop").(string) 174 175 var oldRts []routers.Route = n.Routes 176 var newRts []routers.Route 177 178 for _, r := range oldRts { 179 180 if r.DestinationCIDR != destCidr || r.NextHop != nextHop { 181 newRts = append(newRts, r) 182 } 183 } 184 185 if len(oldRts) != len(newRts) { 186 r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop} 187 log.Printf( 188 "[INFO] Deleting route %s", r) 189 updateOpts.Routes = newRts 190 191 log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts) 192 193 _, err = routers.Update(networkingClient, routerId, updateOpts).Extract() 194 if err != nil { 195 return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err) 196 } 197 } else { 198 return fmt.Errorf("Route did not exist already") 199 } 200 201 return nil 202 }