Add commands related to loadbalancer agent scheduling

blueprint lbaas-agent-scheduler

Change-Id: Iba1682b122d5ae411f67ab535aecbfc4696aa925
This commit is contained in:
Oleg Bondarev
2013-07-17 14:22:42 +04:00
parent 31e9374525
commit 0e64fbb061
5 changed files with 129 additions and 3 deletions

View File

@@ -232,3 +232,58 @@ class ListL3AgentsHostingRouter(neutronV20.ListCommand):
search_opts['router'] = _id
data = neutron_client.list_l3_agent_hosting_routers(**search_opts)
return data
class ListPoolsOnLbaasAgent(neutronV20.ListCommand):
"""List the pools on a loadbalancer agent."""
log = logging.getLogger(__name__ + '.ListPoolsOnLbaasAgent')
list_columns = ['id', 'name', 'lb_method', 'protocol',
'admin_state_up', 'status']
resource = 'pool'
unknown_parts_flag = False
def get_parser(self, prog_name):
parser = super(ListPoolsOnLbaasAgent, self).get_parser(prog_name)
parser.add_argument(
'lbaas_agent',
help='ID of the loadbalancer agent to query')
return parser
def call_server(self, neutron_client, search_opts, parsed_args):
data = neutron_client.list_pools_on_lbaas_agent(
parsed_args.lbaas_agent, **search_opts)
return data
class GetLbaasAgentHostingPool(neutronV20.ListCommand):
"""Get loadbalancer agent hosting a pool.
Deriving from ListCommand though server will return only one agent
to keep common output format for all agent schedulers
"""
resource = 'agent'
log = logging.getLogger(__name__ + '.GetLbaasAgentHostingPool')
list_columns = ['id', 'host', 'admin_state_up', 'alive']
unknown_parts_flag = False
def get_parser(self, prog_name):
parser = super(GetLbaasAgentHostingPool,
self).get_parser(prog_name)
parser.add_argument('pool',
help='pool to query')
return parser
def extend_list(self, data, parsed_args):
for agent in data:
agent['alive'] = ":-)" if agent['alive'] else 'xxx'
def call_server(self, neutron_client, search_opts, parsed_args):
_id = neutronV20.find_resourceid_by_name_or_id(neutron_client,
'pool',
parsed_args.pool)
search_opts['pool'] = _id
agent = neutron_client.get_lbaas_agent_hosting_pool(**search_opts)
data = {'agents': [agent['agent']]}
return data

View File

@@ -177,6 +177,8 @@ COMMAND_V2 = {
'l3-agent-router-remove': agentscheduler.RemoveRouterFromL3Agent,
'router-list-on-l3-agent': agentscheduler.ListRoutersOnL3Agent,
'l3-agent-list-hosting-router': agentscheduler.ListL3AgentsHostingRouter,
'lb-pool-list-on-agent': agentscheduler.ListPoolsOnLbaasAgent,
'lb-agent-hosting-pool': agentscheduler.GetLbaasAgentHostingPool,
}
COMMANDS = {'2.0': COMMAND_V2}

View File

@@ -187,6 +187,8 @@ class Client(object):
DHCP_AGENTS = '/dhcp-agents'
L3_ROUTERS = '/l3-routers'
L3_AGENTS = '/l3-agents'
LOADBALANCER_POOLS = '/loadbalancer-pools'
LOADBALANCER_AGENT = '/loadbalancer-agent'
# API has no way to report plurals, so we have to hard code them
EXTED_PLURALS = {'routers': 'router',
'floatingips': 'floatingip',
@@ -717,6 +719,18 @@ class Client(object):
return self.delete((self.agent_path + self.L3_ROUTERS + "/%s") % (
l3_agent, router_id))
@APIParamsCall
def get_lbaas_agent_hosting_pool(self, pool, **_params):
"""Fetches a loadbalancer agent hosting a pool."""
return self.get((self.pool_path + self.LOADBALANCER_AGENT) % pool,
params=_params)
@APIParamsCall
def list_pools_on_lbaas_agent(self, lbaas_agent, **_params):
"""Fetches a list of pools hosted by the loadbalancer agent."""
return self.get((self.agent_path + self.LOADBALANCER_POOLS) %
lbaas_agent, params=_params)
def __init__(self, **kwargs):
"""Initialize a new client for the Neutron v2.0 API."""
super(Client, self).__init__()

View File

@@ -251,7 +251,8 @@ class CLITestV20Base(testtools.TestCase):
def _test_list_resources(self, resources, cmd, detail=False, tags=[],
fields_1=[], fields_2=[], page_size=None,
sort_key=[], sort_dir=[], response_contents=None):
sort_key=[], sort_dir=[], response_contents=None,
base_args=None, path=None):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
@@ -265,7 +266,9 @@ class CLITestV20Base(testtools.TestCase):
resstr = self.client.serialize(reses)
# url method body
query = ""
args = detail and ['-D', ] or []
args = base_args if base_args is not None else []
if detail:
args.append('-D')
args.extend(['--request-format', self.format])
if fields_1:
for field in fields_1:
@@ -323,7 +326,8 @@ class CLITestV20Base(testtools.TestCase):
if query:
query += '&'
query += 'sort_dir=%s' % dir
path = getattr(self.client, resources + "_path")
if path is None:
path = getattr(self.client, resources + "_path")
self.client.httpclient.request(
MyUrlComparator(end_url(path, query, format=self.format),
self.client),

View File

@@ -0,0 +1,51 @@
# Copyright 2013 Mirantis Inc.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# @author: Oleg Bondarev, Mirantis Inc.
#
# vim: tabstop=4 shiftwidth=4 softtabstop=4
import sys
from neutronclient.neutron.v2_0 import agentscheduler
from tests.unit import test_cli20
class CLITestV20LBaaSAgentScheduler(test_cli20.CLITestV20Base):
def test_list_pools_on_agent(self):
resources = 'pools'
cmd = agentscheduler.ListPoolsOnLbaasAgent(
test_cli20.MyApp(sys.stdout), None)
agent_id = 'agent_id1'
path = ((self.client.agent_path + self.client.LOADBALANCER_POOLS) %
agent_id)
self._test_list_resources(resources, cmd, base_args=[agent_id],
path=path)
def test_get_lbaas_agent_hosting_pool(self):
resources = 'agent'
cmd = agentscheduler.GetLbaasAgentHostingPool(
test_cli20.MyApp(sys.stdout), None)
pool_id = 'pool_id1'
path = ((self.client.pool_path + self.client.LOADBALANCER_AGENT) %
pool_id)
contents = {self.id_field: 'myid1', 'alive': True}
self._test_list_resources(resources, cmd, base_args=[pool_id],
path=path, response_contents=contents)
class CLITestV20LBaaSAgentSchedulerXML(CLITestV20LBaaSAgentScheduler):
format = 'xml'