| 1 |
# Copyright 2013 Mirantis Inc. |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
# |
| 16 |
|
| 17 |
from neutronclient._i18n import _ |
| 18 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 19 |
|
| 20 |
|
| 21 |
def _format_provider(pool): |
| 22 |
return pool.get('provider') or 'N/A' |
| 23 |
|
| 24 |
|
| 25 |
class ListPool(neutronV20.ListCommand): |
| 26 |
"""List pools that belong to a given tenant.""" |
| 27 |
|
| 28 |
resource = 'pool' |
| 29 |
list_columns = ['id', 'name', 'provider', 'lb_method', 'protocol', |
| 30 |
'admin_state_up', 'status'] |
| 31 |
_formatters = {'provider': _format_provider} |
| 32 |
pagination_support = True |
| 33 |
sorting_support = True |
| 34 |
|
| 35 |
|
| 36 |
class ShowPool(neutronV20.ShowCommand): |
| 37 |
"""Show information of a given pool.""" |
| 38 |
|
| 39 |
resource = 'pool' |
| 40 |
|
| 41 |
|
| 42 |
class CreatePool(neutronV20.CreateCommand): |
| 43 |
"""Create a pool.""" |
| 44 |
|
| 45 |
resource = 'pool' |
| 46 |
|
| 47 |
def add_known_arguments(self, parser): |
| 48 |
parser.add_argument( |
| 49 |
'--admin-state-down', |
| 50 |
dest='admin_state', action='store_false', |
| 51 |
help=_('Set admin state up to false.')) |
| 52 |
parser.add_argument( |
| 53 |
'--description', |
| 54 |
help=_('Description of the pool.')) |
| 55 |
parser.add_argument( |
| 56 |
'--lb-method', |
| 57 |
required=True, |
| 58 |
choices=['ROUND_ROBIN', 'LEAST_CONNECTIONS', 'SOURCE_IP'], |
| 59 |
help=_('The algorithm used to distribute load between the members ' |
| 60 |
'of the pool.')) |
| 61 |
parser.add_argument( |
| 62 |
'--name', |
| 63 |
required=True, |
| 64 |
help=_('The name of the pool.')) |
| 65 |
parser.add_argument( |
| 66 |
'--protocol', |
| 67 |
required=True, |
| 68 |
choices=['HTTP', 'HTTPS', 'TCP'], |
| 69 |
help=_('Protocol for balancing.')) |
| 70 |
parser.add_argument( |
| 71 |
'--subnet-id', metavar='SUBNET', |
| 72 |
required=True, |
| 73 |
help=_('The subnet on which the members of the pool will be ' |
| 74 |
'located.')) |
| 75 |
parser.add_argument( |
| 76 |
'--provider', |
| 77 |
help=_('Provider name of the loadbalancer service.')) |
| 78 |
|
| 79 |
def args2body(self, parsed_args): |
| 80 |
_subnet_id = neutronV20.find_resourceid_by_name_or_id( |
| 81 |
self.get_client(), 'subnet', parsed_args.subnet_id) |
| 82 |
body = {'admin_state_up': parsed_args.admin_state, |
| 83 |
'subnet_id': _subnet_id} |
| 84 |
neutronV20.update_dict(parsed_args, body, |
| 85 |
['description', 'lb_method', 'name', |
| 86 |
'protocol', 'tenant_id', 'provider']) |
| 87 |
return {self.resource: body} |
| 88 |
|
| 89 |
|
| 90 |
class UpdatePool(neutronV20.UpdateCommand): |
| 91 |
"""Update a given pool.""" |
| 92 |
|
| 93 |
resource = 'pool' |
| 94 |
|
| 95 |
|
| 96 |
class DeletePool(neutronV20.DeleteCommand): |
| 97 |
"""Delete a given pool.""" |
| 98 |
|
| 99 |
resource = 'pool' |
| 100 |
|
| 101 |
|
| 102 |
class RetrievePoolStats(neutronV20.ShowCommand): |
| 103 |
"""Retrieve stats for a given pool.""" |
| 104 |
|
| 105 |
resource = 'pool' |
| 106 |
|
| 107 |
def take_action(self, parsed_args): |
| 108 |
neutron_client = self.get_client() |
| 109 |
pool_id = neutronV20.find_resourceid_by_name_or_id( |
| 110 |
self.get_client(), 'pool', parsed_args.id) |
| 111 |
params = {} |
| 112 |
if parsed_args.fields: |
| 113 |
params = {'fields': parsed_args.fields} |
| 114 |
|
| 115 |
data = neutron_client.retrieve_pool_stats(pool_id, **params) |
| 116 |
self.format_output_data(data) |
| 117 |
stats = data['stats'] |
| 118 |
if 'stats' in data: |
| 119 |
return zip(*sorted(stats.items())) |
| 120 |
else: |
| 121 |
return None |