Merge pull request #789 from david22swan/FM-7316
[puppet-modules/puppetlabs-apt.git] / manifests / key.pp
index dfa1daf2b61935886b79cf22b5876511807397c4..4564c6fdf19074c456ecff2c8697bd1bd66d2ace 100644 (file)
@@ -1,27 +1,17 @@
 # == Define: apt::key
 define apt::key (
-    String $id                           = $title,
-    Enum['present', 'absent'] $ensure    = present,
-    Optional[String] $content            = undef,
-    Optional[String] $source             = undef,
-    String $server                       = $::apt::keyserver,
-    Optional[String] $options            = undef,
-    ) {
-
-  validate_re($id, ['\A(0x)?[0-9a-fA-F]{8}\Z', '\A(0x)?[0-9a-fA-F]{16}\Z', '\A(0x)?[0-9a-fA-F]{40}\Z'])
-
-  if $source {
-    validate_re($source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+'])
-  }
-
-  if $server {
-    validate_re($server,['\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$'])
-  }
+  Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/, /\A(0x)?[0-9a-fA-F]{16}\Z/, /\A(0x)?[0-9a-fA-F]{40}\Z/] $id = $title,
+  Enum['present', 'absent'] $ensure                                                              = present,
+  Optional[String] $content                                                                      = undef,
+  Optional[Pattern[/\Ahttps?:\/\//, /\Aftp:\/\//, /\A\/\w+/]] $source                            = undef,
+  Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/] $server  = $::apt::keyserver,
+  Optional[String] $options                                                                      = undef,
+  ) {
 
   case $ensure {
     present: {
       if defined(Anchor["apt_key ${id} absent"]){
-        fail("key with id ${id} already ensured as absent")
+        fail(translate('key with id %{_id} already ensured as absent'),{'_id' => id})
       }
 
       if !defined(Anchor["apt_key ${id} present"]) {
@@ -33,12 +23,28 @@ define apt::key (
           server  => $server,
           options => $options,
         } -> anchor { "apt_key ${id} present": }
+
+        case $facts['os']['name'] {
+          'Debian': {
+            if versioncmp($facts['os']['release']['major'], '9') >= 0 {
+              ensure_packages(['dirmngr'])
+              Apt::Key<| title == $title |>
+            }
+          }
+          'Ubuntu': {
+            if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
+              ensure_packages(['dirmngr'])
+              Apt::Key<| title == $title |>
+            }
+          }
+          default: { }
+        }
       }
     }
 
     absent: {
       if defined(Anchor["apt_key ${id} present"]){
-        fail("key with id ${id} already ensured as present")
+        fail(translate('key with id %{_id} already ensured as present', {'_id' => id}))
       }
 
       if !defined(Anchor["apt_key ${id} absent"]){
@@ -54,7 +60,7 @@ define apt::key (
     }
 
     default: {
-      fail "Invalid 'ensure' value '${ensure}' for apt::key"
+      fail translate('Invalid \'ensure\' value \'%{_ensure}\' for apt::key', {'_ensure' => ensure})
     }
   }
 }