Skip to content

Commit

Permalink
Sync bitbucket and GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
carchi8py committed Jan 2, 2024
1 parent ae0e9d6 commit 17caa5b
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ The following modules do not have REST equivalent APIs. They will stop working o
- na_ontap_cli_timeout - REST only support for setting CLI inactivity timeout value, requires ONTAP 9.6 or later.
- na_ontap_cifs_unix_symlink_mapping - REST only support for managing UNIX symbolic link mapping for CIFS clients, requires ONTAP 9.6 or later.

### New Options
- na_ontap_ems_destination - new options `syslog`, `port`, `transport`, `message_format`, `timestamp_format_override` and `hostname_format_override` added in REST, requires ONTAP 9.12.1 or later.

## 22.8.3

### Bug Fixes
Expand Down
2 changes: 2 additions & 0 deletions changelogs/fragments/DEVOPS-6524.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- na_ontap_ems_destination - new options `syslog`, `port`, `transport`, `message_format`, `timestamp_format_override` and `hostname_format_override` added in REST, requires ONTAP 9.12.1 or later.
117 changes: 110 additions & 7 deletions plugins/modules/na_ontap_ems_destination.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python

# (c) 2022, NetApp, Inc
# (c) 2023, NetApp, Inc
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

'''
Expand Down Expand Up @@ -60,7 +60,42 @@
required: false
type: str
version_added: 22.8.0
syslog:
description:
- The parameter is specified when the EMS destination type is C(syslog).
required: false
version_added: 22.9.0
type: dict
suboptions:
transport:
choices: [udp_unencrypted, tcp_unencrypted, tcp_encrypted]
description:
- Syslog Transport Protocol.
type: str
default: 'udp_unencrypted'
timestamp_format_override:
choices: [no_override, rfc_3164, iso_8601_local_time, iso_8601_utc]
description:
- Syslog Timestamp Format Override.
type: str
default: 'no_override'
hostname_format_override:
choices: [no_override, fqdn, hostname_only]
description:
- Syslog Hostname Format Override.
type: str
default: 'no_override'
message_format:
choices: [legacy_netapp, rfc_5424]
description:
- Syslog Message Format.
type: str
default: 'legacy_netapp'
port:
description:
- Syslog Port.
type: int
default: 514
notes:
- Supports check_mode.
- This module only supports REST.
Expand Down Expand Up @@ -91,6 +126,25 @@
username: "{{username}}"
password: "{{password}}"
- name: Configure REST EMS destination with type syslog
netapp.ontap.na_ontap_ems_destination:
state: present
name: syslog_destination
type: syslog
filters: ['important_events']
destination: http://my.rest.api/address
certificate: my_cert
ca: my_cert_ca
syslog:
transport: udp_unencrypted
port: 514
message_format: legacy_netapp
hostname_format_override: no_override
timestamp_format_override: no_override
hostname: "{{hostname}}"
username: "{{username}}"
password: "{{password}}"
- name: Remove email EMS destination
netapp.ontap.na_ontap_ems_destination:
state: absent
Expand Down Expand Up @@ -120,6 +174,16 @@ def __init__(self):
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
name=dict(required=True, type='str'),
type=dict(required=True, type='str', choices=['email', 'syslog', 'rest_api']),
syslog=dict(required=False, type='dict',
options=dict(
transport=dict(required=False, type='str', choices=['udp_unencrypted', 'tcp_unencrypted', 'tcp_encrypted'],
default='udp_unencrypted'),
port=dict(required=False, type='int', default=514),
message_format=dict(required=False, type='str', choices=['legacy_netapp', 'rfc_5424'], default='legacy_netapp'),
timestamp_format_override=dict(required=False, type='str',
choices=['no_override', 'rfc_3164', 'iso_8601_local_time', 'iso_8601_utc'], default='no_override'),
hostname_format_override=dict(required=False, type='str', choices=['no_override', 'fqdn', 'hostname_only'], default='no_override')
)),
destination=dict(required=True, type='str'),
filters=dict(required=True, type='list', elements='str'),
certificate=dict(required=False, type='str'),
Expand All @@ -133,7 +197,7 @@ def __init__(self):
self.na_helper = NetAppModule()
self.parameters = self.na_helper.set_parameters(self.module.params)
self.rest_api = netapp_utils.OntapRestAPI(self.module)
partially_supported_rest_properties = [['certificate', (9, 11, 1)]]
partially_supported_rest_properties = [['certificate', (9, 11, 1)], ['syslog', (9, 12, 1)]]
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, partially_supported_rest_properties=partially_supported_rest_properties)

if not self.use_rest:
Expand All @@ -149,10 +213,20 @@ def generate_filters_list(self, filters):

def get_ems_destination(self, name):
api = 'support/ems/destinations'
fields = 'name,type,destination,filters.name,certificate.ca'
query = {'name': name,
'fields': 'type,'
'destination,'
'filters.name,'
'certificate.ca,'}
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 11, 1):
fields += ',certificate.name'
query = dict(name=name, fields=fields)
query['fields'] += 'certificate.name,'
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 12, 1):
syslog_option_9_12 = ('syslog.transport,'
'syslog.port,'
'syslog.format.message,'
'syslog.format.timestamp_override,'
'syslog.format.hostname_override,')
query['fields'] += syslog_option_9_12
record, error = rest_generic.get_one_record(self.rest_api, api, query)
self.fail_on_error(error, 'fetching EMS destination for %s' % name)
if record:
Expand All @@ -164,6 +238,14 @@ def get_ems_destination(self, name):
'certificate': self.na_helper.safe_get(record, ['certificate', 'name']),
'ca': self.na_helper.safe_get(record, ['certificate', 'ca']),
}
if record.get('syslog') is not None:
current['syslog'] = {
'port': self.na_helper.safe_get(record, ['syslog', 'port']),
'transport': self.na_helper.safe_get(record, ['syslog', 'transport']),
'timestamp_format_override': self.na_helper.safe_get(record, ['syslog', 'format', 'timestamp_override']),
'hostname_format_override': self.na_helper.safe_get(record, ['syslog', 'format', 'hostname_override']),
'message_format': self.na_helper.safe_get(record, ['syslog', 'format', 'message']),
}
# 9.9.0 and earlier versions returns rest-api, convert it to rest_api.
if current['type'] and '-' in current['type']:
current['type'] = current['type'].replace('-', '_')
Expand Down Expand Up @@ -206,7 +288,18 @@ def create_ems_destination(self):
'serial_number': self.get_certificate_serial(self.parameters['certificate']),
'ca': self.parameters['ca'],
}

if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 12, 1):
if self.parameters.get('syslog') is not None:
body['syslog'] = {}
for key, option in [
('syslog.port', 'port'),
('syslog.transport', 'transport'),
('syslog.format.message', 'message_format'),
('syslog.format.timestamp_override', 'timestamp_format_override'),
('syslog.format.hostname_override', 'hostname_format_override')
]:
if self.parameters['syslog'].get(option) is not None:
body[key] = self.parameters['syslog'][option]
dummy, error = rest_generic.post_async(self.rest_api, api, body)
self.fail_on_error(error, 'creating EMS destinations for %s' % name)

Expand All @@ -231,6 +324,16 @@ def modify_ems_destination(self, name, modify):
body[option]['serial_number'] = self.get_certificate_serial(modify[option])
elif option == 'ca':
body['certificate']['ca'] = modify[option]
elif option == 'syslog':
for key, option in [
('syslog.port', 'port'),
('syslog.transport', 'transport'),
('syslog.format.message', 'message_format'),
('syslog.format.timestamp_override', 'timestamp_format_override'),
('syslog.format.hostname_override', 'hostname_format_override')
]:
if option in modify['syslog']:
body[key] = modify['syslog'][option]
else:
body[option] = modify[option]
if body:
Expand Down
113 changes: 113 additions & 0 deletions tests/unit/plugins/modules/test_na_ontap_ems_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@
}],
"num_records": 1
}, None),
'ems_destination_type_syslog': (200, {
"records": [
{
"name": "test",
"type": "syslog",
"destination": "https://test.destination",
"filters": [
{
"name": "test-filter"
}
],
"syslog": {
"port": 514,
"transport": "udp_unencrypted",
"message_format": "legacy_netapp",
"timestamp_format_override": "no_override",
"hostname_format_override": "no_override"
}
}],
"num_records": 1
}, None),
})

DEFAULT_ARGS = {
Expand Down Expand Up @@ -346,3 +367,95 @@ def test_modify_ems_cert():
'ca': 'cert_ca',
}
assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']


def test_create_ems_destination_with_type_syslog():
register_responses([
('GET', 'cluster', SRR['is_rest_9_12_1']),
('GET', 'support/ems/destinations', SRR['empty_records']),
('POST', 'support/ems/destinations', SRR['empty_good'])
])
module_args = {
'name': 'test',
'type': 'syslog',
'destination': 'https://test.destination',
'filters': ['test-filter'],
'syslog': {
'port': 514,
'transport': 'udp_unencrypted',
'message_format': 'legacy_netapp',
'timestamp_format_override': 'no_override',
'hostname_format_override': 'no_override'
}
}
assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']


def test_error_ontap_9_12_1():
''' syslog option supported from 9.12.1 '''
register_responses([
('GET', 'cluster', SRR['is_rest_9_10_1'])
])
module_args = {
'name': 'test',
'type': 'syslog',
'use_rest': 'always',
'destination': 'https://test.destination',
'filters': ['test-filter'],
'syslog': {
'port': 514,
'transport': 'udp_unencrypted',
'message_format': 'legacy_netapp',
'timestamp_format_override': 'no_override',
'hostname_format_override': 'no_override'
}
}
assert 'Error: Minimum version of ONTAP for syslog is (9, 12, 1). Current version: (9, 10, 1).' in call_main(my_main, DEFAULT_ARGS,
module_args, fail=True)['msg']


def test_create_ems_destination_with_type_syslog_and_add_certs():
register_responses([
('GET', 'cluster', SRR['is_rest_9_12_1']),
('GET', 'support/ems/destinations', SRR['empty_records']),
('GET', 'security/certificates', SRR['certificate_record_1']),
('POST', 'support/ems/destinations', SRR['empty_good'])
])
module_args = {
'name': 'test',
'type': 'syslog',
'destination': 'https://test.destination',
'filters': ['test-filter'],
'certificate': 'cert1',
'ca': 'cert_ca',
'syslog': {
'port': 514,
'transport': 'udp_unencrypted',
'message_format': 'legacy_netapp',
'timestamp_format_override': 'no_override',
'hostname_format_override': 'no_override'
}
}
assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']


def test_modify_ems_destination_with_type_syslog():
register_responses([
('GET', 'cluster', SRR['is_rest_9_12_1']),
('GET', 'support/ems/destinations', SRR['ems_destination_type_syslog']),
('PATCH', 'support/ems/destinations/test', SRR['empty_good'])
])
module_args = {
'name': 'test',
'type': 'syslog',
'destination': 'https://test.destination',
'filters': ['test-filter'],
'syslog': {
'port': 614,
'transport': 'tcp_unencrypted',
'message_format': 'rfc_5424',
'timestamp_format_override': 'no_override',
'hostname_format_override': 'fqdn'
}
}
assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']

0 comments on commit 17caa5b

Please sign in to comment.