Class: Dcmgr::Cli::Network

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

Constant Summary collapse

M =
Dcmgr::Models

Instance Method Summary collapse

Instance Method Details

#addObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dcmgr/cli/network.rb', line 23

def add
  vlan_pk = if options[:vlan_id].to_i > 0
              vlan = M::VlanLease.find(:tag_id=>options[:vlan_id]) || Error.raise("Invalid or Unknown VLAN ID: #{options[:vlan_id]}", 100)
              vlan.id
            else
              0
            end
  
  fields = {
     :ipv4_gw => options[:ipv4_gw],
     :prefix => options[:prefix],
     :dns_server => options[:dns],
     :domain_name => options[:domain],
     :dhcp_server => options[:dhcp],
     :metadata_server => options[:metadata],
     :metadata_server_port => options[:metadata_port],
     :description => options[:description],
     :account_id => options[:account_id],
     :bandwidth => options[:bandwidth],
     :vlan_lease_id => vlan_pk,
  }
  fields.merge!({:uuid => options[:uuid]}) unless options[:uuid].nil?

  puts super(M::Network,fields)
end

#del(uuid) ⇒ Object



50
51
52
# File 'lib/dcmgr/cli/network.rb', line 50

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

#leases(uuid) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/dcmgr/cli/network.rb', line 158

def leases(uuid)
  nw = M::Network[uuid] || Error.raise("Unknown network UUID: #{uuid}", 100)

  print ERB.new(<<__END, nil, '-').result(binding)
<%- nw.ip_lease_dataset.order(:ipv4).all.each { |l| -%>
<%= "%-20s  %-15s" % [l.ipv4, M::IpLease::TYPE_MESSAGES[l.alloc_type]] %>
<%- } -%>
__END
end

#modify(uuid) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dcmgr/cli/network.rb', line 66

def modify(uuid)
  vlan_pk = if options[:vlan_id].to_i > 0
              vlan = M::VlanLease.find(:tag_id=>options[:vlan_id]) || Error.raise("Invalid or Unknown VLAN ID: #{options[:vlan_id]}", 100)
              vlan.id
            else
              0
            end
  
  fields = {
     :ipv4_gw => options[:ipv4_gw],
     :prefix => options[:prefix],
     :dns_server => options[:dns],
     :domain_name => options[:domain],
     :dhcp_server => options[:dhcp],
     :metadata_server => options[:metadata],
     :metadata_server_port => options[:metadata_port],
     :description => options[:description],
     :account_id => options[:account_id],
     :bandwidth => options[:bandwidth],
     :vlan_lease_id => vlan_pk,
  }
  super(M::Network,uuid,fields)
end

#nat(uuid) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dcmgr/cli/network.rb', line 93

def nat(uuid)
  in_nw = M::Network[uuid] || Error.raise("Unknown network UUID: #{uuid}", 100)
  ex_nw = M::Network[options[:outside_network_id]] || Error.raise("Unknown network UUID: #{uuid}", 100) unless options[:outside_network_id].nil?

  if options[:clear] then
    in_nw.set_only({:nat_network_id => nil},:nat_network_id)
    in_nw.save_changes
  else
    in_nw.set_only({:nat_network_id => ex_nw.id},:nat_network_id)
    in_nw.save_changes
  end
end

#release(uuid) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/dcmgr/cli/network.rb', line 182

def release(uuid)
  nw = M::Network[uuid] || UnknownUUIDError.raise(uuid)

  if nw.ip_lease_dataset.filter(:ipv4=>options[:ipv4]).delete == 0
    Error.raise("The IP is not reserved in network #{uuid}: #{options[:ipv4]}", 100)
  end
end

#reserve(uuid) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/dcmgr/cli/network.rb', line 170

def reserve(uuid)
  nw = M::Network[uuid] || UnknownUUIDError.raise(uuid)

  if nw.ipaddress.include?(IPAddress(options[:ipv4]))
    nw.ip_lease_dataset.add_reserved(options[:ipv4])
  else
    Error.raise("IP address is out of range: #{options[:ipv4]} => #{nw.ipaddress.network}/#{nw.ipaddress.prefix}",100)
  end
end

#show(uuid = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dcmgr/cli/network.rb', line 109

def show(uuid=nil)
  if uuid
    nw = M::Network[uuid] || Error.raise("Unknown network UUID: #{uuid}", 100)
    puts ERB.new(<<__END, nil, '-').result(binding)
Network UUID:
<%= nw.canonical_uuid %>
Tag VLAN:
<%= nw.vlan_lease_id == 0 ? 'none' : nw.vlan_lease.tag_id %>
IPv4:
Network address: <%= nw.ipaddress.network %>/<%= nw.prefix %>
Gateway address: <%= nw.ipv4_gw %>
<%- if nw.nat_network_id -%>
Outside NAT network address: <%= nw.nat_network.ipaddress.network %>/<%= nw.nat_network.prefix %> (<%= nw.nat_network.canonical_uuid %>)
<%- end -%>
DHCP Information:
DHCP Server: <%= nw.dhcp_server %>
DNS Server: <%= nw.dns_server %>
<%- if nw.metadata_server -%>
Metadata Server: <%= nw.metadata_server %>
<%- end -%>
Bandwidth:
<%- if nw.bandwidth.nil? -%>
unlimited
<%- else -%>
<%= nw.bandwidth %> Mbit/s
<%- end -%>
<%- if nw.description -%>
Description:
<%= nw.description %>
<%- end -%>
__END
  else
    cond = {}
    cond[:account_id]= options[:account_id] if options[:account_id]
    if options[:vlan_id]
      vlan = M::VlanLease.find(:tag_id=>options[:vlan_id]) || abort("Unknown Tag VLAN ID: #{options[:vlan_id]}")
      cond[:vlan_lease_id] = vlan.id
    end

    nw = M::Network.filter(cond).all
    puts ERB.new(<<__END, nil, '-').result(binding)
<%- nw.each { |row| -%>
<%= row.canonical_uuid %>\t<%= row.ipaddress.network %>/<%= row.prefix %>\t<%= (row.vlan_lease && row.vlan_lease.tag_id) %>
<%- } -%>
__END
  end
end