Commit dff6bfba authored by Greg Cox's avatar Greg Cox
Browse files

Handle [host] vs [host]:port nexthop

parent d73f04db
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -67,14 +67,24 @@ define postfix::transport (
      }

      if ($nexthop) {
        if ($smtp_nexthop) {
          $nexthop_split = split($nexthop, ':')
        # This is some paranoia over splitting.  We can't do a simple split here, as there
        # could be ipv6 addrs in a nexthop's host.  So we're definitely into neeeding a regexp.
        # The regexp needs to be 'the same base' as used in smtp_nexthop above, while also
        # capturing the port.
        # From https://github.com/voxpupuli/puppet-postfix/issues/241, this exists
        # to handle [host]:port nexthop.  Other cases are handled by simply passing nexthop
        # straight through.
        $nexthop_match = $nexthop.match(/(:?\[.*\]):(\d+)/)
        # If this matched, we'll have a length of 3: [whole string, host, port].
        if ($nexthop_match =~ Array and $nexthop_match.length == 3) {
          $change_nexthop = [
            "rm pattern[. = '${name}']/nexthop",
            "set pattern[. = '${name}']/host '${nexthop_split[0]}'",
            "set pattern[. = '${name}']/port '${nexthop_split[1]}'",
            "set pattern[. = '${name}']/host '${nexthop_match[1]}'",
            "set pattern[. = '${name}']/port '${nexthop_match[2]}'",
          ]
        } else {
          # If it didn't match, we just report the nexthop unmodified; remove any breakout of
          # host/port that may have existed.
          $change_nexthop = [
            "rm pattern[. = '${name}']/host",
            "rm pattern[. = '${name}']/port",
+80 −1
Original line number Diff line number Diff line
@@ -101,7 +101,59 @@ describe 'postfix::transport' do
        }
      end

      context 'when overriding default values with [host]' do
      context 'when overriding nexthop with [host]' do
        let(:params) do
          {
            nexthop: '[baz]',
            file: '/tmp/transport',
            ensure: 'present',
          }
        end

        it { is_expected.to contain_class('postfix::augeas') }

        it {
          is_expected.to contain_augeas('Postfix transport - foo').with(
            incl: '/tmp/transport',
            lens: 'Postfix_Transport.lns',
            changes: [
              "set pattern[. = 'foo'] 'foo'",
              "rm pattern[. = 'foo']/transport",
              "rm pattern[. = 'foo']/host",
              "rm pattern[. = 'foo']/port",
              "set pattern[. = 'foo']/nexthop '[baz]'",
            ]
          )
        }
      end

      context 'when overriding nexthop with :[host]' do
        let(:params) do
          {
            nexthop: ':[baz]',
            file: '/tmp/transport',
            ensure: 'present',
          }
        end

        it { is_expected.to contain_class('postfix::augeas') }

        it {
          is_expected.to contain_augeas('Postfix transport - foo').with(
            incl: '/tmp/transport',
            lens: 'Postfix_Transport.lns',
            changes: [
              "set pattern[. = 'foo'] 'foo'",
              "rm pattern[. = 'foo']/transport",
              "rm pattern[. = 'foo']/host",
              "rm pattern[. = 'foo']/port",
              "set pattern[. = 'foo']/nexthop ':[baz]'",
            ]
          )
        }
      end

      context 'when overriding default values with [host]:port' do
        let(:params) do
          {
            destination: 'bar',
@@ -128,6 +180,33 @@ describe 'postfix::transport' do
        }
      end

      context 'when overriding default values with :[host]:port' do
        let(:params) do
          {
            destination: 'bar',
            nexthop: ':[baz]:1234',
            file: '/tmp/transport',
            ensure: 'present',
          }
        end

        it { is_expected.to contain_class('postfix::augeas') }

        it {
          is_expected.to contain_augeas('Postfix transport - foo').with(
            incl: '/tmp/transport',
            lens: 'Postfix_Transport.lns',
            changes: [
              "set pattern[. = 'foo'] 'foo'",
              "rm pattern[. = 'foo']/transport",
              "rm pattern[. = 'foo']/nexthop",
              "set pattern[. = 'foo']/host ':[baz]'",
              "set pattern[. = 'foo']/port '1234'",
            ]
          )
        }
      end

      context 'when ensuring absence' do
        let(:params) do
          {