Merge pull request #657 from puppetlabs/binford2k-patch-1
[puppet-modules/puppetlabs-apt.git] / manifests / setting.pp
1 define apt::setting (
2   Variant[String, Integer, Array] $priority           = 50,
3   Optional[Enum['file', 'present', 'absent']] $ensure = file,
4   Optional[String] $source                            = undef,
5   Optional[String] $content                           = undef,
6   Boolean $notify_update                              = true,
7 ) {
8
9   if $content and $source {
10     fail('apt::setting cannot have both content and source')
11   }
12
13   if !$content and !$source {
14     fail('apt::setting needs either of content or source')
15   }
16
17   $title_array = split($title, '-')
18   $setting_type = $title_array[0]
19   $base_name = join(delete_at($title_array, 0), '-')
20
21   validate_re($setting_type, ['\Aconf\z', '\Apref\z', '\Alist\z'], "apt::setting resource name/title must start with either 'conf-', 'pref-' or 'list-'")
22
23   unless is_integer($priority) {
24     # need this to allow zero-padded priority.
25     validate_re($priority, '^\d+$', 'apt::setting priority must be an integer or a zero-padded integer')
26   }
27
28   if ($setting_type == 'list') or ($setting_type == 'pref') {
29     $_priority = ''
30   } else {
31     $_priority = $priority
32   }
33
34   $_path = $::apt::config_files[$setting_type]['path']
35   $_ext  = $::apt::config_files[$setting_type]['ext']
36
37   if $notify_update {
38     $_notify = Class['apt::update']
39   } else {
40     $_notify = undef
41   }
42
43   file { "${_path}/${_priority}${base_name}${_ext}":
44     ensure  => $ensure,
45     owner   => 'root',
46     group   => 'root',
47     mode    => '0644',
48     content => $content,
49     source  => $source,
50     notify  => $_notify,
51   }
52 }