Class: Chef::Knife::RdsInstanceFromDataBag

Inherits:
Chef::Knife
  • Object
show all
Includes:
RdsBase, RdsBaseDataBag
Defined in:
lib/chef/knife/rds_instance_from_data_bag.rb

Constant Summary collapse

IGNORE_DIFF =
[:master_user_password]

Constants included from RdsBase

Chef::Knife::RdsBase::APPLY_METHODS

Instance Method Summary collapse

Methods included from RdsBaseDataBag

#assert_data_bag_exists!, #assert_data_bag_item_exists!, #assert_data_bag_item_valid!, #assert_required_data_bag_options_present!, #data_bag_exists?, #data_bag_item, #defined_params, included, #required_data_bag_options

Methods included from RdsBase

#assert_name_args_at_least!, #assert_valid_apply_method!, #authenticate!, #connect!, included, #rds

Instance Method Details

#create_db_instance!Object

Build API call using Data bag methods



107
108
109
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 107

def create_db_instance!
  rds.client.create_db_instance(create_params)
end

#create_paramsObject



111
112
113
114
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 111

def create_params
  { db_instance_identifier: db_instance_id }
    .merge(defined_params)
end

#data_bag_item_nameObject

Identifier for base data bag methods. Identifies the data bag item

Returns stringbas



119
120
121
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 119

def data_bag_item_name
  db_instance_id
end

#db_instanceObject



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 130

def db_instance
  unless @db_instance
    begin
      @db_instance = rds.client.describe_db_instances(db_instance_identifier: db_instance_id)
      @db_instance = @db_instance[:db_instances].first
    rescue AWS::RDS::Errors::DBInstanceNotFound => e
      @db_instance = nil
    end
  end
  @db_instance
end

#db_instance_idObject

Database instance identifier

Returns string



126
127
128
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 126

def db_instance_id
  name_args.first
end

#generate_diff_with_data_bagObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 66

def generate_diff_with_data_bag
  diff = {}
  ui.info "Comparing #{data_bag_item_name} with data bag settings..."
  defined_params.each do |k, v|
    if skip_parameter?(k)
      ui.info("Will not update #{k}...")
      next
    end
    if k == :db_parameter_group_name
      # DB Parameter groups returned from AWS as hash. Needs to be handled separately
      groups = db_instance[:db_parameter_groups].map { |g| g[:db_parameter_group_name] }
      if groups.include?(v)
        ui.info "DB Parameter groups unchanged."
      else
        diff[:db_parameter_group_name] = v
        ui.info "* #{k} differs. data bag #{v} | instance #{groups}"
      end
    elsif k == :db_security_groups
      groups = db_instance[:db_security_groups].map { |g| g[:db_security_group_name] }
      if (groups - v).empty? && (v - groups).empty?
        ui.info "DB Security groups unchanged"
      else
        ui.info "* #{k} differs. data bag #{v} | instance #{groups.join(', ')}"
        diff[:db_security_groups] = v
      end
    else
      if v != db_instance[k]
        diff[k] = v
        ui.info "* #{k} differs. data bag: #{v} | instance: #{db_instance[k]}"
      end
    end
  end
  diff
end

#interrupt_unless_available!Object

Halt task is instance is not available



56
57
58
59
60
61
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 56

def interrupt_unless_available!
  if ['creating', 'deleting'].include?(db_instance[:db_instance_status])
    ui.warn("The instance is currently in the #{db_instance[:db_instance_status]} stage. Please wait until its finished.")
    exit 0
  end
end

#modify_db_instance!(diff = {}) ⇒ Object



101
102
103
104
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 101

def modify_db_instance!(diff = {})
  params = {db_instance_identifier: db_instance_id}.merge(diff)
  rds.client.modify_db_instance(params)
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 27

def run

  assert_name_args_at_least!(1, "DB Instance identifier required")

  assert_data_bag_item_valid!

  authenticate!

  if db_instance.nil?
    ui.info("The instance #{db_instance_id} does not exit.")
    confirm("Would you like to create it")
    create_db_instance!
    ui.info("Creating RDS instance #{db_instance_id}")
  else
    interrupt_unless_available!
    ui.info("The instance #{db_instance_id} already exists")
    confirm("Update with data bag values")
    diff = generate_diff_with_data_bag
    unless diff.empty?
      confirm("Apply changes")
      modify_db_instance!(diff)
    else
      ui.info("Your data bag does not contain any differences to instance.")
    end
  end

end

#skip_parameter?(parameter) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/chef/knife/rds_instance_from_data_bag.rb', line 63

def skip_parameter?(parameter)
  IGNORE_DIFF.include?(parameter)
end