Vasyl Saienko | ba42073 | 2018-09-07 10:19:32 +0000 | [diff] [blame] | 1 | import logging |
| 2 | import time |
| 3 | from salt.exceptions import CommandExecutionError |
| 4 | |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 5 | from neutronv2.common import send |
| 6 | from neutronv2.arg_converter import get_by_name_or_uuid_multiple |
Vasyl Saienko | ba42073 | 2018-09-07 10:19:32 +0000 | [diff] [blame] | 7 | from neutronv2.lists import agent_list |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 8 | |
| 9 | |
Vasyl Saienko | ba42073 | 2018-09-07 10:19:32 +0000 | [diff] [blame] | 10 | log = logging.getLogger(__name__) |
| 11 | |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 12 | try: |
| 13 | from urllib.parse import urlencode |
| 14 | except ImportError: |
| 15 | from urllib import urlencode |
| 16 | |
| 17 | |
| 18 | @send('get') |
| 19 | def agent_get_details(agent_id, **kwargs): |
| 20 | url = '/agents/{}?{}'.format(agent_id, urlencode(kwargs)) |
| 21 | return url, {} |
| 22 | |
| 23 | |
| 24 | @send('put') |
| 25 | def agent_update(agent_id, **kwargs): |
| 26 | url = '/agents/{}'.format(agent_id) |
| 27 | json = { |
| 28 | 'agent': kwargs, |
| 29 | } |
| 30 | return url, {'json': json} |
| 31 | |
| 32 | |
| 33 | @send('delete') |
| 34 | def agent_delete(agent_id, **kwargs): |
| 35 | url = '/agents/{}'.format(agent_id) |
| 36 | return url, {} |
| 37 | |
| 38 | |
| 39 | @send('get') |
| 40 | def l3_agent_router_list(agent_id, **kwargs): |
| 41 | url = '/agents/{}/l3-routers'.format(agent_id) |
| 42 | return url, {} |
| 43 | |
| 44 | |
| 45 | @get_by_name_or_uuid_multiple([('router', 'router_id')]) |
| 46 | @send('post') |
| 47 | def l3_agent_router_schedule(router_id, agent_id, **kwargs): |
| 48 | url = '/agents/{}/l3-routers'.format(agent_id) |
| 49 | json = { |
| 50 | 'router_id': router_id, |
| 51 | } |
| 52 | return url, {'json': json} |
| 53 | |
| 54 | |
| 55 | @get_by_name_or_uuid_multiple([('router', 'router_id')]) |
| 56 | @send('delete') |
| 57 | def l3_agent_router_remove(router_id, agent_id, **kwargs): |
| 58 | url = '/agents/{}/l3-routers/{}'.format(agent_id, router_id) |
| 59 | return url, {} |
| 60 | |
| 61 | |
| 62 | @get_by_name_or_uuid_multiple([('router', 'router_id')]) |
| 63 | @send('get') |
| 64 | def l3_agent_by_router_list(router_id, **kwargs): |
| 65 | url = '/routers/{}/l3-agents'.format(router_id) |
| 66 | return url, {} |
| 67 | |
| 68 | |
| 69 | @send('get') |
| 70 | def dhcp_agent_list_networks(agent_id, **kwargs): |
| 71 | url = '/agents/{}/dhcp-networks'.format(agent_id) |
| 72 | return url, {} |
| 73 | |
| 74 | |
| 75 | @get_by_name_or_uuid_multiple([('network', 'network_id')]) |
| 76 | @send('post') |
| 77 | def dhcp_agent_network_schedule(network_id, agent_id, **kwargs): |
| 78 | url = '/agents/{}/dhcp-networks'.format(agent_id) |
| 79 | json = { |
| 80 | 'network_id': network_id, |
| 81 | } |
| 82 | return url, {'json': json} |
| 83 | |
| 84 | |
| 85 | @get_by_name_or_uuid_multiple([('network', 'network_id')]) |
| 86 | @send('delete') |
| 87 | def dhcp_agent_network_remove(network_id, agent_id, **kwargs): |
| 88 | url = '/agents/{}/dhcp-networks/{}'.format(agent_id, network_id) |
| 89 | return url, {} |
| 90 | |
| 91 | |
| 92 | @get_by_name_or_uuid_multiple([('network', 'network_id')]) |
| 93 | @send('get') |
| 94 | def dhcp_agent_by_network_list(network_id, **kwargs): |
| 95 | url = '/networks/{}/dhcp-agents'.format(network_id) |
| 96 | return url, {} |
Vasyl Saienko | ba42073 | 2018-09-07 10:19:32 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def wait_for_network_services(cloud_name, host_id=None, |
| 100 | admin_up_only=True, |
| 101 | retries=18, timeout=10): |
| 102 | """ |
| 103 | Ensure services on specified host are alive, othervise fail with exception. |
| 104 | |
| 105 | :param host_id: host name to wait or None (to check for all hosts) |
| 106 | :param cloud_name: name of cloud from os client config |
| 107 | :param admin_up_only: do not check for admin disabled agents |
| 108 | :param timeout: number of seconds to wait before retries |
| 109 | :param retries: number of retries |
| 110 | """ |
| 111 | |
| 112 | kwargs = {'alive': False} |
| 113 | |
| 114 | if admin_up_only: |
| 115 | kwargs['admin_state_up'] = True |
| 116 | |
| 117 | if host_id is not None: |
| 118 | kwargs['host'] = host_id |
| 119 | |
| 120 | res = None |
| 121 | for i in range(retries): |
| 122 | try: |
| 123 | agents = agent_list(cloud_name=cloud_name, **kwargs)['agents'] |
| 124 | res = len(agents) |
| 125 | except Exception as e: |
| 126 | msg = "Failed to get agent list {0}".format(e) |
| 127 | log.trace(msg) |
| 128 | raise CommandExecutionError(e) |
| 129 | |
| 130 | if res == 0: |
| 131 | return "All services are up" |
| 132 | time.sleep(timeout) |
| 133 | raise CommandExecutionError("Some agents are still down {}".format(agents)) |