Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / concat / lib / puppet / type / concat_fragment.rb
1 Puppet::Type.newtype(:concat_fragment) do
2   @doc = <<-DOC
3     @summary
4       Manages the fragment.
5
6     @example
7       # The example is based on exported resources.
8
9       concat_fragment { \"uniqe_name_${::fqdn}\":
10         tag => 'unique_name',
11         order => 10, # Optional. Default to 10
12         content => 'some content' # OR
13         # content => template('template.erb')
14         source  => 'puppet:///path/to/file'
15       }
16   DOC
17
18   newparam(:name, namevar: true) do
19     desc 'Name of resource.'
20   end
21
22   newparam(:target) do
23     desc <<-DOC
24       Required. Specifies the destination file of the fragment. Valid options: a string containing the path or title of the parent
25       concat_file resource.
26     DOC
27
28     validate do |value|
29       raise ArgumentError, _('Target must be a String') unless value.is_a?(String)
30     end
31   end
32
33   newparam(:content) do
34     desc <<-DOC
35       Supplies the content of the fragment. Note: You must supply either a content parameter or a source parameter. Valid options: a string
36     DOC
37
38     validate do |value|
39       raise ArgumentError, _('Content must be a String') unless value.is_a?(String)
40     end
41   end
42
43   newparam(:source) do
44     desc <<-DOC
45       Specifies a file to read into the content of the fragment. Note: You must supply either a content parameter or a source parameter.
46       Valid options: a string or an array, containing one or more Puppet URLs.
47     DOC
48
49     validate do |value|
50       raise ArgumentError, _('Content must be a String or Array') unless [String, Array].include?(value.class)
51     end
52   end
53
54   newparam(:order) do
55     desc <<-DOC
56       Reorders your fragments within the destination file. Fragments that share the same order number are ordered by name. The string
57       option is recommended.
58     DOC
59
60     defaultto '10'
61     validate do |val|
62       raise Puppet::ParseError, _('$order is not a string or integer.') unless val.is_a?(String) || val.is_a?(Integer)
63       raise Puppet::ParseError, _('Order cannot contain \'/\', \':\', or \'\\n\'.') if val.to_s =~ %r{[:\n\/]}
64     end
65   end
66
67   newparam(:tag) do
68     desc 'Specifies a unique tag to be used by concat_file to reference and collect content.'
69   end
70
71   autorequire(:file) do
72     found = catalog.resources.select do |resource|
73       next unless resource.is_a?(Puppet::Type.type(:concat_file))
74
75       resource[:path] == self[:target] || resource.title == self[:target] ||
76         (resource[:tag] && resource[:tag] == self[:tag])
77     end
78
79     if found.empty?
80       tag_message = (self[:tag]) ? "or tag '#{self[:tag]} " : ''
81       warning "Target Concat_file with path or title '#{self[:target]}' #{tag_message}not found in the catalog"
82     end
83   end
84
85   validate do
86     # Check if target is set
87     raise Puppet::ParseError, _("No 'target' or 'tag' set") unless self[:target] || self[:tag]
88
89     # Check if either source or content is set. raise error if none is set
90     raise Puppet::ParseError, _("Set either 'source' or 'content'") if self[:source].nil? && self[:content].nil?
91
92     # Check if both are set, if so rais error
93     raise Puppet::ParseError, _("Can't use 'source' and 'content' at the same time") if !self[:source].nil? && !self[:content].nil?
94   end
95 end