Debian Codegraph @

neutronclient/neutron/v2_0/lb/v2/pool.py

raw
1 # Copyright 2013 Mirantis Inc.
2 # Copyright 2014 Blue Box Group, Inc.
3 # Copyright 2015 Hewlett-Packard Development Company, L.P.
4 # Copyright 2015 Blue Box, an IBM Company
5 # All Rights Reserved
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18 #
19
20 from neutronclient._i18n import _
21 from neutronclient.common import exceptions
22 from neutronclient.common import utils
23 from neutronclient.neutron import v2_0 as neutronV20
24
25
26 def _get_loadbalancer_id(client, lb_id_or_name):
27 return neutronV20.find_resourceid_by_name_or_id(
28 client, 'loadbalancer', lb_id_or_name,
29 cmd_resource='lbaas_loadbalancer')
30
31
32 def _get_listener(client, listener_id_or_name):
33 return neutronV20.find_resource_by_name_or_id(
34 client, 'listener', listener_id_or_name)
35
36
37 def _get_listener_id(client, listener_id_or_name):
38 return neutronV20.find_resourceid_by_name_or_id(
39 client, 'listener', listener_id_or_name)
40
41
42 def _add_common_args(parser, is_create=True):
43 parser.add_argument(
44 '--description',
45 help=_('Description of the pool.'))
46 parser.add_argument(
47 '--name', help=_('The name of the pool.'))
48 parser.add_argument(
49 '--lb-algorithm',
50 required=is_create,
51 type=utils.convert_to_uppercase,
52 choices=['ROUND_ROBIN', 'LEAST_CONNECTIONS', 'SOURCE_IP'],
53 help=_('The algorithm used to distribute load between the members '
54 'of the pool.'))
55
56
57 def _parse_common_args(parsed_args):
58 body = {}
59 neutronV20.update_dict(parsed_args,
60 body, ['description', 'lb_algorithm', 'name'])
61 return body
62
63
64 class ListPool(neutronV20.ListCommand):
65 """LBaaS v2 List pools that belong to a given tenant."""
66
67 resource = 'pool'
68 shadow_resource = 'lbaas_pool'
69 list_columns = ['id', 'name', 'lb_algorithm', 'protocol',
70 'admin_state_up']
71 pagination_support = True
72 sorting_support = True
73
74
75 class ShowPool(neutronV20.ShowCommand):
76 """LBaaS v2 Show information of a given pool."""
77
78 resource = 'pool'
79 shadow_resource = 'lbaas_pool'
80
81 def cleanup_output_data(self, data):
82 if 'members' not in data['pool']:
83 return []
84 member_info = []
85 for member in data['pool']['members']:
86 member_info.append(member['id'])
87 data['pool']['members'] = member_info
88
89
90 class CreatePool(neutronV20.CreateCommand):
91 """LBaaS v2 Create a pool."""
92
93 resource = 'pool'
94 shadow_resource = 'lbaas_pool'
95
96 def add_known_arguments(self, parser):
97 _add_common_args(parser)
98 parser.add_argument(
99 '--admin-state-down',
100 dest='admin_state', action='store_false',
101 help=_('Set admin state up to false.'))
102 parser.add_argument(
103 '--listener',
104 help=_('Listener whose default-pool should be set to this pool. '
105 'At least one of --listener or --loadbalancer must be '
106 'specified.'))
107 parser.add_argument(
108 '--loadbalancer',
109 help=_('Loadbalancer with which this pool should be associated. '
110 'At least one of --listener or --loadbalancer must be '
111 'specified.'))
112 parser.add_argument(
113 '--protocol',
114 type=utils.convert_to_uppercase,
115 required=True,
116 choices=['HTTP', 'HTTPS', 'TCP'],
117 help=_('Protocol for balancing.'))
118 parser.add_argument(
119 '--session-persistence',
120 metavar='type=TYPE[,cookie_name=COOKIE_NAME]',
121 type=utils.str2dict_type(required_keys=['type'],
122 optional_keys=['cookie_name']),
123 help=_('The type of session persistence to use and associated '
124 'cookie name.'))
125
126 def args2body(self, parsed_args):
127 if not parsed_args.listener and not parsed_args.loadbalancer:
128 message = _('At least one of --listener or --loadbalancer must be '
129 'specified.')
130 raise exceptions.CommandError(message)
131 body = _parse_common_args(parsed_args)
132 if parsed_args.listener:
133 listener_id = _get_listener_id(
134 self.get_client(),
135 parsed_args.listener)
136 body['listener_id'] = listener_id
137 if parsed_args.loadbalancer:
138 loadbalancer_id = _get_loadbalancer_id(
139 self.get_client(),
140 parsed_args.loadbalancer)
141 body['loadbalancer_id'] = loadbalancer_id
142 body['admin_state_up'] = parsed_args.admin_state
143 neutronV20.update_dict(parsed_args, body,
144 ['tenant_id', 'protocol',
145 'session_persistence'])
146 return {self.resource: body}
147
148
149 class UpdatePool(neutronV20.UpdateCommand):
150 """LBaaS v2 Update a given pool."""
151
152 resource = 'pool'
153 shadow_resource = 'lbaas_pool'
154
155 def add_known_arguments(self, parser):
156 utils.add_boolean_argument(
157 parser, '--admin-state-up',
158 help=_('Update the administrative state of '
159 'the pool (True meaning "Up").'))
160 session_group = parser.add_mutually_exclusive_group()
161 session_group.add_argument(
162 '--session-persistence',
163 metavar='type=TYPE[,cookie_name=COOKIE_NAME]',
164 type=utils.str2dict_type(required_keys=['type'],
165 optional_keys=['cookie_name']),
166 help=_('The type of session persistence to use and associated '
167 'cookie name.'))
168 session_group.add_argument(
169 '--no-session-persistence',
170 action='store_true',
171 help=_('Clear session persistence for the pool.'))
172 _add_common_args(parser, False)
173
174 def args2body(self, parsed_args):
175 body = _parse_common_args(parsed_args)
176 if parsed_args.no_session_persistence:
177 body['session_persistence'] = None
178 elif parsed_args.session_persistence:
179 body['session_persistence'] = parsed_args.session_persistence
180 neutronV20.update_dict(parsed_args, body,
181 ['admin_state_up'])
182 return {self.resource: body}
183
184
185 class DeletePool(neutronV20.DeleteCommand):
186 """LBaaS v2 Delete a given pool."""
187
188 resource = 'pool'
189 shadow_resource = 'lbaas_pool'