Debian Codegraph @

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

raw
1 # Copyright 2014 Blue Box Group, Inc.
2 # Copyright 2015 Hewlett-Packard Development Company, L.P.
3 # All Rights Reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16 #
17 from oslo_serialization import jsonutils
18
19 from neutronclient._i18n import _
20 from neutronclient.common import utils
21 from neutronclient.neutron import v2_0 as neutronV20
22
23
24 def _add_common_args(parser):
25 parser.add_argument(
26 '--description',
27 help=_('Description of the load balancer.'))
28 parser.add_argument(
29 '--name', metavar='NAME',
30 help=_('Name of the load balancer.'))
31
32
33 def _parse_common_args(body, parsed_args):
34 neutronV20.update_dict(parsed_args, body,
35 ['name', 'description'])
36
37
38 class ListLoadBalancer(neutronV20.ListCommand):
39 """LBaaS v2 List loadbalancers that belong to a given tenant."""
40
41 resource = 'loadbalancer'
42 list_columns = ['id', 'name', 'vip_address',
43 'provisioning_status', 'provider']
44 pagination_support = True
45 sorting_support = True
46
47
48 class ShowLoadBalancer(neutronV20.ShowCommand):
49 """LBaaS v2 Show information of a given loadbalancer."""
50
51 resource = 'loadbalancer'
52
53
54 class CreateLoadBalancer(neutronV20.CreateCommand):
55 """LBaaS v2 Create a loadbalancer."""
56
57 resource = 'loadbalancer'
58
59 def add_known_arguments(self, parser):
60 _add_common_args(parser)
61 parser.add_argument(
62 '--admin-state-down',
63 dest='admin_state', action='store_false',
64 help=_('Set admin state up to false.'))
65 parser.add_argument(
66 '--provider',
67 help=_('Provider name of the load balancer service.'))
68 parser.add_argument(
69 '--flavor',
70 help=_('ID or name of the flavor.'))
71 parser.add_argument(
72 '--vip-address',
73 help=_('VIP address for the load balancer.'))
74 parser.add_argument(
75 'vip_subnet', metavar='VIP_SUBNET',
76 help=_('Load balancer VIP subnet.'))
77
78 def args2body(self, parsed_args):
79 _subnet_id = neutronV20.find_resourceid_by_name_or_id(
80 self.get_client(), 'subnet', parsed_args.vip_subnet)
81 body = {'vip_subnet_id': _subnet_id,
82 'admin_state_up': parsed_args.admin_state}
83 if parsed_args.flavor:
84 _flavor_id = neutronV20.find_resourceid_by_name_or_id(
85 self.get_client(), 'flavor', parsed_args.flavor)
86 body['flavor_id'] = _flavor_id
87
88 neutronV20.update_dict(parsed_args, body,
89 ['provider', 'vip_address', 'tenant_id'])
90 _parse_common_args(body, parsed_args)
91 return {self.resource: body}
92
93
94 class UpdateLoadBalancer(neutronV20.UpdateCommand):
95 """LBaaS v2 Update a given loadbalancer."""
96
97 resource = 'loadbalancer'
98
99 def add_known_arguments(self, parser):
100 utils.add_boolean_argument(
101 parser, '--admin-state-up',
102 help=_('Update the administrative state of '
103 'the load balancer (True meaning "Up").'))
104 _add_common_args(parser)
105
106 def args2body(self, parsed_args):
107 body = {}
108 _parse_common_args(body, parsed_args)
109 neutronV20.update_dict(parsed_args, body,
110 ['admin_state_up'])
111 return {self.resource: body}
112
113
114 class DeleteLoadBalancer(neutronV20.DeleteCommand):
115 """LBaaS v2 Delete a given loadbalancer."""
116
117 resource = 'loadbalancer'
118
119
120 class RetrieveLoadBalancerStats(neutronV20.ShowCommand):
121 """Retrieve stats for a given loadbalancer."""
122
123 resource = 'loadbalancer'
124
125 def take_action(self, parsed_args):
126 neutron_client = self.get_client()
127 loadbalancer_id = neutronV20.find_resourceid_by_name_or_id(
128 self.get_client(), 'loadbalancer', parsed_args.id)
129 params = {}
130 if parsed_args.fields:
131 params = {'fields': parsed_args.fields}
132
133 data = neutron_client.retrieve_loadbalancer_stats(loadbalancer_id,
134 **params)
135 self.format_output_data(data)
136 stats = data['stats']
137 if 'stats' in data:
138 # To render the output table like:
139 # +--------------------+-------+
140 # | Field | Value |
141 # +--------------------+-------+
142 # | field1 | value1|
143 # | field2 | value2|
144 # | field3 | value3|
145 # | ... | ... |
146 # +--------------------+-------+
147 # it has two columns and the Filed column is alphabetical,
148 # here convert the data dict to the 1-1 vector format below:
149 # [(field1, field2, field3, ...), (value1, value2, value3, ...)]
150 return list(zip(*sorted(stats.items())))
151
152
153 class RetrieveLoadBalancerStatus(neutronV20.NeutronCommand):
154 """Retrieve status for a given loadbalancer.
155
156 The only output is a formatted JSON tree, and the table format
157 does not support this type of data.
158 """
159 resource = 'loadbalancer'
160
161 def get_parser(self, prog_name):
162 parser = super(RetrieveLoadBalancerStatus, self).get_parser(prog_name)
163 parser.add_argument(
164 self.resource, metavar=self.resource.upper(),
165 help=_('ID or name of %s to show.') % self.resource)
166
167 return parser
168
169 def take_action(self, parsed_args):
170 self.log.debug('run(%s)', parsed_args)
171 neutron_client = self.get_client()
172 lb_id = neutronV20.find_resourceid_by_name_or_id(
173 neutron_client, self.resource, parsed_args.loadbalancer)
174 params = {}
175 data = neutron_client.retrieve_loadbalancer_status(lb_id, **params)
176 res = data['statuses']
177 if 'statuses' in data:
178 print(jsonutils.dumps(res, indent=4))