blob: b8f35eb73fc04eade4825ed32ba7afa7ce3f9473 [file] [log] [blame]
Vasyl Saienkoba420732018-09-07 10:19:32 +00001import logging
2import time
3from salt.exceptions import CommandExecutionError
4
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +03005from neutronv2.common import send
6from neutronv2.arg_converter import get_by_name_or_uuid_multiple
Vasyl Saienkoba420732018-09-07 10:19:32 +00007from neutronv2.lists import agent_list
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +03008
9
Vasyl Saienkoba420732018-09-07 10:19:32 +000010log = logging.getLogger(__name__)
11
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +030012try:
13 from urllib.parse import urlencode
14except ImportError:
15 from urllib import urlencode
16
17
18@send('get')
19def agent_get_details(agent_id, **kwargs):
20 url = '/agents/{}?{}'.format(agent_id, urlencode(kwargs))
21 return url, {}
22
23
24@send('put')
25def 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')
34def agent_delete(agent_id, **kwargs):
35 url = '/agents/{}'.format(agent_id)
36 return url, {}
37
38
39@send('get')
40def 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')
47def 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')
57def 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')
64def l3_agent_by_router_list(router_id, **kwargs):
65 url = '/routers/{}/l3-agents'.format(router_id)
66 return url, {}
67
68
69@send('get')
70def 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')
77def 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')
87def 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')
94def dhcp_agent_by_network_list(network_id, **kwargs):
95 url = '/networks/{}/dhcp-agents'.format(network_id)
96 return url, {}
Vasyl Saienkoba420732018-09-07 10:19:32 +000097
98
99def 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))