Merge pull request #657 from puppetlabs/binford2k-patch-1
[puppet-modules/puppetlabs-apt.git] / manifests / key.pp
1 # == Define: apt::key
2 define apt::key (
3     String $id                           = $title,
4     Enum['present', 'absent'] $ensure    = present,
5     Optional[String] $content            = undef,
6     Optional[String] $source             = undef,
7     String $server                       = $::apt::keyserver,
8     Optional[String] $options            = undef,
9     ) {
10
11   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'])
12
13   if $source {
14     validate_re($source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+'])
15   }
16
17   if $server {
18     validate_re($server,['\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$'])
19   }
20
21   case $ensure {
22     present: {
23       if defined(Anchor["apt_key ${id} absent"]){
24         fail("key with id ${id} already ensured as absent")
25       }
26
27       if !defined(Anchor["apt_key ${id} present"]) {
28         apt_key { $title:
29           ensure  => $ensure,
30           id      => $id,
31           source  => $source,
32           content => $content,
33           server  => $server,
34           options => $options,
35         } -> anchor { "apt_key ${id} present": }
36
37         case $facts['os']['name'] {
38           'Debian': {
39             if versioncmp($facts['os']['release']['full'], '9.0') >= 0 {
40               Apt::Key<| title == $title |> {
41                 require => Package['dirmngr']
42               }
43             }
44           }
45           'Ubuntu': {
46             if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
47               Apt::Key<| title == $title |> {
48                 require => Package['dirmngr']
49               }
50             }
51           }
52           default: { }
53         }
54       }
55     }
56
57     absent: {
58       if defined(Anchor["apt_key ${id} present"]){
59         fail("key with id ${id} already ensured as present")
60       }
61
62       if !defined(Anchor["apt_key ${id} absent"]){
63         apt_key { $title:
64           ensure  => $ensure,
65           id      => $id,
66           source  => $source,
67           content => $content,
68           server  => $server,
69           options => $options,
70         } -> anchor { "apt_key ${id} absent": }
71       }
72     }
73
74     default: {
75       fail "Invalid 'ensure' value '${ensure}' for apt::key"
76     }
77   }
78 }