| OLD | NEW |
| (Empty) | |
| 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 |
| 22 from boto.rds.dbsecuritygroup import DBSecurityGroup |
| 23 from boto.rds.parametergroup import ParameterGroup |
| 24 from boto.resultset import ResultSet |
| 25 |
| 26 |
| 27 class DBInstance(object): |
| 28 """ |
| 29 Represents a RDS DBInstance |
| 30 |
| 31 Properties reference available from the AWS documentation at |
| 32 http://goo.gl/sC2Kn |
| 33 |
| 34 :ivar connection: connection |
| 35 :ivar id: The name and identifier of the DBInstance |
| 36 :ivar create_time: The date and time of creation |
| 37 :ivar engine: The database engine being used |
| 38 :ivar status: The status of the database in a string. e.g. "available" |
| 39 :ivar allocated_storage: The size of the disk in gigabytes (int). |
| 40 :ivar endpoint: A tuple that describes the hostname and port of |
| 41 the instance. This is only available when the database is |
| 42 in status "available". |
| 43 :ivar instance_class: Contains the name of the compute and memory |
| 44 capacity class of the DB Instance. |
| 45 :ivar master_username: The username that is set as master username |
| 46 at creation time. |
| 47 :ivar parameter_groups: Provides the list of DB Parameter Groups |
| 48 applied to this DB Instance. |
| 49 :ivar security_groups: Provides List of DB Security Group elements |
| 50 containing only DBSecurityGroup.Name and DBSecurityGroup.Status |
| 51 subelements. |
| 52 :ivar availability_zone: Specifies the name of the Availability Zone |
| 53 the DB Instance is located in. |
| 54 :ivar backup_retention_period: Specifies the number of days for |
| 55 which automatic DB Snapshots are retained. |
| 56 :ivar preferred_backup_window: Specifies the daily time range during |
| 57 which automated backups are created if automated backups are |
| 58 enabled, as determined by the backup_retention_period. |
| 59 :ivar preferred_maintenance_window: Specifies the weekly time |
| 60 range (in UTC) during which system maintenance can occur. (string) |
| 61 :ivar latest_restorable_time: Specifies the latest time to which |
| 62 a database can be restored with point-in-time restore. (string) |
| 63 :ivar multi_az: Boolean that specifies if the DB Instance is a |
| 64 Multi-AZ deployment. |
| 65 :ivar iops: The current number of provisioned IOPS for the DB Instance. |
| 66 Can be None if this is a standard instance. |
| 67 :ivar pending_modified_values: Specifies that changes to the |
| 68 DB Instance are pending. This element is only included when changes |
| 69 are pending. Specific changes are identified by subelements. |
| 70 """ |
| 71 |
| 72 def __init__(self, connection=None, id=None): |
| 73 self.connection = connection |
| 74 self.id = id |
| 75 self.create_time = None |
| 76 self.engine = None |
| 77 self.status = None |
| 78 self.allocated_storage = None |
| 79 self.endpoint = None |
| 80 self.instance_class = None |
| 81 self.master_username = None |
| 82 self.parameter_groups = [] |
| 83 self.security_groups = [] |
| 84 self.availability_zone = None |
| 85 self.backup_retention_period = None |
| 86 self.preferred_backup_window = None |
| 87 self.preferred_maintenance_window = None |
| 88 self.latest_restorable_time = None |
| 89 self.multi_az = False |
| 90 self.iops = None |
| 91 self.pending_modified_values = None |
| 92 self._in_endpoint = False |
| 93 self._port = None |
| 94 self._address = None |
| 95 |
| 96 def __repr__(self): |
| 97 return 'DBInstance:%s' % self.id |
| 98 |
| 99 def startElement(self, name, attrs, connection): |
| 100 if name == 'Endpoint': |
| 101 self._in_endpoint = True |
| 102 elif name == 'DBParameterGroups': |
| 103 self.parameter_groups = ResultSet([('DBParameterGroup', |
| 104 ParameterGroup)]) |
| 105 return self.parameter_groups |
| 106 elif name == 'DBSecurityGroups': |
| 107 self.security_groups = ResultSet([('DBSecurityGroup', |
| 108 DBSecurityGroup)]) |
| 109 return self.security_groups |
| 110 elif name == 'PendingModifiedValues': |
| 111 self.pending_modified_values = PendingModifiedValues() |
| 112 return self.pending_modified_values |
| 113 return None |
| 114 |
| 115 def endElement(self, name, value, connection): |
| 116 if name == 'DBInstanceIdentifier': |
| 117 self.id = value |
| 118 elif name == 'DBInstanceStatus': |
| 119 self.status = value |
| 120 elif name == 'InstanceCreateTime': |
| 121 self.create_time = value |
| 122 elif name == 'Engine': |
| 123 self.engine = value |
| 124 elif name == 'DBInstanceStatus': |
| 125 self.status = value |
| 126 elif name == 'AllocatedStorage': |
| 127 self.allocated_storage = int(value) |
| 128 elif name == 'DBInstanceClass': |
| 129 self.instance_class = value |
| 130 elif name == 'MasterUsername': |
| 131 self.master_username = value |
| 132 elif name == 'Port': |
| 133 if self._in_endpoint: |
| 134 self._port = int(value) |
| 135 elif name == 'Address': |
| 136 if self._in_endpoint: |
| 137 self._address = value |
| 138 elif name == 'Endpoint': |
| 139 self.endpoint = (self._address, self._port) |
| 140 self._in_endpoint = False |
| 141 elif name == 'AvailabilityZone': |
| 142 self.availability_zone = value |
| 143 elif name == 'BackupRetentionPeriod': |
| 144 self.backup_retention_period = value |
| 145 elif name == 'LatestRestorableTime': |
| 146 self.latest_restorable_time = value |
| 147 elif name == 'PreferredMaintenanceWindow': |
| 148 self.preferred_maintenance_window = value |
| 149 elif name == 'PreferredBackupWindow': |
| 150 self.preferred_backup_window = value |
| 151 elif name == 'MultiAZ': |
| 152 if value.lower() == 'true': |
| 153 self.multi_az = True |
| 154 elif name == 'Iops': |
| 155 self.iops = int(value) |
| 156 else: |
| 157 setattr(self, name, value) |
| 158 |
| 159 @property |
| 160 def security_group(self): |
| 161 """ |
| 162 Provide backward compatibility for previous security_group |
| 163 attribute. |
| 164 """ |
| 165 if len(self.security_groups) > 0: |
| 166 return self.security_groups[-1] |
| 167 else: |
| 168 return None |
| 169 |
| 170 @property |
| 171 def parameter_group(self): |
| 172 """ |
| 173 Provide backward compatibility for previous parameter_group |
| 174 attribute. |
| 175 """ |
| 176 if len(self.parameter_groups) > 0: |
| 177 return self.parameter_groups[-1] |
| 178 else: |
| 179 return None |
| 180 |
| 181 def snapshot(self, snapshot_id): |
| 182 """ |
| 183 Create a new DB snapshot of this DBInstance. |
| 184 |
| 185 :type identifier: string |
| 186 :param identifier: The identifier for the DBSnapshot |
| 187 |
| 188 :rtype: :class:`boto.rds.dbsnapshot.DBSnapshot` |
| 189 :return: The newly created DBSnapshot |
| 190 """ |
| 191 return self.connection.create_dbsnapshot(snapshot_id, self.id) |
| 192 |
| 193 def reboot(self): |
| 194 """ |
| 195 Reboot this DBInstance |
| 196 |
| 197 :rtype: :class:`boto.rds.dbsnapshot.DBSnapshot` |
| 198 :return: The newly created DBSnapshot |
| 199 """ |
| 200 return self.connection.reboot_dbinstance(self.id) |
| 201 |
| 202 def update(self, validate=False): |
| 203 """ |
| 204 Update the DB instance's status information by making a call to fetch |
| 205 the current instance attributes from the service. |
| 206 |
| 207 :type validate: bool |
| 208 :param validate: By default, if EC2 returns no data about the |
| 209 instance the update method returns quietly. If the |
| 210 validate param is True, however, it will raise a |
| 211 ValueError exception if no data is returned from EC2. |
| 212 """ |
| 213 rs = self.connection.get_all_dbinstances(self.id) |
| 214 if len(rs) > 0: |
| 215 for i in rs: |
| 216 if i.id == self.id: |
| 217 self.__dict__.update(i.__dict__) |
| 218 elif validate: |
| 219 raise ValueError('%s is not a valid Instance ID' % self.id) |
| 220 return self.status |
| 221 |
| 222 def stop(self, skip_final_snapshot=False, final_snapshot_id=''): |
| 223 """ |
| 224 Delete this DBInstance. |
| 225 |
| 226 :type skip_final_snapshot: bool |
| 227 :param skip_final_snapshot: This parameter determines whether |
| 228 a final db snapshot is created before the instance is |
| 229 deleted. If True, no snapshot is created. If False, a |
| 230 snapshot is created before deleting the instance. |
| 231 |
| 232 :type final_snapshot_id: str |
| 233 :param final_snapshot_id: If a final snapshot is requested, this |
| 234 is the identifier used for that snapshot. |
| 235 |
| 236 :rtype: :class:`boto.rds.dbinstance.DBInstance` |
| 237 :return: The deleted db instance. |
| 238 """ |
| 239 return self.connection.delete_dbinstance(self.id, |
| 240 skip_final_snapshot, |
| 241 final_snapshot_id) |
| 242 |
| 243 def modify(self, param_group=None, security_groups=None, |
| 244 preferred_maintenance_window=None, |
| 245 master_password=None, allocated_storage=None, |
| 246 instance_class=None, |
| 247 backup_retention_period=None, |
| 248 preferred_backup_window=None, |
| 249 multi_az=False, |
| 250 iops=None, |
| 251 apply_immediately=False): |
| 252 """ |
| 253 Modify this DBInstance. |
| 254 |
| 255 :type security_groups: list of str or list of DBSecurityGroup objects |
| 256 :param security_groups: List of names of DBSecurityGroup to |
| 257 authorize on this DBInstance. |
| 258 |
| 259 :type preferred_maintenance_window: str |
| 260 :param preferred_maintenance_window: The weekly time range (in |
| 261 UTC) during which maintenance can occur. Default is |
| 262 Sun:05:00-Sun:09:00 |
| 263 |
| 264 :type master_password: str |
| 265 :param master_password: Password of master user for the DBInstance. |
| 266 Must be 4-15 alphanumeric characters. |
| 267 |
| 268 :type allocated_storage: int |
| 269 :param allocated_storage: The new allocated storage size, in GBs. |
| 270 Valid values are [5-1024] |
| 271 |
| 272 :type instance_class: str |
| 273 :param instance_class: The compute and memory capacity of the |
| 274 DBInstance. Changes will be applied at next maintenance |
| 275 window unless apply_immediately is True. |
| 276 |
| 277 Valid values are: |
| 278 |
| 279 * db.m1.small |
| 280 * db.m1.large |
| 281 * db.m1.xlarge |
| 282 * db.m2.xlarge |
| 283 * db.m2.2xlarge |
| 284 * db.m2.4xlarge |
| 285 |
| 286 :type apply_immediately: bool |
| 287 :param apply_immediately: If true, the modifications will be |
| 288 applied as soon as possible rather than waiting for the |
| 289 next preferred maintenance window. |
| 290 |
| 291 :type backup_retention_period: int |
| 292 :param backup_retention_period: The number of days for which |
| 293 automated backups are retained. Setting this to zero |
| 294 disables automated backups. |
| 295 |
| 296 :type preferred_backup_window: str |
| 297 :param preferred_backup_window: The daily time range during |
| 298 which automated backups are created (if enabled). Must be |
| 299 in h24:mi-hh24:mi format (UTC). |
| 300 |
| 301 :type multi_az: bool |
| 302 :param multi_az: If True, specifies the DB Instance will be |
| 303 deployed in multiple availability zones. |
| 304 |
| 305 :type iops: int |
| 306 :param iops: The amount of IOPS (input/output operations per |
| 307 second) to Provisioned for the DB Instance. Can be |
| 308 modified at a later date. |
| 309 |
| 310 Must scale linearly. For every 1000 IOPS provision, you |
| 311 must allocated 100 GB of storage space. This scales up to |
| 312 1 TB / 10 000 IOPS for MySQL and Oracle. MSSQL is limited |
| 313 to 700 GB / 7 000 IOPS. |
| 314 |
| 315 If you specify a value, it must be at least 1000 IOPS and |
| 316 you must allocate 100 GB of storage. |
| 317 |
| 318 :rtype: :class:`boto.rds.dbinstance.DBInstance` |
| 319 :return: The modified db instance. |
| 320 """ |
| 321 return self.connection.modify_dbinstance(self.id, |
| 322 param_group, |
| 323 security_groups, |
| 324 preferred_maintenance_window, |
| 325 master_password, |
| 326 allocated_storage, |
| 327 instance_class, |
| 328 backup_retention_period, |
| 329 preferred_backup_window, |
| 330 multi_az, |
| 331 apply_immediately, |
| 332 iops) |
| 333 |
| 334 |
| 335 class PendingModifiedValues(dict): |
| 336 |
| 337 def startElement(self, name, attrs, connection): |
| 338 return None |
| 339 |
| 340 def endElement(self, name, value, connection): |
| 341 if name != 'PendingModifiedValues': |
| 342 self[name] = value |
| OLD | NEW |