lib/kakine/security_group.rb in kakine-0.3.0 vs lib/kakine/security_group.rb in kakine-0.4.0

- old
+ new

@@ -1,80 +1,52 @@ require 'json' module Kakine class SecurityGroup - attr_reader :name, :tenant_id, :tenant_name, :description, :rules + attr_reader :id, :name, :tenant_name, :description, :rules - def initialize(tenant_name, parameter) - @name = parameter[0] + def initialize(tenant_name, params) + @name = params[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 ||= [] + @id = params[1]["id"] || "" + @description = params[1]["description"] || "" + @rules = get_rule_instances(params) || [] end - def initialize_copy(obj) - @rules = Marshal.load(Marshal.dump(obj.rules)) + def tenant_id + Kakine::Resource.get(:openstack).tenant(@tenant_name).id end 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 + same_group?(target_sg) && same_rule?(self, target_sg) && same_rule?(target_sg, self) end def !=(target_sg) !(self == target_sg) end - def find_by_rule(target_rule) - @rules.find { |rule| rule == target_rule } + def same_group?(target_sg) + %i(@name @tenant_name @description).all? do |val| + instance_variable_get(val) == target_sg.instance_variable_get(val) + end end - def register! - Kakine::Operation.create_security_group(self) - @rules.each { |rule| rule.register! } if has_rules? + def same_rule?(a, b) + a.rules.all? do |rule| + b.find_by_rule(rule) + end end - def unregister! - Kakine::Operation.delete_security_group(self) + def get_rule_instances(params) + params[1]["rules"].map do |rule| + SecurityRule.new(rule, @tenant_name, @name) + end unless params[1]["rules"].nil? end - 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 + def find_by_rule(target_rule) + @rules.find { |rule| rule == target_rule } end def has_rules? @rules.detect {|v| !v.nil?} - end - - def get_default_rule_instance - default_sg = self.clone - default_sg.set_default_rule - default_sg - end - - 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