module Puppet
  newtype(:cobbler_nic) do
  @doc = "Set up a cobbler distribution"

  ensurable

  newparam(:name, :namevar => true) do
    desc "The name of the system-network interface, eg server-eth0, server-br0"
  end
  newparam(:system) do
    desc "The name of the system this interface if for"
  end
  newparam(:interface) do
    desc "The name of the network interface, eg eth0, br0"
  end
  newparam(:bondmode) do
    desc "The bonding mode to set"
  end
  newparam(:bondmaster) do
    desc "The interface which will act as the master to the bond"
  end
  newparam(:bondopts) do
    desc "Additional options to pass to the bond"
  end
  newparam(:dhcp) do
    desc "The dhcp tag"
  end
  newparam(:dnsname) do
    desc "The name to create in dns for the interface"
  end
  newparam(:ip) do
    desc "The statis ip addr for this interface."
  end
  newparam(:static) do
    desc "Set to true if the interface needs to be static"
  end
  newparam(:routes) do
    desc "List of statis routes for the interface"
  end
  newparam(:subnet) do
    desc "The subnet mask."
  end
  newparam(:virtbridge) do
    desc "Virtual bridge the interface will bind to"
  end
  newparam(:mac) do
    desc "Static mac addr for the interface"
  end
  autorequire(:cobbler_system) do
    [@parameters[:system].value]
  end
end
end

Puppet::Type.type(:cobblerdistro).provide(:rhcobblerdistro) do
  desc "Red Hat support for cobbler, should work on any cobbler install"

  def create
    opts = {
      :system => '--name',
      :interface => '--interface',
      :bondmode => '--bonding',
      :bondmaster => '--bonding-master',
      :bondopts => '--bonding-opts',
      :dhcp => '--dhcp-tag',
      :dnsname => '--dns-name',
      :ip => '--ip-address',
      :static => '--static',
      :routes => '--static-routes',
      :subnet => '--subnet',
      :virtbridge => '--virt-bridge',
      :mac => '--mac-address',
      }
    cmd = 'cobbler system edit '
    if `cobbler system report --name=#{@resource[:system]}`.match(/^No/)
      puts "The system #{@resource[:system]} is not present."
      return false
    end
    @resource.to_hash.each do |key, value|
      opt = opts[key]
      if opt
        cmd << " #{opt}='#{value}'"
      end
    end
    `#{cmd}`
  end

  def destroy
    `cobbler system edit --delete-interface --interface=#{@resource[:interface]} --name=#{@resource[:system]}`
  end
    
  def exists?
    rep = `cobbler system report --name=#{@resource[:system]}`
    if rep.match(/^No/)
      return false
    end
    avail = { 
      "Name" => :system,
      "Interface =====" => :interface,
      "Bonding Mode" => :bondmode,
      "Bonding Master" => :bondmaster,
      "Bonding Opts" => :bondopts,
      "DHCP Tag" => :dhcp,
      "DNS Name" => :dnsname,
      "IP Address" => :ip,
      "MAC Address" => :mac,
      "Static" => :static,
      "Static Routes" => :routes,
      "Subnet" => :subnet,
      "Virt Bridge" => :virtbridge,
      }
    rep.each do |line|
      val = avail[line.split(":")[0].strip]
      unless val
        next
      end
      unless @resource[val]
        next
      end
      if val
        stat = line.split(":")[1].strip
        if stat.match(/^\{/)
          stat.gsub!('{', '')
          stat.gsub!('}', '')
          stat.gsub!('\'', '')
          stat.gsub!(': ', '=')
          stat.gsub!(',', ' ')
        elsif stat.match(/^\[/)
          stat.gsub!('[', '')
          stat.gsub!(']', '')
          stat.gsub!('\'', '')
          stat.gsub!(' ', '')
          stat.gsub!(',', ' ')
        end
        unless @resource[val] == stat
          return false
        end
      end
    end
    return true
  end
end
