lib/kakine/security_group.rb in kakine-0.2.0 vs lib/kakine/security_group.rb in kakine-0.3.0
- old
+ new
@@ -1,76 +1,80 @@
+require 'json'
module Kakine
class SecurityGroup
- attr_reader :target_object_name, :name, :transaction_type, :tenant_id, :tenant_name, :description, :rules, :prev_rules
+ attr_reader :name, :tenant_id, :tenant_name, :description, :rules
- def initialize(tenant_name, diff)
- unset_security_rules
- Kakine::DiffParser.parse_parameters(tenant_name, diff).each do|k,v|
- instance_variable_set(eval(":@#{k.to_s}"), v)
- end
- set_remote_security_group_id
+ def initialize(tenant_name, parameter)
+ @name = parameter[0]
+ @tenant_name = tenant_name
+ @tenant_id = Kakine::Resource.tenant(tenant_name).id
+ @description = parameter[1]["description"] || ""
+ @rules = parameter[1]["rules"].map do |rule|
+ SecurityRule.new(rule, @tenant_name, @name)
+ end unless parameter[1]["rules"].nil?
+ @rules ||= []
end
def initialize_copy(obj)
@rules = Marshal.load(Marshal.dump(obj.rules))
- @prev_rules = Marshal.load(Marshal.dump(obj.prev_rules))
- unset_security_rules
end
- def has_rules?
- @rules.detect {|v| !v.nil? && v.size > 0}
+ def ==(target_sg)
+ instance_variables.reject{ |k| k == :@rules }.each do |val|
+ return false unless self.instance_variable_get(val) == target_sg.instance_variable_get(val)
+ end
+ @rules.each do |rule|
+ return false unless target_sg.find_by_rule(rule)
+ end
+ target_sg.rules.each do |rule|
+ return false unless find_by_rule(rule)
+ end
+ true
end
- def add?
- @transaction_type == "+"
+ def !=(target_sg)
+ !(self == target_sg)
end
- def delete?
- @transaction_type == "-"
+ def find_by_rule(target_rule)
+ @rules.find { |rule| rule == target_rule }
end
- def update_attr?
- @transaction_type == "~"
+ def register!
+ Kakine::Operation.create_security_group(self)
+ @rules.each { |rule| rule.register! } if has_rules?
end
- def update_rule?
- !@target_object_name.split(/[\[]/, 2)[1].nil?
+ def unregister!
+ Kakine::Operation.delete_security_group(self)
end
- def get_prev_instance
- prev_sg = self.clone
- prev_sg.add_security_rules(@prev_rules)
- prev_sg
- end
-
- def set_default_rules
- unset_security_rules
- ["IPv4", "IPv6"].each do |ip|
- add_security_rules({"direction"=>"egress", "protocol"=>nil, "port"=>nil, "remote_ip"=>nil, "ethertype"=>ip})
+ def convergence!(target_sg)
+ if @description != target_sg.description
+ target_sg.unregister!
+ register!
+ else
+ target_sg.rules.each do |rule|
+ rule.unregister! unless find_by_rule(rule)
+ end
+ @rules.each do |rule|
+ rule.register! unless target_sg.find_by_rule(rule)
+ end
end
end
- def add_security_rules(rule)
- case
- when rule.instance_of?(Array)
- @rules = rule
- when rule.instance_of?(Hash)
- @rules << rule
- end
+ def has_rules?
+ @rules.detect {|v| !v.nil?}
end
- private
-
- def unset_security_rules
- @rules = []
+ def get_default_rule_instance
+ default_sg = self.clone
+ default_sg.set_default_rule
+ default_sg
end
- def set_remote_security_group_id
- @rules.each do |rule|
- unless rule['remote_group'].nil?
- remote_security_group = Kakine::Resource.security_group(@tenant_name, rule.delete("remote_group"))
- rule["remote_group_id"] = remote_security_group.id
- end
- end if has_rules?
+ def set_default_rule
+ @rules = %w(IPv4 IPv6).map { |v| {"direction"=>"egress", "protocol" => nil, "port"=>nil, "remote_ip"=>nil, "ethertype"=>v } }.
+ map{ |rule| SecurityRule.new(rule, @tenant_name, @name) }
end
end
end