1 # Copyright 2012, Nachi Ueno, NTT MCL, Inc.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 from cliff import lister
17 from neutronclient.common import utils
18 from neutronclient.neutron import v2_0 as client
19 from neutronclient.neutron.v2_0 import port
21 from neutron._i18n import _, _LI
24 class ProbeCommand(client.NeutronCommand):
26 def get_debug_agent(self):
27 return self.app.debug_agent
30 class CreateProbe(ProbeCommand):
31 """Create probe port and interface, then plug it in."""
33 def get_parser(self, prog_name):
34 parser = super(CreateProbe, self).get_parser(prog_name)
36 'id', metavar='network_id',
37 help=_('ID of network to probe'))
40 default='network', choices=['network', 'compute'],
41 help=_('Owner type of the device: network/compute'))
44 def take_action(self, parsed_args):
45 debug_agent = self.get_debug_agent()
46 probe_port = debug_agent.create_probe(parsed_args.id,
47 parsed_args.device_owner)
48 self.log.info(_('Probe created : %s '), probe_port.id)
51 class DeleteProbe(ProbeCommand):
52 """Delete probe - delete port then uplug."""
54 def get_parser(self, prog_name):
55 parser = super(DeleteProbe, self).get_parser(prog_name)
57 'id', metavar='port_id',
58 help=_('ID of probe port to delete'))
61 def take_action(self, parsed_args):
62 debug_agent = self.get_debug_agent()
63 debug_agent.delete_probe(parsed_args.id)
64 self.log.info(_('Probe %s deleted'), parsed_args.id)
67 class ListProbe(ProbeCommand, lister.Lister):
70 _formatters = {'fixed_ips': port._format_fixed_ips, }
72 def take_action(self, parsed_args):
73 debug_agent = self.get_debug_agent()
74 info = debug_agent.list_probes()
75 columns = sorted(info[0].keys()) if info else []
76 return (columns, (utils.get_item_properties(
77 s, columns, formatters=self._formatters, )
81 class ClearProbe(ProbeCommand):
82 """Clear All probes."""
84 def take_action(self, parsed_args):
85 debug_agent = self.get_debug_agent()
86 cleared_probes_count = debug_agent.clear_probes()
87 self.log.info(_LI('%d probe(s) deleted'), cleared_probes_count)
90 class ExecProbe(ProbeCommand):
91 """Exec commands on the namespace of the probe."""
93 def get_parser(self, prog_name):
94 parser = super(ExecProbe, self).get_parser(prog_name)
96 'id', metavar='port_id',
97 help=_('ID of probe port to execute command'))
99 'command', metavar='command',
102 help=_('Command to execute'))
105 def take_action(self, parsed_args):
106 debug_agent = self.get_debug_agent()
107 result = debug_agent.exec_command(parsed_args.id, parsed_args.command)
108 self.app.stdout.write(result + '\n')
111 class PingAll(ProbeCommand):
112 """Ping all fixed_ip."""
114 def get_parser(self, prog_name):
115 parser = super(PingAll, self).get_parser(prog_name)
117 '--timeout', metavar='<timeout>',
119 help=_('Ping timeout'))
121 '--id', metavar='network_id',
123 help=_('ID of network'))
126 def take_action(self, parsed_args):
127 debug_agent = self.get_debug_agent()
128 result = debug_agent.ping_all(parsed_args.id,
129 timeout=parsed_args.timeout)
130 self.app.stdout.write(result + '\n')