Merge "Switch lb_listener module to OpenStackModule"
This commit is contained in:
commit
d07039a7ec
|
@ -143,41 +143,11 @@ EXAMPLES = '''
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
||||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (openstack_full_argument_spec,
|
|
||||||
openstack_module_kwargs,
|
|
||||||
openstack_cloud_from_module)
|
|
||||||
|
|
||||||
|
|
||||||
def _lb_wait_for_status(module, cloud, lb, status, failures, interval=5):
|
class LoadbalancerListenerModule(OpenStackModule):
|
||||||
"""Wait for load balancer to be in a particular provisioning status."""
|
argument_spec = dict(
|
||||||
timeout = module.params['timeout']
|
|
||||||
|
|
||||||
total_sleep = 0
|
|
||||||
if failures is None:
|
|
||||||
failures = []
|
|
||||||
|
|
||||||
while total_sleep < timeout:
|
|
||||||
lb = cloud.load_balancer.get_load_balancer(lb.id)
|
|
||||||
if lb.provisioning_status == status:
|
|
||||||
return None
|
|
||||||
if lb.provisioning_status in failures:
|
|
||||||
module.fail_json(
|
|
||||||
msg="Load Balancer %s transitioned to failure state %s" %
|
|
||||||
(lb.id, lb.provisioning_status)
|
|
||||||
)
|
|
||||||
|
|
||||||
time.sleep(interval)
|
|
||||||
total_sleep += interval
|
|
||||||
|
|
||||||
module.fail_json(
|
|
||||||
msg="Timeout waiting for Load Balancer %s to transition to %s" %
|
|
||||||
(lb.id, status)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
argument_spec = openstack_full_argument_spec(
|
|
||||||
name=dict(required=True),
|
name=dict(required=True),
|
||||||
state=dict(default='present', choices=['absent', 'present']),
|
state=dict(default='present', choices=['absent', 'present']),
|
||||||
loadbalancer=dict(required=True),
|
loadbalancer=dict(required=True),
|
||||||
|
@ -185,70 +155,98 @@ def main():
|
||||||
choices=['HTTP', 'HTTPS', 'TCP', 'TERMINATED_HTTPS']),
|
choices=['HTTP', 'HTTPS', 'TCP', 'TERMINATED_HTTPS']),
|
||||||
protocol_port=dict(default=80, type='int', required=False),
|
protocol_port=dict(default=80, type='int', required=False),
|
||||||
)
|
)
|
||||||
module_kwargs = openstack_module_kwargs()
|
module_kwargs = dict()
|
||||||
module = AnsibleModule(argument_spec, **module_kwargs)
|
|
||||||
sdk, cloud = openstack_cloud_from_module(module)
|
def _lb_wait_for_status(self, lb, status, failures, interval=5):
|
||||||
loadbalancer = module.params['loadbalancer']
|
"""Wait for load balancer to be in a particular provisioning status."""
|
||||||
loadbalancer_id = None
|
timeout = self.params['timeout']
|
||||||
|
|
||||||
|
total_sleep = 0
|
||||||
|
if failures is None:
|
||||||
|
failures = []
|
||||||
|
|
||||||
|
while total_sleep < timeout:
|
||||||
|
lb = self.conn.load_balancer.get_load_balancer(lb.id)
|
||||||
|
if lb.provisioning_status == status:
|
||||||
|
return None
|
||||||
|
if lb.provisioning_status in failures:
|
||||||
|
self.fail_json(
|
||||||
|
msg="Load Balancer %s transitioned to failure state %s" %
|
||||||
|
(lb.id, lb.provisioning_status)
|
||||||
|
)
|
||||||
|
|
||||||
|
time.sleep(interval)
|
||||||
|
total_sleep += interval
|
||||||
|
|
||||||
|
self.fail_json(
|
||||||
|
msg="Timeout waiting for Load Balancer %s to transition to %s" %
|
||||||
|
(lb.id, status)
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
loadbalancer = self.params['loadbalancer']
|
||||||
|
loadbalancer_id = None
|
||||||
|
|
||||||
try:
|
|
||||||
changed = False
|
changed = False
|
||||||
listener = cloud.load_balancer.find_listener(
|
listener = self.conn.load_balancer.find_listener(
|
||||||
name_or_id=module.params['name'])
|
name_or_id=self.params['name'])
|
||||||
|
|
||||||
if module.params['state'] == 'present':
|
if self.params['state'] == 'present':
|
||||||
if not listener:
|
if not listener:
|
||||||
lb = cloud.load_balancer.find_load_balancer(loadbalancer)
|
lb = self.conn.load_balancer.find_load_balancer(loadbalancer)
|
||||||
if not lb:
|
if not lb:
|
||||||
module.fail_json(
|
self.fail_json(
|
||||||
msg='load balancer %s is not found' % loadbalancer
|
msg='load balancer %s is not found' % loadbalancer
|
||||||
)
|
)
|
||||||
loadbalancer_id = lb.id
|
loadbalancer_id = lb.id
|
||||||
|
|
||||||
listener = cloud.load_balancer.create_listener(
|
listener = self.conn.load_balancer.create_listener(
|
||||||
name=module.params['name'],
|
name=self.params['name'],
|
||||||
loadbalancer_id=loadbalancer_id,
|
loadbalancer_id=loadbalancer_id,
|
||||||
protocol=module.params['protocol'],
|
protocol=self.params['protocol'],
|
||||||
protocol_port=module.params['protocol_port'],
|
protocol_port=self.params['protocol_port'],
|
||||||
)
|
)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
if not module.params['wait']:
|
if not self.params['wait']:
|
||||||
module.exit_json(changed=changed,
|
self.exit_json(
|
||||||
listener=listener.to_dict(),
|
changed=changed, listener=listener.to_dict(),
|
||||||
id=listener.id)
|
id=listener.id)
|
||||||
|
|
||||||
if module.params['wait']:
|
if self.params['wait']:
|
||||||
# Check in case the listener already exists.
|
# Check in case the listener already exists.
|
||||||
lb = cloud.load_balancer.find_load_balancer(loadbalancer)
|
lb = self.conn.load_balancer.find_load_balancer(loadbalancer)
|
||||||
if not lb:
|
if not lb:
|
||||||
module.fail_json(
|
self.fail_json(
|
||||||
msg='load balancer %s is not found' % loadbalancer
|
msg='load balancer %s is not found' % loadbalancer
|
||||||
)
|
)
|
||||||
_lb_wait_for_status(module, cloud, lb, "ACTIVE", ["ERROR"])
|
self._lb_wait_for_status(lb, "ACTIVE", ["ERROR"])
|
||||||
|
|
||||||
module.exit_json(changed=changed, listener=listener.to_dict(),
|
self.exit_json(
|
||||||
id=listener.id)
|
changed=changed, listener=listener.to_dict(), id=listener.id)
|
||||||
elif module.params['state'] == 'absent':
|
elif self.params['state'] == 'absent':
|
||||||
if not listener:
|
if not listener:
|
||||||
changed = False
|
changed = False
|
||||||
else:
|
else:
|
||||||
cloud.load_balancer.delete_listener(listener)
|
self.conn.load_balancer.delete_listener(listener)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
if module.params['wait']:
|
if self.params['wait']:
|
||||||
# Wait for the load balancer to be active after deleting
|
# Wait for the load balancer to be active after deleting
|
||||||
# the listener.
|
# the listener.
|
||||||
lb = cloud.load_balancer.find_load_balancer(loadbalancer)
|
lb = self.conn.load_balancer.find_load_balancer(loadbalancer)
|
||||||
if not lb:
|
if not lb:
|
||||||
module.fail_json(
|
self.fail_json(
|
||||||
msg='load balancer %s is not found' % loadbalancer
|
msg='load balancer %s is not found' % loadbalancer
|
||||||
)
|
)
|
||||||
_lb_wait_for_status(module, cloud, lb, "ACTIVE", ["ERROR"])
|
self._lb_wait_for_status(lb, "ACTIVE", ["ERROR"])
|
||||||
|
|
||||||
module.exit_json(changed=changed)
|
self.exit_json(changed=changed)
|
||||||
except sdk.exceptions.OpenStackCloudException as e:
|
|
||||||
module.fail_json(msg=str(e), extra_data=e.extra_data)
|
|
||||||
|
def main():
|
||||||
|
module = LoadbalancerListenerModule()
|
||||||
|
module()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue