require 'puppet'

Puppet::Type.newtype(:ve) do
	@doc = "Virtual Environment."
	ensurable

	newparam(:id) do
		isnamevar
		desc "The integer identifier of the VE, must be > 100."
		validate do |id|
			unless id =~ /^\d+$/ and id.to_i > 100
				raise ArgumentError, "\"#{id}\" is not a valid VE ID."
			end
		end
	end

	newproperty(:hostname) do
		desc "Hostname of the VE."
		defaultto { "ve#{@resource[:id]}" }
	end

	newproperty(:name) do
		desc "Symbolic name of the VE, to be used instead of CTID. Defaults to hostname without the domain."
		defaultto { @resource.should(:hostname).sub(/\..*/, '') }
	end

	newproperty(:ipaddr, :array_matching => :all) do
		desc "IP address(es) of the VE."
		validate do |ip|
			unless ip =~ /^\d+\.\d+\.\d+\.\d+$/
				raise ArgumentError, "\"#{ip}\" is not a valid IP address."
			end
		end
	end

	newproperty(:nameserver, :array_matching => :all) do
		desc "DNS name server(s)."
		validate do |val|
			unless val =~ /^\d+\.\d+\.\d+\.\d+( +\d+\.\d+\.\d+\.\d+)*$/
				raise ArgumentError, "\"#{val}\" is not a valid space-separated list of IP addresses."
			end
		end
	end

	newparam(:searchdomain) do
		desc "DNS search domain name(s)."
	end

	newparam(:vgname) do
		desc "Volume Group name of the filesystem device."
	end

	newparam(:lvname) do
		desc "Logical Volume name of the filesystem device."
	end

	newparam(:fstype) do
		desc "Root filesystem type."
		defaultto(:xfs)
	end

	newparam(:scratchdevice) do
		desc "Whether to zero the first few blocks of the root filesystem device before creation or after destroy."
		newvalues(:true, :false)
		defaultto(:false)
	end

	newparam(:ostemplate) do
		desc "Template name."
		isrequired
	end

	newparam(:private) do
		desc "Path to the private directory. Not passed to vzctl, if not specified."
	end

	newproperty(:status) do
		desc "Status of the VE."
		newvalue(:running)
		newvalue(:stopped)
		newvalue(:mounted)
		defaultto(:running)
	end

end

