Class: Bosh::AwsCliPlugin::RDS
- Inherits:
-
Object
- Object
- Bosh::AwsCliPlugin::RDS
- Defined in:
- lib/bosh_cli_plugin_aws/rds.rb
Constant Summary collapse
- DEFAULT_RDS_OPTIONS =
{ :allocated_storage => 5, :db_instance_class => "db.m1.small", :engine => "mysql", :multi_az => true, :engine_version => "5.5.40a" }
- DEFAULT_RDS_PROTOCOL =
:tcp
- DEFAULT_MYSQL_PORT =
3306
Instance Method Summary collapse
- #aws_rds ⇒ Object
- #aws_rds_client ⇒ Object
- #create_database(name, subnet_ids, vpc_id, options = {}) ⇒ Object
- #create_db_parameter_group(name) ⇒ Object
- #create_subnet_group(name, subnet_ids) ⇒ Object
- #create_vpc_db_security_group(vpc, name) ⇒ Object
- #database(name) ⇒ Object
- #database_exists?(name) ⇒ Boolean
- #database_names ⇒ Object
- #databases ⇒ Object
- #db_parameter_group_names ⇒ Object
- #delete_databases ⇒ Object
- #delete_db_parameter_group(name) ⇒ Object
- #delete_security_group(name) ⇒ Object
- #delete_security_groups ⇒ Object
- #delete_subnet_group(name) ⇒ Object
- #delete_subnet_groups ⇒ Object
-
#initialize(credentials) ⇒ RDS
constructor
A new instance of RDS.
- #security_group_names ⇒ Object
- #subnet_group_exists?(name) ⇒ Boolean
- #subnet_group_names ⇒ Object
Constructor Details
#initialize(credentials) ⇒ RDS
Returns a new instance of RDS.
16 17 18 19 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 16 def initialize(credentials) @credentials = credentials @aws_provider = AwsProvider.new(@credentials) end |
Instance Method Details
#aws_rds ⇒ Object
157 158 159 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 157 def aws_rds aws_provider.rds end |
#aws_rds_client ⇒ Object
161 162 163 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 161 def aws_rds_client aws_provider.rds_client end |
#create_database(name, subnet_ids, vpc_id, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 21 def create_database(name, subnet_ids, vpc_id, = {}) create_db_parameter_group('utf8') vpc = Bosh::AwsCliPlugin::VPC.find(Bosh::AwsCliPlugin::EC2.new(@credentials), vpc_id) create_vpc_db_security_group(vpc, name) if vpc.security_group_by_name(name).nil? create_subnet_group(name, subnet_ids) unless subnet_group_exists?(name) # symbolize options keys = .inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo } = DEFAULT_RDS_OPTIONS.merge() [:db_instance_identifier] = name [:db_name] ||= name [:vpc_security_group_ids] = [vpc.security_group_by_name(name).id] [:db_subnet_group_name] = name [:db_parameter_group_name] = 'utf8' [:master_username] ||= generate_user [:master_user_password] ||= generate_password response = aws_rds_client.create_db_instance() response.data.merge(:master_user_password => [:master_user_password]) end |
#create_db_parameter_group(name) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 93 def create_db_parameter_group(name) aws_rds_client.describe_db_parameter_groups(:db_parameter_group_name => name) rescue AWS::RDS::Errors::DBParameterGroupNotFound aws_rds_client.create_db_parameter_group(:db_parameter_group_name => name, :db_parameter_group_family => 'mysql5.5', :description => name) aws_rds_client.modify_db_parameter_group(:db_parameter_group_name => name, :parameters => db_parameter_group_names) end |
#create_subnet_group(name, subnet_ids) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 108 def create_subnet_group(name, subnet_ids) aws_rds_client.create_db_subnet_group( :db_subnet_group_name => name, :db_subnet_group_description => name, :subnet_ids => subnet_ids ) end |
#create_vpc_db_security_group(vpc, name) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 128 def create_vpc_db_security_group(vpc, name) group_spec = { "name" => name, "ingress" => [ { "ports" => DEFAULT_MYSQL_PORT, "protocol" => DEFAULT_RDS_PROTOCOL, "sources" => vpc.cidr_block, }, ], } vpc.create_security_groups([group_spec]) end |
#database(name) ⇒ Object
46 47 48 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 46 def database(name) databases.find { |v| v.id == name } end |
#database_exists?(name) ⇒ Boolean
50 51 52 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 50 def database_exists?(name) !database(name).nil? end |
#database_names ⇒ Object
58 59 60 61 62 63 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 58 def database_names databases.inject({}) do |memo, db_instance| memo[db_instance.id] = db_instance.name memo end end |
#databases ⇒ Object
42 43 44 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 42 def databases aws_rds.db_instances end |
#db_parameter_group_names ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 72 def db_parameter_group_names charset = 'utf8' param_names = %w(character_set_server character_set_client character_set_results character_set_database character_set_connection) params = param_names.map{|param_name| {:parameter_name => param_name, :parameter_value => charset, :apply_method => 'immediate'}} params << {:parameter_name => 'collation_connection', :parameter_value => 'utf8_unicode_ci', :apply_method => 'immediate'} params << {:parameter_name => 'collation_server', :parameter_value => 'utf8_unicode_ci', :apply_method => 'immediate'} params end |
#delete_databases ⇒ Object
54 55 56 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 54 def delete_databases databases.each { |db| db.delete(skip_final_snapshot: true) unless db.db_instance_status == "deleting" } end |
#delete_db_parameter_group(name) ⇒ Object
102 103 104 105 106 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 102 def delete_db_parameter_group(name) aws_rds_client.describe_db_parameter_groups(:db_parameter_group_name => name) aws_rds_client.delete_db_parameter_group(:db_parameter_group_name => name) rescue AWS::RDS::Errors::DBParameterGroupNotFound end |
#delete_security_group(name) ⇒ Object
147 148 149 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 147 def delete_security_group(name) aws_rds_client.delete_db_security_group(:db_security_group_name => name) end |
#delete_security_groups ⇒ Object
151 152 153 154 155 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 151 def delete_security_groups security_group_names.each do |name| delete_security_group(name) unless name == "default" end end |
#delete_subnet_group(name) ⇒ Object
120 121 122 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 120 def delete_subnet_group(name) aws_rds_client.delete_db_subnet_group(:db_subnet_group_name => name) end |
#delete_subnet_groups ⇒ Object
124 125 126 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 124 def delete_subnet_groups subnet_group_names.each { |name| delete_subnet_group(name) } end |
#security_group_names ⇒ Object
143 144 145 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 143 def security_group_names aws_rds_client.describe_db_security_groups.data[:db_security_groups].map { |sg| sg[:db_security_group_name] } end |
#subnet_group_exists?(name) ⇒ Boolean
65 66 67 68 69 70 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 65 def subnet_group_exists?(name) aws_rds_client.describe_db_subnet_groups(:db_subnet_group_name => name) return true rescue AWS::RDS::Errors::DBSubnetGroupNotFoundFault return false end |
#subnet_group_names ⇒ Object
116 117 118 |
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 116 def subnet_group_names aws_rds_client.describe_db_subnet_groups.data[:db_subnet_groups].map { |sg| sg[:db_subnet_group_name] } end |