Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_kerberos_interface.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: NetApp, Inc
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 module: na_ontap_kerberos_interface
12 short_description: NetApp ONTAP module to modify kerberos interface.
13 description:
14 - Enable or disable kerberos interface.
15 extends_documentation_fragment:
16 - netapp.ontap.netapp.na_ontap_rest
17 version_added: '22.6.0'
18 author: NetApp Ansible Team (@carchi8py) <[email protected]>
19 options:
20 state:
21 description:
22 - Modify kerberos interface, only present is supported.
23 choices: ['present']
24 type: str
25 default: present
26
27 interface_name:
28 description:
29 - Specifies the name of the logical interface associated with the NFS Kerberos configuration you want to modify.
30 type: str
31 required: true
32
33 vserver:
34 description:
35 - Specifies the Vserver associated with the NFS Kerberos configuration you want to modify.
36 type: str
37 required: true
38
39 enabled:
40 description:
41 - Specifies whether to enable or disable Kerberos for NFS on the specified Vserver and logical interface.
42 - C(service_principal_name) is required when try to enable kerberos.
43 type: bool
44 required: true
45
46 keytab_uri:
47 description:
48 - Specifies loading a keytab file from the specified URI.
49 - This value must be in the form of "(ftp|http|https)://(hostname|IPv4 Address|'['IPv6 Address']')...".
50 type: str
51
52 machine_account:
53 description:
54 - Specifies the machine account to create in Active Directory.
55 - Requires ONTAP 9.12.1 or later.
56 type: str
57
58 organizational_unit:
59 description:
60 - Specifies the organizational unit (OU) under which the Microsoft Active Directory server account will be created
61 when you enable Kerberos using a realm for Microsoft KDC
62 type: str
63
64 admin_username:
65 description:
66 - Specifies the administrator username.
67 type: str
68
69 admin_password:
70 description:
71 - Specifies the administrator password.
72 type: str
73
74 service_principal_name:
75 description:
76 - Specifies the service principal name (SPN) of the Kerberos configuration you want to modify.
77 - This value must be in the form nfs/host_name@REALM.
78 - host_name is the fully qualified host name of the Kerberos server, nfs is the service, and REALM is the name of the Kerberos realm.
79 - Specify Kerberos realm names in uppercase.
80 aliases: ['spn']
81 type: str
82
83 notes:
84 - Supports check_mode.
85 - Module supports only REST and requires ONTAP 9.7 or later.
86 '''
87
88 EXAMPLES = '''
89 - name: Enable kerberos interface.
90 netapp.ontap.na_ontap_kerberos_interface:
91 interface_name: lif_svm1_284
92 vserver: ansibleSVM
93 enabled: true
94 service_principal_name: nfs/lif_svm1_284@RELAM2
95 admin_username: "{{ admin_user }}"
96 admin_password: "{{ admin_pass }}"
97 hostname: "{{ netapp_hostname }}"
98 username: "{{ netapp_username }}"
99 password: "{{ netapp_password }}"
100
101
102 - name: Disable kerberos interface.
103 netapp.ontap.na_ontap_kerberos_interface:
104 interface_name: lif_svm1_284
105 vserver: ansibleSVM
106 enabled: false
107 admin_username: "{{ admin_user }}"
108 admin_password: "{{ admin_pass }}"
109 hostname: "{{ netapp_hostname }}"
110 username: "{{ netapp_username }}"
111 password: "{{ netapp_password }}"
112 '''
113
114 RETURN = '''
115 '''
116
117 import traceback
118
119 from ansible.module_utils.basic import AnsibleModule
120 from ansible.module_utils._text import to_native
121 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
122 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
123 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
124
125
126 class NetAppOntapKerberosInterface:
127 """Modify Kerberos interface"""
128 def __init__(self):
129 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
130 self.argument_spec.update(dict(
131 state=dict(required=False, type='str', choices=['present'], default='present'),
132 interface_name=dict(required=True, type='str'),
133 vserver=dict(required=True, type='str'),
134 enabled=dict(required=True, type='bool'),
135 keytab_uri=dict(required=False, type='str', no_log=True),
136 machine_account=dict(required=False, type='str'),
137 organizational_unit=dict(required=False, type='str'),
138 admin_username=dict(required=False, type='str'),
139 admin_password=dict(required=False, type='str', no_log=True),
140 service_principal_name=dict(required=False, type='str', aliases=['spn'])
141 ))
142
143 self.module = AnsibleModule(
144 argument_spec=self.argument_spec,
145 supports_check_mode=True,
146 required_if=[('enabled', True, ['service_principal_name'])],
147 required_together=[('admin_username', 'admin_password')],
148 mutually_exclusive=[('keytab_uri', 'machine_account')]
149 )
150
151 self.na_helper = NetAppModule(self.module)
152 self.parameters = self.na_helper.check_and_set_parameters(self.module)
153 self.rest_api = netapp_utils.OntapRestAPI(self.module)
154 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_kerberos_interface', 9, 7)
155 self.rest_api.is_rest_supported_properties(self.parameters, None, [['machine_account', (9, 12, 1)]])
156 self.uuid = None
157
158 def get_kerberos_interface(self):
159 """
160 Get kerberos interface.
161 """
162 api = 'protocols/nfs/kerberos/interfaces'
163 query = {
164 'interface.name': self.parameters['interface_name'],
165 'svm.name': self.parameters['vserver'],
166 'fields': 'interface.uuid,enabled,spn'
167 }
168 if 'machine_account' in self.parameters:
169 query['fields'] += ',machine_account'
170 record, error = rest_generic.get_one_record(self.rest_api, api, query)
171 if error:
172 self.module.fail_json(msg='Error fetching kerberos interface info %s: %s' % (self.parameters['interface_name'], to_native(error)),
173 exception=traceback.format_exc())
174 if record is None:
175 self.module.fail_json(msg='Error: Kerberos interface config does not exist for %s' % self.parameters['interface_name'])
176 self.uuid = self.na_helper.safe_get(record, ['interface', 'uuid'])
177 return {
178 'enabled': record.get('enabled')
179 }
180
181 def modify_kerberos_interface(self):
182 """
183 Modify kerberos interface.
184 """
185 api = 'protocols/nfs/kerberos/interfaces'
186 body = {'enabled': self.parameters['enabled']}
187 if 'keytab_uri' in self.parameters:
188 body['keytab_uri'] = self.parameters['keytab_uri']
189 if 'organizational_unit' in self.parameters:
190 body['organizational_unit'] = self.parameters['organizational_unit']
191 if 'service_principal_name' in self.parameters:
192 body['spn'] = self.parameters['service_principal_name']
193 if 'admin_username' in self.parameters:
194 body['user'] = self.parameters['admin_username']
195 if 'admin_password' in self.parameters:
196 body['password'] = self.parameters['admin_password']
197 if 'machine_account' in self.parameters:
198 body['machine_account'] = self.parameters['machine_account']
199 dummy, error = rest_generic.patch_async(self.rest_api, api, self.uuid, body)
200 if error:
201 self.module.fail_json(msg='Error modifying kerberos interface %s: %s.' % (self.parameters['interface_name'], to_native(error)),
202 exception=traceback.format_exc())
203
204 def apply(self):
205 modify = self.na_helper.get_modified_attributes(self.get_kerberos_interface(), self.parameters)
206 if self.na_helper.changed and not self.module.check_mode:
207 self.modify_kerberos_interface()
208 result = netapp_utils.generate_result(self.na_helper.changed, modify=modify)
209 self.module.exit_json(**result)
210
211
212 def main():
213 kerberos_obj = NetAppOntapKerberosInterface()
214 kerberos_obj.apply()
215
216
217 if __name__ == '__main__':
218 main()