Apply remaining rubocop 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   Optional[Enum['file', 'present', 'absent']] $ensure = file,
25   Optional[String] $source                            = undef,
26   Optional[String] $content                           = undef,
27   Boolean $notify_update                              = true,
28 ) {
29
30   if $content and $source {
31     fail(translate('apt::setting cannot have both content and source'))
32   }
33
34   if !$content and !$source {
35     fail(translate('apt::setting needs either of content or source'))
36   }
37
38   $title_array = split($title, '-')
39   $setting_type = $title_array[0]
40   $base_name = join(delete_at($title_array, 0), '-')
41
42   assert_type(Pattern[/\Aconf\z/, /\Apref\z/, /\Alist\z/], $setting_type) |$a, $b| {
43     fail(translate("apt::setting resource name/title must start with either 'conf-', 'pref-' or 'list-'"))
44   }
45
46   if $priority !~ Integer {
47     # need this to allow zero-padded priority.
48     assert_type(Pattern[/^\d+$/], $priority) |$a, $b| {
49       fail(translate('apt::setting priority must be an integer or a zero-padded integer'))
50     }
51   }
52
53   if ($setting_type == 'list') or ($setting_type == 'pref') {
54     $_priority = ''
55   } else {
56     $_priority = $priority
57   }
58
59   $_path = $::apt::config_files[$setting_type]['path']
60   $_ext  = $::apt::config_files[$setting_type]['ext']
61
62   if $notify_update {
63     $_notify = Class['apt::update']
64   } else {
65     $_notify = undef
66   }
67
68   file { "${_path}/${_priority}${base_name}${_ext}":
69     ensure  => $ensure,
70     owner   => 'root',
71     group   => 'root',
72     content => $content,
73     source  => $source,
74     notify  => $_notify,
75   }
76 }