| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
module: na_ontap_fcp |
| 11 |
short_description: NetApp ONTAP Start, Stop and Enable FCP services. |
| 12 |
extends_documentation_fragment: |
| 13 |
- netapp.ontap.netapp.na_ontap |
| 14 |
version_added: 2.7.0 |
| 15 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 16 |
description: |
| 17 |
- Start, Stop and Enable FCP services. |
| 18 |
options: |
| 19 |
state: |
| 20 |
description: |
| 21 |
- Whether the FCP should be enabled or not. |
| 22 |
choices: ['present', 'absent'] |
| 23 |
type: str |
| 24 |
default: present |
| 25 |
|
| 26 |
status: |
| 27 |
description: |
| 28 |
- Whether the FCP should be up or down |
| 29 |
choices: ['up', 'down'] |
| 30 |
type: str |
| 31 |
default: up |
| 32 |
|
| 33 |
vserver: |
| 34 |
description: |
| 35 |
- The name of the vserver to use. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
|
| 39 |
''' |
| 40 |
|
| 41 |
EXAMPLES = """ |
| 42 |
- name: Create FCP |
| 43 |
netapp.ontap.na_ontap_fcp: |
| 44 |
state: present |
| 45 |
status: down |
| 46 |
username: "{{ netapp_username }}" |
| 47 |
password: "{{ netapp_password }}" |
| 48 |
hostname: "{{ netapp_hostname }}" |
| 49 |
vserver: "{{ vserver_name }}" |
| 50 |
""" |
| 51 |
|
| 52 |
RETURN = """ |
| 53 |
|
| 54 |
""" |
| 55 |
import traceback |
| 56 |
|
| 57 |
from ansible.module_utils.basic import AnsibleModule |
| 58 |
from ansible.module_utils._text import to_native |
| 59 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 60 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 61 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 62 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 63 |
|
| 64 |
HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() |
| 65 |
|
| 66 |
|
| 67 |
class NetAppOntapFCP: |
| 68 |
""" |
| 69 |
Enable and Disable FCP |
| 70 |
""" |
| 71 |
|
| 72 |
def __init__(self): |
| 73 |
self.use_rest = False |
| 74 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 75 |
self.argument_spec.update(dict( |
| 76 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 77 |
vserver=dict(required=True, type='str'), |
| 78 |
status=dict(required=False, type='str', choices=['up', 'down'], default='up') |
| 79 |
)) |
| 80 |
|
| 81 |
self.module = AnsibleModule( |
| 82 |
argument_spec=self.argument_spec, |
| 83 |
supports_check_mode=True |
| 84 |
) |
| 85 |
|
| 86 |
self.na_helper = NetAppModule() |
| 87 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 88 |
|
| 89 |
self.rest_api = OntapRestAPI(self.module) |
| 90 |
if self.rest_api.is_rest(): |
| 91 |
self.use_rest = True |
| 92 |
elif HAS_NETAPP_LIB is False: |
| 93 |
self.module.fail_json(msg="the python NetApp-Lib module is required") |
| 94 |
else: |
| 95 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 96 |
|
| 97 |
def create_fcp(self): |
| 98 |
""" |
| 99 |
Create's and Starts an FCP |
| 100 |
:return: none |
| 101 |
""" |
| 102 |
try: |
| 103 |
self.server.invoke_successfully(netapp_utils.zapi.NaElement('fcp-service-create'), True) |
| 104 |
except netapp_utils.zapi.NaApiError as error: |
| 105 |
self.module.fail_json(msg='Error creating FCP: %s' % |
| 106 |
(to_native(error)), |
| 107 |
exception=traceback.format_exc()) |
| 108 |
|
| 109 |
def start_fcp(self): |
| 110 |
""" |
| 111 |
Starts an existing FCP |
| 112 |
:return: none |
| 113 |
""" |
| 114 |
try: |
| 115 |
self.server.invoke_successfully(netapp_utils.zapi.NaElement('fcp-service-start'), True) |
| 116 |
except netapp_utils.zapi.NaApiError as error: |
| 117 |
# Error 13013 denotes fcp service already started. |
| 118 |
if to_native(error.code) == "13013": |
| 119 |
return None |
| 120 |
else: |
| 121 |
self.module.fail_json(msg='Error starting FCP %s' % (to_native(error)), |
| 122 |
exception=traceback.format_exc()) |
| 123 |
|
| 124 |
def stop_fcp(self): |
| 125 |
""" |
| 126 |
Steps an Existing FCP |
| 127 |
:return: none |
| 128 |
""" |
| 129 |
try: |
| 130 |
self.server.invoke_successfully(netapp_utils.zapi.NaElement('fcp-service-stop'), True) |
| 131 |
except netapp_utils.zapi.NaApiError as error: |
| 132 |
self.module.fail_json(msg='Error Stoping FCP %s' % |
| 133 |
(to_native(error)), |
| 134 |
exception=traceback.format_exc()) |
| 135 |
|
| 136 |
def destroy_fcp(self): |
| 137 |
""" |
| 138 |
Destroys an already stopped FCP |
| 139 |
:return: |
| 140 |
""" |
| 141 |
try: |
| 142 |
self.server.invoke_successfully(netapp_utils.zapi.NaElement('fcp-service-destroy'), True) |
| 143 |
except netapp_utils.zapi.NaApiError as error: |
| 144 |
self.module.fail_json(msg='Error destroying FCP %s' % |
| 145 |
(to_native(error)), |
| 146 |
exception=traceback.format_exc()) |
| 147 |
|
| 148 |
def get_fcp(self): |
| 149 |
if self.use_rest: |
| 150 |
return self.get_fcp_rest() |
| 151 |
fcp_obj = netapp_utils.zapi.NaElement('fcp-service-get-iter') |
| 152 |
fcp_info = netapp_utils.zapi.NaElement('fcp-service-info') |
| 153 |
fcp_info.add_new_child('vserver', self.parameters['vserver']) |
| 154 |
query = netapp_utils.zapi.NaElement('query') |
| 155 |
query.add_child_elem(fcp_info) |
| 156 |
fcp_obj.add_child_elem(query) |
| 157 |
result = self.server.invoke_successfully(fcp_obj, True) |
| 158 |
# There can only be 1 FCP per vserver. If true, one is set up, else one isn't set up |
| 159 |
if result.get_child_by_name('num-records') and \ |
| 160 |
int(result.get_child_content('num-records')) >= 1: |
| 161 |
return True |
| 162 |
else: |
| 163 |
return False |
| 164 |
|
| 165 |
def current_status(self): |
| 166 |
try: |
| 167 |
status = self.server.invoke_successfully(netapp_utils.zapi.NaElement('fcp-service-status'), True) |
| 168 |
return status.get_child_content('is-available') == 'true' |
| 169 |
except netapp_utils.zapi.NaApiError as error: |
| 170 |
self.module.fail_json(msg='Error destroying FCP: %s' % |
| 171 |
(to_native(error)), |
| 172 |
exception=traceback.format_exc()) |
| 173 |
|
| 174 |
def status_to_bool(self): |
| 175 |
return self.parameters['status'] == 'up' |
| 176 |
|
| 177 |
def get_fcp_rest(self): |
| 178 |
options = {'fields': 'enabled,svm.uuid', |
| 179 |
'svm.name': self.parameters['vserver']} |
| 180 |
api = 'protocols/san/fcp/services' |
| 181 |
record, error = rest_generic.get_one_record(self.rest_api, api, options) |
| 182 |
if error: |
| 183 |
self.module.fail_json(msg="Error on fetching fcp: %s" % error) |
| 184 |
if record: |
| 185 |
record['status'] = 'up' if record.pop('enabled') else 'down' |
| 186 |
return record |
| 187 |
|
| 188 |
def create_fcp_rest(self): |
| 189 |
params = {'svm.name': self.parameters['vserver'], |
| 190 |
'enabled': self.status_to_bool()} |
| 191 |
api = 'protocols/san/fcp/services' |
| 192 |
dummy, error = rest_generic.post_async(self.rest_api, api, params) |
| 193 |
if error is not None: |
| 194 |
self.module.fail_json(msg="Error on creating fcp: %s" % error) |
| 195 |
|
| 196 |
def destroy_fcp_rest(self, current): |
| 197 |
api = 'protocols/san/fcp/services' |
| 198 |
dummy, error = rest_generic.delete_async(self.rest_api, api, current['svm']['uuid']) |
| 199 |
if error is not None: |
| 200 |
self.module.fail_json(msg=" Error on deleting fcp policy: %s" % error) |
| 201 |
|
| 202 |
def start_stop_fcp_rest(self, enabled, current): |
| 203 |
params = {'enabled': enabled} |
| 204 |
api = 'protocols/san/fcp/services' |
| 205 |
dummy, error = rest_generic.patch_async(self.rest_api, api, current['svm']['uuid'], params) |
| 206 |
if error is not None: |
| 207 |
self.module.fail_json(msg="Error on modifying fcp: %s" % error) |
| 208 |
|
| 209 |
def zapi_apply(self, current): |
| 210 |
changed = False |
| 211 |
# this is a mess i don't want to touch... |
| 212 |
if self.parameters['state'] == 'present': |
| 213 |
if current: |
| 214 |
if self.parameters['status'] == 'up': |
| 215 |
if not self.current_status(): |
| 216 |
if not self.module.check_mode: |
| 217 |
self.start_fcp() |
| 218 |
changed = True |
| 219 |
else: |
| 220 |
if self.current_status(): |
| 221 |
if not self.module.check_mode: |
| 222 |
self.stop_fcp() |
| 223 |
changed = True |
| 224 |
else: |
| 225 |
if not self.module.check_mode: |
| 226 |
self.create_fcp() |
| 227 |
if self.parameters['status'] == 'up': |
| 228 |
self.start_fcp() |
| 229 |
elif self.parameters['status'] == 'down': |
| 230 |
self.stop_fcp() |
| 231 |
changed = True |
| 232 |
else: |
| 233 |
if current: |
| 234 |
if not self.module.check_mode: |
| 235 |
if self.current_status(): |
| 236 |
self.stop_fcp() |
| 237 |
self.destroy_fcp() |
| 238 |
changed = True |
| 239 |
return changed |
| 240 |
|
| 241 |
def apply(self): |
| 242 |
current = self.get_fcp() |
| 243 |
if not self.use_rest: |
| 244 |
changed = self.zapi_apply(current) |
| 245 |
result = netapp_utils.generate_result(changed) |
| 246 |
else: |
| 247 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 248 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 249 |
changed = self.na_helper.changed |
| 250 |
if self.na_helper.changed and not self.module.check_mode: |
| 251 |
if cd_action == 'create': |
| 252 |
self.create_fcp_rest() |
| 253 |
elif modify: |
| 254 |
if modify['status'] == 'up': |
| 255 |
self.start_stop_fcp_rest(True, current) |
| 256 |
else: |
| 257 |
self.start_stop_fcp_rest(False, current) |
| 258 |
elif cd_action == 'delete': |
| 259 |
if current['status'] == 'up': |
| 260 |
self.start_stop_fcp_rest(False, current) |
| 261 |
self.destroy_fcp_rest(current) |
| 262 |
result = netapp_utils.generate_result(changed, cd_action, modify) |
| 263 |
self.module.exit_json(**result) |
| 264 |
|
| 265 |
|
| 266 |
def main(): |
| 267 |
""" |
| 268 |
Start, Stop and Enable FCP services. |
| 269 |
""" |
| 270 |
obj = NetAppOntapFCP() |
| 271 |
obj.apply() |
| 272 |
|
| 273 |
|
| 274 |
if __name__ == '__main__': |
| 275 |
main() |