50f70dd1f5ccc091b6974fbb8c9bb21da70dbd1c
[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']['major'], '9') >= 0 {
40               ensure_packages(['dirmngr'])
41               Apt::Key<| title == $title |>
42             }
43           }
44           'Ubuntu': {
45             if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
46               ensure_packages(['dirmngr'])
47               Apt::Key<| title == $title |>
48             }
49           }
50           default: { }
51         }
52       }
53     }
54
55     absent: {
56       if defined(Anchor["apt_key ${id} present"]){
57         fail("key with id ${id} already ensured as present")
58       }
59
60       if !defined(Anchor["apt_key ${id} absent"]){
61         apt_key { $title:
62           ensure  => $ensure,
63           id      => $id,
64           source  => $source,
65           content => $content,
66           server  => $server,
67           options => $options,
68         } -> anchor { "apt_key ${id} absent": }
69       }
70     }
71
72     default: {
73       fail "Invalid 'ensure' value '${ensure}' for apt::key"
74     }
75   }
76 }