• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2006-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
22class ParameterGroup(dict):
23
24    def __init__(self, connection=None):
25        dict.__init__(self)
26        self.connection = connection
27        self.name = None
28        self.description = None
29        self.engine = None
30        self._current_param = None
31
32    def __repr__(self):
33        return 'ParameterGroup:%s' % self.name
34
35    def startElement(self, name, attrs, connection):
36        if name == 'Parameter':
37            if self._current_param:
38                self[self._current_param.name] = self._current_param
39            self._current_param = Parameter(self)
40            return self._current_param
41
42    def endElement(self, name, value, connection):
43        if name == 'DBParameterGroupName':
44            self.name = value
45        elif name == 'Description':
46            self.description = value
47        elif name == 'Engine':
48            self.engine = value
49        else:
50            setattr(self, name, value)
51
52    def modifiable(self):
53        mod = []
54        for key in self:
55            p = self[key]
56            if p.is_modifiable:
57                mod.append(p)
58        return mod
59
60    def get_params(self):
61        pg = self.connection.get_all_dbparameters(self.name)
62        self.update(pg)
63
64    def add_param(self, name, value, apply_method):
65        param = Parameter()
66        param.name = name
67        param.value = value
68        param.apply_method = apply_method
69        self.params.append(param)
70
71class Parameter(object):
72    """
73    Represents a RDS Parameter
74    """
75
76    ValidTypes = {'integer' : int,
77                  'string' : str,
78                  'boolean' : bool}
79    ValidSources = ['user', 'system', 'engine-default']
80    ValidApplyTypes = ['static', 'dynamic']
81    ValidApplyMethods = ['immediate', 'pending-reboot']
82
83    def __init__(self, group=None, name=None):
84        self.group = group
85        self.name = name
86        self._value = None
87        self.type = 'string'
88        self.source = None
89        self.is_modifiable = True
90        self.description = None
91        self.apply_method = None
92        self.allowed_values = None
93
94    def __repr__(self):
95        return 'Parameter:%s' % self.name
96
97    def startElement(self, name, attrs, connection):
98        pass
99
100    def endElement(self, name, value, connection):
101        if name == 'ParameterName':
102            self.name = value
103        elif name == 'ParameterValue':
104            self._value = value
105        elif name == 'DataType':
106            if value in self.ValidTypes:
107                self.type = value
108        elif name == 'Source':
109            if value in self.ValidSources:
110                self.source = value
111        elif name == 'IsModifiable':
112            if value.lower() == 'true':
113                self.is_modifiable = True
114            else:
115                self.is_modifiable = False
116        elif name == 'Description':
117            self.description = value
118        elif name == 'ApplyType':
119            if value in self.ValidApplyTypes:
120                self.apply_type = value
121        elif name == 'AllowedValues':
122            self.allowed_values = value
123        else:
124            setattr(self, name, value)
125
126    def merge(self, d, i):
127        prefix = 'Parameters.member.%d.' % i
128        if self.name:
129            d[prefix+'ParameterName'] = self.name
130        if self._value is not None:
131            d[prefix+'ParameterValue'] = self._value
132        if self.apply_type:
133            d[prefix+'ApplyMethod'] = self.apply_method
134
135    def _set_string_value(self, value):
136        if not isinstance(value, basestring):
137            raise ValueError('value must be of type str')
138        if self.allowed_values:
139            choices = self.allowed_values.split(',')
140            if value not in choices:
141                raise ValueError('value must be in %s' % self.allowed_values)
142        self._value = value
143
144    def _set_integer_value(self, value):
145        if isinstance(value, basestring):
146            value = int(value)
147        if isinstance(value, int) or isinstance(value, long):
148            if self.allowed_values:
149                min, max = self.allowed_values.split('-')
150                if value < int(min) or value > int(max):
151                    raise ValueError('range is %s' % self.allowed_values)
152            self._value = value
153        else:
154            raise ValueError('value must be integer')
155
156    def _set_boolean_value(self, value):
157        if isinstance(value, bool):
158            self._value = value
159        elif isinstance(value, basestring):
160            if value.lower() == 'true':
161                self._value = True
162            else:
163                self._value = False
164        else:
165            raise ValueError('value must be boolean')
166
167    def set_value(self, value):
168        if self.type == 'string':
169            self._set_string_value(value)
170        elif self.type == 'integer':
171            self._set_integer_value(value)
172        elif self.type == 'boolean':
173            self._set_boolean_value(value)
174        else:
175            raise TypeError('unknown type (%s)' % self.type)
176
177    def get_value(self):
178        if self._value is None:
179            return self._value
180        if self.type == 'string':
181            return self._value
182        elif self.type == 'integer':
183            if not isinstance(self._value, int) and not isinstance(self._value, long):
184                self._set_integer_value(self._value)
185            return self._value
186        elif self.type == 'boolean':
187            if not isinstance(self._value, bool):
188                self._set_boolean_value(self._value)
189            return self._value
190        else:
191            raise TypeError('unknown type (%s)' % self.type)
192
193    value = property(get_value, set_value, 'The value of the parameter')
194
195    def apply(self, immediate=False):
196        if immediate:
197            self.apply_method = 'immediate'
198        else:
199            self.apply_method = 'pending-reboot'
200        self.group.connection.modify_parameter_group(self.group.name, [self])
201
202