Sha256: 47384d98ee641602b3174d1e4ca29acaa3e81c01e268ad60c48ec5ca4b513159

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

module Kakine
  class SecurityRule
    ATTRIBUTES = %i(direction protocol port_range_max port_range_min remote_ip remote_group ethertype).freeze

    attr_reader :id, *ATTRIBUTES

    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)
    end

    def ==(target_sg)
      ATTRIBUTES.all? do |attr|
        self.public_send(attr) == target_sg.public_send(attr)
      end
    end

    def convert_port_format(rule)
      unless format = port?(rule) || icmp?(rule) || range?(rule)
        raise(Kakine::SecurityRuleError, "no match port format")
      end
      format
    end

    def port?(rule)
      [rule['port'] ,rule['port']] if rule.has_key?('port')
    end

    def icmp?(rule)
      if rule.has_key?('type') && rule.has_key?('code')
        [rule['type'] ,rule['code']]
      end
    end

    def range?(rule)
      if rule.has_key?('port_range_max') && rule.has_key?('port_range_min')
        [rule['port_range_min'] ,rule['port_range_max']]
      end
    end

    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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kakine-0.7.0 lib/kakine/security_rule.rb
kakine-0.6.5 lib/kakine/security_rule.rb
kakine-0.6.4 lib/kakine/security_rule.rb
kakine-0.6.2 lib/kakine/security_rule.rb
kakine-0.6.1 lib/kakine/security_rule.rb
kakine-0.6.0 lib/kakine/security_rule.rb
kakine-0.5.0 lib/kakine/security_rule.rb