port.rb

Path: lib/puppet/type/port.rb
Last Update: Wed Dec 09 12:54:29 -0800 2009

module Puppet

    newtype(:port) do
        @doc = "Installs and manages port entries.  For most systems, these
            entries will just be in /etc/services, but some systems (notably OS X)
            will have different solutions."

        ensurable

        newproperty(:protocols) do
            desc "The protocols the port uses.  Valid values are *udp* and *tcp*.
                Most services have both protocols, but not all.  If you want
                both protocols, you must specify that; Puppet replaces the
                current values, it does not merge with them.  If you specify
                multiple protocols they must be as an array."

            def is=(value)
                case value
                when String
                    @is = value.split(/\s+/)
                else
                    @is = value
                end
            end

            def is
                @is
            end

            # We actually want to return the whole array here, not just the first
            # value.
            def should
                if defined? @should
                    if @should[0] == :absent
                        return :absent
                    else
                        return @should
                    end
                else
                    return nil
                end
            end

            validate do |value|
                valids = ["udp", "tcp", "ddp", :absent]
                unless valids.include? value
                    raise Puppet::Error,
                        "Protocols can be either 'udp' or 'tcp', not %s" % value
                end
            end
        end

        newproperty(:number) do
            desc "The port number."
        end

        newproperty(:description) do
            desc "The port description."
        end

        newproperty(:alias) do
            desc "Any aliases the port might have.  Multiple values must be
                specified as an array.  Note that this property has the same name as
                one of the metaparams; using this property to set aliases will make
                those aliases available in your Puppet scripts and also on disk."

            # We actually want to return the whole array here, not just the first
            # value.
            def should
                if defined? @should
                    if @should[0] == :absent
                        return :absent
                    else
                        return @should
                    end
                else
                    return nil
                end
            end

            validate do |value|
                if value.is_a? String and value =~ /\s/
                    raise Puppet::Error,
                        "Aliases cannot have whitespace in them: %s" %
                        value.inspect
                end
            end

            munge do |value|
                unless value == "absent" or value == :absent
                    # Add the :alias metaparam in addition to the property
                    @resource.newmetaparam(
                        @resource.class.metaparamclass(:alias), value
                    )
                end
                value
            end
        end

        newproperty(:target) do
            desc "The file in which to store service information.  Only used by
                those providers that write to disk."

            defaultto { if @resource.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile)
                    @resource.class.defaultprovider.default_target
                else
                    nil
                end
            }
        end

        newparam(:name) do
            desc "The port name."

            isnamevar
        end
    end

end

[Validate]