(GH-cat-9) syntax:hiera:yaml fixes
[puppet-modules/puppetlabs-apt.git] / manifests / setting.pp
1 # @summary Manages Apt configuration files.
2 #
3 # @see https://docs.puppetlabs.com/references/latest/type.html#file-attributes for more information on source and content parameters
4 #
5 # @param priority
6 #   Determines the order in which Apt processes the configuration file. Files with higher priority numbers are loaded first.
7 #
8 # @param ensure
9 #   Specifies whether the file should exist. Valid options: 'present', 'absent', and 'file'.
10 #
11 # @param source
12 #   Required, unless `content` is set. Specifies a source file to supply the content of the configuration file. Cannot be used in combination 
13 #   with `content`. Valid options: see link above for Puppet's native file type source attribute.
14 #
15 # @param content
16 #   Required, unless `source` is set. Directly supplies content for the configuration file. Cannot be used in combination with `source`. Valid 
17 #   options: see link above for Puppet's native file type content attribute.
18 #
19 # @param notify_update
20 #   Specifies whether to trigger an `apt-get update` run.
21 #
22 define apt::setting (
23   Variant[String, Integer, Array] $priority           = 50,
24   Enum['file', 'present', 'absent'] $ensure           = file,
25   Optional[String] $source                            = undef,
26   Optional[String] $content                           = undef,
27   Boolean $notify_update                              = true,
28 ) {
29   if $content and $source {
30     fail('apt::setting cannot have both content and source')
31   }
32
33   if !$content and !$source {
34     fail('apt::setting needs either of content or source')
35   }
36
37   $title_array = split($title, '-')
38   $setting_type = $title_array[0]
39   $base_name = join(delete_at($title_array, 0), '-')
40
41   assert_type(Pattern[/\Aconf\z/, /\Apref\z/, /\Alist\z/], $setting_type) |$a, $b| {
42     fail("apt::setting resource name/title must start with either 'conf-', 'pref-' or 'list-'")
43   }
44
45   if $priority !~ Integer {
46     # need this to allow zero-padded priority.
47     assert_type(Pattern[/^\d+$/], $priority) |$a, $b| {
48       fail('apt::setting priority must be an integer or a zero-padded integer')
49     }
50   }
51
52   if ($setting_type == 'list') or ($setting_type == 'pref') {
53     $_priority = ''
54   } else {
55     $_priority = $priority
56   }
57
58   $_path = $::apt::config_files[$setting_type]['path']
59   $_ext  = $::apt::config_files[$setting_type]['ext']
60
61   if $notify_update {
62     $_notify = Class['apt::update']
63   } else {
64     $_notify = undef
65   }
66
67   file { "${_path}/${_priority}${base_name}${_ext}":
68     ensure  => $ensure,
69     owner   => 'root',
70     group   => 'root',
71     content => $content,
72     source  => $source,
73     notify  => $_notify,
74   }
75 }