OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2009 Mitch Garnaat http://garnaat.org/ |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 |
| 22 """ |
| 23 Represents an DBSecurityGroup |
| 24 """ |
| 25 from boto.ec2.securitygroup import SecurityGroup |
| 26 |
| 27 class DBSecurityGroup(object): |
| 28 """ |
| 29 Represents an RDS database security group |
| 30 |
| 31 Properties reference available from the AWS documentation at http://docs.ama
zonwebservices.com/AmazonRDS/latest/APIReference/API_DeleteDBSecurityGroup.html |
| 32 |
| 33 :ivar Status: The current status of the security group. Possibile values are
[ active, ? ]. Reference documentation lacks specifics of possibilities |
| 34 :ivar connection: boto.rds.RDSConnection associated with the current object |
| 35 :ivar description: The description of the security group |
| 36 :ivar ec2_groups: List of EC2SecurityGroup objects that this security group
PERMITS |
| 37 :ivar ip_ranges: List of IPRange objects (containing CIDR addresses) that th
is security group PERMITS |
| 38 :ivar name: Name of the security group |
| 39 :ivar owner_id: ID of the owner of the security group. Can be 'None' |
| 40 """ |
| 41 def __init__(self, connection=None, owner_id=None, |
| 42 name=None, description=None): |
| 43 self.connection = connection |
| 44 self.owner_id = owner_id |
| 45 self.name = name |
| 46 self.description = description |
| 47 self.ec2_groups = [] |
| 48 self.ip_ranges = [] |
| 49 |
| 50 def __repr__(self): |
| 51 return 'DBSecurityGroup:%s' % self.name |
| 52 |
| 53 def startElement(self, name, attrs, connection): |
| 54 if name == 'IPRange': |
| 55 cidr = IPRange(self) |
| 56 self.ip_ranges.append(cidr) |
| 57 return cidr |
| 58 elif name == 'EC2SecurityGroup': |
| 59 ec2_grp = EC2SecurityGroup(self) |
| 60 self.ec2_groups.append(ec2_grp) |
| 61 return ec2_grp |
| 62 else: |
| 63 return None |
| 64 |
| 65 def endElement(self, name, value, connection): |
| 66 if name == 'OwnerId': |
| 67 self.owner_id = value |
| 68 elif name == 'DBSecurityGroupName': |
| 69 self.name = value |
| 70 elif name == 'DBSecurityGroupDescription': |
| 71 self.description = value |
| 72 elif name == 'IPRanges': |
| 73 pass |
| 74 else: |
| 75 setattr(self, name, value) |
| 76 |
| 77 def delete(self): |
| 78 return self.connection.delete_dbsecurity_group(self.name) |
| 79 |
| 80 def authorize(self, cidr_ip=None, ec2_group=None): |
| 81 """ |
| 82 Add a new rule to this DBSecurity group. |
| 83 You need to pass in either a CIDR block to authorize or |
| 84 and EC2 SecurityGroup. |
| 85 |
| 86 @type cidr_ip: string |
| 87 @param cidr_ip: A valid CIDR IP range to authorize |
| 88 |
| 89 @type ec2_group: :class:`boto.ec2.securitygroup.SecurityGroup>` |
| 90 |
| 91 @rtype: bool |
| 92 @return: True if successful. |
| 93 """ |
| 94 if isinstance(ec2_group, SecurityGroup): |
| 95 group_name = ec2_group.name |
| 96 group_owner_id = ec2_group.owner_id |
| 97 else: |
| 98 group_name = None |
| 99 group_owner_id = None |
| 100 return self.connection.authorize_dbsecurity_group(self.name, |
| 101 cidr_ip, |
| 102 group_name, |
| 103 group_owner_id) |
| 104 |
| 105 def revoke(self, cidr_ip=None, ec2_group=None): |
| 106 """ |
| 107 Revoke access to a CIDR range or EC2 SecurityGroup. |
| 108 You need to pass in either a CIDR block or |
| 109 an EC2 SecurityGroup from which to revoke access. |
| 110 |
| 111 @type cidr_ip: string |
| 112 @param cidr_ip: A valid CIDR IP range to revoke |
| 113 |
| 114 @type ec2_group: :class:`boto.ec2.securitygroup.SecurityGroup>` |
| 115 |
| 116 @rtype: bool |
| 117 @return: True if successful. |
| 118 """ |
| 119 if isinstance(ec2_group, SecurityGroup): |
| 120 group_name = ec2_group.name |
| 121 group_owner_id = ec2_group.owner_id |
| 122 return self.connection.revoke_dbsecurity_group( |
| 123 self.name, |
| 124 ec2_security_group_name=group_name, |
| 125 ec2_security_group_owner_id=group_owner_id) |
| 126 |
| 127 # Revoking by CIDR IP range |
| 128 return self.connection.revoke_dbsecurity_group( |
| 129 self.name, cidr_ip=cidr_ip) |
| 130 |
| 131 class IPRange(object): |
| 132 """ |
| 133 Describes a CIDR address range for use in a DBSecurityGroup |
| 134 """ |
| 135 |
| 136 def __init__(self, parent=None): |
| 137 self.parent = parent |
| 138 self.cidr_ip = None |
| 139 self.status = None |
| 140 |
| 141 def __repr__(self): |
| 142 return 'IPRange:%s' % self.cidr_ip |
| 143 |
| 144 def startElement(self, name, attrs, connection): |
| 145 pass |
| 146 |
| 147 def endElement(self, name, value, connection): |
| 148 if name == 'CIDRIP': |
| 149 self.cidr_ip = value |
| 150 elif name == 'Status': |
| 151 self.status = value |
| 152 else: |
| 153 setattr(self, name, value) |
| 154 |
| 155 class EC2SecurityGroup(object): |
| 156 """ |
| 157 Describes an EC2 security group for use in a DBSecurityGroup |
| 158 """ |
| 159 |
| 160 def __init__(self, parent=None): |
| 161 self.parent = parent |
| 162 self.name = None |
| 163 self.owner_id = None |
| 164 |
| 165 def __repr__(self): |
| 166 return 'EC2SecurityGroup:%s' % self.name |
| 167 |
| 168 def startElement(self, name, attrs, connection): |
| 169 pass |
| 170 |
| 171 def endElement(self, name, value, connection): |
| 172 if name == 'EC2SecurityGroupName': |
| 173 self.name = value |
| 174 elif name == 'EC2SecurityGroupOwnerId': |
| 175 self.owner_id = value |
| 176 else: |
| 177 setattr(self, name, value) |
OLD | NEW |