transport.pp 3.24 KB
Newer Older
Raphaël Pinson's avatar
Doc    
Raphaël Pinson committed
1
2
3
4
5
6
7
8
9
# == Definition: postfix::transport
#
# Manages content of the /etc/postfix/transport map.
#
# === Parameters
#
# [*name*]        - name of address postfix will lookup. See transport(5).
# [*destination*] - where the emails will be delivered to. See transport(5).
# [*ensure*]      - present/absent, defaults to present.
Erinn Looney-Triggs's avatar
Erinn Looney-Triggs committed
10
# [*nexthop*]     - A string to define where and how to deliver the mail. See transport(5).
Raphaël Pinson's avatar
Doc    
Raphaël Pinson committed
11
12
13
14
15
16
17
18
19
20
#
# === Requires
#
# - Class["postfix"]
# - Postfix::Hash["/etc/postfix/transport"]
# - Postfix::Config["transport_maps"]
# - augeas
#
# === Examples
#
21
#   node 'toto.example.com' {
Raphaël Pinson's avatar
Doc    
Raphaël Pinson committed
22
23
24
#
#     include postfix
#
25
#     postfix::hash { '/etc/postfix/transport':
Raphaël Pinson's avatar
Doc    
Raphaël Pinson committed
26
27
#       ensure => present,
#     }
28
#     postfix::config { 'transport_maps':
29
#       value => 'hash:/etc/postfix/transport, regexp:/etc/postfix/transport_regexp',
Raphaël Pinson's avatar
Doc    
Raphaël Pinson committed
30
#     }
31
32
33
34
35
36
37
38
39
#     postfix::transport {
#       'mailman.example.com':
#          ensure      => present,
#          destination => 'mailman';
#       'slow_transport':
#          ensure      => present,
#          nexthop     => '/^user-.*@mydomain\.com/'
#          file        => '/etc/postfix/transport_regexp',
#          destination => 'slow'
Raphaël Pinson's avatar
Doc    
Raphaël Pinson committed
40
#     }
41
#     
Raphaël Pinson's avatar
Doc    
Raphaël Pinson committed
42
#   }
43
#
44
define postfix::transport (
Raphaël Pinson's avatar
Raphaël Pinson committed
45
46
47
48
  Optional[String]          $destination = undef,
  Optional[String]          $nexthop=undef,
  Stdlib::Absolutepath      $file='/etc/postfix/transport',
  Enum['present', 'absent'] $ensure='present'
49
) {
50
  include ::postfix::augeas
51

Romain Tartière's avatar
Romain Tartière committed
52
  $smtp_nexthop = (String($nexthop) =~ /\[.*\]/)
53

54
55
  case $ensure {
    'present': {
56
57
58
59
60
61
62
63
      if ($smtp_nexthop) {
        $change_destination = "rm pattern[. = '${name}']/transport"
      } else  {
        if ($destination) {
          $change_destination = "set pattern[. = '${name}']/transport '${destination}'"
        } else {
          $change_destination = "clear pattern[. = '${name}']/transport"
        }
64
65
      }

66
      if ($nexthop) {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        if ($smtp_nexthop) {
          $nexthop_split = split($nexthop, ':')
          $change_nexthop = [
            "rm pattern[. = '${name}']/nexthop",
            "set pattern[. = '${name}']/host '${nexthop_split[0]}'",
            "set pattern[. = '${name}']/port '${nexthop_split[1]}'",
          ]
        } else {
          $change_nexthop = [
            "rm pattern[. = '${name}']/host",
            "rm pattern[. = '${name}']/port",
            "set pattern[. = '${name}']/nexthop '${nexthop}'",
          ]
        }
81
      } else {
82
83
84
85
86
        $change_nexthop = [
          "clear pattern[. = '${name}']/nexthop",
          "rm pattern[. = '${name}']/host",
          "rm pattern[. = '${name}']/port",
        ]
87
      }
88

89
      $changes = flatten([
90
91
92
        "set pattern[. = '${name}'] '${name}'",
        $change_destination,
        $change_nexthop,
93
      ])
94
95
96
97
98
99
100
    }

    'absent': {
      $changes = "rm pattern[. = '${name}']"
    }

    default: {
101
      fail "\$ensure must be either 'present' or 'absent', got '${ensure}'"
102
103
104
105
    }
  }

  augeas {"Postfix transport - ${name}":
106
107
108
    lens    => 'Postfix_Transport.lns',
    incl    => $file,
    changes => $changes,
109
110
111
112
113
114
115
    require => Augeas::Lens['postfix_transport'],
  }

  if defined(Package['postfix']) {
    Package['postfix'] -> Postfix::Transport[$title]
  }

Tobias Wolter's avatar
Tobias Wolter committed
116
117
  if defined(Postfix::Hash[$file]) {
    Postfix::Transport[$title] ~> Postfix::Hash[$file]
118
119
  }
}