Class: Kakine::SecurityRule

Inherits:
Object
  • Object
show all
Defined in:
lib/kakine/security_rule.rb

Constant Summary collapse

ATTRIBUTES =
%i(direction protocol port_range_max port_range_min remote_ip remote_group ethertype).freeze

Instance Method Summary collapse

Constructor Details

#initialize(rule, tenant_name, sg_name) ⇒ SecurityRule

Returns a new instance of SecurityRule.



7
8
9
10
11
12
13
14
15
16
# File 'lib/kakine/security_rule.rb', line 7

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

Instance Method Details

#==(target_sg) ⇒ Object



18
19
20
21
22
# File 'lib/kakine/security_rule.rb', line 18

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

#convert_port_format(rule) ⇒ Object



24
25
26
27
28
29
# File 'lib/kakine/security_rule.rb', line 24

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

#icmp?(rule) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/kakine/security_rule.rb', line 35

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

#port?(rule) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/kakine/security_rule.rb', line 31

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

#range?(rule) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/kakine/security_rule.rb', line 41

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

#remote_group_idObject



47
48
49
50
51
52
53
54
# File 'lib/kakine/security_rule.rb', line 47

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