lbaasv2.py 25.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright 2015 IBM Corp.
#
# 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.
"""API over the neutron LBaaS v2 service.
"""

17 18 19
from six.moves import _thread as thread
from time import sleep

20 21
from django.views import generic

22 23
from horizon import conf

24
from openstack_dashboard.api import network
25 26 27 28 29 30 31
from openstack_dashboard.api import neutron
from openstack_dashboard.api.rest import urls
from openstack_dashboard.api.rest import utils as rest_utils

neutronclient = neutron.neutronclient


32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
def poll_loadbalancer_status(request, loadbalancer_id, callback,
                             from_state='PENDING_UPDATE', to_state='ACTIVE',
                             callback_kwargs=None):
    """Poll for the status of the load balancer.

    Polls for the status of the load balancer and calls a function when the
    status changes to a specified state.

    :param request: django request object
    :param loadbalancer_id: id of the load balancer to poll
    :param callback: function to call when polling is complete
    :param from_state: initial expected state of the load balancer
    :param to_state: state to check for
    :param callback_kwargs: kwargs to pass into the callback function
    """
    interval = conf.HORIZON_CONFIG['ajax_poll_interval'] / 1000.0
    status = from_state
    while status == from_state:
        sleep(interval)
        lb = neutronclient(request).show_loadbalancer(
            loadbalancer_id).get('loadbalancer')
        status = lb['provisioning_status']

    if status == to_state:
        kwargs = {'loadbalancer_id': loadbalancer_id}
        if callback_kwargs:
            kwargs.update(callback_kwargs)
        callback(request, **kwargs)


62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
def create_loadbalancer(request):
    data = request.DATA
    spec = {
        'vip_subnet_id': data['loadbalancer']['subnet']
    }
    if data['loadbalancer'].get('name'):
        spec['name'] = data['loadbalancer']['name']
    if data['loadbalancer'].get('description'):
        spec['description'] = data['loadbalancer']['description']
    if data['loadbalancer'].get('ip'):
        spec['vip_address'] = data['loadbalancer']['ip']
    loadbalancer = neutronclient(request).create_loadbalancer(
        {'loadbalancer': spec}).get('loadbalancer')
    if data.get('listener'):
        # There is work underway to add a new API to LBaaS v2 that will
        # allow us to pass in all information at once. Until that is
        # available we use a separate thread to poll for the load
        # balancer status and create the other resources when it becomes
        # active.
        args = (request, loadbalancer['id'], create_listener)
        kwargs = {'from_state': 'PENDING_CREATE'}
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)
    return loadbalancer


87 88 89 90 91 92 93 94 95 96 97 98 99 100
def create_listener(request, **kwargs):
    """Create a new listener.

    """
    data = request.DATA
    listenerSpec = {
        'protocol': data['listener']['protocol'],
        'protocol_port': data['listener']['port'],
        'loadbalancer_id': kwargs['loadbalancer_id']
    }
    if data['listener'].get('name'):
        listenerSpec['name'] = data['listener']['name']
    if data['listener'].get('description'):
        listenerSpec['description'] = data['listener']['description']
101 102 103 104
    if data.get('certificates'):
        listenerSpec['default_tls_container_ref'] = data['certificates'][0]
        listenerSpec['sni_container_refs'] = data['certificates']

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    listener = neutronclient(request).create_listener(
        {'listener': listenerSpec}).get('listener')

    if data.get('pool'):
        args = (request, kwargs['loadbalancer_id'], create_pool)
        kwargs = {'callback_kwargs': {'listener_id': listener['id']}}
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)

    return listener


def create_pool(request, **kwargs):
    """Create a new pool.

    """
    data = request.DATA
    poolSpec = {
        'protocol': data['pool']['protocol'],
        'lb_algorithm': data['pool']['method'],
        'listener_id': kwargs['listener_id']
    }
    if data['pool'].get('name'):
        poolSpec['name'] = data['pool']['name']
    if data['pool'].get('description'):
        poolSpec['description'] = data['pool']['description']
130
    pool = neutronclient(request).create_lbaas_pool(
131 132
        {'pool': poolSpec}).get('pool')

133 134 135 136 137
    if data.get('members'):
        args = (request, kwargs['loadbalancer_id'], add_member)
        kwargs = {'callback_kwargs': {'pool_id': pool['id'],
                                      'index': 0}}
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)
138
    elif data.get('monitor'):
139
        args = (request, kwargs['loadbalancer_id'], create_health_monitor)
140 141
        kwargs = {'callback_kwargs': {'pool_id': pool['id']}}
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)
142 143 144 145

    return pool


146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
def create_health_monitor(request, **kwargs):
    """Create a new health monitor for a pool.

    """
    data = request.DATA
    monitorSpec = {
        'type': data['monitor']['type'],
        'delay': data['monitor']['interval'],
        'timeout': data['monitor']['timeout'],
        'max_retries': data['monitor']['retry'],
        'pool_id': kwargs['pool_id']
    }
    if data['monitor'].get('method'):
        monitorSpec['http_method'] = data['monitor']['method']
    if data['monitor'].get('path'):
        monitorSpec['url_path'] = data['monitor']['path']
    if data['monitor'].get('status'):
        monitorSpec['expected_codes'] = data['monitor']['status']
    return neutronclient(request).create_lbaas_healthmonitor(
        {'healthmonitor': monitorSpec}).get('healthmonitor')


168 169 170 171 172
def add_member(request, **kwargs):
    """Add a member to a pool.

    """
    data = request.DATA
173
    members = data.get('members')
174
    pool_id = kwargs.get('pool_id')
175 176 177 178 179 180 181 182 183 184

    if kwargs.get('members_to_add'):
        members_to_add = kwargs['members_to_add']
        index = [members.index(member) for member in members
                 if member['id'] == members_to_add[0]][0]
        loadbalancer_id = data.get('loadbalancer_id')
    else:
        index = kwargs.get('index')
        loadbalancer_id = kwargs.get('loadbalancer_id')

185 186 187 188 189 190 191 192
    member = members[index]
    memberSpec = {
        'address': member['address'],
        'protocol_port': member['port'],
        'subnet_id': member['subnet']
    }
    if member.get('weight'):
        memberSpec['weight'] = member['weight']
193

194
    member = neutronclient(request).create_lbaas_member(
195
        pool_id, {'member': memberSpec}).get('member')
196 197

    index += 1
198 199 200 201 202 203 204
    if kwargs.get('members_to_add'):
        args = (request, loadbalancer_id, update_member_list)
        members_to_add = kwargs['members_to_add']
        members_to_add.pop(0)
        kwargs = {'callback_kwargs': {
            'existing_members': kwargs.get('existing_members'),
            'members_to_add': members_to_add,
205 206
            'members_to_delete': kwargs.get('members_to_delete'),
            'pool_id': pool_id}}
207 208 209 210
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)
    elif len(members) > index:
        args = (request, loadbalancer_id, add_member)
        kwargs = {'callback_kwargs': {'pool_id': pool_id,
211 212
                                      'index': index}}
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)
213
    elif data.get('monitor'):
214
        args = (request, loadbalancer_id, create_health_monitor)
215
        kwargs = {'callback_kwargs': {'pool_id': pool_id}}
216
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)
217 218 219

    return member

220

221 222 223 224 225 226
def remove_member(request, **kwargs):
    """Remove a member from the pool.

    """
    data = request.DATA
    loadbalancer_id = data.get('loadbalancer_id')
227
    pool_id = kwargs.get('pool_id')
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

    if kwargs.get('members_to_delete'):
        members_to_delete = kwargs['members_to_delete']
        member_id = members_to_delete.pop(0)

        neutronclient(request).delete_lbaas_member(member_id, pool_id)

        args = (request, loadbalancer_id, update_member_list)
        kwargs = {'callback_kwargs': {
            'existing_members': kwargs.get('existing_members'),
            'members_to_add': kwargs.get('members_to_add'),
            'members_to_delete': members_to_delete}}
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)


def update_loadbalancer(request, **kwargs):
    """Update a load balancer.

    """
    data = request.DATA
    spec = {}
    loadbalancer_id = kwargs.get('loadbalancer_id')

    if data['loadbalancer'].get('name'):
        spec['name'] = data['loadbalancer']['name']
    if data['loadbalancer'].get('description'):
        spec['description'] = data['loadbalancer']['description']
    return neutronclient(request).update_loadbalancer(
        loadbalancer_id, {'loadbalancer': spec}).get('loadbalancer')


def update_listener(request, **kwargs):
    """Update a listener.

    """
    data = request.DATA
    listener_spec = {}
    listener_id = data['listener'].get('id')
    loadbalancer_id = data.get('loadbalancer_id')

    if data['listener'].get('name'):
        listener_spec['name'] = data['listener']['name']
    if data['listener'].get('description'):
        listener_spec['description'] = data['listener']['description']

    listener = neutronclient(request).update_listener(
        listener_id, {'listener': listener_spec}).get('listener')

    if data.get('pool'):
        args = (request, loadbalancer_id, update_pool)
        thread.start_new_thread(poll_loadbalancer_status, args)

    return listener


def update_pool(request, **kwargs):
    """Update a pool.

    """
    data = request.DATA
    pool_spec = {}
    pool_id = data['pool'].get('id')
    loadbalancer_id = data.get('loadbalancer_id')

    if data['pool'].get('name'):
        pool_spec['name'] = data['pool']['name']
    if data['pool'].get('description'):
        pool_spec['description'] = data['pool']['description']

    pools = neutronclient(request).update_lbaas_pool(
        pool_id, {'pool': pool_spec}).get('pools')

    # Assemble the lists of member id's to add and remove, if any exist
    tenant_id = request.user.project_id
302
    request_member_data = data.get('members', [])
303 304
    existing_members = neutronclient(request).list_lbaas_members(
        pool_id, tenant_id=tenant_id).get('members')
305 306
    (members_to_add, members_to_delete) = get_members_to_add_remove(
        request_member_data, existing_members)
307 308 309 310 311

    if members_to_add or members_to_delete:
        args = (request, loadbalancer_id, update_member_list)
        kwargs = {'callback_kwargs': {'existing_members': existing_members,
                                      'members_to_add': members_to_add,
312 313
                                      'members_to_delete': members_to_delete,
                                      'pool_id': pool_id}}
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        thread.start_new_thread(poll_loadbalancer_status, args, kwargs)
    elif data.get('monitor'):
        args = (request, loadbalancer_id, update_monitor)
        thread.start_new_thread(poll_loadbalancer_status, args)

    return pools


def update_monitor(request, **kwargs):
    """Update a health monitor.

    """
    data = request.DATA
    monitor_spec = {}
    monitor_id = data['monitor']['id']

    if data['monitor'].get('interval'):
        monitor_spec['delay'] = data['monitor']['interval']
    if data['monitor'].get('timeout'):
        monitor_spec['timeout'] = data['monitor']['timeout']
    if data['monitor'].get('retry'):
        monitor_spec['max_retries'] = data['monitor']['retry']
    if data['monitor'].get('method'):
        monitor_spec['http_method'] = data['monitor']['method']
    if data['monitor'].get('path'):
        monitor_spec['url_path'] = data['monitor']['path']
    if data['monitor'].get('status'):
        monitor_spec['expected_codes'] = data['monitor']['status']

    healthmonitor = neutronclient(request).update_lbaas_healthmonitor(
        monitor_id, {'healthmonitor': monitor_spec}).get('healthmonitor')

    return healthmonitor


def update_member_list(request, **kwargs):
    """Update the list of members by adding or removing the necessary members.

    """
    data = request.DATA
    loadbalancer_id = data.get('loadbalancer_id')
355
    pool_id = kwargs.get('pool_id')
356 357 358 359
    existing_members = kwargs.get('existing_members')
    members_to_add = kwargs.get('members_to_add')
    members_to_delete = kwargs.get('members_to_delete')

360
    if members_to_delete:
361 362
        kwargs = {'existing_members': existing_members,
                  'members_to_add': members_to_add,
363 364 365 366
                  'members_to_delete': members_to_delete,
                  'pool_id': pool_id}
        remove_member(request, **kwargs)
    elif members_to_add:
367 368
        kwargs = {'existing_members': existing_members,
                  'members_to_add': members_to_add,
369 370 371
                  'members_to_delete': members_to_delete,
                  'pool_id': pool_id}
        add_member(request, **kwargs)
372 373 374 375 376
    elif data.get('monitor'):
        args = (request, loadbalancer_id, update_monitor)
        thread.start_new_thread(poll_loadbalancer_status, args)


377 378 379 380 381 382 383 384 385 386
def get_members_to_add_remove(request_member_data, existing_members):
    new_member_ids = [member['id'] for member in request_member_data]
    existing_member_ids = [member['id'] for member in existing_members]
    members_to_add = [member_id for member_id in new_member_ids
                      if member_id not in existing_member_ids]
    members_to_delete = [member_id for member_id in existing_member_ids
                         if member_id not in new_member_ids]
    return members_to_add, members_to_delete


387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
def add_floating_ip_info(request, loadbalancers):
    """Add floating IP address info to each load balancer.

    """
    floating_ips = network.tenant_floating_ip_list(request)
    for lb in loadbalancers:
        floating_ip = {}
        associated_ip = next((fip for fip in floating_ips
                              if fip['fixed_ip'] == lb['vip_address']), None)
        if associated_ip is not None:
            floating_ip['id'] = associated_ip['id']
            floating_ip['ip'] = associated_ip['ip']
        lb['floating_ip'] = floating_ip


402 403 404 405 406 407 408 409 410 411 412 413 414 415
@urls.register
class LoadBalancers(generic.View):
    """API for load balancers.

    """
    url_regex = r'lbaas/loadbalancers/$'

    @rest_utils.ajax()
    def get(self, request):
        """List load balancers for current project.

        The listing result is an object with property "items".
        """
        tenant_id = request.user.project_id
416 417 418 419 420
        loadbalancers = neutronclient(request).list_loadbalancers(
            tenant_id=tenant_id).get('loadbalancers')
        if request.GET.get('full') and network.floating_ip_supported(request):
            add_floating_ip_info(request, loadbalancers)
        return {'items': loadbalancers}
421

422 423 424 425 426 427 428
    @rest_utils.ajax()
    def post(self, request):
        """Create a new load balancer.

        Creates a new load balancer as well as other optional resources such as
        a listener, pool, monitor, etc.
        """
429
        return create_loadbalancer(request)
430

431 432 433

@urls.register
class LoadBalancer(generic.View):
434
    """API for retrieving, updating, and deleting a single load balancer.
435 436 437 438 439 440 441 442 443 444

    """
    url_regex = r'lbaas/loadbalancers/(?P<loadbalancer_id>[^/]+)/$'

    @rest_utils.ajax()
    def get(self, request, loadbalancer_id):
        """Get a specific load balancer.

        http://localhost/api/lbaas/loadbalancers/cc758c90-3d98-4ea1-af44-aab405c9c915
        """
445 446 447 448 449
        loadbalancer = neutronclient(request).show_loadbalancer(
            loadbalancer_id).get('loadbalancer')
        if request.GET.get('full') and network.floating_ip_supported(request):
            add_floating_ip_info(request, [loadbalancer])
        return loadbalancer
450 451 452 453 454 455

    @rest_utils.ajax()
    def put(self, request, loadbalancer_id):
        """Edit a load balancer.

        """
456 457
        kwargs = {'loadbalancer_id': loadbalancer_id}
        update_loadbalancer(request, **kwargs)
458

459 460 461 462 463 464 465 466
    @rest_utils.ajax()
    def delete(self, request, loadbalancer_id):
        """Delete a specific load balancer.

        http://localhost/api/lbaas/loadbalancers/cc758c90-3d98-4ea1-af44-aab405c9c915
        """
        neutronclient(request).delete_loadbalancer(loadbalancer_id)

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

@urls.register
class Listeners(generic.View):
    """API for load balancer listeners.

    """
    url_regex = r'lbaas/listeners/$'

    @rest_utils.ajax()
    def get(self, request):
        """List of listeners for the current project.

        The listing result is an object with property "items".
        """
        loadbalancer_id = request.GET.get('loadbalancerId')
        tenant_id = request.user.project_id
        result = neutronclient(request).list_listeners(tenant_id=tenant_id)
        listener_list = result.get('listeners')
        if loadbalancer_id:
            listener_list = self._filter_listeners(listener_list,
                                                   loadbalancer_id)
        return {'items': listener_list}

490 491 492 493 494 495 496 497 498 499
    @rest_utils.ajax()
    def post(self, request):
        """Create a new listener.

        Creates a new listener as well as other optional resources such as
        a pool, members, and health monitor.
        """
        kwargs = {'loadbalancer_id': request.DATA.get('loadbalancer_id')}
        return create_listener(request, **kwargs)

500 501 502 503 504 505 506 507 508 509 510 511
    def _filter_listeners(self, listener_list, loadbalancer_id):
        filtered_listeners = []

        for listener in listener_list:
            if listener['loadbalancers'][0]['id'] == loadbalancer_id:
                filtered_listeners.append(listener)

        return filtered_listeners


@urls.register
class Listener(generic.View):
512
    """API for retrieving, updating, and deleting a single listener.
513 514 515 516 517 518 519 520

    """
    url_regex = r'lbaas/listeners/(?P<listener_id>[^/]+)/$'

    @rest_utils.ajax()
    def get(self, request, listener_id):
        """Get a specific listener.

521 522 523
        If the param 'includeChildResources' is passed in as a truthy value,
        the details of all resources that exist under the listener will be
        returned along with the listener details.
524

525 526
        http://localhost/api/lbaas/listeners/cc758c90-3d98-4ea1-af44-aab405c9c915
        """
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
        listener = neutronclient(request).show_listener(
            listener_id).get('listener')

        if request.GET.get('includeChildResources'):
            resources = {}
            resources['listener'] = listener

            if listener.get('default_pool_id'):
                pool_id = listener['default_pool_id']
                pool = neutronclient(request).show_lbaas_pool(
                    pool_id).get('pool')
                resources['pool'] = pool

                if pool.get('members'):
                    tenant_id = request.user.project_id
                    members = neutronclient(request).list_lbaas_members(
                        pool_id, tenant_id=tenant_id).get('members')
                    resources['members'] = members

                if pool.get('healthmonitor_id'):
                    monitor_id = pool['healthmonitor_id']
                    monitor = neutronclient(request).show_lbaas_healthmonitor(
                        monitor_id).get('healthmonitor')
                    resources['monitor'] = monitor

            return resources
        else:
            return listener

    @rest_utils.ajax()
    def put(self, request, listener_id):
        """Edit a listener as well as any resources below it.

        """
        kwargs = {'listener_id': listener_id}
        update_listener(request, **kwargs)
563

564 565 566 567 568 569 570 571
    @rest_utils.ajax()
    def delete(self, request, listener_id):
        """Delete a specific listener.

        http://localhost/api/lbaas/listeners/cc758c90-3d98-4ea1-af44-aab405c9c915
        """
        neutronclient(request).delete_listener(listener_id)

572

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
@urls.register
class Pools(generic.View):
    """API for load balancer pools.

    """
    url_regex = r'lbaas/pools/$'

    @rest_utils.ajax()
    def post(self, request):
        """Create a new pool.

        Creates a new pool as well as other optional resources such as
        members and health monitor.
        """
        kwargs = {'loadbalancer_id': request.DATA.get('loadbalancer_id'),
                  'listener_id': request.DATA.get('parentResourceId')}
        return create_pool(request, **kwargs)


592 593 594 595 596 597 598 599 600 601 602
@urls.register
class Pool(generic.View):
    """API for retrieving a single pool.

    """
    url_regex = r'lbaas/pools/(?P<pool_id>[^/]+)/$'

    @rest_utils.ajax()
    def get(self, request, pool_id):
        """Get a specific pool.

603 604 605 606
        If the param 'includeChildResources' is passed in as a truthy value,
        the details of all resources that exist under the pool will be
        returned along with the pool details.

607 608
        http://localhost/api/lbaas/pools/cc758c90-3d98-4ea1-af44-aab405c9c915
        """
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
        pool = neutronclient(request).show_lbaas_pool(pool_id).get('pool')

        if request.GET.get('includeChildResources'):
            resources = {}
            resources['pool'] = pool

            if pool.get('members'):
                tenant_id = request.user.project_id
                members = neutronclient(request).list_lbaas_members(
                    pool_id, tenant_id=tenant_id).get('members')
                resources['members'] = members

            if pool.get('healthmonitor_id'):
                monitor_id = pool['healthmonitor_id']
                monitor = neutronclient(request).show_lbaas_healthmonitor(
                    monitor_id).get('healthmonitor')
                resources['monitor'] = monitor

            return resources
        else:
            return pool

    @rest_utils.ajax()
    def put(self, request, pool_id):
        """Edit a listener as well as any resources below it.

        """
        kwargs = {'pool_id': pool_id}
        update_pool(request, **kwargs)
638

Justin Pomeroy's avatar
Justin Pomeroy committed
639 640 641 642 643 644 645 646
    @rest_utils.ajax()
    def delete(self, request, pool_id):
        """Delete a specific pool.

        http://localhost/api/lbaas/pools/cc758c90-3d98-4ea1-af44-aab405c9c915
        """
        neutronclient(request).delete_lbaas_pool(pool_id)

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

@urls.register
class Members(generic.View):
    """API for load balancer members.

    """
    url_regex = r'lbaas/pools/(?P<pool_id>[^/]+)/members/$'

    @rest_utils.ajax()
    def get(self, request, pool_id):
        """List of members for the current project.

        The listing result is an object with property "items".
        """
        tenant_id = request.user.project_id
        result = neutronclient(request).list_lbaas_members(pool_id,
                                                           tenant_id=tenant_id)
        return {'items': result.get('members')}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    @rest_utils.ajax()
    def put(self, request, pool_id):
        """Update the list of members for the current project.

        """
        # Assemble the lists of member id's to add and remove, if any exist
        tenant_id = request.user.project_id
        request_member_data = request.DATA.get('members', [])
        existing_members = neutronclient(request).list_lbaas_members(
            pool_id, tenant_id=tenant_id).get('members')
        (members_to_add, members_to_delete) = get_members_to_add_remove(
            request_member_data, existing_members)

        if members_to_add or members_to_delete:
            kwargs = {'existing_members': existing_members,
                      'members_to_add': members_to_add,
                      'members_to_delete': members_to_delete,
                      'pool_id': pool_id}
            update_member_list(request, **kwargs)

686 687 688 689 690 691 692 693 694 695 696 697 698 699

@urls.register
class Member(generic.View):
    """API for retrieving a single member.

    """
    url_regex = r'lbaas/pools/(?P<pool_id>[^/]+)' + \
                '/members/(?P<member_id>[^/]+)/$'

    @rest_utils.ajax()
    def get(self, request, member_id, pool_id):
        """Get a specific member belonging to a specific pool.

        """
700 701 702 703 704 705 706 707 708 709 710 711 712 713
        return neutronclient(request).show_lbaas_member(
            member_id, pool_id).get('member')

    @rest_utils.ajax()
    def put(self, request, member_id, pool_id):
        """Edit a pool member.

        """
        data = request.DATA
        spec = {
            'weight': data['weight']
        }
        return neutronclient(request).update_lbaas_member(
            member_id, pool_id, {'member': spec})
714 715


716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
@urls.register
class HealthMonitors(generic.View):
    """API for load balancer pool health monitors.

    """
    url_regex = r'lbaas/healthmonitors/$'

    @rest_utils.ajax()
    def post(self, request):
        """Create a new health monitor.

        """
        kwargs = {'loadbalancer_id': request.DATA.get('loadbalancer_id'),
                  'pool_id': request.DATA.get('parentResourceId')}
        return create_health_monitor(request, **kwargs)


733 734 735 736 737 738 739 740 741 742 743 744
@urls.register
class HealthMonitor(generic.View):
    """API for retrieving a single health monitor.

    """
    url_regex = r'lbaas/healthmonitors/(?P<healthmonitor_id>[^/]+)/$'

    @rest_utils.ajax()
    def get(self, request, healthmonitor_id):
        """Get a specific health monitor.

        """
745 746
        return neutronclient(request).show_lbaas_healthmonitor(
            healthmonitor_id).get('healthmonitor')
747 748 749 750 751 752 753 754

    @rest_utils.ajax()
    def delete(self, request, healthmonitor_id):
        """Delete a specific health monitor.

        http://localhost/api/lbaas/healthmonitors/cc758c90-3d98-4ea1-af44-aab405c9c915
        """
        neutronclient(request).delete_lbaas_healthmonitor(healthmonitor_id)
755 756 757 758 759 760 761

    @rest_utils.ajax()
    def put(self, request, healthmonitor_id):
        """Edit a health monitor.

        """
        update_monitor(request)