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