Class: Dcmgr::Models::SecurityGroup

Inherits:
AccountResource show all
Defined in:
lib/dcmgr/models/security_group.rb

Constant Summary

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Instance Method Summary collapse

Methods inherited from AccountResource

#account

Methods inherited from BaseNew

Proxy, dataset, default_row_lock_mode=, install_data, install_data_hooks, lock!, unlock!, #with_timestamps?

Instance Method Details

#after_saveObject



24
25
26
27
# File 'lib/dcmgr/models/security_group.rb', line 24

def after_save
  super
  self.rebuild_rule
end

#before_destroyObject



33
34
35
36
37
38
# File 'lib/dcmgr/models/security_group.rb', line 33

def before_destroy
  return false if self.instances.size > 0

  self.flush_rule
  super
end

#build_ruleObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dcmgr/models/security_group.rb', line 46

def build_rule
  return if self.rule.nil?

  self.rule.split("\n").each { |permission|
    # [ToDo]
    # to make strong parser
    next if permission =~ /\A#/
    next if permission.length == 0

    # [format] protocol,source,destination
    # - protocol: tcp|udp|icmp
    # - source: IPAddr|CIDR|Owner:Group
    # - destination: port|icmp-type
    SecurityGroupRule.create(:security_group_id => self.id,
                             :permission         => permission)

  }
end

#flush_ruleObject



29
30
31
# File 'lib/dcmgr/models/security_group.rb', line 29

def flush_rule
  SecurityGroupRule.filter(:security_group_id => self.id).destroy
end

#rebuild_ruleObject



41
42
43
44
# File 'lib/dcmgr/models/security_group.rb', line 41

def rebuild_rule
  self.flush_rule
  self.build_rule
end

#to_api_documentObject



17
18
19
20
21
22
# File 'lib/dcmgr/models/security_group.rb', line 17

def to_api_document
  super.merge({
                :rule => rule.to_s,
                :rules => security_group_rules.map { |rule| rule.to_hash },
              })
end

#to_hashObject



10
11
12
13
14
15
# File 'lib/dcmgr/models/security_group.rb', line 10

def to_hash
  super.merge({
                :rule => rule.to_s,
                :rules => security_group_rules.map { |rule| rule.to_hash },
              })
end