|  | 
|  | 1 | +require_relative '../../../puppet_x/puppetlabs/aws.rb' | 
|  | 2 | + | 
|  | 3 | +Puppet::Type.type(:rds_instance).provide(:v2, :parent => PuppetX::Puppetlabs::Aws) do | 
|  | 4 | +  confine feature: :aws | 
|  | 5 | + | 
|  | 6 | +  mk_resource_methods | 
|  | 7 | + | 
|  | 8 | +  def self.instances | 
|  | 9 | +    regions.collect do |region| | 
|  | 10 | +      instances = [] | 
|  | 11 | +      rds_client(region).describe_db_instances.each do |response| | 
|  | 12 | +        response.data.db_instances.each do |db| | 
|  | 13 | +          unless db.db_instance_status =~ /^deleted$|^deleting$/ | 
|  | 14 | +            hash = db_instance_to_hash(region, db) | 
|  | 15 | +            instances << new(hash) if hash[:name] | 
|  | 16 | +          end | 
|  | 17 | +        end | 
|  | 18 | +      end | 
|  | 19 | +      instances | 
|  | 20 | +    end.flatten | 
|  | 21 | +  end | 
|  | 22 | + | 
|  | 23 | +  read_only(:auto_minor_version_upgrade, | 
|  | 24 | +    :backup_retention_period, :character_set_name, :creation_date_time, | 
|  | 25 | +    :iops, :master_username, | 
|  | 26 | +    :multi_az, :backup_window, :vpc_id, :license_model) | 
|  | 27 | + | 
|  | 28 | +  def self.prefetch(resources) | 
|  | 29 | +    instances.each do |prov| | 
|  | 30 | +      if resource = resources[prov.name] # rubocop:disable Lint/AssignmentInCondition | 
|  | 31 | +        resource.provider = prov if resource[:region] == prov.region | 
|  | 32 | +      end | 
|  | 33 | +    end | 
|  | 34 | +  end | 
|  | 35 | + | 
|  | 36 | +  def self.db_instance_to_hash(region, instance) | 
|  | 37 | +    if instance.respond_to?('skip_final_snapshot') | 
|  | 38 | +      skip_final_snapshot = instance.skip_final_snapshot | 
|  | 39 | +    else | 
|  | 40 | +      skip_final_snapshot = true | 
|  | 41 | +    end | 
|  | 42 | +    if instance.respond_to?('final_db_snapshot_identifier') | 
|  | 43 | +      final_db_snapshot_identifier = instance.final_db_snapshot_identifier | 
|  | 44 | +    else | 
|  | 45 | +      final_db_snapshot_identifier = '' | 
|  | 46 | +    end | 
|  | 47 | +    if instance.respond_to?('backup_retention_period') | 
|  | 48 | +      backup_retention_period = instance.backup_retention_period | 
|  | 49 | +    else | 
|  | 50 | +      backup_retention_period = 0 | 
|  | 51 | +    end | 
|  | 52 | +    config = { | 
|  | 53 | +      ensure: :present, | 
|  | 54 | +      name: instance.db_instance_identifier, | 
|  | 55 | +      region: region, | 
|  | 56 | +      engine: instance.engine, | 
|  | 57 | +      db_instance_class: instance.db_instance_class, | 
|  | 58 | +      master_username: instance.master_username, | 
|  | 59 | +      db_name: instance.db_name, | 
|  | 60 | +      allocated_storage: instance.allocated_storage, | 
|  | 61 | +      storage_type: instance.storage_type, | 
|  | 62 | +      license_model: instance.license_model, | 
|  | 63 | +      multi_az: instance.multi_az, | 
|  | 64 | +      iops: instance.iops, | 
|  | 65 | +      backup_retention_period: backup_retention_period, | 
|  | 66 | +      skip_final_snapshot: skip_final_snapshot, | 
|  | 67 | +      final_db_snapshot_identifier: final_db_snapshot_identifier, | 
|  | 68 | +      db_parameter_group_name: instance.db_parameter_groups.collect(&:db_parameter_group_name).first, | 
|  | 69 | +      db_security_groups: instance.db_security_groups.collect(&:db_security_group_name), | 
|  | 70 | +    } | 
|  | 71 | +    if instance.respond_to?('endpoint') && !instance.endpoint.nil? | 
|  | 72 | +      config[:endpoint] = instance.endpoint.address | 
|  | 73 | +      config[:port]     = instance.endpoint.port | 
|  | 74 | +    end | 
|  | 75 | +    config | 
|  | 76 | +  end | 
|  | 77 | + | 
|  | 78 | +  def exists? | 
|  | 79 | +    dest_region = resource[:region] if resource | 
|  | 80 | +    Puppet.info("Checking if instance #{name} exists in region #{dest_region || region}") | 
|  | 81 | +    [:present, :creating, :available, :backing_up].include? @property_hash[:ensure] | 
|  | 82 | +  end | 
|  | 83 | + | 
|  | 84 | +  def create | 
|  | 85 | +    Puppet.info("Starting DB instance #{name}") | 
|  | 86 | +    config = { | 
|  | 87 | +      db_instance_identifier: resource[:name], | 
|  | 88 | +      db_name: resource[:db_name], | 
|  | 89 | +      db_instance_class: resource[:db_instance_class], | 
|  | 90 | +      engine: resource[:engine], | 
|  | 91 | +      engine_version: resource[:engine_version], | 
|  | 92 | +      license_model: resource[:license_model], | 
|  | 93 | +      storage_type: resource[:storage_type], | 
|  | 94 | +      multi_az: resource[:multi_az], | 
|  | 95 | +      allocated_storage: resource[:allocated_storage], | 
|  | 96 | +      iops: resource[:iops], | 
|  | 97 | +      master_username: resource[:master_username], | 
|  | 98 | +      master_user_password: resource[:master_user_password], | 
|  | 99 | +      subnet_group_name: resource[:subnet_group_name], | 
|  | 100 | +      db_security_groups: resource[:db_security_groups], | 
|  | 101 | +      db_parameter_group_name: resource[:db_parameter_group_name], | 
|  | 102 | +    } | 
|  | 103 | + | 
|  | 104 | +    rds_client(resource[:region]).create_db_instance(config) | 
|  | 105 | + | 
|  | 106 | +    @property_hash[:ensure] = :present | 
|  | 107 | +  end | 
|  | 108 | + | 
|  | 109 | +  def destroy | 
|  | 110 | +    Puppet.info("Deleting database #{name} in region #{resource[:region]}") | 
|  | 111 | +    rds = rds_client(resource[:region]) | 
|  | 112 | +    Puppet.info("Skip Final Snapshot: #{resource[:skip_final_snapshot]}") | 
|  | 113 | +    config = { | 
|  | 114 | +      db_instance_identifier: name, | 
|  | 115 | +      skip_final_snapshot: skip_final_snapshot, | 
|  | 116 | +      final_db_snapshot_identifier: final_db_snapshot_identifier, | 
|  | 117 | +    } | 
|  | 118 | +    rds.delete_db_instance(config) | 
|  | 119 | +    @property_hash[:ensure] = :absent | 
|  | 120 | +  end | 
|  | 121 | + | 
|  | 122 | +end | 
0 commit comments