lib/dyna/wrapper/table.rb in dyna-0.1.9 vs lib/dyna/wrapper/table.rb in dyna-0.2.0
- old
+ new
@@ -18,22 +18,24 @@
def eql?(dsl)
definition_eql?(dsl)
end
def update(dsl)
- unless provisioned_throughput_eql?(dsl)
- wait_until_table_is_active
- update_table(dsl_provisioned_throughput(dsl))
+ unless billing_mode_eql?(dsl) && provisioned_throughput_eql?(dsl)
+ update_table(dsl)
end
unless global_secondary_indexes_eql?(dsl)
wait_until_table_is_active
update_table_index(dsl, dsl_global_secondary_index_updates(dsl))
end
unless stream_specification_eql?(dsl)
wait_until_table_is_active
update_stream_specification(dsl_stream_specification(dsl))
end
+ unless auto_scaling_eql?(dsl)
+ update_auto_scaling(dsl)
+ end
end
def delete
log(:info, 'Delete Table', :red, "#{table_name}")
@@ -61,18 +63,58 @@
sleep 3
end
end
private
+ def auto_scaling_eql?(dsl)
+ scalable_targets_eql?(dsl) && scaling_policies_eql?(dsl)
+ end
+
+ def scalable_targets_eql?(dsl)
+ df = definition[:scalable_targets].map do |target|
+ cmp = target.to_h
+ cmp.delete(:creation_time)
+ cmp.delete(:role_arn)
+ Dyna::Utils.normalize_hash(cmp)
+ end
+ df.sort_by {|s| s[:scalable_dimension] } == dsl[:scalable_targets].map { |target| Dyna::Utils.normalize_hash(target) }.sort_by {|s| s[:scalable_dimension] }
+ end
+
+ def scaling_policies_for_diff
+ definition[:scaling_policies].map { |policy|
+ #Dyna::Utils.normalize_hash({target_tracking_scaling_policy_configuration: {target_value: policy.target_tracking_scaling_policy_configuration.target_value} })
+ cmp = policy.to_h
+ cmp.delete(:alarms)
+ cmp.delete(:creation_time)
+ cmp.delete(:policy_arn)
+ Dyna::Utils.normalize_hash(cmp)
+ }.sort_by {|s| s[:scalable_dimension] }
+ end
+
+ def scaling_policies_eql?(dsl)
+ scaling_policies_for_diff == dsl.scaling_policies.map { |policy| Dyna::Utils.normalize_hash(policy) }.sort_by {|s| s[:scalable_dimension] }
+ end
+
def definition_eql?(dsl)
definition == dsl.definition
end
def provisioned_throughput_eql?(dsl)
+ if definition[:billing_mode] == 'PROVISIONED' && billing_mode_eql?(dsl)
+ return true
+ end
self_provisioned_throughput == dsl_provisioned_throughput(dsl)
end
+ def billing_mode_eql?(dsl)
+ if definition[:billing_mode] == dsl[:billing_mode]
+ return true
+ end
+
+ definition[:billing_mode].nil? && dsl[:billing_mode].to_s == 'PROVISIONED'
+ end
+
def self_provisioned_throughput
definition.select {|k,v| k == :provisioned_throughput}
end
def dsl_provisioned_throughput(dsl)
@@ -167,15 +209,28 @@
@options.updated = true
end
end
def update_table(dsl)
- log(:info, " table: #{@table.table_name}\n".green + Dyna::Utils.diff(self_provisioned_throughput, dsl, :color => @options.color, :indent => ' '), false)
+ params = {}
+ df_params = {}
+ unless billing_mode_eql?(dsl)
+ params[:billing_mode] = dsl[:billing_mode]
+ df_params[:billing_mode] = definition[:billing_mode]
+ end
+
+ if provisioned_throughput_eql?(dsl) == false && dsl[:scalable_targets].empty?
+ params[:provisioned_throughput] = dsl[:provisioned_throughput].symbolize_keys
+ df_params[:provisioned_throughput] = self_provisioned_throughput[:provisioned_throughput]
+ end
+
+ return if params.empty?
+ log(:info, " table: #{@table.table_name}\n".green + Dyna::Utils.diff(df_params, params, :color => @options.color, :indent => ' '), false)
unless @options.dry_run
- params = dsl.dup
+ wait_until_table_is_active
params[:table_name] = @table.table_name
- @ddb.update_table(params)
+ @ddb.update_table(params.symbolize_keys)
@options.updated = true
end
end
def update_table_index(dsl, index_params)
@@ -200,9 +255,51 @@
table_name: @table.table_name,
attribute_definitions: dsl.symbolize_keys[:attribute_definitions],
global_secondary_index_updates: index_params,
}
@ddb.update_table(params)
+ @options.updated = true
+ end
+ end
+
+ def update_auto_scaling(dsl)
+ has_change = false
+ unless scalable_targets_eql?(dsl)
+ has_change = true
+ df_cmp = definition[:scalable_targets].sort_by { |target| target[:scalable_dimension] }.map do |target|
+ h = target.to_h
+ h.delete(:creation_time)
+ h.delete(:role_arn)
+ Dyna::Utils.normalize_hash(h)
+ end
+ dsl_cmp = dsl.scalable_targets.sort_by { |target| target[:scalable_dimension] }.map { |target| Dyna::Utils.normalize_hash(target) }
+ log(:info, " table: #{@table.table_name}(update scalable targets)\n".green + Dyna::Utils.diff(df_cmp, dsl_cmp, :color => @options.color, :indent => ' '), false)
+ end
+
+ unless scaling_policies_eql?(dsl)
+ has_change = true
+ dsl_cmp = dsl.scaling_policies.map { |policy| Dyna::Utils.normalize_hash(policy) }.sort_by {|s| s[:scalable_dimension] }
+ log(:info, " table: #{@table.table_name}(update scaling policies)\n".green + Dyna::Utils.diff(scaling_policies_for_diff, dsl_cmp, :color => @options.color, :indent => ' '), false)
+ end
+
+ unless @options.dry_run
+ if has_change
+ definition[:scalable_targets].each do |target|
+ @options.aas.deregister_scalable_target(
+ service_namespace: 'dynamodb',
+ resource_id: target.resource_id,
+ scalable_dimension: target.scalable_dimension,
+ )
+ end
+
+ dsl.scalable_targets.each do |target|
+ @options.aas.register_scalable_target(target)
+ end
+
+ dsl.scaling_policies.each do |policy|
+ @options.aas.put_scaling_policy(policy)
+ end
+ end
@options.updated = true
end
end
end
end