Skip to content

Commit

Permalink
fix #704 Tune icinga::icinga2_attributes function call
Browse files Browse the repository at this point in the history
  • Loading branch information
lbetz committed Jun 27, 2022
1 parent 88e0570 commit f0d3536
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 94 deletions.
68 changes: 42 additions & 26 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ start on boot and will be restarted if stopped.
* [`icinga2::icinga2_attributes`](#icinga2icinga2_attributes): Calls the simple parser to decide what to quote.
For more information, see lib/puppet_x/icinga2/utils.rb.
* [`icinga2::icinga2_ticket_id`](#icinga2icinga2_ticket_id): Summarise what the function does here
* [`icinga2_attributes`](#icinga2_attributes): DEPRECATED. Use the namespaced function [`icinga2::icinga2_attributes`](#icinga2_attributes) instead.
* [`icinga2_ticket_id`](#icinga2_ticket_id): DEPRECATED. Use the namespaced function [`icinga2::icinga2_ticket_id`](#icinga2_ticket_id) instead.
* [`icinga2::parse`](#icinga2parse): This function parse icinga object attributes.

### Data types

Expand Down Expand Up @@ -5258,18 +5257,35 @@ Type: Ruby 4.x API
Calls the simple parser to decide what to quote.
For more information, see lib/puppet_x/icinga2/utils.rb.

#### `icinga2::icinga2_attributes(Any *$args)`
#### `icinga2::icinga2_attributes(Hash $attrs, Array $globals, Hash $constants, Optional[Numeric] $indent)`

The icinga2::icinga2_attributes function.

Returns: `Data type` Describe what the function returns here
Returns: `String` Parsed attributes as String.

##### `*args`
##### `attrs`

Data type: `Any`
Data type: `Hash`

Object attributes to parse.

##### `globals`

Data type: `Array`

A list of addational reserved words.

##### `constants`

Data type: `Hash`

A hash of additional constants.

The original array of arguments. Port this to individually managed params
to get the full benefit of the modern function API.
##### `indent`

Data type: `Optional[Numeric]`

Indent to use.

### <a name="icinga2icinga2_ticket_id"></a>`icinga2::icinga2_ticket_id`

Expand All @@ -5295,41 +5311,41 @@ Data type: `Variant[String, Sensitive[String]]`

The ticket salt of the Icinga CA.

### <a name="icinga2_attributes"></a>`icinga2_attributes`
### <a name="icinga2parse"></a>`icinga2::parse`

Type: Ruby 4.x API
Type: Puppet Language

DEPRECATED. Use the namespaced function [`icinga2::icinga2_attributes`](#icinga2_attributes) instead.
This function parse icinga object attributes.

#### `icinga2_attributes(Any *$args)`
#### `icinga2::parse(Hash[String, Any] $attrs, Integer $indent = 0, Array[String] $reserved = [], Hash[String, Any] $constants = {})`

The icinga2_attributes function.
The icinga2::parse function.

Returns: `Data type` Describe what the function returns here
Returns: `Any`

##### `*args`
##### `attrs`

Data type: `Any`
Data type: `Hash[String, Any]`

The original array of arguments.

### <a name="icinga2_ticket_id"></a>`icinga2_ticket_id`

Type: Ruby 4.x API
##### `indent`

DEPRECATED. Use the namespaced function [`icinga2::icinga2_ticket_id`](#icinga2_ticket_id) instead.
Data type: `Integer`

#### `icinga2_ticket_id(Any *$args)`

The icinga2_ticket_id function.

Returns: `String` Calculated ticket to receive a certificate.
##### `reserved`

Data type: `Array[String]`



##### `constants`

##### `*args`
Data type: `Hash[String, Any]`

Data type: `Any`

The original array of arguments.

## Data types

Expand Down
28 changes: 28 additions & 0 deletions functions/parse.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @summary
# This function parse icinga object attributes.
#
function icinga2::parse(
Hash[String, Any] $attrs,
Integer $indent = 0,
Array[String] $reserved = [],
Hash[String, Any] $constants = {},
) {
# @param attr
# Object attributes to parse.
#
# @param indent
# Indent to use.
#
# @param reserved
# A list of addational reserved words.
#
# @param constants
# A hash of additional constants.
#
icinga2::icinga2_attributes(
$attrs,
concat($::icinga2::globals::reserved, $reserved),
merge($::icinga2::_constants, $constants),
$indent
)
}
52 changes: 21 additions & 31 deletions lib/puppet/functions/icinga2/icinga2_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,30 @@
# For more information, see lib/puppet_x/icinga2/utils.rb.
#
Puppet::Functions.create_function(:'icinga2::icinga2_attributes') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
# @param attrs
# Object attributes to parse.
#
# @return [Data type]
# Describe what the function returns here
# @param globals
# A list of addational reserved words.
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
# @param constants
# A hash of additional constants.
#
# @return [String]
# Parsed attributes as String.
#
# @param indent
# Indent to use.
#
dispatch :icinga2_attributes do
required_param 'Hash', :attrs
required_param 'Array', :globals
required_param 'Hash', :constants
optional_param 'Numeric', :indent
return_type 'String'
end

def default_impl(*args)
raise Puppet::ParseError, 'icinga2::atributes(): Must provide at least one argument.' if args.length > 4 || args.empty?

indent = if args[1]
args[1]
else
0
end

globals = if args[2]
closure_scope['::icinga2::_reserved'].concat(args[2])
else
closure_scope['::icinga2::_reserved']
end

constants = if args[3]
closure_scope['::icinga2::_constants'].merge(args[3])
else
closure_scope['::icinga2::_constants']
end

Puppet::Icinga2::Utils.attributes(args[0], globals, constants, indent)
def icinga2_attributes(attrs, globals, constants, indent = 0)
Puppet::Icinga2::Utils.attributes(attrs, globals, constants, indent)
end
end
16 changes: 0 additions & 16 deletions lib/puppet/functions/icinga2_attributes.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/puppet/functions/icinga2_ticket_id.rb

This file was deleted.

4 changes: 2 additions & 2 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
$purge_features = $::icinga2::purge_features

if $::facts['kernel'] != 'windows' {
$template_constants = icinga2::icinga2_attributes($constants)
$template_constants = icinga2::parse($constants)
$template_mainconfig = template('icinga2/icinga2.conf.erb')
$file_permissions = '0640'
} else {
$template_constants = regsubst(icinga2::icinga2_attributes($constants), '\n', "\r\n", 'EMG')
$template_constants = regsubst(icinga2::parse($constants), '\n', "\r\n", 'EMG')
$template_mainconfig = regsubst(template('icinga2/icinga2.conf.erb'), '\n', "\r\n", 'EMG')
$file_permissions = undef
}
Expand Down
43 changes: 43 additions & 0 deletions templates/object.conf.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<%- | Hash $attrs,
Array $attrs_list,
Variant[String, Boolean] $apply,
Optional[String] $apply_target = undef,
Variant[String, Boolean] $prefix,
String $object_type,
String $object_name,
Boolean $template,
Array $import,
| -%>

<% if $apply =~ String { %>apply <%= $object_type -%>
<% if $prefix { -%>
<% if $prefix =~ String { %> "<%= $prefix %>"<% } else { -%>
<% if $object_name in $icinga2::_constants { -%>
<%= $object_name -%>
<% } else { -%>
"<%= $object_name -%>"<% } -%>
<% } -%>
<% } -%>
for (<%= $apply %>)<% if $apply_target { %> to <%= $apply_target %><% } %> {
<% } else { -%>
<% if $apply { %>apply<% } else { -%>
<% if $template { %>template<% } else { %>object<% } -%>
<% } %> <%= $object_type -%>
<% if $object_name in $icinga2::_constants { -%>
<%= $object_name -%>
<% } else { -%>
"<%= $object_name %>"<% } -%>
<% if $apply and $apply_target { %> to <%= $apply_target %><% } %> {
<% } -%>
<% $import.each |$i| { -%>
import "<%= $i %>"
<% } -%>
<% unless $import =~ Array[Data,0,0] { %><%= "\n" %><% } -%>
<% if $apply =~String and $apply =~ /^([A-Za-z_]+)\s+in\s+.+$/ { -%>
<%= icinga2::parse($attrs, 2, $attrs_list, {$1=>{}}) -%>
<% } elsif $apply =~ String and $apply =~ /^([A-Za-z_]+)\s+=>\s+([A-Za-z_]+)\s+in\s+.+$/ { -%>
<%= icinga2::parse($attrs, 2, $attrs_list+[$1], {$2=>{}}) -%>
<% } else { -%>
<%= icinga2::parse($attrs, 2, $attrs_list) -%>
<% } -%>
}
6 changes: 3 additions & 3 deletions templates/object.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<% end -%>
<% unless @import.empty? %><%= "\n" %><% end -%>
<% if @apply.is_a?(String) and @apply =~ /^([A-Za-z_]+)\s+in\s+.+$/ -%>
<%= scope.call_function('icinga2::icinga2_attributes', [@_attrs,2,@attrs_list,{$1=>{}}]) -%>
<%= scope.call_function('icinga2::parse', [@_attrs,2,@attrs_list,{$1=>{}}]) -%>
<% elsif @apply.is_a?(String) and @apply =~ /^([A-Za-z_]+)\s+=>\s+([A-Za-z_]+)\s+in\s+.+$/ -%>
<%= scope.call_function('icinga2::icinga2_attributes', [@_attrs,2,@attrs_list.concat([$1]),{$2=>{}}]) -%>
<%= scope.call_function('icinga2::parse', [@_attrs,2,@attrs_list.concat([$1]),{$2=>{}}]) -%>
<% else -%>
<%= scope.call_function('icinga2::icinga2_attributes', [@_attrs,2,@attrs_list]) -%>
<%= scope.call_function('icinga2::parse', [@_attrs,2,@attrs_list]) -%>
<% end -%>
}

0 comments on commit f0d3536

Please sign in to comment.