rds 22.9 KB
Newer Older
Bruce Pennypacker committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/python
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: rds
20
version_added: "1.3"
21
short_description: create, delete, or modify an Amazon rds instance
Bruce Pennypacker committed
22
description:
23
     - Creates, deletes, or modifies rds instances.  When creating an instance it can be either a new instance or a read-only replica of an existing instance. This module has a dependency on python-boto >= 2.5. The 'promote' command requires boto >= 2.18.0.
Bruce Pennypacker committed
24
options:
25
  command:
Bruce Pennypacker committed
26
    description:
27
      - Specifies the action to take.  
Bruce Pennypacker committed
28 29 30
    required: true
    default: null
    aliases: []
31
    choices: [ 'create', 'replicate', 'delete', 'facts', 'modify' , 'promote' ]
Bruce Pennypacker committed
32 33 34 35 36 37 38 39
  instance_name:
    description:
      - Database instance identifier.
    required: true
    default: null
    aliases: []
  source_instance:
    description:
40
      - Name of the database to replicate. Used only when command=replicate.
Bruce Pennypacker committed
41 42 43 44 45
    required: false
    default: null
    aliases: []
  db_engine:
    description:
46
      - The type of database.  Used only when command=create. 
Bruce Pennypacker committed
47 48 49
    required: false
    default: null
    aliases: []
50
    choices: [ 'MySQL', 'oracle-se1', 'oracle-se', 'oracle-ee', 'sqlserver-ee', 'sqlserver-se', 'sqlserver-ex', 'sqlserver-web' ]
Bruce Pennypacker committed
51 52
  size:
    description:
53
      - Size in gigabytes of the initial storage for the DB instance. Used only when command=create or command=modify.
Bruce Pennypacker committed
54 55 56 57 58
    required: false
    default: null
    aliases: []
  instance_type:
    description:
59
      - The instance type of the database.  Must be specified when command=create. Optional when command=replicate or command=modify. If not specified then the replica inherits the same instance type as the source instance. 
Bruce Pennypacker committed
60 61 62
    required: false
    default: null
    aliases: []
63
    choices: [ 'db.t1.micro', 'db.m1.small', 'db.m1.medium', 'db.m1.large', 'db.m1.xlarge', 'db.m2.xlarge', 'db.m2.2xlarge', 'db.m2.4xlarge' ]
Bruce Pennypacker committed
64 65
  username:
    description:
66
      - Master database username. Used only when command=create.
Bruce Pennypacker committed
67 68 69 70 71
    required: false
    default: null
    aliases: []
  password:
    description:
72
      - Password for the master database username. Used only when command=create or command=modify.
Bruce Pennypacker committed
73 74 75
    required: false
    default: null
    aliases: []
Bruce Pennypacker committed
76
  region:
Bruce Pennypacker committed
77
    description:
Bruce Pennypacker committed
78
      - The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
Bruce Pennypacker committed
79 80
    required: true
    default: null
Bruce Pennypacker committed
81
    aliases: [ 'aws_region', 'ec2_region' ]
Bruce Pennypacker committed
82 83
  db_name:
    description:
84
      - Name of a database to create within the instance.  If not specified then no database is created. Used only when command=create.
Bruce Pennypacker committed
85 86 87 88 89
    required: false
    default: null
    aliases: []
  engine_version:
    description:
90
      - Version number of the database engine to use. Used only when command=create. If not specified then the current Amazon RDS default engine version is used.
Bruce Pennypacker committed
91 92 93 94 95
    required: false
    default: null
    aliases: []
  parameter_group:
    description:
96
      - Name of the DB parameter group to associate with this instance.  If omitted then the RDS default DBParameterGroup will be used. Used only when command=create or command=modify.
Bruce Pennypacker committed
97 98 99 100 101
    required: false
    default: null
    aliases: []
  license_model:
    description:
102
      - The license model for this DB instance. Used only when command=create. 
Bruce Pennypacker committed
103 104 105
    required: false
    default: null
    aliases: []
106
    choices:  [ 'license-included', 'bring-your-own-license', 'general-public-license' ]
Bruce Pennypacker committed
107 108
  multi_zone:
    description:
109 110
      - Specifies if this is a Multi-availability-zone deployment. Can not be used in conjunction with zone parameter. Used only when command=create or command=modify.
    choices: [ "yes", "no" ] 
Bruce Pennypacker committed
111 112 113 114 115
    required: false
    default: null
    aliases: []
  iops:
    description:
116
      - Specifies the number of IOPS for the instance.  Used only when command=create or command=modify. Must be an integer greater than 1000.
Bruce Pennypacker committed
117 118 119 120 121
    required: false
    default: null
    aliases: []
  security_groups:
    description:
122
      - Comma separated list of one or more security groups.  Used only when command=create or command=modify. If a subnet is specified then this is treated as a list of VPC security groups.
Bruce Pennypacker committed
123 124 125 126 127
    required: false
    default: null
    aliases: []
  port:
    description:
128
      - Port number that the DB instance uses for connections.  Defaults to 3306 for mysql, 1521 for Oracle, 1443 for SQL Server. Used only when command=create or command=replicate.
Bruce Pennypacker committed
129 130 131 132 133
    required: false
    default: null
    aliases: []
  upgrade:
    description:
134
      - Indicates that minor version upgrades should be applied automatically. Used only when command=create or command=replicate. 
Bruce Pennypacker committed
135 136 137 138 139 140
    required: false
    default: no
    choices: [ "yes", "no" ]
    aliases: []
  option_group:
    description:
141
      - The name of the option group to use.  If not specified then the default option group is used. Used only when command=create.
Bruce Pennypacker committed
142 143 144 145 146
    required: false
    default: null
    aliases: []
  maint_window:
    description:
147
      - "Maintenance window in format of ddd:hh24:mi-ddd:hh24:mi.  (Example: Mon:22:00-Mon:23:15) If not specified then a random maintenance window is assigned. Used only when command=create or command=modify."
Bruce Pennypacker committed
148 149 150 151 152
    required: false
    default: null
    aliases: []
  backup_window:
    description:
153
      - Backup window in format of hh24:mi-hh24:mi.  If not specified then a random backup window is assigned. Used only when command=create or command=modify.
Bruce Pennypacker committed
154 155 156 157 158
    required: false
    default: null
    aliases: []
  backup_retention:
    description:
159
      - "Number of days backups are retained.  Set to 0 to disable backups.  Default is 1 day.  Valid range: 0-35. Used only when command=create or command=modify."
Bruce Pennypacker committed
160 161 162 163 164
    required: false
    default: null
    aliases: []
  zone:
    description:
165
      - availability zone in which to launch the instance. Used only when command=create or command=replicate.
Bruce Pennypacker committed
166 167
    required: false
    default: null
Bruce Pennypacker committed
168
    aliases: ['aws_zone', 'ec2_zone']
Bruce Pennypacker committed
169 170
  subnet:
    description:
171
      - VPC subnet group.  If specified then a VPC instance is created. Used only when command=create.
Bruce Pennypacker committed
172 173 174 175 176
    required: false
    default: null
    aliases: []
  snapshot:
    description:
177
      - Name of final snapshot to take when deleting an instance.  If no snapshot name is provided then no snapshot is taken. Used only when command=delete.
Bruce Pennypacker committed
178 179 180
    required: false
    default: null
    aliases: []
Bruce Pennypacker committed
181
  aws_secret_key:
Bruce Pennypacker committed
182
    description:
Bruce Pennypacker committed
183
      - AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used. 
Bruce Pennypacker committed
184 185
    required: false
    default: null
Bruce Pennypacker committed
186 187
    aliases: [ 'ec2_secret_key', 'secret_key' ]
  aws_access_key:
Bruce Pennypacker committed
188
    description:
Bruce Pennypacker committed
189
      - AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used.
Bruce Pennypacker committed
190 191
    required: false
    default: null
Bruce Pennypacker committed
192
    aliases: [ 'ec2_access_key', 'access_key' ]
Bruce Pennypacker committed
193 194
  wait:
    description:
195
      - When command=create, replicate, or modify then wait for the database to enter the 'available' state.  When command=delete wait for the database to be terminated.
Bruce Pennypacker committed
196 197 198 199 200 201 202 203 204
    required: false
    default: "no"
    choices: [ "yes", "no" ]
    aliases: []
  wait_timeout:
    description:
      - how long before wait gives up, in seconds
    default: 300
    aliases: []
Bruce Pennypacker committed
205 206
  apply_immediately:
    description:
207
      - Used only when command=modify.  If enabled, the modifications will be applied as soon as possible rather than waiting for the next preferred maintenance window.
Bruce Pennypacker committed
208 209 210
    default: no
    choices: [ "yes", "no" ]
    aliases: []
211 212 213
  new_instance_name:
    description:
      - Name to rename an instance to. Used only when command=modify.
214
    required: false
215 216
    default: null
    aliases: []
Bruce Pennypacker committed
217 218 219 220 221 222
requirements: [ "boto" ]
author: Bruce Pennypacker
'''

EXAMPLES = '''
# Basic mysql provisioning example
223 224 225 226 227 228 229 230
- rds: >
      command=create
      instance_name=new_database
      db_engine=MySQL
      size=10
      instance_type=db.m1.small
      username=mysql_admin
      password=1nsecure
Bruce Pennypacker committed
231 232

# Create a read-only replica and wait for it to become available
233 234 235 236 237 238
- rds: >
      command=replicate
      instance_name=new_database_replica
      source_instance=new_database
      wait=yes
      wait_timeout=600
Bruce Pennypacker committed
239 240

# Delete an instance, but create a snapshot before doing so
241 242 243 244
- rds: >
      command=delete
      instance_name=new_database
      snapshot=new_database_snapshot
Bruce Pennypacker committed
245 246

# Get facts about an instance
247 248 249 250
- rds: >
      command=facts
      instance_name=new_database
      register: new_database_facts
251 252 253 254 255 256 257

# Rename an instance and wait for the change to take effect
- rds: >
      command=modify
      instance_name=new_database
      new_instance_name=renamed_database
      wait=yes
Bruce Pennypacker committed
258
    
Bruce Pennypacker committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
'''

import sys
import time

AWS_REGIONS = ['ap-northeast-1',
               'ap-southeast-1',
               'ap-southeast-2',
               'eu-west-1',
               'sa-east-1',
               'us-east-1',
               'us-west-1',
               'us-west-2']

try:
    import boto.rds
except ImportError:
    print "failed=True msg='boto required for this module'"
    sys.exit(1)

def main():
    module = AnsibleModule(
        argument_spec = dict(
282
            command           = dict(choices=['create', 'replicate', 'delete', 'facts', 'modify', 'promote'], required=True),
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
            instance_name     = dict(required=True),
            source_instance   = dict(required=False),
            db_engine         = dict(choices=['MySQL', 'oracle-se1', 'oracle-se', 'oracle-ee', 'sqlserver-ee', 'sqlserver-se', 'sqlserver-ex', 'sqlserver-web'], required=False),
            size              = dict(required=False), 
            instance_type     = dict(aliases=['type'], choices=['db.t1.micro', 'db.m1.small', 'db.m1.medium', 'db.m1.large', 'db.m1.xlarge', 'db.m2.xlarge', 'db.m2.2xlarge', 'db.m2.4xlarge'], required=False),
            username          = dict(required=False),
            password          = dict(no_log=True, required=False),
            db_name           = dict(required=False),
            engine_version    = dict(required=False),
            parameter_group   = dict(required=False),
            license_model     = dict(choices=['license-included', 'bring-your-own-license', 'general-public-license'], required=False),
            multi_zone        = dict(type='bool', default=False),
            iops              = dict(required=False), 
            security_groups   = dict(required=False),
            port              = dict(required=False),
            upgrade           = dict(type='bool', default=False),
            option_group      = dict(required=False),
            maint_window      = dict(required=False),
            backup_window     = dict(required=False),
            backup_retention  = dict(required=False), 
Bruce Pennypacker committed
303 304
            region            = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS, required=False),
            zone              = dict(aliases=['aws_zone', 'ec2_zone'], required=False),
305
            subnet            = dict(required=False),
Bruce Pennypacker committed
306 307
            aws_secret_key    = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True, required=False),
            aws_access_key    = dict(aliases=['ec2_access_key', 'access_key'], required=False),
308 309 310 311
            wait              = dict(type='bool', default=False),
            wait_timeout      = dict(default=300),
            snapshot          = dict(required=False),
            apply_immediately = dict(type='bool', default=False),
312
            new_instance_name = dict(required=False),
Bruce Pennypacker committed
313 314 315
        )
    )

316
    command            = module.params.get('command')
Michael DeHaan committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    instance_name      = module.params.get('instance_name')
    source_instance    = module.params.get('source_instance')
    db_engine          = module.params.get('db_engine')
    size               = module.params.get('size')
    instance_type      = module.params.get('instance_type')
    username           = module.params.get('username')
    password           = module.params.get('password')
    db_name            = module.params.get('db_name')
    engine_version     = module.params.get('engine_version')
    parameter_group    = module.params.get('parameter_group')
    license_model      = module.params.get('license_model')
    multi_zone         = module.params.get('multi_zone')
    iops               = module.params.get('iops')
    security_groups    = module.params.get('security_groups')
    port               = module.params.get('port')
    upgrade            = module.params.get('upgrade')
    option_group       = module.params.get('option_group')
    maint_window       = module.params.get('maint_window')
    subnet             = module.params.get('subnet')
    backup_window      = module.params.get('backup_window')
    backup_retention   = module.params.get('module_retention')
Bruce Pennypacker committed
338
    region             = module.params.get('region')
339
    zone               = module.params.get('zone')
Bruce Pennypacker committed
340 341
    aws_secret_key     = module.params.get('aws_secret_key')
    aws_access_key     = module.params.get('aws_access_key')
342 343 344 345
    wait               = module.params.get('wait')
    wait_timeout       = int(module.params.get('wait_timeout'))
    snapshot           = module.params.get('snapshot')
    apply_immediately  = module.params.get('apply_immediately')
346
    new_instance_name  = module.params.get('new_instance_name')
Bruce Pennypacker committed
347 348

    # allow environment variables to be used if ansible vars aren't set
Bruce Pennypacker committed
349 350 351 352 353 354 355 356 357 358 359
    if not region:
        if   'AWS_REGION' in os.environ:
            region = os.environ['AWS_REGION']
        elif   'EC2_REGION' in os.environ:
            region = os.environ['EC2_REGION']

    if not aws_secret_key:
        if  'AWS_SECRET_KEY' in os.environ:
            aws_secret_key = os.environ['AWS_SECRET_KEY']
        elif 'EC2_SECRET_KEY' in os.environ:
            aws_secret_key = os.environ['EC2_SECRET_KEY']
Bruce Pennypacker committed
360

Bruce Pennypacker committed
361 362 363 364 365
    if not aws_access_key:
        if 'AWS_ACCESS_KEY' in os.environ:
            aws_access_key = os.environ['AWS_ACCESS_KEY']
        elif 'EC2_ACCESS_KEY' in os.environ:
            aws_access_key = os.environ['EC2_ACCESS_KEY']
Bruce Pennypacker committed
366

367
    if not region:
Bruce Pennypacker committed
368
        module.fail_json(msg = str("region not specified and unable to determine region from EC2_REGION."))
Bruce Pennypacker committed
369 370 371

    # connect to the rds endpoint
    try:
372
        conn = boto.rds.connect_to_region(region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)
Bruce Pennypacker committed
373 374 375
    except boto.exception.BotoServerError, e:
        module.fail_json(msg = e.error_message)

376 377
    # Validate parameters for each command
    if command == 'create':
Bruce Pennypacker committed
378
        required_vars = [ 'instance_name', 'db_engine', 'size', 'instance_type', 'username', 'password' ]
379
        invalid_vars  = [ 'source_instance', 'snapshot', 'apply_immediately', 'new_instance_name' ]
380

381
    elif command == 'replicate':
Bruce Pennypacker committed
382
        required_vars = [ 'instance_name', 'source_instance' ]
383
        invalid_vars  = [ 'db_engine', 'size', 'username', 'password', 'db_name', 'engine_version', 'parameter_group', 'license_model', 'multi_zone', 'iops', 'security_groups', 'option_group', 'maint_window', 'backup_window', 'backup_retention', 'subnet', 'snapshot', 'apply_immediately', 'new_instance_name' ]
384

385
    elif command == 'delete':
Bruce Pennypacker committed
386
        required_vars = [ 'instance_name' ]
387
        invalid_vars  = [ 'db_engine', 'size', 'instance_type', 'username', 'password', 'db_name', 'engine_version', 'parameter_group', 'license_model', 'multi_zone', 'iops', 'security_groups', 'option_group', 'maint_window', 'backup_window', 'backup_retention', 'port', 'upgrade', 'subnet', 'zone' , 'source_instance', 'apply_immediately', 'new_instance_name' ]
388

389
    elif command == 'facts':
Bruce Pennypacker committed
390
        required_vars = [ 'instance_name' ]
391
        invalid_vars  = [ 'db_engine', 'size', 'instance_type', 'username', 'password', 'db_name', 'engine_version', 'parameter_group', 'license_model', 'multi_zone', 'iops', 'security_groups', 'option_group', 'maint_window', 'backup_window', 'backup_retention', 'port', 'upgrade', 'subnet', 'zone', 'wait', 'source_instance' 'apply_immediately', 'new_instance_name' ]
392

393
    elif command == 'modify':
Bruce Pennypacker committed
394
        required_vars = [ 'instance_name' ]
395 396
        if password:
            params["master_password"] = password
Bruce Pennypacker committed
397
        invalid_vars = [ 'db_engine', 'username', 'db_name', 'engine_version',  'license_model', 'option_group', 'port', 'upgrade', 'subnet', 'zone', 'source_instance' ]
398 399 400 401

    elif command == 'promote':
        required_vars = [ 'instance_name' ]
        invalid_vars = [ 'db_engine', 'size', 'username', 'password', 'db_name', 'engine_version', 'parameter_group', 'license_model', 'multi_zone', 'iops', 'security_groups', 'option_group', 'maint_window', 'subnet', 'source_instance', 'snapshot', 'apply_immediately', 'new_instance_name' ]
Bruce Pennypacker committed
402 403 404
 
    for v in required_vars:
        if not module.params.get(v):
405
            module.fail_json(msg = str("Parameter %s required for %s command" % (v, command)))
Bruce Pennypacker committed
406 407 408
            
    for v in invalid_vars:
        if module.params.get(v):
409
            module.fail_json(msg = str("Parameter %s invalid for %s command" % (v, command)))
Bruce Pennypacker committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

    # Package up the optional parameters
    params = {}

    if db_engine:
        params["engine"] = db_engine

    if port:
        params["port"] = port

    if db_name:
        params["db_name"] = db_name

    if parameter_group:
        params["param_group"] = parameter_group

    if zone:
        params["availability_zone"] = zone
  
    if maint_window:
        params["preferred_maintenance_window"] = maint_window

    if backup_window:
        params["preferred_backup_window"] = backup_window

    if backup_retention:
        params["backup_retention_period"] = backup_retention

    if multi_zone:
        params["multi_az"] = multi_zone

    if engine_version:
        params["engine_version"] = engine_version

    if upgrade:
        params["auto_minor_version_upgrade"] = upgrade

    if subnet:
        params["db_subnet_group_name"] = subnet

    if license_model:
        params["license_model"] = license_model

    if option_group:
        params["option_group_name"] = option_group

    if iops:
        params["iops"] = iops

    if security_groups:
460 461 462 463
        if subnet:
            params["vpc_security_groups"] = security_groups.split(',')
        else:
            params["security_groups"] = security_groups.split(',')
Bruce Pennypacker committed
464

465 466 467
    if new_instance_name:
        params["new_instance_id"] = new_instance_name

Bruce Pennypacker committed
468
    try: 
469
        if command == 'create':
Bruce Pennypacker committed
470
            db = conn.create_dbinstance(instance_name, size, instance_type, username, password, **params)
471
        elif command == 'replicate':
Bruce Pennypacker committed
472
            if instance_type:
Michael DeHaan committed
473
                params["instance_class"] = instance_type
Bruce Pennypacker committed
474
            db = conn.create_dbinstance_read_replica(instance_name, source_instance, **params)
475
        elif command == 'delete':
Bruce Pennypacker committed
476 477 478
            if snapshot:
                params["skip_final_snapshot"] = False
                params["final_snapshot_id"] = snapshot
479 480 481
            else:
                params["skip_final_snapshot"] = True

Bruce Pennypacker committed
482
            db = conn.delete_dbinstance(instance_name, **params)
483
        elif command == 'modify':
Bruce Pennypacker committed
484 485
            params["apply_immediately"] = apply_immediately
            db = conn.modify_dbinstance(instance_name, **params)
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
            if apply_immediately:
                if new_instance_name:
                    # Wait until the new instance name is valid
                    found = 0
                    while found == 0:
                        instances = conn.get_all_dbinstances()
                        for i in instances:
                            if i.id == new_instance_name:
                                instance_name = new_instance_name
                                found = 1
                        if found == 0:
                            time.sleep(5)
                else:
                    # Wait for a few seconds since it takes a while for AWS
                    # to change the instance from 'available' to 'modifying'
                    time.sleep(5)

        elif command == 'promote':
            db = conn.promote_read_replica(instance_name, **params)
Bruce Pennypacker committed
505

506
    # Don't do anything for the 'facts' command since we'll just drop down
Bruce Pennypacker committed
507 508
    # to get_all_dbinstances below to collect the facts

Bruce Pennypacker committed
509 510 511 512 513
    except boto.exception.BotoServerError, e:
        module.fail_json(msg = e.error_message)

    # If we're not waiting for a delete to complete then we're all done
    # so just return
514
    if command == 'delete' and not wait:
Bruce Pennypacker committed
515 516
        module.exit_json(changed=True)

517 518 519 520 521 522
    try: 
        instances = conn.get_all_dbinstances(instance_name)
        my_inst = instances[0]
    except boto.exception.BotoServerError, e:
        module.fail_json(msg = e.error_message)

Bruce Pennypacker committed
523 524 525 526

    # Wait for the instance to be available if requested
    if wait:
        try: 
Bruce Pennypacker committed
527
            wait_timeout = time.time() + wait_timeout
Bruce Pennypacker committed
528 529 530 531 532 533 534 535 536 537 538 539
            time.sleep(5)

            while wait_timeout > time.time() and my_inst.status != 'available':
                time.sleep(5)
                if wait_timeout <= time.time():
                    module.fail_json(msg = "Timeout waiting for database instance %s" % instance_name)
                instances = conn.get_all_dbinstances(instance_name)
                my_inst = instances[0]
        except boto.exception.BotoServerError, e:
            # If we're waiting for an instance to be deleted then
            # get_all_dbinstances will eventually throw a 
            # DBInstanceNotFound error.
540
            if command == 'delete' and e.error_code == 'DBInstanceNotFound':
Bruce Pennypacker committed
541 542 543 544 545 546 547
                module.exit_json(changed=True)                
            else:
                module.fail_json(msg = e.error_message)

    # If we got here then pack up all the instance details to send
    # back to ansible
    d = {
548 549 550 551 552 553 554 555 556 557 558
        'id'                 : my_inst.id,
        'create_time'        : my_inst.create_time,
        'status'             : my_inst.status,
        'availability_zone'  : my_inst.availability_zone,
        'backup_retention'   : my_inst.backup_retention_period,
        'backup_window'      : my_inst.preferred_backup_window,
        'maintenance_window' : my_inst.preferred_maintenance_window,
        'multi_zone'         : my_inst.multi_az,
        'instance_type'      : my_inst.instance_class,
        'username'           : my_inst.master_username,
        'iops'               : my_inst.iops
Bruce Pennypacker committed
559
        }
560 561 562 563 564 565 566 567 568

    # Endpoint exists only if the instance is available
    if my_inst.status == 'available':
        d["endpoint"] = my_inst.endpoint[0]
        d["port"] = my_inst.endpoint[1]
    else:
        d["endpoint"] = None
        d["port"] = None

569 570 571 572 573 574
    # ReadReplicaSourceDBInstanceIdentifier may or may not exist
    try:
        d["replication_source"] = my_inst.ReadReplicaSourceDBInstanceIdentifier
    except Exception, e:
        d["replication_source"] = None

Bruce Pennypacker committed
575 576
    module.exit_json(changed=True, instance=d)

577
# import module snippets
578
from ansible.module_utils.basic import *
Bruce Pennypacker committed
579 580

main()