Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2022-2026, NetApp, Inc
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 '''
10 na_ontap_name_mappings
11 '''
12
13
14 DOCUMENTATION = '''
15 module: na_ontap_name_mappings
16 short_description: NetApp ONTAP name mappings
17 extends_documentation_fragment:
18 - netapp.ontap.netapp.na_ontap_rest
19 version_added: 22.0.0
20 author: NetApp Ansible Team (@carchi8py) <[email protected]>
21 description:
22 - Create/Delete/Modify name mappings for an SVM on ONTAP.
23 options:
24 state:
25 description:
26 - Whether the specified name mappings should exist or not.
27 choices: ['present', 'absent']
28 type: str
29 default: present
30 vserver:
31 description:
32 - Name of the vserver to use.
33 required: true
34 type: str
35 client_match:
36 description:
37 - Client workstation IP Address which is matched when searching for the pattern.
38 - Example '10.254.101.111/28'
39 - Client match value can be in any of the following formats,
40 - As an IPv4 address with a subnet mask expressed as a number of bits; for instance, 10.1.12.0/24
41 - As an IPv6 address with a subnet mask expressed as a number of bits; for instance, fd20:8b1e:b255:4071::/64
42 - As an IPv4 address with a network mask; for instance, 10.1.16.0/255.255.255.0
43 - As a hostname
44 type: str
45 direction:
46 description:
47 - Direction in which the name mapping is applied.
48 - The possible values are,
49 krb_unix - Kerberos principal name to UNIX user name
50 win_unix - Windows user name to UNIX user name
51 unix_win - UNIX user name to Windows user name mapping
52 s3_unix - S3 user name to UNIX user name mapping
53 s3_win - S3 user name to Windows user name mapping
54 - s3_unix and s3_win requires ONTAP 9.12.1 or later.
55 choices: ['krb_unix', 'win_unix', 'unix_win', 's3_unix', 's3_win']
56 required: true
57 type: str
58 index:
59 description:
60 - Position in the list of name mappings.
61 - Minimum value is 1 and maximum is 2147483647.
62 required: true
63 type: int
64 pattern:
65 description:
66 - Pattern used to match the name while searching for a name that can be used as a replacement.
67 - The pattern is a UNIX-style regular expression.
68 - Regular expressions are case-insensitive when mapping from Windows to UNIX,
69 and they are case-sensitive for mappings from Kerberos to UNIX and UNIX to Windows.
70 - Minimum length is 1 and maximum length is 256.
71 - Pattern should be unique for each index of vserver.
72 - Example ENGCIFS_AD_USER.
73 type: str
74 replacement:
75 description:
76 - The name that is used as a replacement, if the pattern associated with this entry matches.
77 - Minimum length is 1 and maximum length is 256.
78 - Example unix_user1.
79 type: str
80 from_index:
81 description:
82 - If no entry with index is found, it is created by reindexing the entry for from_index.
83 - If no entry is found for index and from_index, an error is reported.
84 - Minimum value is 1 and maximum is 2147483647.
85 - Requires ONTAP version 9.7 or later.
86 type: int
87 lambda_config:
88 description:
89 - Configuration parameters for AWS Lambda proxy functionality.
90 - These option and suboptions are only supported with REST.
91 type: dict
92 version_added: 23.5.0
93 suboptions:
94 function_name:
95 description:
96 - The name of the AWS Lambda function to invoke.
97 type: str
98 required: true
99 aws_region:
100 description:
101 - The name of the AWS region.
102 type: str
103 required: true
104 aws_profile:
105 description:
106 - The name of the AWS profile to use for authentication.
107 type: str
108
109 notes:
110 - Supports AWS Lambda proxy functionality. See the README file for examples.
111
112 '''
113
114 EXAMPLES = '''
115 - name: Create name mappings configuration
116 netapp.ontap.na_ontap_name_mappings:
117 vserver: vserverName
118 direction: win_unix
119 index: 1
120 pattern: ENGCIFS_AD_USER
121 replacement: unix_user
122 client_match: 10.254.101.111/28
123 hostname: "{{ netapp_hostname }}"
124 username: "{{ netapp_username }}"
125 password: "{{ netapp_password }}"
126
127 - name: Modify name mappings configuration
128 netapp.ontap.na_ontap_name_mappings:
129 vserver: vserverName
130 direction: win_unix
131 index: 1
132 pattern: ENGCIFS_AD_USERS
133 replacement: unix_user1
134 client_match: 10.254.101.112/28
135 hostname: "{{ netapp_hostname }}"
136 username: "{{ netapp_username }}"
137 password: "{{ netapp_password }}"
138
139 - name: Swap name mappings position
140 netapp.ontap.na_ontap_name_mappings:
141 vserver: vserverName
142 direction: win_unix
143 index: 1
144 pattern: ENGCIFS_AD_USERS
145 replacement: unix_user1
146 from_index: 2
147 hostname: "{{ netapp_hostname }}"
148 username: "{{ netapp_username }}"
149 password: "{{ netapp_password }}"
150
151 - name: Delete name mappings configuration
152 netapp.ontap.na_ontap_name_mappings:
153 vserver: vserverName
154 direction: win_unix
155 index: 1
156 hostname: "{{ netapp_hostname }}"
157 username: "{{ netapp_username }}"
158 password: "{{ netapp_password }}"
159 '''
160
161 RETURN = """
162 """
163
164 from ansible.module_utils.basic import AnsibleModule
165 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
166 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
167 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
168
169
170 class NetAppOntapNameMappings:
171 """ object initialize and class methods """
172 def __init__(self):
173 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
174 self.argument_spec.update(dict(
175 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
176 vserver=dict(required=True, type='str'),
177 client_match=dict(required=False, type='str'),
178 direction=dict(required=True, type='str', choices=['krb_unix', 'win_unix', 'unix_win', 's3_unix', 's3_win']),
179 index=dict(required=True, type='int'),
180 from_index=dict(required=False, type='int'),
181 pattern=dict(required=False, type='str'),
182 replacement=dict(required=False, type='str'),
183 ))
184
185 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
186
187 self.module = AnsibleModule(
188 argument_spec=self.argument_spec,
189 supports_check_mode=True,
190 required_if=[
191 ['use_lambda', True, ('lambda_config',)]
192 ],
193 )
194 self.na_helper = NetAppModule(self)
195 self.parameters = self.na_helper.set_parameters(self.module.params)
196 self.svm_uuid = None
197 self.rest_api = netapp_utils.OntapRestAPI(self.module)
198 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_name_mappings', 9, 6)
199 self.rest_api.is_rest_supported_properties(self.parameters, None, [['from_index', (9, 7)]])
200 if self.parameters['direction'] in ['s3_unix', 's3_win'] and not self.rest_api.meets_rest_minimum_version(True, 9, 12, 1):
201 self.module.fail_json(msg="Error: direction %s requires ONTAP 9.12.1 or later version." % self.parameters['direction'])
202
203 def get_name_mappings_rest(self, index=None):
204 '''
205 Retrieves the name mapping configuration for SVM with rest API.
206 '''
207 if index is None:
208 index = self.parameters['index']
209 query = {'svm.name': self.parameters.get('vserver'),
210 'index': index, # the existing current index or from_index to be swapped
211 'direction': self.parameters.get('direction'), # different directions can have same index
212 'fields': 'svm.uuid,'
213 'client_match,'
214 'direction,'
215 'index,'
216 'pattern,'
217 'replacement,'}
218 api = 'name-services/name-mappings'
219 record, error = rest_generic.get_one_record(self.rest_api, api, query)
220 if error:
221 self.module.fail_json(msg=error)
222 if record:
223 self.svm_uuid = record['svm']['uuid']
224 return {
225 'pattern': self.na_helper.safe_get(record, ['pattern']),
226 'direction': self.na_helper.safe_get(record, ['direction']),
227 'replacement': self.na_helper.safe_get(record, ['replacement']),
228 'client_match': record.get('client_match', None),
229 }
230 return None
231
232 def create_name_mappings_rest(self):
233 """
234 Creates name mappings for an SVM with REST API.
235 """
236 body = {'svm.name': self.parameters.get('vserver'),
237 'index': self.parameters.get('index'),
238 'direction': self.parameters.get('direction'),
239 'pattern': self.parameters.get('pattern'),
240 'replacement': self.parameters.get('replacement')}
241 if 'client_match' in self.parameters:
242 body['client_match'] = self.parameters['client_match']
243 api = 'name-services/name-mappings'
244 dummy, error = rest_generic.post_async(self.rest_api, api, body)
245 if error is not None:
246 self.module.fail_json(msg="Error on creating name mappings rest: %s" % error)
247
248 def modify_name_mappings_rest(self, modify=None, reindex=False):
249 """
250 Updates the name mapping configuration of an SVM with rest API.
251 Swap the position with new position(new_index).
252 """
253 body = {}
254 query = None
255 if modify:
256 for option in ['pattern', 'replacement', 'client_match']:
257 if option in modify:
258 body[option] = self.parameters[option]
259 # Cannot swap entries which have hostname or address configured.
260 # Delete and recreate the new entry at the specified position.
261 index = self.parameters['index']
262 if reindex:
263 query = {'new_index': self.parameters.get('index')}
264 index = self.parameters['from_index']
265
266 api = 'name-services/name-mappings/%s/%s/%s' % (self.svm_uuid, self.parameters['direction'], index)
267 dummy, error = rest_generic.patch_async(self.rest_api, api, None, body, query)
268 if error is not None:
269 self.module.fail_json(msg="Error on modifying name mappings rest: %s" % error)
270
271 def delete_name_mappings_rest(self):
272 """
273 Delete the name mapping configuration of an SVM with rest API.
274 """
275 api = 'name-services/name-mappings/%s/%s/%s' % (self.svm_uuid, self.parameters['direction'], self.parameters['index'])
276 dummy, error = rest_generic.delete_async(self.rest_api, api, None)
277 if error is not None:
278 self.module.fail_json(msg="Error on deleting name mappings rest: %s" % error)
279
280 def apply(self):
281 reindex = False
282 current = self.get_name_mappings_rest()
283 cd_action = self.na_helper.get_cd_action(current, self.parameters)
284
285 # Throws error when trying to swap with non existing index
286 if cd_action == 'create':
287 if self.parameters.get('from_index') is not None:
288 current = self.get_name_mappings_rest(self.parameters['from_index'])
289 if not current:
290 self.module.fail_json(msg="Error from_index entry does not exist")
291 reindex = True
292 cd_action = None
293 else:
294 # pattern and replacement are required when creating name mappings.
295 if not self.parameters.get('pattern') or not self.parameters.get('replacement'):
296 self.module.fail_json(msg="Error creating name mappings for an SVM, pattern and replacement are required in create.")
297 modify = self.na_helper.get_modified_attributes(current, self.parameters) if cd_action is None else None
298
299 if self.na_helper.changed and not self.module.check_mode:
300 if cd_action == 'create':
301 self.create_name_mappings_rest()
302 elif cd_action == 'delete':
303 self.delete_name_mappings_rest()
304 elif modify or reindex:
305 self.modify_name_mappings_rest(modify, reindex)
306 if reindex:
307 modify['new_index'] = self.parameters.get('index')
308 modify['from_index'] = self.parameters['from_index']
309 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
310 self.module.exit_json(**result)
311
312
313 def main():
314 """ Create object and call apply """
315 mapping_obj = NetAppOntapNameMappings()
316 mapping_obj.apply()
317
318
319 if __name__ == '__main__':
320 main()