~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/api/openstack/compute/contrib/security_groups.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from nova import db
30
30
from nova import exception
31
31
from nova import flags
32
 
from nova.openstack.common import excutils
33
32
from nova.openstack.common import log as logging
34
 
from nova import utils
35
33
 
36
34
 
37
35
LOG = logging.getLogger(__name__)
38
36
FLAGS = flags.FLAGS
39
37
authorize = extensions.extension_authorizer('compute', 'security_groups')
 
38
softauth = extensions.soft_extension_authorizer('compute', 'security_groups')
40
39
 
41
40
 
42
41
def make_rule(elem):
456
455
                            context, id, group_name)
457
456
 
458
457
 
 
458
class SecurityGroupsOutputController(wsgi.Controller):
 
459
    def __init__(self, *args, **kwargs):
 
460
        super(SecurityGroupsOutputController, self).__init__(*args, **kwargs)
 
461
        self.compute_api = compute.API()
 
462
 
 
463
    def _extend_servers(self, req, servers):
 
464
        key = "security_groups"
 
465
        for server in servers:
 
466
            instance = req.get_db_instance(server['id'])
 
467
            groups = instance.get(key)
 
468
            if groups:
 
469
                server[key] = [{"name": group["name"]} for group in groups]
 
470
 
 
471
    def _show(self, req, resp_obj):
 
472
        if not softauth(req.environ['nova.context']):
 
473
            return
 
474
        if 'server' in resp_obj.obj:
 
475
            resp_obj.attach(xml=SecurityGroupServerTemplate())
 
476
            self._extend_servers(req, [resp_obj.obj['server']])
 
477
 
 
478
    @wsgi.extends
 
479
    def show(self, req, resp_obj, id):
 
480
        return self._show(req, resp_obj)
 
481
 
 
482
    @wsgi.extends
 
483
    def create(self, req, resp_obj, body):
 
484
        return self._show(req, resp_obj)
 
485
 
 
486
    @wsgi.extends
 
487
    def detail(self, req, resp_obj):
 
488
        if not softauth(req.environ['nova.context']):
 
489
            return
 
490
        resp_obj.attach(xml=SecurityGroupServersTemplate())
 
491
        self._extend_servers(req, list(resp_obj.obj['servers']))
 
492
 
 
493
 
 
494
class SecurityGroupsTemplateElement(xmlutil.TemplateElement):
 
495
    def will_render(self, datum):
 
496
        return "security_groups" in datum
 
497
 
 
498
 
 
499
def make_server(elem):
 
500
    secgrps = SecurityGroupsTemplateElement('security_groups')
 
501
    elem.append(secgrps)
 
502
    secgrp = xmlutil.SubTemplateElement(secgrps, 'security_group',
 
503
                                        selector="security_groups")
 
504
    secgrp.set('name')
 
505
 
 
506
 
 
507
class SecurityGroupServerTemplate(xmlutil.TemplateBuilder):
 
508
    def construct(self):
 
509
        root = xmlutil.TemplateElement('server')
 
510
        make_server(root)
 
511
        return xmlutil.SlaveTemplate(root, 1)
 
512
 
 
513
 
 
514
class SecurityGroupServersTemplate(xmlutil.TemplateBuilder):
 
515
    def construct(self):
 
516
        root = xmlutil.TemplateElement('servers')
 
517
        elem = xmlutil.SubTemplateElement(root, 'server', selector='servers')
 
518
        make_server(elem)
 
519
        return xmlutil.SlaveTemplate(root, 1)
 
520
 
 
521
 
459
522
class Security_groups(extensions.ExtensionDescriptor):
460
523
    """Security group support"""
461
 
 
462
524
    name = "SecurityGroups"
463
 
    alias = "security_groups"
 
525
    alias = "os-security-groups"
464
526
    namespace = "http://docs.openstack.org/compute/ext/securitygroups/api/v1.1"
465
527
    updated = "2011-07-21T00:00:00+00:00"
466
528
 
467
529
    def get_controller_extensions(self):
468
530
        controller = SecurityGroupActionController()
469
 
        extension = extensions.ControllerExtension(self, 'servers', controller)
470
 
        return [extension]
 
531
        actions = extensions.ControllerExtension(self, 'servers', controller)
 
532
        controller = SecurityGroupsOutputController()
 
533
        output = extensions.ControllerExtension(self, 'servers', controller)
 
534
        return [actions, output]
471
535
 
472
536
    def get_resources(self):
473
537
        resources = []
509
573
 
510
574
    @staticmethod
511
575
    def raise_over_quota(msg):
512
 
        raise exc.HTTPBadRequest(explanation=msg)
 
576
        raise exception.SecurityGroupLimitExceeded(msg)
513
577
 
514
578
    @staticmethod
515
579
    def raise_not_found(msg):