| 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 | |
| 25 class DBInstance(object): | |
| 26 """ | |
| 27 Represents a RDS DBInstance | |
| 28 """ | |
| 29 | |
| 30 def __init__(self, connection=None, id=None): | |
| 31 self.connection = connection | |
| 32 self.id = id | |
| 33 self.create_time = None | |
| 34 self.engine = None | |
| 35 self.status = None | |
| 36 self.allocated_storage = None | |
| 37 self.endpoint = None | |
| 38 self.instance_class = None | |
| 39 self.master_username = None | |
| 40 self.parameter_group = None | |
| 41 self.security_group = None | |
| 42 self.availability_zone = None | |
| 43 self.backup_retention_period = None | |
| 44 self.preferred_backup_window = None | |
| 45 self.preferred_maintenance_window = None | |
| 46 self.latest_restorable_time = None | |
| 47 self.multi_az = False | |
| 48 self.pending_modified_values = None | |
| 49 self._in_endpoint = False | |
| 50 self._port = None | |
| 51 self._address = None | |
| 52 | |
| 53 def __repr__(self): | |
| 54 return 'DBInstance:%s' % self.id | |
| 55 | |
| 56 def startElement(self, name, attrs, connection): | |
| 57 if name == 'Endpoint': | |
| 58 self._in_endpoint = True | |
| 59 elif name == 'DBParameterGroup': | |
| 60 self.parameter_group = ParameterGroup(self.connection) | |
| 61 return self.parameter_group | |
| 62 elif name == 'DBSecurityGroup': | |
| 63 self.security_group = DBSecurityGroup(self.connection) | |
| 64 return self.security_group | |
| 65 elif name == 'PendingModifiedValues': | |
| 66 self.pending_modified_values = PendingModifiedValues() | |
| 67 return self.pending_modified_values | |
| 68 return None | |
| 69 | |
| 70 def endElement(self, name, value, connection): | |
| 71 if name == 'DBInstanceIdentifier': | |
| 72 self.id = value | |
| 73 elif name == 'DBInstanceStatus': | |
| 74 self.status = value | |
| 75 elif name == 'InstanceCreateTime': | |
| 76 self.create_time = value | |
| 77 elif name == 'Engine': | |
| 78 self.engine = value | |
| 79 elif name == 'DBInstanceStatus': | |
| 80 self.status = value | |
| 81 elif name == 'AllocatedStorage': | |
| 82 self.allocated_storage = int(value) | |
| 83 elif name == 'DBInstanceClass': | |
| 84 self.instance_class = value | |
| 85 elif name == 'MasterUsername': | |
| 86 self.master_username = value | |
| 87 elif name == 'Port': | |
| 88 if self._in_endpoint: | |
| 89 self._port = int(value) | |
| 90 elif name == 'Address': | |
| 91 if self._in_endpoint: | |
| 92 self._address = value | |
| 93 elif name == 'Endpoint': | |
| 94 self.endpoint = (self._address, self._port) | |
| 95 self._in_endpoint = False | |
| 96 elif name == 'AvailabilityZone': | |
| 97 self.availability_zone = value | |
| 98 elif name == 'BackupRetentionPeriod': | |
| 99 self.backup_retention_period = value | |
| 100 elif name == 'LatestRestorableTime': | |
| 101 self.latest_restorable_time = value | |
| 102 elif name == 'PreferredMaintenanceWindow': | |
| 103 self.preferred_maintenance_window = value | |
| 104 elif name == 'PreferredBackupWindow': | |
| 105 self.preferred_backup_window = value | |
| 106 elif name == 'MultiAZ': | |
| 107 if value.lower() == 'true': | |
| 108 self.multi_az = True | |
| 109 else: | |
| 110 setattr(self, name, value) | |
| 111 | |
| 112 def snapshot(self, snapshot_id): | |
| 113 """ | |
| 114 Create a new DB snapshot of this DBInstance. | |
| 115 | |
| 116 :type identifier: string | |
| 117 :param identifier: The identifier for the DBSnapshot | |
| 118 | |
| 119 :rtype: :class:`boto.rds.dbsnapshot.DBSnapshot` | |
| 120 :return: The newly created DBSnapshot | |
| 121 """ | |
| 122 return self.connection.create_dbsnapshot(snapshot_id, self.id) | |
| 123 | |
| 124 def reboot(self): | |
| 125 """ | |
| 126 Reboot this DBInstance | |
| 127 | |
| 128 :rtype: :class:`boto.rds.dbsnapshot.DBSnapshot` | |
| 129 :return: The newly created DBSnapshot | |
| 130 """ | |
| 131 return self.connection.reboot_dbinstance(self.id) | |
| 132 | |
| 133 def update(self, validate=False): | |
| 134 """ | |
| 135 Update the DB instance's status information by making a call to fetch | |
| 136 the current instance attributes from the service. | |
| 137 | |
| 138 :type validate: bool | |
| 139 :param validate: By default, if EC2 returns no data about the | |
| 140 instance the update method returns quietly. If | |
| 141 the validate param is True, however, it will | |
| 142 raise a ValueError exception if no data is | |
| 143 returned from EC2. | |
| 144 """ | |
| 145 rs = self.connection.get_all_dbinstances(self.id) | |
| 146 if len(rs) > 0: | |
| 147 for i in rs: | |
| 148 if i.id == self.id: | |
| 149 self.__dict__.update(i.__dict__) | |
| 150 elif validate: | |
| 151 raise ValueError('%s is not a valid Instance ID' % self.id) | |
| 152 return self.status | |
| 153 | |
| 154 | |
| 155 def stop(self, skip_final_snapshot=False, final_snapshot_id=''): | |
| 156 """ | |
| 157 Delete this DBInstance. | |
| 158 | |
| 159 :type skip_final_snapshot: bool | |
| 160 :param skip_final_snapshot: This parameter determines whether a final | |
| 161 db snapshot is created before the instance | |
| 162 is deleted. If True, no snapshot is created
. | |
| 163 If False, a snapshot is created before | |
| 164 deleting the instance. | |
| 165 | |
| 166 :type final_snapshot_id: str | |
| 167 :param final_snapshot_id: If a final snapshot is requested, this | |
| 168 is the identifier used for that snapshot. | |
| 169 | |
| 170 :rtype: :class:`boto.rds.dbinstance.DBInstance` | |
| 171 :return: The deleted db instance. | |
| 172 """ | |
| 173 return self.connection.delete_dbinstance(self.id, | |
| 174 skip_final_snapshot, | |
| 175 final_snapshot_id) | |
| 176 | |
| 177 def modify(self, param_group=None, security_groups=None, | |
| 178 preferred_maintenance_window=None, | |
| 179 master_password=None, allocated_storage=None, | |
| 180 instance_class=None, | |
| 181 backup_retention_period=None, | |
| 182 preferred_backup_window=None, | |
| 183 multi_az=False, | |
| 184 apply_immediately=False): | |
| 185 """ | |
| 186 Modify this DBInstance. | |
| 187 | |
| 188 :type security_groups: list of str or list of DBSecurityGroup objects | |
| 189 :param security_groups: List of names of DBSecurityGroup to authorize on | |
| 190 this DBInstance. | |
| 191 | |
| 192 :type preferred_maintenance_window: str | |
| 193 :param preferred_maintenance_window: The weekly time range (in UTC) | |
| 194 during which maintenance can | |
| 195 occur. | |
| 196 Default is Sun:05:00-Sun:09:00 | |
| 197 | |
| 198 :type master_password: str | |
| 199 :param master_password: Password of master user for the DBInstance. | |
| 200 Must be 4-15 alphanumeric characters. | |
| 201 | |
| 202 :type allocated_storage: int | |
| 203 :param allocated_storage: The new allocated storage size, in GBs. | |
| 204 Valid values are [5-1024] | |
| 205 | |
| 206 :type instance_class: str | |
| 207 :param instance_class: The compute and memory capacity of the | |
| 208 DBInstance. Changes will be applied at | |
| 209 next maintenance window unless | |
| 210 apply_immediately is True. | |
| 211 | |
| 212 Valid values are: | |
| 213 | |
| 214 * db.m1.small | |
| 215 * db.m1.large | |
| 216 * db.m1.xlarge | |
| 217 * db.m2.xlarge | |
| 218 * db.m2.2xlarge | |
| 219 * db.m2.4xlarge | |
| 220 | |
| 221 :type apply_immediately: bool | |
| 222 :param apply_immediately: If true, the modifications will be applied | |
| 223 as soon as possible rather than waiting for | |
| 224 the next preferred maintenance window. | |
| 225 | |
| 226 :type backup_retention_period: int | |
| 227 :param backup_retention_period: The number of days for which automated | |
| 228 backups are retained. Setting this to | |
| 229 zero disables automated backups. | |
| 230 | |
| 231 :type preferred_backup_window: str | |
| 232 :param preferred_backup_window: The daily time range during which | |
| 233 automated backups are created (if | |
| 234 enabled). Must be in h24:mi-hh24:mi | |
| 235 format (UTC). | |
| 236 | |
| 237 :type multi_az: bool | |
| 238 :param multi_az: If True, specifies the DB Instance will be | |
| 239 deployed in multiple availability zones. | |
| 240 | |
| 241 :rtype: :class:`boto.rds.dbinstance.DBInstance` | |
| 242 :return: The modified db instance. | |
| 243 """ | |
| 244 return self.connection.modify_dbinstance(self.id, | |
| 245 param_group, | |
| 246 security_groups, | |
| 247 preferred_maintenance_window, | |
| 248 master_password, | |
| 249 allocated_storage, | |
| 250 instance_class, | |
| 251 backup_retention_period, | |
| 252 preferred_backup_window, | |
| 253 multi_az, | |
| 254 apply_immediately) | |
| 255 | |
| 256 class PendingModifiedValues(dict): | |
| 257 | |
| 258 def startElement(self, name, attrs, connection): | |
| 259 return None | |
| 260 | |
| 261 def endElement(self, name, value, connection): | |
| 262 if name != 'PendingModifiedValues': | |
| 263 self[name] = value | |
| 264 | |
| OLD | NEW |