Class: Dcmgr::Cli::Network

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

Defined Under Namespace

Classes: DhcpOps, PhyOps

Constant Summary collapse

M =
Dcmgr::Models

Instance Method Summary collapse

Instance Method Details

#addObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dcmgr/cli/network.rb', line 57

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

  validate_ipv4_range

  fields = map_network_params

  puts super(M::Network,fields)
end

#del(uuid) ⇒ Object



73
74
75
# File 'lib/dcmgr/cli/network.rb', line 73

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

#forward(uuid, phynet) ⇒ Object



207
208
209
210
211
212
# File 'lib/dcmgr/cli/network.rb', line 207

def forward(uuid, phynet)
  nw = M::Network[uuid] || UnknownUUIDError.raise(uuid)
  phy = M::PhysicalNetwork.find(:name=>phynet) || Error.raise("Unknown physical network: #{phynet}")
  nw.physical_network = phy
  nw.save
end

#leases(uuid) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/dcmgr/cli/network.rb', line 174

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



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

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
  
  validate_ipv4_range

  fields = map_network_params
  
  super(M::Network,uuid,fields)
end

#nat(uuid) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dcmgr/cli/network.rb', line 109

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



198
199
200
201
202
203
204
# File 'lib/dcmgr/cli/network.rb', line 198

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



186
187
188
189
190
191
192
193
194
# File 'lib/dcmgr/cli/network.rb', line 186

def reserve(uuid)
  nw = M::Network[uuid] || UnknownUUIDError.raise(uuid)
  
  if nw.include?(IPAddress(options[:ipv4]))
    nw.ip_lease_dataset.add_reserved(options[:ipv4])
  else
    Error.raise("IP address is out of range: #{options[:ipv4]} => #{nw.ipv4_ipaddress}/#{nw.prefix}",100)
  end
end

#show(uuid = nil) ⇒ Object



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dcmgr/cli/network.rb', line 125

def show(uuid=nil)
  if uuid
    nw = M::Network[uuid] || UnknownUUIDError.raise(uuid)
    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.ipv4_ipaddress %>/<%= nw.prefix %>
Gateway address: <%= nw.ipv4_gw %>
<%- if nw.nat_network_id -%>
Outside NAT network address: <%= nw.nat_network.ipv4_ipaddress %>/<%= 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.ipv4_ipaddress %>/<%= row.prefix %>\t<%= (row.vlan_lease && row.vlan_lease.tag_id) %>
<%- } -%>
__END
  end
end