Skip to content

Commit 22dab0e

Browse files
committedMar 14, 2023
Add support for SLM policies
1 parent d526dfd commit 22dab0e

File tree

16 files changed

+919
-0
lines changed

16 files changed

+919
-0
lines changed
 

‎README.md

+49
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,55 @@ elasticsearch::snapshot_repository { 'backups':
424424
}
425425
```
426426

427+
### SLM (Snapshot Lifecycle Management)
428+
429+
By default SLM use the top-level `elasticsearch::api_*` settings to communicate with Elasticsearch.
430+
The following is an example of how to override these settings:
431+
432+
```puppet
433+
elasticsearch::slm_policy { 'policiyname':
434+
api_protocol => 'https',
435+
api_host => $::ipaddress,
436+
api_port => 9201,
437+
api_timeout => 60,
438+
api_basic_auth_username => 'admin',
439+
api_basic_auth_password => 'adminpassword',
440+
api_ca_file => '/etc/ssl/certs',
441+
api_ca_path => '/etc/pki/certs',
442+
validate_tls => false,
443+
source => 'puppet:///path/to/policy.json',
444+
}
445+
```
446+
447+
#### Add a new SLM policy using a file
448+
449+
This will install and/or replace the SLM ploicy in Elasticsearch:
450+
451+
```puppet
452+
elasticsearch::slm_policy { 'policyname':
453+
source => 'puppet:///path/to/policy.json',
454+
}
455+
```
456+
457+
#### Add a new SLM policy using content
458+
This will install and/or replace ILM policy in Elasticsearch:
459+
460+
```puppet
461+
elasticsearch::slm_policy { 'policyname':
462+
content => {
463+
name => '<backup-{now/d}>',
464+
schedule => '0 30 1 * * ?',
465+
repository => 'backup',
466+
config => { },
467+
retention => {
468+
expire_after => '60d',
469+
min_count => 2,
470+
max_count => 10
471+
}
472+
}
473+
}
474+
```
475+
427476
### ILM (Index Lifecycle Management)
428477

429478
By default ILM use the top-level `elasticsearch::api_*` settings to communicate with Elasticsearch.

‎REFERENCE.md

+184
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [`elasticsearch::plugin`](#elasticsearch--plugin): This define allows you to install arbitrary Elasticsearch plugins either by using the default repositories or by specifying an URL
2323
* [`elasticsearch::role`](#elasticsearch--role): Manage x-pack roles.
2424
* [`elasticsearch::script`](#elasticsearch--script): This define allows you to insert, update or delete scripts that are used within Elasticsearch.
25+
* [`elasticsearch::slm_policy`](#elasticsearch--slm_policy): This define allows you to insert, update or delete Elasticsearch SLM policies. Policy content should be defined through either the `conten
2526
* [`elasticsearch::snapshot_repository`](#elasticsearch--snapshot_repository): This define allows you to insert, update or delete Elasticsearch snapshot repositories.
2627
* [`elasticsearch::template`](#elasticsearch--template): This define allows you to insert, update or delete Elasticsearch index templates. Template content should be defined through either the `c
2728
* [`elasticsearch::user`](#elasticsearch--user): Manages x-pack users.
@@ -38,6 +39,7 @@
3839
* [`elasticsearch_plugin`](#elasticsearch_plugin): Plugin installation type
3940
* [`elasticsearch_role`](#elasticsearch_role): Type to model Elasticsearch roles.
4041
* [`elasticsearch_role_mapping`](#elasticsearch_role_mapping): Type to model Elasticsearch role mappings.
42+
* [`elasticsearch_slm_policy`](#elasticsearch_slm_policy): Manages Elasticsearch SLM policies.
4143
* [`elasticsearch_snapshot_repository`](#elasticsearch_snapshot_repository): Manages Elasticsearch snapshot repositories.
4244
* [`elasticsearch_template`](#elasticsearch_template): Manages Elasticsearch index templates.
4345
* [`elasticsearch_user`](#elasticsearch_user): Type to model Elasticsearch users.
@@ -169,6 +171,7 @@ The following parameters are available in the `elasticsearch` class:
169171
* [`security_logging_source`](#-elasticsearch--security_logging_source)
170172
* [`service_name`](#-elasticsearch--service_name)
171173
* [`service_provider`](#-elasticsearch--service_provider)
174+
* [`slm_policies`](#-elasticsearch--slm_policies)
172175
* [`snapshot_repositories`](#-elasticsearch--snapshot_repositories)
173176
* [`ssl`](#-elasticsearch--ssl)
174177
* [`status`](#-elasticsearch--status)
@@ -706,6 +709,14 @@ Data type: `Enum['init', 'openbsd', 'openrc', 'systemd']`
706709

707710
The service resource type provider to use when managing elasticsearch instances.
708711

712+
##### <a name="-elasticsearch--slm_policies"></a>`slm_policies`
713+
714+
Data type: `Hash`
715+
716+
Define slm_policies via a hash. This is mainly used with Hiera's auto binding.
717+
718+
Default value: `{}`
719+
709720
##### <a name="-elasticsearch--snapshot_repositories"></a>`snapshot_repositories`
710721

711722
Data type: `Hash`
@@ -1798,6 +1809,136 @@ Data type: `String`
17981809

17991810
Puppet source of the script
18001811

1812+
### <a name="elasticsearch--slm_policy"></a>`elasticsearch::slm_policy`
1813+
1814+
This define allows you to insert, update or delete Elasticsearch SLM
1815+
policies.
1816+
1817+
Policy content should be defined through either the `content` parameter
1818+
(when passing a hash or json string) or the `source` parameter (when passing
1819+
the puppet file URI to a policy json file).
1820+
1821+
#### Parameters
1822+
1823+
The following parameters are available in the `elasticsearch::slm_policy` defined type:
1824+
1825+
* [`ensure`](#-elasticsearch--slm_policy--ensure)
1826+
* [`api_basic_auth_password`](#-elasticsearch--slm_policy--api_basic_auth_password)
1827+
* [`api_basic_auth_username`](#-elasticsearch--slm_policy--api_basic_auth_username)
1828+
* [`api_ca_file`](#-elasticsearch--slm_policy--api_ca_file)
1829+
* [`api_ca_path`](#-elasticsearch--slm_policy--api_ca_path)
1830+
* [`api_host`](#-elasticsearch--slm_policy--api_host)
1831+
* [`api_port`](#-elasticsearch--slm_policy--api_port)
1832+
* [`api_protocol`](#-elasticsearch--slm_policy--api_protocol)
1833+
* [`api_timeout`](#-elasticsearch--slm_policy--api_timeout)
1834+
* [`content`](#-elasticsearch--slm_policy--content)
1835+
* [`source`](#-elasticsearch--slm_policy--source)
1836+
* [`validate_tls`](#-elasticsearch--slm_policy--validate_tls)
1837+
1838+
##### <a name="-elasticsearch--slm_policy--ensure"></a>`ensure`
1839+
1840+
Data type: `Enum['absent', 'present']`
1841+
1842+
Controls whether the named SLM policy should be present or absent in the
1843+
cluster.
1844+
1845+
Default value: `'present'`
1846+
1847+
##### <a name="-elasticsearch--slm_policy--api_basic_auth_password"></a>`api_basic_auth_password`
1848+
1849+
Data type: `Optional[String]`
1850+
1851+
HTTP basic auth password to use when communicating over the Elasticsearch
1852+
API.
1853+
1854+
Default value: `$elasticsearch::api_basic_auth_password`
1855+
1856+
##### <a name="-elasticsearch--slm_policy--api_basic_auth_username"></a>`api_basic_auth_username`
1857+
1858+
Data type: `Optional[String]`
1859+
1860+
HTTP basic auth username to use when communicating over the Elasticsearch
1861+
API.
1862+
1863+
Default value: `$elasticsearch::api_basic_auth_username`
1864+
1865+
##### <a name="-elasticsearch--slm_policy--api_ca_file"></a>`api_ca_file`
1866+
1867+
Data type: `Optional[Stdlib::Absolutepath]`
1868+
1869+
Path to a CA file which will be used to validate server certs when
1870+
communicating with the Elasticsearch API over HTTPS.
1871+
1872+
Default value: `$elasticsearch::api_ca_file`
1873+
1874+
##### <a name="-elasticsearch--slm_policy--api_ca_path"></a>`api_ca_path`
1875+
1876+
Data type: `Optional[Stdlib::Absolutepath]`
1877+
1878+
Path to a directory with CA files which will be used to validate server
1879+
certs when communicating with the Elasticsearch API over HTTPS.
1880+
1881+
Default value: `$elasticsearch::api_ca_path`
1882+
1883+
##### <a name="-elasticsearch--slm_policy--api_host"></a>`api_host`
1884+
1885+
Data type: `String`
1886+
1887+
Host name or IP address of the ES instance to connect to.
1888+
1889+
Default value: `$elasticsearch::api_host`
1890+
1891+
##### <a name="-elasticsearch--slm_policy--api_port"></a>`api_port`
1892+
1893+
Data type: `Integer[0, 65535]`
1894+
1895+
Port number of the ES instance to connect to
1896+
1897+
Default value: `$elasticsearch::api_port`
1898+
1899+
##### <a name="-elasticsearch--slm_policy--api_protocol"></a>`api_protocol`
1900+
1901+
Data type: `Enum['http', 'https']`
1902+
1903+
Protocol that should be used to connect to the Elasticsearch API.
1904+
1905+
Default value: `$elasticsearch::api_protocol`
1906+
1907+
##### <a name="-elasticsearch--slm_policy--api_timeout"></a>`api_timeout`
1908+
1909+
Data type: `Integer`
1910+
1911+
Timeout period (in seconds) for the Elasticsearch API.
1912+
1913+
Default value: `$elasticsearch::api_timeout`
1914+
1915+
##### <a name="-elasticsearch--slm_policy--content"></a>`content`
1916+
1917+
Data type: `Optional[Variant[String, Hash]]`
1918+
1919+
Contents of the policy. Can be either a puppet hash or a string containing
1920+
JSON.
1921+
1922+
Default value: `undef`
1923+
1924+
##### <a name="-elasticsearch--slm_policy--source"></a>`source`
1925+
1926+
Data type: `Optional[String]`
1927+
1928+
Source path for the policy file. Can be any value similar to `source`
1929+
values for `file` resources.
1930+
1931+
Default value: `undef`
1932+
1933+
##### <a name="-elasticsearch--slm_policy--validate_tls"></a>`validate_tls`
1934+
1935+
Data type: `Boolean`
1936+
1937+
Determines whether the validity of SSL/TLS certificates received from the
1938+
Elasticsearch API should be verified or ignored.
1939+
1940+
Default value: `$elasticsearch::validate_tls`
1941+
18011942
### <a name="elasticsearch--snapshot_repository"></a>`elasticsearch::snapshot_repository`
18021943

18031944
This define allows you to insert, update or delete Elasticsearch snapshot
@@ -2598,6 +2739,49 @@ Role name.
25982739
The specific backend to use for this `elasticsearch_role_mapping` resource. You will seldom need to specify this ---
25992740
Puppet will usually discover the appropriate provider for your platform.
26002741

2742+
### <a name="elasticsearch_slm_policy"></a>`elasticsearch_slm_policy`
2743+
2744+
Manages Elasticsearch SLM policies.
2745+
2746+
#### Properties
2747+
2748+
The following properties are available in the `elasticsearch_slm_policy` type.
2749+
2750+
##### `content`
2751+
2752+
Structured content of policy.
2753+
2754+
##### `ensure`
2755+
2756+
Valid values: `present`, `absent`
2757+
2758+
The basic property that the resource should be in.
2759+
2760+
Default value: `present`
2761+
2762+
#### Parameters
2763+
2764+
The following parameters are available in the `elasticsearch_slm_policy` type.
2765+
2766+
* [`name`](#-elasticsearch_slm_policy--name)
2767+
* [`provider`](#-elasticsearch_slm_policy--provider)
2768+
* [`source`](#-elasticsearch_slm_policy--source)
2769+
2770+
##### <a name="-elasticsearch_slm_policy--name"></a>`name`
2771+
2772+
namevar
2773+
2774+
Policy name.
2775+
2776+
##### <a name="-elasticsearch_slm_policy--provider"></a>`provider`
2777+
2778+
The specific backend to use for this `elasticsearch_slm_policy` resource. You will seldom need to specify this ---
2779+
Puppet will usually discover the appropriate provider for your platform.
2780+
2781+
##### <a name="-elasticsearch_slm_policy--source"></a>`source`
2782+
2783+
Puppet source to file containing SLM policy contents.
2784+
26012785
### <a name="elasticsearch_snapshot_repository"></a>`elasticsearch_snapshot_repository`
26022786

26032787
Manages Elasticsearch snapshot repositories.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
4+
5+
require 'puppet/provider/elastic_rest'
6+
7+
require 'puppet_x/elastic/deep_to_i'
8+
require 'puppet_x/elastic/deep_to_s'
9+
10+
Puppet::Type.type(:elasticsearch_slm_policy).provide(
11+
:ruby,
12+
parent: Puppet::Provider::ElasticREST,
13+
api_uri: '_slm/policy',
14+
metadata: :content,
15+
metadata_pipeline: [
16+
# Since API returns actual policy keyed under policy.
17+
->(data) { data['policy'] },
18+
->(data) { Puppet_X::Elastic.deep_to_s data },
19+
->(data) { Puppet_X::Elastic.deep_to_i data }
20+
]
21+
) do
22+
desc 'A REST API based provider to manage Elasticsearch ILM policies.'
23+
24+
mk_resource_methods
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# frozen_string_literal: true
2+
3+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
4+
5+
require 'puppet/file_serving/content'
6+
require 'puppet/file_serving/metadata'
7+
8+
require 'puppet_x/elastic/deep_implode'
9+
require 'puppet_x/elastic/deep_to_i'
10+
require 'puppet_x/elastic/deep_to_s'
11+
require 'puppet_x/elastic/elasticsearch_rest_resource'
12+
13+
Puppet::Type.newtype(:elasticsearch_slm_policy) do
14+
extend ElasticsearchRESTResource
15+
16+
desc 'Manages Elasticsearch SLM policies.'
17+
18+
ensurable
19+
20+
newparam(:name, namevar: true) do
21+
desc 'Policy name.'
22+
end
23+
24+
newproperty(:content) do
25+
desc 'Structured content of policy.'
26+
27+
validate do |value|
28+
raise Puppet::Error, 'hash expected' unless value.is_a? Hash
29+
end
30+
31+
def insync?(value)
32+
Puppet_X::Elastic.deep_implode(value) == \
33+
Puppet_X::Elastic.deep_implode(should)
34+
end
35+
36+
munge do |value|
37+
Puppet_X::Elastic.deep_to_i(Puppet_X::Elastic.deep_to_s(value))
38+
end
39+
end
40+
41+
newparam(:source) do
42+
desc 'Puppet source to file containing SLM policy contents.'
43+
44+
validate do |value|
45+
raise Puppet::Error, 'string expected' unless value.is_a? String
46+
end
47+
end
48+
49+
# rubocop:disable Style/SignalException
50+
validate do
51+
# Ensure that at least one source of ILM policy content has been provided
52+
if self[:ensure] == :present
53+
fail Puppet::ParseError, '"content" or "source" required' \
54+
if self[:content].nil? && self[:source].nil?
55+
56+
if !self[:content].nil? && !self[:source].nil?
57+
fail(
58+
Puppet::ParseError,
59+
"'content' and 'source' cannot be simultaneously defined"
60+
)
61+
end
62+
end
63+
64+
# If a source was passed, retrieve the source content from Puppet's
65+
# FileServing indirection and set the content property
66+
unless self[:source].nil?
67+
fail(format('Could not retrieve source %s', self[:source])) unless Puppet::FileServing::Metadata.indirection.find(self[:source])
68+
69+
tmp = if !catalog.nil? \
70+
&& catalog.respond_to?(:environment_instance)
71+
Puppet::FileServing::Content.indirection.find(
72+
self[:source],
73+
environment: catalog.environment_instance
74+
)
75+
else
76+
Puppet::FileServing::Content.indirection.find(self[:source])
77+
end
78+
79+
fail(format('Could not find any content at %s', self[:source])) unless tmp
80+
81+
self[:content] = PSON.load(tmp.content)
82+
end
83+
end
84+
# rubocop:enable Style/SignalException
85+
end

0 commit comments

Comments
 (0)
Please sign in to comment.