github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/resource_openstack_networking_router_interface_v2.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 11 "github.com/gophercloud/gophercloud" 12 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" 13 "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" 14 ) 15 16 func resourceNetworkingRouterInterfaceV2() *schema.Resource { 17 return &schema.Resource{ 18 Create: resourceNetworkingRouterInterfaceV2Create, 19 Read: resourceNetworkingRouterInterfaceV2Read, 20 Delete: resourceNetworkingRouterInterfaceV2Delete, 21 22 Schema: map[string]*schema.Schema{ 23 "region": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""), 28 }, 29 "router_id": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 "subnet_id": &schema.Schema{ 35 Type: schema.TypeString, 36 Optional: true, 37 ForceNew: true, 38 }, 39 "port_id": &schema.Schema{ 40 Type: schema.TypeString, 41 Optional: true, 42 ForceNew: true, 43 }, 44 }, 45 } 46 } 47 48 func resourceNetworkingRouterInterfaceV2Create(d *schema.ResourceData, meta interface{}) error { 49 config := meta.(*Config) 50 networkingClient, err := config.networkingV2Client(GetRegion(d)) 51 if err != nil { 52 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 53 } 54 55 createOpts := routers.AddInterfaceOpts{ 56 SubnetID: d.Get("subnet_id").(string), 57 PortID: d.Get("port_id").(string), 58 } 59 60 log.Printf("[DEBUG] Create Options: %#v", createOpts) 61 n, err := routers.AddInterface(networkingClient, d.Get("router_id").(string), createOpts).Extract() 62 if err != nil { 63 return fmt.Errorf("Error creating OpenStack Neutron router interface: %s", err) 64 } 65 log.Printf("[INFO] Router interface Port ID: %s", n.PortID) 66 67 log.Printf("[DEBUG] Waiting for Router Interface (%s) to become available", n.PortID) 68 69 stateConf := &resource.StateChangeConf{ 70 Pending: []string{"BUILD", "PENDING_CREATE", "PENDING_UPDATE"}, 71 Target: []string{"ACTIVE"}, 72 Refresh: waitForRouterInterfaceActive(networkingClient, n.PortID), 73 Timeout: 2 * time.Minute, 74 Delay: 5 * time.Second, 75 MinTimeout: 3 * time.Second, 76 } 77 78 _, err = stateConf.WaitForState() 79 80 d.SetId(n.PortID) 81 82 return resourceNetworkingRouterInterfaceV2Read(d, meta) 83 } 84 85 func resourceNetworkingRouterInterfaceV2Read(d *schema.ResourceData, meta interface{}) error { 86 config := meta.(*Config) 87 networkingClient, err := config.networkingV2Client(GetRegion(d)) 88 if err != nil { 89 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 90 } 91 92 n, err := ports.Get(networkingClient, d.Id()).Extract() 93 if err != nil { 94 if _, ok := err.(gophercloud.ErrDefault404); ok { 95 d.SetId("") 96 return nil 97 } 98 99 return fmt.Errorf("Error retrieving OpenStack Neutron Router Interface: %s", err) 100 } 101 102 log.Printf("[DEBUG] Retrieved Router Interface %s: %+v", d.Id(), n) 103 104 return nil 105 } 106 107 func resourceNetworkingRouterInterfaceV2Delete(d *schema.ResourceData, meta interface{}) error { 108 config := meta.(*Config) 109 networkingClient, err := config.networkingV2Client(GetRegion(d)) 110 if err != nil { 111 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 112 } 113 114 stateConf := &resource.StateChangeConf{ 115 Pending: []string{"ACTIVE"}, 116 Target: []string{"DELETED"}, 117 Refresh: waitForRouterInterfaceDelete(networkingClient, d), 118 Timeout: 2 * time.Minute, 119 Delay: 5 * time.Second, 120 MinTimeout: 3 * time.Second, 121 } 122 123 _, err = stateConf.WaitForState() 124 if err != nil { 125 return fmt.Errorf("Error deleting OpenStack Neutron Router Interface: %s", err) 126 } 127 128 d.SetId("") 129 return nil 130 } 131 132 func waitForRouterInterfaceActive(networkingClient *gophercloud.ServiceClient, rId string) resource.StateRefreshFunc { 133 return func() (interface{}, string, error) { 134 r, err := ports.Get(networkingClient, rId).Extract() 135 if err != nil { 136 return nil, "", err 137 } 138 139 log.Printf("[DEBUG] OpenStack Neutron Router Interface: %+v", r) 140 return r, r.Status, nil 141 } 142 } 143 144 func waitForRouterInterfaceDelete(networkingClient *gophercloud.ServiceClient, d *schema.ResourceData) resource.StateRefreshFunc { 145 return func() (interface{}, string, error) { 146 routerId := d.Get("router_id").(string) 147 routerInterfaceId := d.Id() 148 149 log.Printf("[DEBUG] Attempting to delete OpenStack Router Interface %s.", routerInterfaceId) 150 151 removeOpts := routers.RemoveInterfaceOpts{ 152 SubnetID: d.Get("subnet_id").(string), 153 PortID: d.Get("port_id").(string), 154 } 155 156 r, err := ports.Get(networkingClient, routerInterfaceId).Extract() 157 if err != nil { 158 if _, ok := err.(gophercloud.ErrDefault404); ok { 159 log.Printf("[DEBUG] Successfully deleted OpenStack Router Interface %s", routerInterfaceId) 160 return r, "DELETED", nil 161 } 162 return r, "ACTIVE", err 163 } 164 165 _, err = routers.RemoveInterface(networkingClient, routerId, removeOpts).Extract() 166 if err != nil { 167 if _, ok := err.(gophercloud.ErrDefault404); ok { 168 log.Printf("[DEBUG] Successfully deleted OpenStack Router Interface %s.", routerInterfaceId) 169 return r, "DELETED", nil 170 } 171 if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok { 172 if errCode.Actual == 409 { 173 log.Printf("[DEBUG] Router Interface %s is still in use.", routerInterfaceId) 174 return r, "ACTIVE", nil 175 } 176 } 177 178 return r, "ACTIVE", err 179 } 180 181 log.Printf("[DEBUG] OpenStack Router Interface %s is still active.", routerInterfaceId) 182 return r, "ACTIVE", nil 183 } 184 }