Revision c79faeb5
b/kamaki/cli/commands/cyclades.py | ||
---|---|---|
803 | 803 |
class network_list(_init_cyclades, _optional_json, _name_filter, _id_filter): |
804 | 804 |
"""List networks""" |
805 | 805 |
|
806 |
PERMANENTS = ('id', 'name') |
|
807 |
|
|
806 | 808 |
arguments = dict( |
807 | 809 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
808 | 810 |
limit=IntArgument('limit # of listed networks', ('-n', '--number')), |
809 | 811 |
more=FlagArgument( |
810 | 812 |
'output results in pages (-n to set items per page, default 10)', |
811 | 813 |
'--more'), |
812 |
enum=FlagArgument('Enumerate results', '--enumerate') |
|
814 |
enum=FlagArgument('Enumerate results', '--enumerate'), |
|
815 |
status=ValueArgument('filter by status', ('--status')), |
|
816 |
public=FlagArgument('only public networks', ('--public')), |
|
817 |
private=FlagArgument('only private networks', ('--private')), |
|
818 |
dhcp=FlagArgument('show networks with dhcp', ('--with-dhcp')), |
|
819 |
no_dhcp=FlagArgument('show networks without dhcp', ('--without-dhcp')), |
|
820 |
user_id=ValueArgument('filter by user id', ('--user-id')), |
|
821 |
user_name=ValueArgument('filter by user name', ('--user-name')), |
|
822 |
gateway=ValueArgument('filter by gateway (IPv4)', ('--gateway')), |
|
823 |
gateway6=ValueArgument('filter by gateway (IPv6)', ('--gateway6')), |
|
824 |
cidr=ValueArgument('filter by cidr (IPv4)', ('--cidr')), |
|
825 |
cidr6=ValueArgument('filter by cidr (IPv6)', ('--cidr6')), |
|
826 |
type=ValueArgument('filter by type', ('--type')), |
|
813 | 827 |
) |
814 | 828 |
|
829 |
def _apply_common_filters(self, networks): |
|
830 |
common_filter = dict() |
|
831 |
if self['public']: |
|
832 |
if self['private']: |
|
833 |
return [] |
|
834 |
common_filter['public'] = self['public'] |
|
835 |
elif self['private']: |
|
836 |
common_filter['public'] = False |
|
837 |
if self['dhcp']: |
|
838 |
if self['no_dhcp']: |
|
839 |
return [] |
|
840 |
common_filter['dhcp'] = True |
|
841 |
elif self['no_dhcp']: |
|
842 |
common_filter['dhcp'] = False |
|
843 |
if self['user_id'] or self['user_name']: |
|
844 |
uuid = self['user_id'] or self._username2uuid(self['user_name']) |
|
845 |
common_filter['user_id'] = uuid |
|
846 |
for term in ('status', 'gateway', 'gateway6', 'cidr', 'cidr6', 'type'): |
|
847 |
if self[term]: |
|
848 |
common_filter[term] = self[term] |
|
849 |
return filter_dicts_by_dict(networks, common_filter) |
|
850 |
|
|
851 |
def _add_name(self, networks, key='user_id'): |
|
852 |
uuids = self._uuids2usernames( |
|
853 |
list(set([net[key] for net in networks]))) |
|
854 |
for net in networks: |
|
855 |
v = net.get(key, None) |
|
856 |
if v: |
|
857 |
net[key] += ' (%s)' % uuids[net[key]] |
|
858 |
return networks |
|
859 |
|
|
815 | 860 |
@errors.generic.all |
816 | 861 |
@errors.cyclades.connection |
817 | 862 |
def _run(self): |
818 |
networks = self.client.list_networks(self['detail']) |
|
863 |
withcommons = False |
|
864 |
for term in ( |
|
865 |
'status', 'public', 'private', 'user_id', 'user_name', 'type', |
|
866 |
'gateway', 'gateway6', 'cidr', 'cidr6', 'dhcp', 'no_dhcp'): |
|
867 |
if self[term]: |
|
868 |
withcommons = True |
|
869 |
break |
|
870 |
detail = self['detail'] or withcommons |
|
871 |
networks = self.client.list_networks(detail) |
|
819 | 872 |
networks = self._filter_by_name(networks) |
820 | 873 |
networks = self._filter_by_id(networks) |
874 |
if withcommons: |
|
875 |
networks = self._apply_common_filters(networks) |
|
821 | 876 |
if not (self['detail'] or self['json_output']): |
822 | 877 |
remove_from_items(networks, 'links') |
878 |
if detail and not self['detail']: |
|
879 |
for net in networks: |
|
880 |
for key in set(net).difference(self.PERMANENTS): |
|
881 |
net.pop(key) |
|
882 |
if self['detail'] and not self['json_output']: |
|
883 |
self._add_name(networks) |
|
884 |
self._add_name(networks, 'tenant_id') |
|
823 | 885 |
kwargs = dict(with_enumeration=self['enum']) |
824 | 886 |
if self['more']: |
825 | 887 |
kwargs['page_size'] = self['limit'] or 10 |
Also available in: Unified diff