Commit 87b97db1 authored by Angel L. Mateo's avatar Angel L. Mateo Committed by Raphaël Pinson
Browse files

Add conffile define (#139)

Add conffile define
parent 4c9d6925
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -342,6 +342,53 @@ postfix::virtual {'user@example.com':
    destination => 'root',
}
```

### postfix::conffile

Manages postfix configuration files. With it, you could create configuration files (other than, main.cf, master.cf, etc.) restarting postfix when necessary.

#### Parameters
##### `ensure`
A string whose valid values are present, absent or directory.  
Default: present.  
Example: absent.

##### `source`
A string with the source of the file. This is the `source` parameter of the underlying file resource.  
Default: `undef`  
Example: 'puppet:///modules/postfix/configfile.cf'  

##### `content`
The content of the postfix configuration file. This is an alternative to the `source` parameter. If you don't provide `source` neither `content` parameters a default template is used and the content is created with values in the `options` hash.  
Default: `undef`  

##### `path`
Path where to create the configuration file.  
Default: '/etc/postfix/${name}'

##### `mode`
Permissions of the configuration file. This option is useful if you want to create the file with specific permissions (for example, because you have passwords in it).  
Default: '0644'  
Example: '0640'

##### `options`
Hash with the options used in the default template that is used when neither `source` neither `content`parameters are provided.  
Default: {}  
Example:
```
 postfix::conffile { 'ldapoptions.cf':
   options            => {
     server_host      => ldap.mydomain.com,
     bind             => 'yes',
     bind_dn          => 'cn=admin,dc=mydomain,dc=com',
     bind_pw          => 'password',
     search_base      => 'dc=example, dc=com',
     query_filter     => 'mail=%s',
     result_attribute => 'uid',
   }
 }
```

## Contributing

Please report bugs and feature request using [GitHub issue

manifests/conffile.pp

0 → 100644
+103 −0
Original line number Diff line number Diff line
# = Define: postfix::conffile
#
# Adds a postfix configuration file.
# It is mainly a file resource that also restarts postfix
#
# == Parameters
#
# [*ensure*]
#   Ensure parameter for the file resource. Defaults to 'present'
#
# [*source*]
#   Sets the value of the source parameter for the file
#
# [*content*]
#   Sets the content of the postfix config file
#   Note: This option is alternative to the source one
#
# [*path*]
#   Where to create the file.
#   Defaults to "/etc/postfix/${name}".
#
# [*mode*]
#   The file permissions of the file.
#   Defaults to 0644
#
# [*options*]
#   Hash with options to use in the template
#
# == Usage:
# postfix::conffile { 'ldapoptions.cf':
#   options            => {
#     server_host      => <ldapserver>,
#     bind             => 'yes',
#     bind_dn          => <bind_dn>,
#     bind_pw          => <bind_pw>,
#     search_base      => 'dc=example, dc=com',
#     query_filter     => 'mail=%s',
#     result_attribute => 'uid',
#   }
# }
#
# postfix::conffile { 'ldapoptions.cf':
#   source => 'puppet:///modules/postfix/ldapoptions.cf',
# }
#
define postfix::conffile (
  $ensure   = 'present',
  $source   = undef,
  $content  = undef,
  $path     = "/etc/postfix/${name}",
  $mode     = '0644',
  $options  = {},
) {
  include ::postfix::params

  validate_absolute_path($path)
  if !is_string($source) and !is_array($source) { fail("value for source should be either String type or Array type got ${source}") }
  if !is_string($content) { fail("value for content should be String type; got ${content}") }
  validate_string($ensure)
  validate_re($ensure, ['present', 'absent', 'directory'],
    "\$ensure must be either 'present', 'absent' or 'directory', got '${ensure}'")
  validate_hash($options)

  if (!defined(Class['postfix'])) {
    fail 'You must define class postfix before using postfix::config!'
  }

  if $source and $content {
    fail 'You must provide either \'source\' or \'content\', not both'
  }

  validate_hash($options)
  if !$source and !$content and $ensure == 'present' and empty($options) {
    fail 'You must provide \'options\' hash parameter if you don\'t provide \'source\' neither \'content\''
  }

  $manage_file_source = $source ? {
    ''        => undef,
    default   => $source,
  }

  $manage_content = $content ? {
    undef   => $source ? {
      undef   => template('postfix/conffile.erb'),
      default => undef,
    },
    default => $content,
  }

  file { "postfix conffile ${name}":
    ensure  => $ensure,
    path    => $path,
    mode    => $mode,
    owner   => 'root',
    group   => 'postfix',
    seltype => $postfix::params::seltype,
    require => Package['postfix'],
    source  => $source,
    content => $manage_content,
    notify  => Service['postfix'],
  }

}
+141 −0
Original line number Diff line number Diff line
require 'spec_helper'

describe 'postfix::conffile' do
  let (:title) { 'foo' }

  let :pre_condition do
    "class { '::postfix': }"
  end

  on_supported_os.each do |os, facts|
    context "on #{os}" do
      let(:facts) do
        facts
      end

      context 'when passing wrong type for ensure' do
        let (:params) { {
          :ensure => ['present'],
        } }
        it 'should fail' do
          expect {
            is_expected.to contain_file('postfix conffile foo')
          }.to raise_error(Puppet::Error, /\["present"\] is not a string/)
        end
      end

      context 'when passing wrong value for ensure' do
        let (:params) { {
          :ensure => 'running',
        } }
        it 'should fail' do
          expect {
            is_expected.to contain_file('postfix conffile foo')
          }.to raise_error(Puppet::Error, /must be either 'present', 'absent' or 'directory'/)
        end
      end

      context 'when passing both source and content' do
        let (:params) { {
          :source  => '/tmp/bar',
          :content => 'bar',
        } }

        it 'should fail' do
          expect {
            is_expected.to contain_file('postfix conffile foo')
          }.to raise_error(Puppet::Error, /You must provide either 'source' or 'content'/)
        end
      end

      context 'when passing source' do
        let (:params) { {
          :source  => 'puppet:///modules/postfix/bar',
        } }

        it { is_expected.to contain_file('postfix conffile foo').with(
          :ensure => 'present',
          :source => 'puppet:///modules/postfix/bar'
        ).without(:content)
        }
      end

      context 'when passing content' do
        let (:params) { {
          :content => 'bar',
        } }

        it { is_expected.to contain_file('postfix conffile foo').with(
          :ensure  => 'present',
          :content => 'bar'
        ).without(:source)
        }
      end

      context 'when not passing source or content' do
        it 'should fail' do
          expect {
            is_expected.to contain_file('postfix conffile foo')
          }.to raise_error(Puppet::Error, /You must provide 'options' hash parameter if you don't provide 'source' neither 'content'/)
        end
      end

      #context 'when passing options parameter' do
        #let (:params) { {
          #:options => {
            #:server_host => 'ldap.mydomain.com',
            #:bind        => 'no',
          #},
        #} }

        #it { is_expected.to contain_file('postfix conffile foo').with(
          #:ensure => 'present',
          #:content => '#
######################################################
## File managed by puppet
## DO NOT EDITY!!!
##

#bind = no
#server_host = ldap.mydomain.com'
        #).without(:source)
        #}

      #end

      context 'when ensuring absence' do
        let (:params) { {
          :ensure => 'absent',
        } }

        it { is_expected.to contain_file('postfix conffile foo').with_ensure('absent') }
      end

      context 'when using mode' do
        let (:params) { {
          :mode => '0644',
          :content => 'bar',
        } }

        it { is_expected.to contain_file('postfix conffile foo').with(
          :mode => '0644',
          :content => 'bar'
        )
        }
      end

      context 'when using path' do
        let (:params) { {
          :path => '/tmp/foo',
          :content => 'bar',
        } }

        it { is_expected.to contain_file('postfix conffile foo').with(
          :path => '/tmp/foo',
          :content => 'bar'
        )
        }
      end
    end
  end
end

templates/conffile.erb

0 → 100644
+9 −0
Original line number Diff line number Diff line
#
#####################################################
# File managed by puppet
# DO NOT EDITY!!!
#

<%- @options.keys.sort.each do | key | -%>
<%= key %> = <%= @options[key] %>
<%- end -%>