From 788d8764693aabd0aaf4bdd6b4aa4998307e0d3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= <github+aem1eeshi1@raphink.net>
Date: Wed, 17 May 2017 09:18:17 +0200
Subject: [PATCH] Data types (#169)

* Use data types

* Use data types

* Use data types

* Use data types

* Use data types

* Use data types

* Use data types
---
 manifests/conffile.pp                  | 20 ++----
 manifests/config.pp                    | 12 ++--
 manifests/files.pp                     |  6 +-
 manifests/hash.pp                      | 12 ++--
 manifests/init.pp                      | 86 +++++++++-----------------
 manifests/ldap.pp                      |  6 +-
 manifests/map.pp                       | 16 ++---
 manifests/mta.pp                       | 13 +---
 manifests/satellite.pp                 |  2 +-
 manifests/transport.pp                 | 14 ++---
 manifests/virtual.pp                   | 11 +---
 spec/defines/postfix_conffile_spec.rb  |  2 +-
 spec/defines/postfix_config_spec.rb    |  2 +-
 spec/defines/postfix_hash_spec.rb      |  4 +-
 spec/defines/postfix_map_spec.rb       |  2 +-
 spec/defines/postfix_transport_spec.rb |  4 +-
 spec/defines/postfix_virtual_spec.rb   |  4 +-
 17 files changed, 78 insertions(+), 138 deletions(-)

diff --git a/manifests/conffile.pp b/manifests/conffile.pp
index 6173916..2c32464 100644
--- a/manifests/conffile.pp
+++ b/manifests/conffile.pp
@@ -44,22 +44,15 @@
 # }
 #
 define postfix::conffile (
-  $ensure   = 'present',
-  $source   = undef,
-  $content  = undef,
-  $path     = "/etc/postfix/${name}",
-  $mode     = '0644',
-  $options  = {},
+  Enum['present', 'absent', 'directory'] $ensure   = 'present',
+  Variant[Array[String], String, Undef]  $source   = undef,
+  Optional[String]                       $content  = undef,
+  Stdlib::Absolutepath                   $path     = "/etc/postfix/${name}",
+  String                                 $mode     = '0644',
+  Hash                                   $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_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!'
   }
@@ -68,7 +61,6 @@ define postfix::conffile (
     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\''
   }
diff --git a/manifests/config.pp b/manifests/config.pp
index 3613578..7eb10ae 100644
--- a/manifests/config.pp
+++ b/manifests/config.pp
@@ -27,13 +27,15 @@
 #     ensure => 'blank',
 #   }
 #
-define postfix::config ($value = undef, $ensure = 'present') {
+define postfix::config (
+  Optional[String]                   $value  = undef,
+  Enum['present', 'absent', 'blank'] $ensure = 'present',
+) {
 
-  validate_re($ensure, ['present', 'absent', 'blank'],
-    "\$ensure must be either 'present', 'absent' or 'blank', got '${ensure}'")
   if ($ensure == 'present') {
-    validate_re($value, '^.+$',
-      '$value can not be empty if ensure = present')
+    assert_type(Pattern[/^.+$/], $value) |$e, $a| {
+      fail '$value can not be empty if ensure = present'
+    }
   }
 
   if (!defined(Class['postfix'])) {
diff --git a/manifests/files.pp b/manifests/files.pp
index 448ae56..22956e1 100644
--- a/manifests/files.pp
+++ b/manifests/files.pp
@@ -19,9 +19,9 @@ class postfix::files {
   $use_schleuder       = $postfix::use_schleuder
   $use_sympa           = $postfix::use_sympa
 
-  validate_string($mastercf_source)
-  validate_string($master_smtp)
-  validate_string($master_smtps)
+  assert_type(Optional[String], $mastercf_source)
+  assert_type(Optional[String], $master_smtp)
+  assert_type(Optional[String], $master_smtps)
 
   File {
     replace => $manage_conffiles,
diff --git a/manifests/hash.pp b/manifests/hash.pp
index 117ca65..ddeb3fd 100644
--- a/manifests/hash.pp
+++ b/manifests/hash.pp
@@ -25,17 +25,13 @@
 #   }
 #
 define postfix::hash (
-  $ensure='present',
-  $source=undef,
-  $content=undef,
+  Enum['present', 'absent']             $ensure='present',
+  Variant[Array[String], String, Undef] $source=undef,
+  Variant[Array[String], String, Undef] $content=undef,
 ) {
   include ::postfix::params
 
-  validate_absolute_path($name)
-  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) and !is_array($content) { fail("value for source should be either String type or Array type got ${content}") }
-  validate_re($ensure, ['present', 'absent'],
-    "\$ensure must be either 'present' or 'absent', got '${ensure}'")
+  assert_type(Stdlib::Absolutepath, $name)
 
   if (!defined(Class['postfix'])) {
     fail 'You must define class postfix before using postfix::config!'
diff --git a/manifests/init.pp b/manifests/init.pp
index eac07ff..ce9e800 100644
--- a/manifests/init.pp
+++ b/manifests/init.pp
@@ -72,64 +72,38 @@
 #   }
 #
 class postfix (
-  $alias_maps          = 'hash:/etc/aliases',
-  $inet_interfaces     = 'all',
-  $ldap                = false,
-  $ldap_base           = undef,
-  $ldap_host           = undef,
-  $ldap_options        = undef,
-  $mail_user           = 'vmail',       # postfix_mail_user
-  $mailman             = false,
-  $maincf_source       = "puppet:///modules/${module_name}/main.cf",
-  $manage_conffiles    = true,
-  $manage_mailx        = true,
-  $mastercf_source     = undef,
-  $master_smtp         = undef,         # postfix_master_smtp
-  $master_smtps        = undef,         # postfix_master_smtps
-  $master_submission   = undef,         # postfix_master_submission
-  $mta                 = false,
-  $mydestination       = '$myorigin',   # postfix_mydestination
-  $mynetworks          = '127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128', # postfix_mynetworks
-  $myorigin            = $::fqdn,
-  $relayhost           = undef,         # postfix_relayhost
-  $manage_root_alias   = true,
-  $root_mail_recipient = 'nobody',      # root_mail_recipient
-  $satellite           = false,
-  $smtp_listen         = '127.0.0.1',   # postfix_smtp_listen
-  $use_amavisd         = false,         # postfix_use_amavisd
-  $use_dovecot_lda     = false,         # postfix_use_dovecot_lda
-  $use_schleuder       = false,         # postfix_use_schleuder
-  $use_sympa           = false,         # postfix_use_sympa
-  $postfix_ensure      = 'present',
-  $mailx_ensure        = 'present',
+  String                          $alias_maps          = 'hash:/etc/aliases',
+  String                          $inet_interfaces     = 'all',
+  Boolean                         $ldap                = false,
+  Optional[String]                $ldap_base           = undef,
+  Optional[String]                $ldap_host           = undef,
+  Optional[String]                $ldap_options        = undef,
+  String                          $mail_user           = 'vmail',       # postfix_mail_user
+  Boolean                         $mailman             = false,
+  String                          $maincf_source       = "puppet:///modules/${module_name}/main.cf",
+  Boolean                         $manage_conffiles    = true,
+  Boolean                         $manage_mailx        = true,
+  Optional[String]                $mastercf_source     = undef,
+  Optional[String]                $master_smtp         = undef,         # postfix_master_smtp
+  Optional[String]                $master_smtps        = undef,         # postfix_master_smtps
+  Optional[String]                $master_submission   = undef,         # postfix_master_submission
+  Boolean                         $mta                 = false,
+  String                          $mydestination       = '$myorigin',   # postfix_mydestination
+  String                          $mynetworks          = '127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128', # postfix_mynetworks
+  String                          $myorigin            = $::fqdn,
+  Optional[String]                $relayhost           = undef,         # postfix_relayhost
+  Boolean                         $manage_root_alias   = true,
+  Variant[Array[String], String]  $root_mail_recipient = 'nobody',      # root_mail_recipient
+  Boolean                         $satellite           = false,
+  String                          $smtp_listen         = '127.0.0.1',   # postfix_smtp_listen
+  Boolean                         $use_amavisd         = false,         # postfix_use_amavisd
+  Boolean                         $use_dovecot_lda     = false,         # postfix_use_dovecot_lda
+  Boolean                         $use_schleuder       = false,         # postfix_use_schleuder
+  Boolean                         $use_sympa           = false,         # postfix_use_sympa
+  String                          $postfix_ensure      = 'present',
+  String                          $mailx_ensure        = 'present',
 ) inherits postfix::params {
 
-
-  validate_bool($ldap)
-  validate_bool($mailman)
-  validate_bool($mta)
-  validate_bool($manage_root_alias)
-  validate_bool($manage_mailx)
-  validate_bool($satellite)
-  validate_bool($use_amavisd)
-  validate_bool($use_dovecot_lda)
-  validate_bool($use_schleuder)
-  validate_bool($use_sympa)
-
-  validate_string($alias_maps)
-  validate_string($inet_interfaces)
-  validate_string($mail_user)
-  validate_string($mydestination)
-  validate_string($mynetworks)
-  validate_string($myorigin)
-  validate_string($relayhost)
-  if ! is_array($root_mail_recipient) {
-    validate_string($root_mail_recipient)
-  }
-  validate_string($smtp_listen)
-
-
-
   $_smtp_listen = $mailman ? {
     true    => '0.0.0.0',
     default => $smtp_listen,
diff --git a/manifests/ldap.pp b/manifests/ldap.pp
index ad1a6be..7dd8384 100644
--- a/manifests/ldap.pp
+++ b/manifests/ldap.pp
@@ -15,9 +15,9 @@
 #
 class postfix::ldap {
 
-  validate_string($postfix::ldap_base)
-  validate_string($postfix::ldap_host)
-  validate_string($postfix::ldap_options)
+  assert_type(String, $postfix::ldap_base)
+  assert_type(String, $postfix::ldap_host)
+  assert_type(String, $postfix::ldap_options)
 
   if $::osfamily == 'Debian' {
     package {'postfix-ldap':
diff --git a/manifests/map.pp b/manifests/map.pp
index bc66ecb..f79e54c 100644
--- a/manifests/map.pp
+++ b/manifests/map.pp
@@ -27,20 +27,14 @@
 #   }
 #
 define postfix::map (
-  $ensure = 'present',
-  $source = undef,
-  $content = undef,
-  $type = 'hash',
-  $path = "/etc/postfix/${name}",
+  Enum['present', 'absent']             $ensure = 'present',
+  Variant[Array[String], String, Undef] $source = undef,
+  Variant[Array[String], String, Undef] $content = undef,
+  String                                $type = 'hash',
+  Stdlib::Absolutepath                  $path = "/etc/postfix/${name}",
 ) {
   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) and !is_array($content) { fail("value for source should be either String type or Array type got ${content}") }
-  validate_re($ensure, ['present', 'absent'],
-    "\$ensure must be either 'present' or 'absent', got '${ensure}'")
-
   if (!defined(Class['postfix'])) {
     fail 'You must define class postfix before using postfix::config!'
   }
diff --git a/manifests/mta.pp b/manifests/mta.pp
index 727c4cf..0d742df 100644
--- a/manifests/mta.pp
+++ b/manifests/mta.pp
@@ -26,18 +26,11 @@
 #   }
 #
 class postfix::mta (
-  $mydestination = $postfix::mydestination,
-  $mynetworks    = $postfix::mynetworks,
-  $relayhost     = $postfix::relayhost,
+  Pattern[/^\S+(?:,\s*\S+)*$/]               $mydestination = $postfix::mydestination,
+  Pattern[/^(?:\S+?(?:(?:,\s)|(?:\s))?)*$/]  $mynetworks    = $postfix::mynetworks,
+  Pattern[/^\S+$/]                           $relayhost     = $postfix::relayhost,
 ) {
 
-  validate_re($relayhost, '^\S+$',
-              'Wrong value for $relayhost')
-  validate_re($mydestination, '^\S+(?:,\s*\S+)*$',
-              'Wrong value for $mydestination')
-  validate_re($mynetworks, '^(?:\S+?(?:(?:,\s)|(?:\s))?)*$',
-              'Wrong value for $mynetworks')
-
   # If direct is specified then relayhost should be blank
   if ($relayhost == 'direct') {
     postfix::config { 'relayhost': ensure => 'blank' }
diff --git a/manifests/satellite.pp b/manifests/satellite.pp
index e355827..4e98b5e 100644
--- a/manifests/satellite.pp
+++ b/manifests/satellite.pp
@@ -28,7 +28,7 @@ class postfix::satellite (
   $relayhost     = $postfix::relayhost,
 ) {
 
-  validate_re($postfix::myorigin, '^\S+$')
+  assert_type(Pattern[/^\S+$/], $postfix::myorigin)
 
   class { '::postfix::mta':
     mydestination => $mydestination,
diff --git a/manifests/transport.pp b/manifests/transport.pp
index 363a753..09a5202 100644
--- a/manifests/transport.pp
+++ b/manifests/transport.pp
@@ -35,19 +35,13 @@
 #   }
 #
 define postfix::transport (
-  $destination=undef,
-  $nexthop=undef,
-  $file='/etc/postfix/transport',
-  $ensure='present'
+  Optional[String]          $destination = undef,
+  Optional[String]          $nexthop=undef,
+  Stdlib::Absolutepath      $file='/etc/postfix/transport',
+  Enum['present', 'absent'] $ensure='present'
 ) {
   include ::postfix::augeas
 
-  validate_string($destination)
-  validate_string($nexthop)
-  validate_string($file)
-  validate_absolute_path($file)
-  validate_string($ensure)
-
   case $ensure {
     'present': {
       if ($destination) {
diff --git a/manifests/virtual.pp b/manifests/virtual.pp
index 7b8a150..07bab2c 100644
--- a/manifests/virtual.pp
+++ b/manifests/virtual.pp
@@ -35,17 +35,12 @@
 #   }
 #
 define postfix::virtual (
-  $destination,
-  $file='/etc/postfix/virtual',
-  $ensure='present'
+  String                    $destination,
+  Stdlib::Absolutepath      $file='/etc/postfix/virtual',
+  Enum['present', 'absent'] $ensure='present'
 ) {
   include ::postfix::augeas
 
-  validate_string($destination)
-  validate_string($file)
-  validate_absolute_path($file)
-  validate_string($ensure)
-
   case $ensure {
     'present': {
       $changes = [
diff --git a/spec/defines/postfix_conffile_spec.rb b/spec/defines/postfix_conffile_spec.rb
index 682da71..17308e9 100644
--- a/spec/defines/postfix_conffile_spec.rb
+++ b/spec/defines/postfix_conffile_spec.rb
@@ -31,7 +31,7 @@ describe 'postfix::conffile' do
         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'/)
+          }.to raise_error(Puppet::Error, /got 'running'/)
         end
       end
 
diff --git a/spec/defines/postfix_config_spec.rb b/spec/defines/postfix_config_spec.rb
index deb9630..f1dd18d 100644
--- a/spec/defines/postfix_config_spec.rb
+++ b/spec/defines/postfix_config_spec.rb
@@ -52,7 +52,7 @@ describe 'postfix::config' do
         it 'should fail' do
           expect {
             is_expected.to contain_augeas("set postfix 'foo'")
-          }.to raise_error(Puppet::Error, /must be either 'present', 'absent' or 'blank'/)
+          }.to raise_error(Puppet::Error, /got 'running'/)
         end
       end
 
diff --git a/spec/defines/postfix_hash_spec.rb b/spec/defines/postfix_hash_spec.rb
index 31c14b0..6502d89 100644
--- a/spec/defines/postfix_hash_spec.rb
+++ b/spec/defines/postfix_hash_spec.rb
@@ -31,7 +31,7 @@ describe 'postfix::hash' do
         it 'should fail' do
           expect {
             is_expected.to contain_file('/tmp/foo')
-          }.to raise_error(Puppet::Error, /must be either 'present' or 'absent'/)
+          }.to raise_error(Puppet::Error, /got 'running'/)
         end
       end
 
@@ -40,7 +40,7 @@ describe 'postfix::hash' do
         it 'should fail' do
           expect {
             is_expected.to contain_file('/tmp/foo')
-          }.to raise_error(Puppet::Error, /"foo" is not an absolute path/)
+          }.to raise_error(Puppet::Error, /got 'foo'/)
         end
       end
 
diff --git a/spec/defines/postfix_map_spec.rb b/spec/defines/postfix_map_spec.rb
index 1e3f224..f525a91 100644
--- a/spec/defines/postfix_map_spec.rb
+++ b/spec/defines/postfix_map_spec.rb
@@ -31,7 +31,7 @@ describe 'postfix::map' do
         it 'should fail' do
           expect {
             is_expected.to contain_file('postfix map foo')
-          }.to raise_error(Puppet::Error, /must be either 'present' or 'absent'/)
+          }.to raise_error(Puppet::Error, /got 'running'/)
         end
       end
 
diff --git a/spec/defines/postfix_transport_spec.rb b/spec/defines/postfix_transport_spec.rb
index ce45381..8e1e31c 100644
--- a/spec/defines/postfix_transport_spec.rb
+++ b/spec/defines/postfix_transport_spec.rb
@@ -63,7 +63,7 @@ describe 'postfix::transport' do
         it 'should fail' do
           expect {
             is_expected.to contain_augeas('Postfix transport - foo')
-          }.to raise_error(Puppet::Error, /"baz" is not an absolute path/)
+          }.to raise_error(Puppet::Error, /got 'baz'/)
         end
       end
 
@@ -89,7 +89,7 @@ describe 'postfix::transport' do
         it 'should fail' do
           expect {
             is_expected.to contain_augeas('Postfix transport - foo')
-          }.to raise_error(Puppet::Error, /\$ensure must be either/)
+          }.to raise_error(Puppet::Error, /got 'running'/)
         end
       end
 
diff --git a/spec/defines/postfix_virtual_spec.rb b/spec/defines/postfix_virtual_spec.rb
index d9fb0b1..86d9b1a 100644
--- a/spec/defines/postfix_virtual_spec.rb
+++ b/spec/defines/postfix_virtual_spec.rb
@@ -58,7 +58,7 @@ describe 'postfix::virtual' do
         it 'should fail' do
           expect {
             is_expected.to contain_augeas('Postfix virtual - foo')
-          }.to raise_error(Puppet::Error, /"baz" is not an absolute path/)
+          }.to raise_error(Puppet::Error, /got 'baz'/)
         end
       end
 
@@ -84,7 +84,7 @@ describe 'postfix::virtual' do
         it 'should fail' do
           expect {
             is_expected.to contain_augeas('Postfix virtual - foo')
-          }.to raise_error(Puppet::Error, /\$ensure must be either/)
+          }.to raise_error(Puppet::Error, /got 'running'/)
         end
       end
 
-- 
GitLab