lib/kakine/cli.rb in kakine-0.2.0 vs lib/kakine/cli.rb in kakine-0.3.0
- old
+ new
@@ -1,10 +1,9 @@
require 'thor'
require 'fog'
require 'yaml'
require 'hashdiff'
-
module Kakine
class CLI < Thor
option :tenant, type: :string, aliases: '-t'
desc 'show', 'show Security Groups specified tenant'
@@ -15,65 +14,26 @@
option :tenant, type: :string, aliases: "-t"
option :dryrun, type: :boolean, aliases: "-d"
option :filename, type: :string, aliases: "-f"
desc 'apply', "apply local configuration into OpenStack"
def apply
- adapter = if options[:dryrun]
- Kakine::Adapter::Mock.new
- else
- Kakine::Adapter::Real.new
- end
-
- operation = Kakine::CLI::Operation.new
- operation.set_adapter(adapter)
-
filename = options[:filename] ? options[:filename] : "#{options[:tenant]}.yaml"
+ Kakine::Adapter.set_option(options[:dryrun])
- security_groups = []
- delay_create = []
+ current_security_groups = Kakine::Resource.get_current(options[:tenant])
+ new_security_groups = Kakine::Resource.load_security_group_by_yaml(filename, options[:tenant])
- diffs = HashDiff.diff(
- Kakine::Resource.security_groups_hash(options[:tenant]),
- Kakine::Resource.yaml(filename)
- )
-
- diffs.each do |diff|
- security_groups << Kakine::SecurityGroup.new(options[:tenant], diff)
- end
-
- security_groups.each do |sg|
- if sg.update_rule? # foo[2]
- case
- when sg.add?
- operation.create_security_rule(sg)
- when sg.delete?
- operation.delete_security_rule(sg)
- when sg.update_attr?
- pre_sg = sg.get_prev_instance
- operation.delete_security_rule(pre_sg)
- delay_create << sg # avoid duplication entry
- else
- raise
- end
- else # foo
- case
- when sg.add?
- security_group_id = operation.create_security_group(sg)
- operation.create_security_rule(sg, security_group_id)
- when sg.delete?
- operation.delete_security_group(sg)
- when sg.update_attr?
- operation.delete_security_group(sg)
- security_group_id = operation.create_security_group(sg)
- operation.create_security_rule(sg, security_group_id)
- else
- raise
- end
+ return unless new_security_groups
+ new_security_groups.each do |new_sg|
+ registered_sg = current_security_groups.find { |cur_sg| cur_sg.name == new_sg.name }
+ if registered_sg
+ new_sg.convergence!(registered_sg) if new_sg != registered_sg
+ else
+ new_sg.register!
end
end
- # update rule attributes delay create
- delay_create.each do |sg|
- operation.create_security_rule(sg)
+ current_security_groups.each do |current_sg|
+ current_sg.unregister! if new_security_groups.none? { |new_sg| current_sg.name == new_sg.name }
end
end
end
end