Class: Fog::Compute::QingCloud::SecurityGroup

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

Instance Method Summary collapse

Methods inherited from QingCloud::Model

#changing?, inherited, #wait_for

Instance Method Details

#add_rule(attrs) ⇒ Object



20
21
22
23
24
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 20

def add_rule(attrs)
  requires :id
  attrs['group_id'] = id
  service.security_group_rules.new(attrs).save
end

#add_rules(rules, auto_apply = true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 26

def add_rules(rules, auto_apply = true)                          
  requires :id                                                   
  args = {}                                                      
  rules.each_with_index { |r, i|                                 
    args.merge! service.security_group_rules.new(r).to_query(i + 1)         
  }                                                              
  service.add_security_group_rules(id, args)                     
  service.apply_security_group(id) if auto_apply                 
  true
end

#applyObject



58
59
60
61
62
63
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 58

def apply
  requires :id
  service.apply_security_group(id)
  merge_attributes('is_applied' => 1)
  true
end

#delete_rules(rule_id) ⇒ Object Also known as: delete_rule



52
53
54
55
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 52

def delete_rules(rule_id)
  service.delete_security_group_rules(rule_id)
  true
end

#destroyObject



65
66
67
68
69
70
71
72
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 65

def destroy
  requires_one :group_id
  service.delete_security_groups(group_id)
  true
rescue Fog::QingCloud::Errors::PermissionDenied => e
  raise e unless e.message =~ /has already been deleted/i
  true
end

#egress_rulesObject



42
43
44
45
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 42

def egress_rules
  requires :id
  rules(:egress)
end

#ingress_rulesObject



37
38
39
40
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 37

def ingress_rules
  requires :id
  rules(:ingress)
end

#rules(direction = nil) ⇒ Object



47
48
49
50
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 47

def rules(direction = nil)
  requires :id
  service.security_group_rules.all('group-id' => id, 'direction' => direction)
end

#saveObject

Create a security group

>> g = QingCloud.security_groups.new(:name => "some_name", :description => "something")
>> g.save

Returns:

True or an exception depending on the result. Keep in mind that this creates a new security group. As such, it yields an InvalidGroup.Duplicate exception if you attempt to save an existing group.



85
86
87
88
89
90
91
92
93
94
# File 'lib/fog/qingcloud/models/compute/security_group.rb', line 85

def save
  if persisted?
    modify_attributes(name, description)
  else
    data = service.create_security_group(name).body
    merge_attributes('id' => data['security_group_id'])
    reload
  end
  true
end