~ubuntu-branches/ubuntu/raring/python-quantumclient/raring-proposed

« back to all changes in this revision

Viewing changes to quantumclient/v2_0/client.py

  • Committer: Package Import Robot
  • Author(s): James Page, Adam Gandelman, James Page
  • Date: 2013-03-15 09:15:00 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130315091500-cc5l87wlu910e6il
Tags: 1:2.2.0-0ubuntu1
[ Adam Gandelman ]
* debian/control: Set version minimum for python-cliff (>= 1.3.1). 

[ James Page ]
* New upstream point release to support OpenStack Grizzly. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import logging
20
20
import time
21
21
import urllib
 
22
import urlparse
22
23
 
23
24
from quantumclient.client import HTTPClient
24
25
from quantumclient.common import _
177
178
    network_gateways_path = "/network-gateways"
178
179
    network_gateway_path = "/network-gateways/%s"
179
180
 
 
181
    DHCP_NETS = '/dhcp-networks'
 
182
    DHCP_AGENTS = '/dhcp-agents'
 
183
    L3_ROUTERS = '/l3-routers'
 
184
    L3_AGENTS = '/l3-agents'
180
185
    # API has no way to report plurals, so we have to hard code them
181
186
    EXTED_PLURALS = {'routers': 'router',
182
187
                     'floatingips': 'floatingip',
241
246
        return self.get(self.ext_path % ext_alias, params=_params)
242
247
 
243
248
    @APIParamsCall
244
 
    def list_ports(self, **_params):
 
249
    def list_ports(self, retrieve_all=True, **_params):
245
250
        """
246
251
        Fetches a list of all networks for a tenant
247
252
        """
248
253
        # Pass filters in "params" argument to do_request
249
 
        return self.get(self.ports_path, params=_params)
 
254
        return self.list('ports', self.ports_path, retrieve_all,
 
255
                         **_params)
250
256
 
251
257
    @APIParamsCall
252
258
    def show_port(self, port, **_params):
277
283
        return self.delete(self.port_path % (port))
278
284
 
279
285
    @APIParamsCall
280
 
    def list_networks(self, **_params):
 
286
    def list_networks(self, retrieve_all=True, **_params):
281
287
        """
282
288
        Fetches a list of all networks for a tenant
283
289
        """
284
290
        # Pass filters in "params" argument to do_request
285
 
        return self.get(self.networks_path, params=_params)
 
291
        return self.list('networks', self.networks_path, retrieve_all,
 
292
                         **_params)
286
293
 
287
294
    @APIParamsCall
288
295
    def show_network(self, network, **_params):
313
320
        return self.delete(self.network_path % (network))
314
321
 
315
322
    @APIParamsCall
316
 
    def list_subnets(self, **_params):
 
323
    def list_subnets(self, retrieve_all=True, **_params):
317
324
        """
318
325
        Fetches a list of all networks for a tenant
319
326
        """
320
 
        return self.get(self.subnets_path, params=_params)
 
327
        return self.list('subnets', self.subnets_path, retrieve_all,
 
328
                         **_params)
321
329
 
322
330
    @APIParamsCall
323
331
    def show_subnet(self, subnet, **_params):
348
356
        return self.delete(self.subnet_path % (subnet))
349
357
 
350
358
    @APIParamsCall
351
 
    def list_routers(self, **_params):
 
359
    def list_routers(self, retrieve_all=True, **_params):
352
360
        """
353
361
        Fetches a list of all routers for a tenant
354
362
        """
355
363
        # Pass filters in "params" argument to do_request
356
 
        return self.get(self.routers_path, params=_params)
 
364
        return self.list('routers', self.routers_path, retrieve_all,
 
365
                         **_params)
357
366
 
358
367
    @APIParamsCall
359
368
    def show_router(self, router, **_params):
416
425
                        body={'router': {'external_gateway_info': {}}})
417
426
 
418
427
    @APIParamsCall
419
 
    def list_floatingips(self, **_params):
 
428
    def list_floatingips(self, retrieve_all=True, **_params):
420
429
        """
421
430
        Fetches a list of all floatingips for a tenant
422
431
        """
423
432
        # Pass filters in "params" argument to do_request
424
 
        return self.get(self.floatingips_path, params=_params)
 
433
        return self.list('floatingips', self.floatingips_path, retrieve_all,
 
434
                         **_params)
425
435
 
426
436
    @APIParamsCall
427
437
    def show_floatingip(self, floatingip, **_params):
459
469
        return self.post(self.security_groups_path, body=body)
460
470
 
461
471
    @APIParamsCall
462
 
    def list_security_groups(self, **_params):
 
472
    def list_security_groups(self, retrieve_all=True, **_params):
463
473
        """
464
474
        Fetches a list of all security groups for a tenant
465
475
        """
466
 
        return self.get(self.security_groups_path, params=_params)
 
476
        return self.list('security_groups', self.security_groups_path,
 
477
                         retrieve_all, **_params)
467
478
 
468
479
    @APIParamsCall
469
480
    def show_security_group(self, security_group, **_params):
496
507
                           (security_group_rule))
497
508
 
498
509
    @APIParamsCall
499
 
    def list_security_group_rules(self, **_params):
 
510
    def list_security_group_rules(self, retrieve_all=True, **_params):
500
511
        """
501
512
        Fetches a list of all security group rules for a tenant
502
513
        """
503
 
        return self.get(self.security_group_rules_path, params=_params)
 
514
        return self.list('security_group_rules',
 
515
                         self.security_group_rules_path,
 
516
                         retrieve_all, **_params)
504
517
 
505
518
    @APIParamsCall
506
519
    def show_security_group_rule(self, security_group_rule, **_params):
511
524
                        params=_params)
512
525
 
513
526
    @APIParamsCall
514
 
    def list_vips(self, **_params):
 
527
    def list_vips(self, retrieve_all=True, **_params):
515
528
        """
516
529
        Fetches a list of all load balancer vips for a tenant
517
530
        """
518
531
        # Pass filters in "params" argument to do_request
519
 
        return self.get(self.vips_path, params=_params)
 
532
        return self.list('vips', self.vips_path, retrieve_all,
 
533
                         **_params)
520
534
 
521
535
    @APIParamsCall
522
536
    def show_vip(self, vip, **_params):
547
561
        return self.delete(self.vip_path % (vip))
548
562
 
549
563
    @APIParamsCall
550
 
    def list_pools(self, **_params):
 
564
    def list_pools(self, retrieve_all=True, **_params):
551
565
        """
552
566
        Fetches a list of all load balancer pools for a tenant
553
567
        """
554
568
        # Pass filters in "params" argument to do_request
555
 
        return self.get(self.pools_path, params=_params)
 
569
        return self.list('pools', self.pools_path, retrieve_all,
 
570
                         **_params)
556
571
 
557
572
    @APIParamsCall
558
573
    def show_pool(self, pool, **_params):
590
605
        return self.get(self.pool_path_stats % (pool), params=_params)
591
606
 
592
607
    @APIParamsCall
593
 
    def list_members(self, **_params):
 
608
    def list_members(self, retrieve_all=True, **_params):
594
609
        """
595
610
        Fetches a list of all load balancer members for a tenant
596
611
        """
597
612
        # Pass filters in "params" argument to do_request
598
 
        return self.get(self.members_path, params=_params)
 
613
        return self.list('members', self.members_path, retrieve_all,
 
614
                         **_params)
599
615
 
600
616
    @APIParamsCall
601
617
    def show_member(self, member, **_params):
626
642
        return self.delete(self.member_path % (member))
627
643
 
628
644
    @APIParamsCall
629
 
    def list_health_monitors(self, **_params):
 
645
    def list_health_monitors(self, retrieve_all=True, **_params):
630
646
        """
631
647
        Fetches a list of all load balancer health monitors for a tenant
632
648
        """
633
649
        # Pass filters in "params" argument to do_request
634
 
        return self.get(self.health_monitors_path, params=_params)
 
650
        return self.list('health_monitors', self.health_monitors_path,
 
651
                         retrieve_all, **_params)
635
652
 
636
653
    @APIParamsCall
637
654
    def show_health_monitor(self, health_monitor, **_params):
788
805
        base_uri = self.network_gateway_path % gateway_id
789
806
        return self.put("%s/disconnect_network" % base_uri, body=body)
790
807
 
 
808
    @APIParamsCall
 
809
    def list_dhcp_agent_hosting_networks(self, network, **_params):
 
810
        """
 
811
        Fetches a list of dhcp agents hosting a network.
 
812
        """
 
813
        return self.get((self.network_path + self.DHCP_AGENTS) % network,
 
814
                        params=_params)
 
815
 
 
816
    @APIParamsCall
 
817
    def list_networks_on_dhcp_agent(self, dhcp_agent, **_params):
 
818
        """
 
819
        Fetches a list of dhcp agents hosting a network.
 
820
        """
 
821
        return self.get((self.agent_path + self.DHCP_NETS) % dhcp_agent,
 
822
                        params=_params)
 
823
 
 
824
    @APIParamsCall
 
825
    def add_network_to_dhcp_agent(self, dhcp_agent, body=None):
 
826
        """
 
827
        Adds a network to dhcp agent.
 
828
        """
 
829
        return self.post((self.agent_path + self.DHCP_NETS) % dhcp_agent,
 
830
                         body=body)
 
831
 
 
832
    @APIParamsCall
 
833
    def remove_network_from_dhcp_agent(self, dhcp_agent, network_id):
 
834
        """
 
835
        Remove a network from dhcp agent.
 
836
        """
 
837
        return self.delete((self.agent_path + self.DHCP_NETS + "/%s") % (
 
838
            dhcp_agent, network_id))
 
839
 
 
840
    @APIParamsCall
 
841
    def list_l3_agent_hosting_routers(self, router, **_params):
 
842
        """
 
843
        Fetches a list of L3 agents hosting a router.
 
844
        """
 
845
        return self.get((self.router_path + self.L3_AGENTS) % router,
 
846
                        params=_params)
 
847
 
 
848
    @APIParamsCall
 
849
    def list_routers_on_l3_agent(self, l3_agent, **_params):
 
850
        """
 
851
        Fetches a list of L3 agents hosting a router.
 
852
        """
 
853
        return self.get((self.agent_path + self.L3_ROUTERS) % l3_agent,
 
854
                        params=_params)
 
855
 
 
856
    @APIParamsCall
 
857
    def add_router_to_l3_agent(self, l3_agent, body):
 
858
        """
 
859
        Adds a router to L3 agent.
 
860
        """
 
861
        return self.post((self.agent_path + self.L3_ROUTERS) % l3_agent,
 
862
                         body=body)
 
863
 
 
864
    @APIParamsCall
 
865
    def remove_router_from_l3_agent(self, l3_agent, router_id):
 
866
        """
 
867
        Remove a router from l3 agent.
 
868
        """
 
869
        return self.delete((self.agent_path + self.L3_ROUTERS + "/%s") % (
 
870
            l3_agent, router_id))
 
871
 
791
872
    def __init__(self, **kwargs):
792
873
        """ Initialize a new client for the Quantum v2.0 API. """
793
874
        super(Client, self).__init__()
908
989
    def put(self, action, body=None, headers=None, params=None):
909
990
        return self.retry_request("PUT", action, body=body,
910
991
                                  headers=headers, params=params)
 
992
 
 
993
    def list(self, collection, path, retrieve_all=True, **params):
 
994
        if retrieve_all:
 
995
            res = []
 
996
            for r in self._pagination(collection, path, **params):
 
997
                res.extend(r[collection])
 
998
            return {collection: res}
 
999
        else:
 
1000
            return self._pagination(collection, path, **params)
 
1001
 
 
1002
    def _pagination(self, collection, path, **params):
 
1003
        if params.get('page_reverse', False):
 
1004
            linkrel = 'previous'
 
1005
        else:
 
1006
            linkrel = 'next'
 
1007
        next = True
 
1008
        while next:
 
1009
            res = self.get(path, params=params)
 
1010
            yield res
 
1011
            next = False
 
1012
            try:
 
1013
                for link in res['%s_links' % collection]:
 
1014
                    if link['rel'] == linkrel:
 
1015
                        query_str = urlparse.urlparse(link['href']).query
 
1016
                        params = urlparse.parse_qs(query_str)
 
1017
                        next = True
 
1018
                        break
 
1019
            except KeyError:
 
1020
                break