1#!/usr/bin/python 2 3""" this is lun mapping module 4 5 (c) 2018-2019, NetApp, Inc 6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 7""" 8 9from __future__ import absolute_import, division, print_function 10__metaclass__ = type 11 12 13ANSIBLE_METADATA = {'metadata_version': '1.1', 14 'status': ['preview'], 15 'supported_by': 'certified'} 16 17 18DOCUMENTATION = """ 19 20module: na_ontap_lun_map 21 22short_description: NetApp ONTAP LUN maps 23extends_documentation_fragment: 24 - netapp.na_ontap 25version_added: '2.6' 26author: NetApp Ansible Team (@carchi8py) <ng-ansibleteam@netapp.com> 27 28description: 29- Map and unmap LUNs on NetApp ONTAP. 30 31options: 32 33 state: 34 description: 35 - Whether the specified LUN should exist or not. 36 choices: ['present', 'absent'] 37 default: present 38 39 initiator_group_name: 40 description: 41 - Initiator group to map to the given LUN. 42 required: true 43 44 path: 45 description: 46 - Path of the LUN.. 47 required: true 48 49 vserver: 50 required: true 51 description: 52 - The name of the vserver to use. 53 54 lun_id: 55 description: 56 - LUN ID assigned for the map. 57 58 59""" 60 61EXAMPLES = """ 62- name: Create LUN mapping 63 na_ontap_lun_map: 64 state: present 65 initiator_group_name: ansibleIgroup3234 66 path: /vol/iscsi_path/iscsi_lun 67 vserver: ci_dev 68 hostname: "{{ netapp_hostname }}" 69 username: "{{ netapp_username }}" 70 password: "{{ netapp_password }}" 71 72- name: Unmap LUN 73 na_ontap_lun_map: 74 state: absent 75 initiator_group_name: ansibleIgroup3234 76 path: /vol/iscsi_path/iscsi_lun 77 vserver: ci_dev 78 hostname: "{{ netapp_hostname }}" 79 username: "{{ netapp_username }}" 80 password: "{{ netapp_password }}" 81""" 82 83RETURN = """ 84lun_node: 85 description: NetApp controller that is hosting the LUN. 86 returned: success 87 type: str 88 sample: node01 89lun_ostype: 90 description: Specifies the OS of the host accessing the LUN. 91 returned: success 92 type: str 93 sample: vmware 94lun_serial: 95 description: A unique, 12-byte, ASCII string used to identify the LUN. 96 returned: success 97 type: str 98 sample: 80E7/]LZp1Tt 99lun_naa_id: 100 description: The Network Address Authority (NAA) identifier for the LUN. 101 returned: success 102 type: str 103 sample: 600a0980383045372f5d4c5a70315474 104lun_state: 105 description: Online or offline status of the LUN. 106 returned: success 107 type: str 108 sample: online 109lun_size: 110 description: Size of the LUN in bytes. 111 returned: success 112 type: int 113 sample: 2199023255552 114""" 115 116import traceback 117 118from ansible.module_utils.basic import AnsibleModule 119from ansible.module_utils._text import to_native 120import ansible.module_utils.netapp as netapp_utils 121import codecs 122from ansible.module_utils._text import to_text, to_bytes 123 124HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() 125 126 127class NetAppOntapLUNMap(object): 128 129 def __init__(self): 130 131 self.argument_spec = netapp_utils.na_ontap_host_argument_spec() 132 self.argument_spec.update(dict( 133 state=dict(required=False, choices=['present', 'absent'], default='present'), 134 initiator_group_name=dict(required=True, type='str'), 135 path=dict(required=True, type='str'), 136 vserver=dict(required=True, type='str'), 137 lun_id=dict(required=False, type='str', default=None), 138 )) 139 140 self.module = AnsibleModule( 141 argument_spec=self.argument_spec, 142 required_if=[ 143 ('state', 'present', ['path']) 144 ], 145 supports_check_mode=True 146 ) 147 148 self.result = dict( 149 changed=False, 150 ) 151 152 p = self.module.params 153 154 # set up state variables 155 self.state = p['state'] 156 self.initiator_group_name = p['initiator_group_name'] 157 self.path = p['path'] 158 self.vserver = p['vserver'] 159 self.lun_id = p['lun_id'] 160 161 if HAS_NETAPP_LIB is False: 162 self.module.fail_json(msg="the python NetApp-Lib module is required") 163 else: 164 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.vserver) 165 166 def get_lun_map(self): 167 """ 168 Return details about the LUN map 169 170 :return: Details about the lun map 171 :rtype: dict 172 """ 173 lun_info = netapp_utils.zapi.NaElement('lun-map-list-info') 174 lun_info.add_new_child('path', self.path) 175 result = self.server.invoke_successfully(lun_info, True) 176 return_value = None 177 igroups = result.get_child_by_name('initiator-groups') 178 if igroups: 179 for igroup_info in igroups.get_children(): 180 initiator_group_name = igroup_info.get_child_content('initiator-group-name') 181 lun_id = igroup_info.get_child_content('lun-id') 182 if initiator_group_name == self.initiator_group_name: 183 return_value = { 184 'lun_id': lun_id 185 } 186 break 187 188 return return_value 189 190 def get_lun(self): 191 """ 192 Return details about the LUN 193 194 :return: Details about the lun 195 :rtype: dict 196 """ 197 # build the lun query 198 query_details = netapp_utils.zapi.NaElement('lun-info') 199 query_details.add_new_child('path', self.path) 200 201 query = netapp_utils.zapi.NaElement('query') 202 query.add_child_elem(query_details) 203 204 lun_query = netapp_utils.zapi.NaElement('lun-get-iter') 205 lun_query.add_child_elem(query) 206 207 # find lun using query 208 result = self.server.invoke_successfully(lun_query, True) 209 return_value = None 210 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: 211 lun = result.get_child_by_name('attributes-list').get_child_by_name('lun-info') 212 213 # extract and assign lun information to return value 214 hexlify = codecs.getencoder('hex') 215 naa_hex = to_text(hexlify(to_bytes(lun.get_child_content('serial-number')))[0]) 216 return_value = { 217 'lun_node': lun.get_child_content('node'), 218 'lun_ostype': lun.get_child_content('multiprotocol-type'), 219 'lun_serial': lun.get_child_content('serial-number'), 220 'lun_naa_id': '600a0980' + naa_hex, 221 'lun_state': lun.get_child_content('state'), 222 'lun_size': lun.get_child_content('size'), 223 } 224 225 return return_value 226 227 def create_lun_map(self): 228 """ 229 Create LUN map 230 """ 231 options = {'path': self.path, 'initiator-group': self.initiator_group_name} 232 if self.lun_id is not None: 233 options['lun-id'] = self.lun_id 234 lun_map_create = netapp_utils.zapi.NaElement.create_node_with_children('lun-map', **options) 235 236 try: 237 self.server.invoke_successfully(lun_map_create, enable_tunneling=True) 238 except netapp_utils.zapi.NaApiError as e: 239 self.module.fail_json(msg="Error mapping lun %s of initiator_group_name %s: %s" % 240 (self.path, self.initiator_group_name, to_native(e)), 241 exception=traceback.format_exc()) 242 243 def delete_lun_map(self): 244 """ 245 Unmap LUN map 246 """ 247 lun_map_delete = netapp_utils.zapi.NaElement.create_node_with_children('lun-unmap', **{'path': self.path, 'initiator-group': self.initiator_group_name}) 248 249 try: 250 self.server.invoke_successfully(lun_map_delete, enable_tunneling=True) 251 except netapp_utils.zapi.NaApiError as e: 252 self.module.fail_json(msg="Error unmapping lun %s of initiator_group_name %s: %s" % 253 (self.path, self.initiator_group_name, to_native(e)), 254 exception=traceback.format_exc()) 255 256 def apply(self): 257 netapp_utils.ems_log_event("na_ontap_lun_map", self.server) 258 lun_details = self.get_lun() 259 lun_map_details = self.get_lun_map() 260 261 if self.state == 'present' and lun_details: 262 self.result.update(lun_details) 263 264 if self.state == 'present' and not lun_map_details: 265 self.result['changed'] = True 266 if not self.module.check_mode: 267 self.create_lun_map() 268 elif self.state == 'absent' and lun_map_details: 269 self.result['changed'] = True 270 if not self.module.check_mode: 271 self.delete_lun_map() 272 273 self.module.exit_json(**self.result) 274 275 276def main(): 277 v = NetAppOntapLUNMap() 278 v.apply() 279 280 281if __name__ == '__main__': 282 main() 283