Sha256: 8f448a99eb485d2f87617a2a01528738ffd67d5afe1dafdaa85bcb23e163bacd

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Kakine
  class SecurityRule
    attr_reader :direction, :protocol, :port_range_max, :port_range_min, :remote_ip, :remote_group, :remote_group_id, :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 register!
      Kakine::Operation.create_security_rule(@tenant_name, @sg_name, self)
    end

    def unregister!
      Kakine::Operation.delete_security_rule(@tenant_name, @sg_name, self)
    end

    def ==(target_sg)
      instance_variables.each do |val|
        unless self.instance_variable_get(val) == target_sg.instance_variable_get(val)
          return false
        end
      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')
        [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
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kakine-0.3.0 lib/kakine/security_rule.rb