| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022-2025, NetApp, Inc. GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = """ |
| 9 |
module: na_ontap_security_ipsec_ca_certificate |
| 10 |
short_description: NetApp ONTAP module to add or delete ipsec ca certificate. |
| 11 |
extends_documentation_fragment: |
| 12 |
- netapp.ontap.netapp.na_ontap_rest |
| 13 |
version_added: '22.1.0' |
| 14 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 15 |
description: |
| 16 |
- Create or delete security IPsec CA Certificate. |
| 17 |
options: |
| 18 |
state: |
| 19 |
description: |
| 20 |
- Create or delete security IPsec CA Certificate. |
| 21 |
- The certificate must already be installed on the system, for instance using na_ontap_security_certificates. |
| 22 |
choices: ['present', 'absent'] |
| 23 |
type: str |
| 24 |
default: present |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Name of the CA certificate. |
| 28 |
- Certificate must be already installed in svm or cluster scope. |
| 29 |
type: str |
| 30 |
required: true |
| 31 |
svm: |
| 32 |
description: |
| 33 |
- Name of svm. |
| 34 |
- If not set cluster scope is assumed. |
| 35 |
type: str |
| 36 |
required: false |
| 37 |
|
| 38 |
notes: |
| 39 |
- Supports check_mode. |
| 40 |
- Only supported with REST and requires ONTAP 9.10.1 or later. |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = """ |
| 44 |
- name: Add IPsec CA certificate to svm. |
| 45 |
netapp.ontap.na_ontap_security_ipsec_ca_certificate: |
| 46 |
name: cert1 |
| 47 |
svm: ansibleSVM |
| 48 |
hostname: "{{ netapp_hostname }}" |
| 49 |
username: "{{ netapp_username }}" |
| 50 |
password: "{{ netapp_password }}" |
| 51 |
https: true |
| 52 |
validate_certs: "{{ validate_certs }}" |
| 53 |
|
| 54 |
- name: Delete IPsec CA certificate in svm. |
| 55 |
netapp.ontap.na_ontap_security_ipsec_ca_certificate: |
| 56 |
name: cert1 |
| 57 |
svm: ansibleSVM |
| 58 |
state: absent |
| 59 |
hostname: "{{ netapp_hostname }}" |
| 60 |
username: "{{ netapp_username }}" |
| 61 |
password: "{{ netapp_password }}" |
| 62 |
https: true |
| 63 |
validate_certs: "{{ validate_certs }}" |
| 64 |
|
| 65 |
- name: Add IPsec CA certificate to cluster. |
| 66 |
netapp.ontap.na_ontap_security_ipsec_ca_certificate: |
| 67 |
name: cert2 |
| 68 |
hostname: "{{ netapp_hostname }}" |
| 69 |
username: "{{ netapp_username }}" |
| 70 |
password: "{{ netapp_password }}" |
| 71 |
https: true |
| 72 |
validate_certs: "{{ validate_certs }}" |
| 73 |
|
| 74 |
- name: Delete IPsec CA certificate from cluster. |
| 75 |
netapp.ontap.na_ontap_security_ipsec_ca_certificate: |
| 76 |
name: cert2 |
| 77 |
state: absent |
| 78 |
hostname: "{{ netapp_hostname }}" |
| 79 |
username: "{{ netapp_username }}" |
| 80 |
password: "{{ netapp_password }}" |
| 81 |
https: true |
| 82 |
validate_certs: "{{ validate_certs }}" |
| 83 |
""" |
| 84 |
|
| 85 |
RETURN = """ |
| 86 |
""" |
| 87 |
|
| 88 |
import traceback |
| 89 |
from ansible.module_utils.basic import AnsibleModule |
| 90 |
from ansible.module_utils._text import to_native |
| 91 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 92 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 93 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 94 |
|
| 95 |
|
| 96 |
class NetAppOntapSecurityCACertificate: |
| 97 |
def __init__(self): |
| 98 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 99 |
self.argument_spec.update(dict( |
| 100 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 101 |
name=dict(required=True, type='str'), |
| 102 |
svm=dict(required=False, type='str') |
| 103 |
)) |
| 104 |
self.module = AnsibleModule( |
| 105 |
argument_spec=self.argument_spec, |
| 106 |
supports_check_mode=True |
| 107 |
) |
| 108 |
self.uuid = None |
| 109 |
self.na_helper = NetAppModule(self.module) |
| 110 |
self.parameters = self.na_helper.check_and_set_parameters(self.module) |
| 111 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 112 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_security_ipsec_ca_certificate', 9, 10, 1) |
| 113 |
|
| 114 |
def get_certificate_uuid(self): |
| 115 |
"""Get certificate UUID.""" |
| 116 |
api = 'security/certificates' |
| 117 |
query = {'name': self.parameters['name']} |
| 118 |
if self.parameters.get('svm'): |
| 119 |
query['svm.name'] = self.parameters['svm'] |
| 120 |
else: |
| 121 |
query['scope'] = 'cluster' |
| 122 |
record, error = rest_generic.get_one_record(self.rest_api, api, query, 'uuid') |
| 123 |
if error: |
| 124 |
self.module.fail_json(msg="Error fetching uuid for certificate %s: %s" % (self.parameters['name'], to_native(error)), |
| 125 |
exception=traceback.format_exc()) |
| 126 |
if record: |
| 127 |
return record['uuid'] |
| 128 |
return None |
| 129 |
|
| 130 |
def get_ipsec_ca_certificate(self): |
| 131 |
"""GET IPsec CA certificate record""" |
| 132 |
self.uuid = self.get_certificate_uuid() |
| 133 |
if self.uuid is None: |
| 134 |
if self.parameters['state'] == 'absent': |
| 135 |
return None |
| 136 |
svm_or_scope = self.parameters['svm'] if self.parameters.get('svm') else 'cluster' |
| 137 |
self.module.fail_json(msg="Error: certificate %s is not installed in %s" % (self.parameters['name'], svm_or_scope)) |
| 138 |
api = 'security/ipsec/ca-certificates/%s' % self.uuid |
| 139 |
record, error = rest_generic.get_one_record(self.rest_api, api) |
| 140 |
if error: |
| 141 |
# REST returns error if ipsec ca-certificates doesn't exist. |
| 142 |
if "entry doesn't exist" in error: |
| 143 |
return None |
| 144 |
self.module.fail_json(msg="Error fetching security IPsec CA certificate %s: %s" % (self.parameters['name'], to_native(error)), |
| 145 |
exception=traceback.format_exc()) |
| 146 |
return record if record else None |
| 147 |
|
| 148 |
def create_ipsec_ca_certificate(self): |
| 149 |
"""Create IPsec CA certifcate""" |
| 150 |
api = 'security/ipsec/ca-certificates' |
| 151 |
body = {'certificate.uuid': self.uuid} |
| 152 |
if self.parameters.get('svm'): |
| 153 |
body['svm.name'] = self.parameters['svm'] |
| 154 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 155 |
if error: |
| 156 |
self.module.fail_json(msg="Error adding security IPsec CA certificate %s: %s" % (self.parameters['name'], to_native(error)), |
| 157 |
exception=traceback.format_exc()) |
| 158 |
|
| 159 |
def delete_ipsec_ca_certificate(self): |
| 160 |
"""Delete IPSec CA certificate""" |
| 161 |
api = 'security/ipsec/ca-certificates' |
| 162 |
dummy, error = rest_generic.delete_async(self.rest_api, api, self.uuid) |
| 163 |
if error: |
| 164 |
self.module.fail_json(msg="Error deleting security IPsec CA certificate %s: %s" % (self.parameters['name'], to_native(error)), |
| 165 |
exception=traceback.format_exc()) |
| 166 |
|
| 167 |
def apply(self): |
| 168 |
current = self.get_ipsec_ca_certificate() |
| 169 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 170 |
if self.na_helper.changed and not self.module.check_mode: |
| 171 |
if cd_action == 'create': |
| 172 |
self.create_ipsec_ca_certificate() |
| 173 |
else: |
| 174 |
self.delete_ipsec_ca_certificate() |
| 175 |
self.module.exit_json(changed=self.na_helper.changed) |
| 176 |
|
| 177 |
|
| 178 |
def main(): |
| 179 |
ipsec_ca_obj = NetAppOntapSecurityCACertificate() |
| 180 |
ipsec_ca_obj.apply() |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == '__main__': |
| 184 |
main() |