Class: Fog::Compute::QingCloud::SecurityGroupRule

Inherits:
QingCloud::Model show all
Defined in:
lib/fog/qingcloud/models/compute/security_group_rule.rb

Instance Method Summary collapse

Methods inherited from QingCloud::Model

#changing?, inherited, #wait_for

Constructor Details

#initialize(attrs = {}) ⇒ SecurityGroupRule

Returns a new instance of SecurityGroupRule.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 20

def initialize(attrs = {})
  if attrs['protocol']
    case attrs['protocol'].to_sym
    when :tcp, :udp
      attrs['val1'], attrs['val2'] = to_range(attrs['port_range']) if attrs['port_range']
    when :icmp
      attrs['val1'] ||= attrs.delete('icmp_type')
      attrs['val2'] ||= attrs.delete('icmp_code')
    end
  end
  attrs['val3'] ||= attrs.delete('src_ip')
  @auto_apply = attrs.delete('auto_apply') || true

  super attrs
end

Instance Method Details

#destroyObject



49
50
51
52
53
54
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 49

def destroy
  requires :id
  service.delete_security_group_rules(id)
  service.apply_security_group(group_id) if @auto_apply
  true
end

#modify_attributes(priority, name = nil) ⇒ Object



56
57
58
59
60
61
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 56

def modify_attributes(priority, name = nil)
  requires :id, :priority
  service.modify_security_group_rule_attributes(id, priority, name)
  merge_attributes('priority' => priority, 'name' => name)
  true
end

#port_rangeObject



73
74
75
76
77
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 73

def port_range
  return nil if protocol == 'icmp'
  port_end = val2.empty? ? val1 : val2
  (val1.to_i .. port_end.to_i)
end

#port_range=(range) ⇒ Object



68
69
70
71
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 68

def port_range=(range)
  r = to_range(range)
  merge_attributes('val1' => r.first, 'val2' => r.last)
end

#saveObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 36

def save
  requires :group_id
  if persisted?
    requires :priority
    modify_attributes(priority, name)
  else
    requires :protocol, :priority
    self.id = service.add_security_group_rules(group_id, to_query).body['security_group_rules'].first
  end
  service.apply_security_group(group_id) if @auto_apply
  true
end

#to_query(n = 1) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 87

def to_query(n = 1)
  query = ['protocol', 'priority', 'action', 
           'direction', 'val1', 'val2', 'val3'
          ].inject({}) do |ret, x|
    ret["rules.#{n}.#{x}"] =  send(x.to_sym)
    ret
  end
  query["rules.#{n}.security_group_rule_name"] = name
  query
end

#to_range(stuff) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/fog/qingcloud/models/compute/security_group_rule.rb', line 79

def to_range(stuff)
  if stuff.is_a? Range
    [stuff.first, stuff.end]
  elsif stuff.is_a? Integer
    [stuff, stuff]
  end
end