crowbar/crowbar-core

View on GitHub
bin/crowbar_network

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
#
# Copyright 2011-2013, Dell
# Copyright 2013-2014, SUSE LINUX Products GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require File.join(File.expand_path(File.dirname(__FILE__)), "barclamp_lib")
@barclamp = "network"

@commands["deallocate_virtual_ip"] = ["deallocate_ip(ARGV.shift,ARGV.shift,ARGV.shift,:service)", "deallocate_virtual_ip <name> <service> <network> - Deallocate an ip from a service on a network"]
@commands["allocate_virtual_ip"] = ["allocate_ip(ARGV.shift,ARGV.shift,ARGV.shift,ARGV.shift,ARGV.shift,:service)", "allocate_virtual_ip <name> <service> <network> <range> [<suggestion>] - Allocate an ip for a service on a network from a range"]
@commands["deallocate_ip"] = ["deallocate_ip(ARGV.shift,ARGV.shift,ARGV.shift,:node)", "deallocate_ip <name> <node> <network> - Deallocate an ip from a node on a network"]
@commands["allocate_ip"] = ["allocate_ip(ARGV.shift,ARGV.shift,ARGV.shift,ARGV.shift,ARGV.shift,:node)", "allocate_ip <name> <node> <network> <range> [<suggestion>] - Allocate an ip for a node on a network from a range"]
@commands["enable_interface"] = ["enable_interface(ARGV.shift,ARGV.shift,ARGV.shift)", "enable_interface <name> <node> <network> - Ensure that an interface is present for the specified network"]
@commands["disable_interface"] = ["disable_interface(ARGV.shift,ARGV.shift,ARGV.shift)", "disable_interface <name> <node> <network> - Ensure that an interface is not enabled for the specified network"]

def enable_interface(name, node, network)
  usage -1 if name.nil? or name == ""
  usage -1 if node.nil? or node == ""
  usage -1 if network.nil? or network == ""

  @data = { "name" => node, "network" => network }.to_json
  struct = post_json("/enable_interface/#{name}", @data)

  if struct[1] == 200
    ["Enabled interface #{name} #{struct[0].inspect}", 0]
  elsif struct[1] == 404
    ["Failed to enable interface: #{name} : Not Found", 1]
  elsif struct[1] == 400
    ["Failed to enable interface: #{name} : Errors in data\n#{struct[0]}", 1]
  else
    ["Failed to talk to service enable_interface: #{struct[1]}: #{struct[0]}", 1]
  end
end

def allocate_ip(name, object, network, range, suggestion, type)
  usage -1 if name.nil? or name == ""
  usage -1 if network.nil? or network == ""
  usage -1 if object.nil? or object == ""
  usage -1 if range.nil? or range == ""

  @data = { "name" => object, "network" => network, "range" => range }
  @data["suggestion"] = suggestion if suggestion
  @data = @data.to_json
  if type == :node
    struct = post_json("/allocate_ip/#{name}", @data)
  else
    struct = post_json("/allocate_virtual_ip/#{name}", @data)
  end

  if struct[1] == 200
    ["Allocate ip #{name} #{struct[0].inspect}", 0]
  elsif struct[1] == 404
    ["Failed to Allocate ip: #{name} : Not Found", 1]
  elsif struct[1] == 400
    ["Failed to Allocate ip: #{name} : Errors in data\n#{struct[0]}", 1]
  else
    ["Failed to talk to service allocate_ip: #{struct[1]}: #{struct[0]}", 1]
  end
end

def deallocate_ip(name, object, network, type)
  usage -1 if name.nil? or name == ""
  usage -1 if network.nil? or network == ""
  usage -1 if object.nil? or object == ""

  @data = { "name" => object, "network" => network }.to_json
  if type == :node
    struct = post_json("/deallocate_ip/#{name}", @data)
  else
    struct = post_json("/deallocate_virtual_ip/#{name}", @data)
  end

  if struct[1] == 200
    ["Deallocate address #{network} #{object}", 0]
  elsif struct[1] == 404
    ["Failed to deallocate ip: #{name} : Not Found", 1]
  elsif struct[1] == 400
    ["Failed to deallocate ip: #{name} : Errors in data\n#{struct[0]}", 1]
  else
    ["Failed to talk to service deallocate_ip: #{struct[1]}: #{struct[0]}", 1]
  end
end

def disable_interface(name, node, network)
  usage -1 if name.nil? or name == ""
  usage -1 if node.nil? or node == ""
  usage -1 if network.nil? or network == ""

  @data = { "name" => node, "network" => network }.to_json
  struct = post_json("/disable_interface/#{name}", @data)

  if struct[1] == 200
    ["Disable interface #{network} #{node}", 0]
  elsif struct[1] == 404
    ["Failed to disable interface: #{name} : Not Found", 1]
  elsif struct[1] == 400
    ["Failed to disable interface: #{name} : Errors in data\n#{struct[0]}", 1]
  else
    ["Failed to talk to service disable_interface: #{struct[1]}: #{struct[0]}", 1]
  end
end

main