|
| 1 | +package openstack |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/resource" |
| 9 | + |
| 10 | + "github.com/gophercloud/gophercloud" |
| 11 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/listeners" |
| 12 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/loadbalancers" |
| 13 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/monitors" |
| 14 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/pools" |
| 15 | +) |
| 16 | + |
| 17 | +func waitForLBV2Listener(networkingClient *gophercloud.ServiceClient, id string, target string, pending []string, timeout time.Duration) error { |
| 18 | + log.Printf("[DEBUG] Waiting for listener %s to become %s.", id, target) |
| 19 | + |
| 20 | + stateConf := &resource.StateChangeConf{ |
| 21 | + Target: []string{target}, |
| 22 | + Pending: pending, |
| 23 | + Refresh: resourceLBV2ListenerRefreshFunc(networkingClient, id), |
| 24 | + Timeout: timeout, |
| 25 | + Delay: 5 * time.Second, |
| 26 | + MinTimeout: 1 * time.Second, |
| 27 | + } |
| 28 | + |
| 29 | + _, err := stateConf.WaitForState() |
| 30 | + if err != nil { |
| 31 | + if _, ok := err.(gophercloud.ErrDefault404); ok { |
| 32 | + switch target { |
| 33 | + case "DELETED": |
| 34 | + return nil |
| 35 | + default: |
| 36 | + return fmt.Errorf("Error: listener %s not found: %s", id, err) |
| 37 | + } |
| 38 | + } |
| 39 | + return fmt.Errorf("Error waiting for listener %s to become %s: %s", id, target, err) |
| 40 | + } |
| 41 | + |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func resourceLBV2ListenerRefreshFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc { |
| 46 | + return func() (interface{}, string, error) { |
| 47 | + listener, err := listeners.Get(networkingClient, id).Extract() |
| 48 | + if err != nil { |
| 49 | + return nil, "", err |
| 50 | + } |
| 51 | + |
| 52 | + // The listener resource has no Status attribute, so a successful Get is the best we can do |
| 53 | + return listener, "ACTIVE", nil |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func waitForLBV2LoadBalancer(networkingClient *gophercloud.ServiceClient, id string, target string, pending []string, timeout time.Duration) error { |
| 58 | + log.Printf("[DEBUG] Waiting for loadbalancer %s to become %s.", id, target) |
| 59 | + |
| 60 | + stateConf := &resource.StateChangeConf{ |
| 61 | + Target: []string{target}, |
| 62 | + Pending: pending, |
| 63 | + Refresh: resourceLBV2LoadBalancerRefreshFunc(networkingClient, id), |
| 64 | + Timeout: timeout, |
| 65 | + Delay: 5 * time.Second, |
| 66 | + MinTimeout: 1 * time.Second, |
| 67 | + } |
| 68 | + |
| 69 | + _, err := stateConf.WaitForState() |
| 70 | + if err != nil { |
| 71 | + if _, ok := err.(gophercloud.ErrDefault404); ok { |
| 72 | + switch target { |
| 73 | + case "DELETED": |
| 74 | + return nil |
| 75 | + default: |
| 76 | + return fmt.Errorf("Error: loadbalancer %s not found: %s", id, err) |
| 77 | + } |
| 78 | + } |
| 79 | + return fmt.Errorf("Error waiting for loadbalancer %s to become %s: %s", id, target, err) |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func resourceLBV2LoadBalancerRefreshFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc { |
| 86 | + return func() (interface{}, string, error) { |
| 87 | + lb, err := loadbalancers.Get(networkingClient, id).Extract() |
| 88 | + if err != nil { |
| 89 | + return nil, "", err |
| 90 | + } |
| 91 | + |
| 92 | + return lb, lb.ProvisioningStatus, nil |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func waitForLBV2Member(networkingClient *gophercloud.ServiceClient, poolID, memberID string, target string, pending []string, timeout time.Duration) error { |
| 97 | + log.Printf("[DEBUG] Waiting for member %s to become %s.", memberID, target) |
| 98 | + |
| 99 | + stateConf := &resource.StateChangeConf{ |
| 100 | + Target: []string{target}, |
| 101 | + Pending: pending, |
| 102 | + Refresh: resourceLBV2MemberRefreshFunc(networkingClient, poolID, memberID), |
| 103 | + Timeout: timeout, |
| 104 | + Delay: 5 * time.Second, |
| 105 | + MinTimeout: 1 * time.Second, |
| 106 | + } |
| 107 | + |
| 108 | + _, err := stateConf.WaitForState() |
| 109 | + if err != nil { |
| 110 | + if _, ok := err.(gophercloud.ErrDefault404); ok { |
| 111 | + switch target { |
| 112 | + case "DELETED": |
| 113 | + return nil |
| 114 | + default: |
| 115 | + return fmt.Errorf("Error: member %s not found: %s", memberID, err) |
| 116 | + } |
| 117 | + } |
| 118 | + return fmt.Errorf("Error waiting for member %s to become %s: %s", memberID, target, err) |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func resourceLBV2MemberRefreshFunc(networkingClient *gophercloud.ServiceClient, poolID, memberID string) resource.StateRefreshFunc { |
| 125 | + return func() (interface{}, string, error) { |
| 126 | + member, err := pools.GetMember(networkingClient, poolID, memberID).Extract() |
| 127 | + if err != nil { |
| 128 | + return nil, "", err |
| 129 | + } |
| 130 | + |
| 131 | + // The member resource has no Status attribute, so a successful Get is the best we can do |
| 132 | + return member, "ACTIVE", nil |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func waitForLBV2Monitor(networkingClient *gophercloud.ServiceClient, id string, target string, pending []string, timeout time.Duration) error { |
| 137 | + log.Printf("[DEBUG] Waiting for monitor %s to become %s.", id, target) |
| 138 | + |
| 139 | + stateConf := &resource.StateChangeConf{ |
| 140 | + Target: []string{target}, |
| 141 | + Pending: pending, |
| 142 | + Refresh: resourceLBV2MonitorRefreshFunc(networkingClient, id), |
| 143 | + Timeout: timeout, |
| 144 | + Delay: 5 * time.Second, |
| 145 | + MinTimeout: 1 * time.Second, |
| 146 | + } |
| 147 | + |
| 148 | + _, err := stateConf.WaitForState() |
| 149 | + if err != nil { |
| 150 | + if _, ok := err.(gophercloud.ErrDefault404); ok { |
| 151 | + switch target { |
| 152 | + case "DELETED": |
| 153 | + return nil |
| 154 | + default: |
| 155 | + return fmt.Errorf("Error: monitor %s not found: %s", id, err) |
| 156 | + } |
| 157 | + } |
| 158 | + return fmt.Errorf("Error waiting for monitor %s to become %s: %s", id, target, err) |
| 159 | + } |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func resourceLBV2MonitorRefreshFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc { |
| 165 | + return func() (interface{}, string, error) { |
| 166 | + monitor, err := monitors.Get(networkingClient, id).Extract() |
| 167 | + if err != nil { |
| 168 | + return nil, "", err |
| 169 | + } |
| 170 | + |
| 171 | + // The monitor resource has no Status attribute, so a successful Get is the best we can do |
| 172 | + return monitor, "ACTIVE", nil |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func waitForLBV2Pool(networkingClient *gophercloud.ServiceClient, id string, target string, pending []string, timeout time.Duration) error { |
| 177 | + log.Printf("[DEBUG] Waiting for pool %s to become %s.", id, target) |
| 178 | + |
| 179 | + stateConf := &resource.StateChangeConf{ |
| 180 | + Target: []string{target}, |
| 181 | + Pending: pending, |
| 182 | + Refresh: resourceLBV2PoolRefreshFunc(networkingClient, id), |
| 183 | + Timeout: timeout, |
| 184 | + Delay: 5 * time.Second, |
| 185 | + MinTimeout: 1 * time.Second, |
| 186 | + } |
| 187 | + |
| 188 | + _, err := stateConf.WaitForState() |
| 189 | + if err != nil { |
| 190 | + if _, ok := err.(gophercloud.ErrDefault404); ok { |
| 191 | + switch target { |
| 192 | + case "DELETED": |
| 193 | + return nil |
| 194 | + default: |
| 195 | + return fmt.Errorf("Error: pool %s not found: %s", id, err) |
| 196 | + } |
| 197 | + } |
| 198 | + return fmt.Errorf("Error waiting for pool %s to become %s: %s", id, target, err) |
| 199 | + } |
| 200 | + |
| 201 | + return nil |
| 202 | +} |
| 203 | + |
| 204 | +func resourceLBV2PoolRefreshFunc(networkingClient *gophercloud.ServiceClient, poolID string) resource.StateRefreshFunc { |
| 205 | + return func() (interface{}, string, error) { |
| 206 | + pool, err := pools.Get(networkingClient, poolID).Extract() |
| 207 | + if err != nil { |
| 208 | + return nil, "", err |
| 209 | + } |
| 210 | + |
| 211 | + // The pool resource has no Status attribute, so a successful Get is the best we can do |
| 212 | + return pool, "ACTIVE", nil |
| 213 | + } |
| 214 | +} |
0 commit comments