Class: Dcmgr::Cli::SecurityGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/dcmgr/cli/security_group.rb

Constant Summary collapse

M =
Dcmgr::Models

Instance Method Summary collapse

Instance Method Details

#addObject



29
30
31
32
33
34
35
36
# File 'lib/dcmgr/cli/security_group.rb', line 29

def add
  UnknownUUIDError.raise(options[:account_id]) if M::Account[options[:account_id]].nil?

  fields = options.dup
  fields[:rule] = read_rule_text
  
  puts super(M::SecurityGroup,fields)
end

#apply(uuid) ⇒ Object



89
90
91
92
93
94
# File 'lib/dcmgr/cli/security_group.rb', line 89

def apply(uuid)
  group = M::SecurityGroup[uuid] || UnknownUUIDError.raise(uuid)
  instance = M::Instance[options[:instance]] || UnknownUUIDError.raise(options[:instance])
  Error.raise("Group #{uuid} is already applied to instance #{options[:instance]}.",100) if group.instances.member?(instance)
  group.add_instance(instance)
end

#del(uuid) ⇒ Object



39
40
41
# File 'lib/dcmgr/cli/security_group.rb', line 39

def del(uuid)
  super(M::SecurityGroup,uuid)
end

#modify(uuid) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/dcmgr/cli/security_group.rb', line 76

def modify(uuid)
  UnknownUUIDError.raise(options[:account_id]) if options[:account_id] && M::Account[options[:account_id]].nil?

  fields = options.dup
  if options[:rule]
    fields[:rule] = read_rule_text
  end
  
  super(M::SecurityGroup,uuid, fields)
end

#remove(uuid) ⇒ Object



98
99
100
101
102
103
# File 'lib/dcmgr/cli/security_group.rb', line 98

def remove(uuid)
  group = M::SecurityGroup[uuid] || UnknownUUIDError.raise(uuid)
  instance = M::Instance[options[:instance]] || UnknownUUIDError.raise(options[:instance])
  Error.raise("Group #{uuid} is not applied to instance #{options[:instance]}.",100) unless group.instances.member?(instance)
  group.remove_instance(instance)
end

#show(uuid = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dcmgr/cli/security_group.rb', line 44

def show(uuid=nil)
  if uuid
    group = M::SecurityGroup[uuid] || UnknownUUIDError.raise(uuid)
    puts ERB.new(<<__END, nil, '-').result(binding)
Group UUID:
  <%= group.canonical_uuid %>
Account id:
  <%= group.account_id %>
<%- if group.description -%>
Description:
  <%= group.description %>
<%- end -%>
<%- unless group.security_group_rules.empty? -%>
Rules:
<%- group.security_group_rules.each { |rule| -%>
  <%= rule.permission %>
<%- } -%>
<%- end -%>
__END
  else
    puts ERB.new(<<__END, nil, '-').result(binding)
<%- M::SecurityGroup.all { |row| -%>
<%= row.canonical_uuid %>\t<%= row.account_id %>\t<%= row.description %>
<%- } -%>
__END
  end
end