Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/modules/na_santricity_iscsi_target.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 # (c) 2024, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8
9 DOCUMENTATION = """
10 ---
11 module: na_santricity_iscsi_target
12 short_description: NetApp E-Series manage iSCSI target configuration
13 description:
14 - Configure the settings of an E-Series iSCSI target
15 author:
16 - Michael Price (@lmprice)
17 - Vu Tran (@VuTran007)
18 extends_documentation_fragment:
19 - netapp_eseries.santricity.santricity.santricity_doc
20 options:
21 name:
22 description:
23 - The name/alias to assign to the iSCSI target.
24 - This alias is often used by the initiator software in order to make an iSCSI target easier to identify.
25 type: str
26 required: false
27 aliases:
28 - alias
29 ping:
30 description:
31 - Enable ICMP ping responses from the configured iSCSI ports.
32 type: bool
33 default: true
34 required: false
35 chap_secret:
36 description:
37 - Enable Challenge-Handshake Authentication Protocol (CHAP), utilizing this value as the password.
38 - When this value is specified, we will always trigger an update (changed=True). We have no way of verifying
39 whether or not the password has changed.
40 - The chap secret may only use ascii characters with values between 32 and 126 decimal.
41 - The chap secret must be no less than 12 characters, but no greater than 57 characters in length.
42 - The chap secret is cleared when not specified or an empty string.
43 type: str
44 required: false
45 aliases:
46 - chap
47 - password
48 unnamed_discovery:
49 description:
50 - When an initiator initiates a discovery session to an initiator port, it is considered an unnamed
51 discovery session if the iSCSI target iqn is not specified in the request.
52 - This option may be disabled to increase security if desired.
53 type: bool
54 default: true
55 required: false
56 notes:
57 - Check mode is supported.
58 - Some of the settings are dependent on the settings applied to the iSCSI interfaces. These can be configured using
59 M(netapp_eseries.santricity.na_santricity_iscsi_interface).
60 - This module requires a Web Services API version of >= 1.3.
61 """
62
63 EXAMPLES = """
64 - name: Enable ping responses and unnamed discovery sessions for all iSCSI ports
65 na_santricity_iscsi_target:
66 ssid: "1"
67 api_url: "https://192.168.1.100:8443/devmgr/v2"
68 api_username: "admin"
69 api_password: "adminpass"
70 validate_certs: true
71 name: myTarget
72 ping: true
73 unnamed_discovery: true
74
75 - name: Set the target alias and the CHAP secret
76 na_santricity_iscsi_target:
77 ssid: "1"
78 api_url: "https://192.168.1.100:8443/devmgr/v2"
79 api_username: "admin"
80 api_password: "adminpass"
81 validate_certs: true
82 name: myTarget
83 chap: password1234
84 """
85
86 RETURN = """
87 msg:
88 description: Success message
89 returned: on success
90 type: str
91 sample: The iSCSI target settings have been updated.
92 alias:
93 description:
94 - The alias assigned to the iSCSI target.
95 returned: on success
96 sample: myArray
97 type: str
98 iqn:
99 description:
100 - The iqn (iSCSI Qualified Name), assigned to the iSCSI target.
101 returned: on success
102 sample: iqn.1992-08.com.netapp:2800.000a132000b006d2000000005a0e8f45
103 type: str
104 """
105 from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule
106 from ansible.module_utils._text import to_native
107
108 HEADERS = {
109 "Content-Type": "application/json",
110 "Accept": "application/json",
111 }
112
113
114 class NetAppESeriesIscsiTarget(NetAppESeriesModule):
115 def __init__(self):
116 ansible_options = dict(name=dict(type="str", required=False, aliases=["alias"]),
117 ping=dict(type="bool", required=False, default=True),
118 chap_secret=dict(type="str", required=False, aliases=["chap", "password"], no_log=True),
119 unnamed_discovery=dict(type="bool", required=False, default=True))
120
121 super(NetAppESeriesIscsiTarget, self).__init__(ansible_options=ansible_options,
122 web_services_version="02.00.0000.0000",
123 supports_check_mode=True)
124
125 args = self.module.params
126
127 self.name = args["name"]
128 self.ping = args["ping"]
129 self.chap_secret = args["chap_secret"]
130 self.unnamed_discovery = args["unnamed_discovery"]
131
132 self.check_mode = self.module.check_mode
133 self.post_body = dict()
134 self.controllers = list()
135
136 if self.chap_secret:
137 if len(self.chap_secret) < 12 or len(self.chap_secret) > 57:
138 self.module.fail_json(msg="The provided CHAP secret is not valid, it must be between 12 and 57"
139 " characters in length.")
140
141 for c in self.chap_secret:
142 ordinal = ord(c)
143 if ordinal < 32 or ordinal > 126:
144 self.module.fail_json(msg="The provided CHAP secret is not valid, it may only utilize ascii"
145 " characters with decimal values between 32 and 126.")
146
147 @property
148 def target(self):
149 """Provide information on the iSCSI Target configuration
150
151 Sample:
152 {
153 "alias": "myCustomName",
154 "ping": True,
155 "unnamed_discovery": True,
156 "chap": False,
157 "iqn": "iqn.1992-08.com.netapp:2800.000a132000b006d2000000005a0e8f45",
158 }
159 """
160 target = dict()
161 try:
162 rc, data = self.request("storage-systems/%s/graph/xpath-filter?query=/storagePoolBundle/target" % self.ssid)
163 # This likely isn"t an iSCSI-enabled system
164 if not data:
165 self.module.fail_json(msg="This storage-system does not appear to have iSCSI interfaces. Array Id [%s]." % self.ssid)
166
167 data = data[0]
168 chap = any(auth for auth in data["configuredAuthMethods"]["authMethodData"] if auth["authMethod"] == "chap")
169 target.update(dict(alias=data["alias"]["iscsiAlias"], iqn=data["nodeName"]["iscsiNodeName"], chap=chap))
170
171 rc, data = self.request("storage-systems/%s/graph/xpath-filter?query=/sa/iscsiEntityData" % self.ssid)
172
173 data = data[0]
174 target.update(dict(ping=data["icmpPingResponseEnabled"], unnamed_discovery=data["unnamedDiscoverySessionsEnabled"]))
175
176 except Exception as err:
177 self.module.fail_json(msg="Failed to retrieve the iSCSI target information. Array Id [%s]. Error [%s]." % (self.ssid, to_native(err)))
178
179 return target
180
181 def apply_iscsi_settings(self):
182 """Update the iSCSI target alias and CHAP settings"""
183 update = False
184 target = self.target
185
186 body = dict()
187
188 if self.name is not None and self.name != target["alias"]:
189 update = True
190 body["alias"] = self.name
191
192 # If the CHAP secret was provided, we trigger an update.
193 if self.chap_secret:
194 update = True
195 body.update(dict(enableChapAuthentication=True,
196 chapSecret=self.chap_secret))
197 # If no secret was provided, then we disable chap
198 elif target["chap"]:
199 update = True
200 body.update(dict(enableChapAuthentication=False))
201
202 if update and not self.check_mode:
203 try:
204 self.request("storage-systems/%s/iscsi/target-settings" % self.ssid, method="POST", data=body)
205 except Exception as err:
206 self.module.fail_json(msg="Failed to update the iSCSI target settings. Array Id [%s]. Error [%s]." % (self.ssid, to_native(err)))
207
208 return update
209
210 def apply_target_changes(self):
211 update = False
212 target = self.target
213
214 body = dict()
215
216 if self.ping != target["ping"]:
217 update = True
218 body["icmpPingResponseEnabled"] = self.ping
219
220 if self.unnamed_discovery != target["unnamed_discovery"]:
221 update = True
222 body["unnamedDiscoverySessionsEnabled"] = self.unnamed_discovery
223
224 if update and not self.check_mode:
225 try:
226 self.request("storage-systems/%s/iscsi/entity" % self.ssid, method="POST", data=body)
227 except Exception as err:
228 self.module.fail_json(msg="Failed to update the iSCSI target settings. Array Id [%s]. Error [%s]." % (self.ssid, to_native(err)))
229 return update
230
231 def update(self):
232 update = self.apply_iscsi_settings()
233 update = self.apply_target_changes() or update
234
235 target = self.target
236 data = dict((key, target[key]) for key in target if key in ["iqn", "alias"])
237
238 self.module.exit_json(msg="The interface settings have been updated.", changed=update, **data)
239
240
241 def main():
242 iface = NetAppESeriesIscsiTarget()
243 iface.update()
244
245
246 if __name__ == "__main__":
247 main()