~jk0/nova/xs-ipv6

« back to all changes in this revision

Viewing changes to vendor/boto/boto/rds/dbsecuritygroup.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    def __init__(self, connection=None, owner_id=None,
 
30
                 name=None, description=None):
 
31
        self.connection = connection
 
32
        self.owner_id = owner_id
 
33
        self.name = name
 
34
        self.description = description
 
35
        self.ec2_groups = []
 
36
        self.ip_ranges = []
 
37
 
 
38
    def __repr__(self):
 
39
        return 'DBSecurityGroup:%s' % self.name
 
40
 
 
41
    def startElement(self, name, attrs, connection):
 
42
        if name == 'IPRange':
 
43
            cidr = IPRange(self)
 
44
            self.ip_ranges.append(cidr)
 
45
            return cidr
 
46
        elif name == 'EC2SecurityGroup':
 
47
            ec2_grp = EC2SecurityGroup(self)
 
48
            self.ec2_groups.append(ec2_grp)
 
49
            return ec2_grp
 
50
        else:
 
51
            return None
 
52
 
 
53
    def endElement(self, name, value, connection):
 
54
        if name == 'OwnerId':
 
55
            self.owner_id = value
 
56
        elif name == 'DBSecurityGroupName':
 
57
            self.name = value
 
58
        elif name == 'DBSecurityGroupDescription':
 
59
            self.description = value
 
60
        elif name == 'IPRanges':
 
61
            pass
 
62
        else:
 
63
            setattr(self, name, value)
 
64
 
 
65
    def delete(self):
 
66
        return self.connection.delete_dbsecurity_group(self.name)
 
67
 
 
68
    def authorize(self, cidr_ip=None, ec2_group=None):
 
69
        """
 
70
        Add a new rule to this DBSecurity group.
 
71
        You need to pass in either a CIDR block to authorize or
 
72
        and EC2 SecurityGroup.
 
73
        
 
74
        @type cidr_ip: string
 
75
        @param cidr_ip: A valid CIDR IP range to authorize
 
76
 
 
77
        @type ec2_group: :class:`boto.ec2.securitygroup.SecurityGroup>`
 
78
                         
 
79
        @rtype: bool
 
80
        @return: True if successful.
 
81
        """
 
82
        if isinstance(ec2_group, SecurityGroup):
 
83
            group_name = ec2_group.name
 
84
            group_owner_id = ec2_group.owner_id
 
85
        else:
 
86
            group_name = None
 
87
            group_owner_id = None
 
88
        return self.connection.authorize_dbsecurity_group(self.name,
 
89
                                                          cidr_ip,
 
90
                                                          group_name,
 
91
                                                          group_owner_id)
 
92
 
 
93
    def revoke(self, cidr_ip=None, ec2_group=None):
 
94
        """
 
95
        Revoke access to a CIDR range or EC2 SecurityGroup
 
96
        You need to pass in either a CIDR block to authorize or
 
97
        and EC2 SecurityGroup.
 
98
        
 
99
        @type cidr_ip: string
 
100
        @param cidr_ip: A valid CIDR IP range to authorize
 
101
 
 
102
        @type ec2_group: :class:`boto.ec2.securitygroup.SecurityGroup>`
 
103
                         
 
104
        @rtype: bool
 
105
        @return: True if successful.
 
106
        """
 
107
        if isinstance(ec2_group, SecurityGroup):
 
108
            group_name = ec2_group.name
 
109
            group_owner_id = ec2_group.owner_id
 
110
        else:
 
111
            group_name = None
 
112
            group_owner_id = None
 
113
        return self.connection.revoke_dbsecurity_group(self.name,
 
114
                                                       cidr_ip,
 
115
                                                       group_name,
 
116
                                                       group_owner_id)
 
117
 
 
118
class IPRange(object):
 
119
 
 
120
    def __init__(self, parent=None):
 
121
        self.parent = parent
 
122
        self.cidr_ip = None
 
123
        self.status = None
 
124
 
 
125
    def __repr__(self):
 
126
        return 'IPRange:%s' % self.cidr_ip
 
127
 
 
128
    def startElement(self, name, attrs, connection):
 
129
        pass
 
130
 
 
131
    def endElement(self, name, value, connection):
 
132
        if name == 'CIDRIP':
 
133
            self.cidr_ip = value
 
134
        elif name == 'Status':
 
135
            self.status = value
 
136
        else:
 
137
            setattr(self, name, value)
 
138
 
 
139
class EC2SecurityGroup(object):
 
140
 
 
141
    def __init__(self, parent=None):
 
142
        self.parent = parent
 
143
        self.name = None
 
144
        self.owner_id = None
 
145
 
 
146
    def __repr__(self):
 
147
        return 'EC2SecurityGroup:%s' % self.name
 
148
 
 
149
    def startElement(self, name, attrs, connection):
 
150
        pass
 
151
 
 
152
    def endElement(self, name, value, connection):
 
153
        if name == 'EC2SecurityGroupName':
 
154
            self.name = value
 
155
        elif name == 'EC2SecurityGroupOwnerId':
 
156
            self.owner_id = value
 
157
        else:
 
158
            setattr(self, name, value)
 
159