Package yakumo :: Package nova :: Package v2 :: Module security_group_rule
[hide private]
[frames] | no frames]

Source Code for Module yakumo.nova.v2.security_group_rule

  1  # Copyright 2014-2017 by Akira Yoshiyama <akirayoshiyama@gmail.com>. 
  2  # All Rights Reserved. 
  3  # 
  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 
  7  # 
  8  #         http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 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 
 14  #    under the License. 
 15   
 16  """ 
 17  Resource class and its manager for security group rules in Compute API v2 
 18  """ 
 19   
 20  from yakumo import base 
 21  from yakumo.constant import UNDEF 
 22  from yakumo import mapper 
 23  from yakumo import utils 
 24   
 25   
 26  ATTRIBUTE_MAPPING = [ 
 27      ('id', 'id', mapper.Noop), 
 28      ('remote_ip_prefix', 'ip_range', mapper.Noop), 
 29      ('port_range_min', 'from_port', mapper.Noop), 
 30      ('port_range_max', 'to_port', mapper.Noop), 
 31      ('protocol', 'ip_protocol', mapper.Noop), 
 32      ('group', 'group', mapper.Noop), 
 33      ('project', 'tenant_id', mapper.Resource('project')), 
 34  ] 
 35   
 36   
37 -class Resource(base.Resource):
38 """Resource class for security group rules in Compute API v2""" 39
40 - def delete(self):
41 """ 42 Delete a security group rule 43 44 @rtype: None 45 """ 46 super(Resource, self).delete() 47 self._reload_rules()
48 49
50 -class Manager(base.SubManager):
51 """Manager class for security group rules in Compute API v2""" 52 53 resource_class = Resource 54 service_type = 'compute' 55 _attr_mapping = ATTRIBUTE_MAPPING 56 _hidden_methods = ["update"] 57 _json_resource_key = 'security_group_rule' 58 _json_resources_key = 'security_group_rules' 59 _url_resource_path = '/os-security-group-rules' 60
61 - def __init__(self, parent_resource, *args, **kwargs):
62 super(Manager, self).__init__(parent_resource, *args, **kwargs) 63 if self.parent_resource._rules: 64 self._rules = [self.resource_class(self, **self._json2attr(x)) 65 for x in self.parent_resource._rules] 66 else: 67 self._rules = []
68
69 - def _reload_rules(self):
70 self.parent_resource.get() 71 if self.parent_resource._rules: 72 self._rules = [self.resource_class(self, **self._json2attr(x)) 73 for x in self.parent_resource._rules] 74 else: 75 self._rules = []
76
77 - def create(self, remote_ip_prefix=UNDEF, port_range_min=UNDEF, 78 port_range_max=UNDEF, protocol=UNDEF, group=UNDEF, 79 project=UNDEF):
80 """ 81 Create a security group rule 82 83 @keyword remote_ip_prefix: IP range (e.q. '0.0.0.0/0', required) 84 @type remote_ip_prefix: str 85 @keyword port_range_min: lower number of destination port 86 @type port_range_min: int 87 @keyword port_range_max: upper number of destination port 88 @type port_range_max: int 89 @keyword protocol: protocol name ('tcp', 'udp', or 'icmp') 90 @type protocol: str 91 @keyword group: List of a project and a security group name 92 @type group: [(yakumo.project.Resource, str)] 93 @keyword project: Project 94 @type project: yakumo.project.Resource 95 @return: Created security group rule 96 @rtype: yakumo.nova.v2.security_group_rule.Resource 97 """ 98 ret = super(Manager, self).create(remote_ip_prefix=remote_ip_prefix, 99 port_range_min=port_range_min, 100 port_range_max=port_range_max, 101 protocol=protocol, 102 group=group, 103 project=project) 104 self._reload_rules() 105 return ret
106
107 - def get(self, id):
108 """ 109 Get a security group rule 110 111 @keyword id: Security group rule ID 112 @type id: str 113 @return: Created security group rule 114 @rtype: yakumo.nova.v2.security_group_rule.Resource 115 """ 116 for rule in self._rules: 117 if rule.id == id: 118 return rule
119
120 - def _find_gen(self, **kwargs):
121 """ 122 Find a security group rule 123 124 :param key=value: search condition 125 """ 126 for sg_rule in self._rules: 127 for k, v in kwargs.items(): 128 if getattr(sg_rule, k, None) != v: 129 break 130 else: 131 yield sg_rule
132