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

- old
+ new

@@ -1,58 +1,54 @@ module Kakine class SecurityRule - attr_reader :direction, :protocol, :port_range_max, :port_range_min, :remote_ip, :remote_group, :remote_group_id, :ethertype + attr_reader :id, :direction, :protocol, :port_range_max, :port_range_min, :remote_ip, :remote_group, :ethertype def initialize(rule, tenant_name, sg_name) @tenant_name = tenant_name @sg_name = sg_name rule.each do|k,v| instance_variable_set(eval(":@#{k.to_s}"), v) unless k.include?("port") end @port_range_min, @port_range_max = *convert_port_format(rule) - set_remote_security_group_id + end + def ==(target_sg) + %i(@direction @protocol @port_range_max @port_range_min @remote_ip @remote_group @ethertype).all? do |val| + self.instance_variable_get(val) == target_sg.instance_variable_get(val) + end end - def register! - Kakine::Operation.create_security_rule(@tenant_name, @sg_name, self) + def convert_port_format(rule) + unless format = port?(rule) || icmp?(rule) || range?(rule) + raise(Kakine::SecurityRuleError, "no match port format") + end + format end - def unregister! - Kakine::Operation.delete_security_rule(@tenant_name, @sg_name, self) + def port?(rule) + [rule['port'] ,rule['port']] if rule.has_key?('port') end - def ==(target_sg) - instance_variables.each do |val| - unless self.instance_variable_get(val) == target_sg.instance_variable_get(val) - return false - end + def icmp?(rule) + if rule.has_key?('type') && rule.has_key?('code') + [rule['type'] ,rule['code']] end - true end - private - - def convert_port_format(rule) - case - when rule.has_key?('port') - [rule['port'] ,rule['port']] - when rule.has_key?('type'), rule.has_key?('code') - [rule['type'] ,rule['code']] - when rule.has_key?('port_range_max'), rule.has_key?('port_range_min') + def range?(rule) + if rule.has_key?('port_range_max') && rule.has_key?('port_range_min') [rule['port_range_min'] ,rule['port_range_max']] - else - raise "no match port format" end end - def set_remote_security_group_id - unless @remote_group.nil? - remote_security_group = Kakine::Resource.security_group(@tenant_name, @remote_group) - raise "not exists #{@remote_group}" unless remote_security_group - @remote_group_id = remote_security_group.id + def remote_group_id + if !!@remote_group + unless remote_security_group = Kakine::Resource.get(:openstack).security_group(@tenant_name, @remote_group) + raise(Kakine::SecurityRuleError, "not exists #{@remote_group}") + end + remote_security_group.id end end end end