Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_snmp_agent.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2019, Simon Dodsley ([email protected])
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
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_snmp_agent
20 version_added: '1.16.0'
21 short_description: Configure the FlashArray SNMP Agent
22 description:
23 - Manage the I(localhost) SNMP Agent on a Pure Storage FlashArray.
24 - This module is not idempotent and will always modify the SNMP Agent
25 due to hidden parameters that cannot be compared to the task parameters.
26 author:
27 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
28 options:
29 state:
30 type: str
31 description:
32 - Used to set or clear the SNMP v2c community string or the SNMP v3
33 auth and privacy protocols.
34 choices: [ absent, present ]
35 default: present
36 user:
37 type: str
38 description:
39 - SNMP v3 only. User ID which must be between 1 and 32 characters.
40 version:
41 type: str
42 description:
43 - Version of SNMP protocol to use for the manager.
44 choices: [ v2c, v3 ]
45 default: v2c
46 community:
47 type: str
48 description:
49 - SNMP v2c only. Manager community ID under which Purity//FA is to
50 communicate with the specified managers.
51 - To remove the string set I(state) to I(absent) with I(version)
52 set to I(v2c)
53 auth_passphrase:
54 type: str
55 description:
56 - SNMP v3 only. Passphrade used by Purity//FA to authenticate the
57 array with the specified managers.
58 - Must be between 8 and 63 non-space ASCII characters.
59 auth_protocol:
60 type: str
61 description:
62 - SNMP v3 only. Encryption protocol to use
63 - To remove the privacy and auth protocols set I(state) to
64 I(absent) with I(version) set to I(v3)
65 choices: [ MD5, SHA ]
66 privacy_passphrase:
67 type: str
68 description:
69 - SNMP v3 only. Passphrase to encrypt SNMP messages.
70 Must be between 8 and 63 non-space ASCII characters.
71 privacy_protocol:
72 type: str
73 description:
74 - SNMP v3 only. Encryption protocol to use
75 - To remove the privacy and auth protocols set I(state) to
76 I(absent) with I(version) set to I(v3)
77 choices: [ AES, DES ]
78 extends_documentation_fragment:
79 - purestorage.flasharray.purestorage.fa
80 """
81
82 EXAMPLES = r"""
83 - name: Clear SNMP agent v2c community string
84 purestorage.flasharray.purefa_snmp_agent:
85 version: v2c
86 state: absent
87 fa_url: 10.10.10.2
88 api_token: e31060a7-21fc-e277-6240-25983c6c4592
89 - name: Clear SNMP agent v3 auth and privacy protocols
90 purestorage.flasharray.purefa_snmp_agent:
91 version: v3
92 user: admin
93 state: absent
94 fa_url: 10.10.10.2
95 api_token: e31060a7-21fc-e277-6240-25983c6c4592
96 - name: Update v2c SNMP agent
97 purestorage.flasharray.purefa_snmp_agent:
98 version: v2c
99 community: public
100 fa_url: 10.10.10.2
101 api_token: e31060a7-21fc-e277-6240-25983c6c4592
102 - name: Update v3 SNMP manager
103 purestorage.flasharray.purefa_snmp_agent:
104 version: v3
105 user: admin
106 auth_protocol: MD5
107 auth_passphrase: password
108 fa_url: 10.10.10.2
109 api_token: e31060a7-21fc-e277-6240-25983c6c4592
110 """
111
112 RETURN = r"""
113 """
114
115 HAS_PURESTORAGE = True
116 try:
117 from pypureclient import flasharray
118 except ImportError:
119 HAS_PURESTORAGE = False
120
121 from ansible.module_utils.basic import AnsibleModule
122 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
123 get_array,
124 purefa_argument_spec,
125 )
126 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
127 LooseVersion,
128 )
129
130 MIN_REQUIRED_API_VERSION = "2.1"
131
132
133 def update_agent(module, array):
134 """Update SNMP Agent"""
135 changed = False
136 if module.params["version"] == "v2c":
137 changed = True
138 if not module.check_mode:
139 if module.params["state"] == "delete":
140 community = ""
141 elif module.params["state"] == "present" and module.params["community"]:
142 community = module.params["community"]
143 else:
144 community = ""
145 res = array.patch_snmp_agents(
146 snmp_agent=flasharray.SnmpAgentPatch(
147 name="localhost",
148 version="v2c",
149 v2c=flasharray.SnmpV2c(community=community),
150 )
151 )
152 if res.status_code != 200:
153 module.fail_json(
154 msg="Failed to update SNMP agent.Error: {0}".format(
155 res.errors[0].message
156 )
157 )
158 else:
159 if module.params["state"] == "delete":
160 changed = True
161 v3 = flasharray.SnmpV3Patch(
162 auth_protocol="",
163 privacy_protocol="",
164 user=module.params["user"],
165 )
166 elif module.params["auth_protocol"] and module.params["privacy_protocol"]:
167 changed = True
168 v3 = flasharray.SnmpV3Patch(
169 auth_passphrase=module.params["auth_passphrase"],
170 auth_protocol=module.params["auth_protocol"],
171 privacy_passphrase=module.params["privacy_passphrase"],
172 privacy_protocol=module.params["privacy_protocol"],
173 user=module.params["user"],
174 )
175 elif module.params["auth_protocol"] and not module.params["privacy_protocol"]:
176 changed = True
177 v3 = flasharray.SnmpV3Patch(
178 auth_passphrase=module.params["auth_passphrase"],
179 auth_protocol=module.params["auth_protocol"],
180 user=module.params["user"],
181 )
182 elif not module.params["auth_protocol"] and module.params["privacy_protocol"]:
183 changed = True
184 v3 = flasharray.SnmpV3Patch(
185 privacy_passphrase=module.params["privacy_passphrase"],
186 privacy_protocol=module.params["privacy_protocol"],
187 user=module.params["user"],
188 )
189 elif (
190 not module.params["auth_protocol"] and not module.params["privacy_protocol"]
191 ):
192 changed = True
193 v3 = flasharray.SnmpV3Patch(user=module.params["user"])
194
195 if not module.check_mode:
196 res = array.patch_snmp_agents(
197 snmp_agent=flasharray.SnmpAgentPatch(
198 name="localhost",
199 version=module.params["version"],
200 v3=v3,
201 )
202 )
203 if res.status_code != 200:
204 module.fail_json(
205 msg="Failed to update SNMP agent.Error: {0}".format(
206 res.errors[0].message
207 )
208 )
209
210 module.exit_json(changed=changed)
211
212
213 def main():
214 argument_spec = purefa_argument_spec()
215 argument_spec.update(
216 dict(
217 state=dict(type="str", default="present", choices=["absent", "present"]),
218 user=dict(type="str"),
219 auth_passphrase=dict(type="str", no_log=True),
220 auth_protocol=dict(type="str", choices=["MD5", "SHA"]),
221 privacy_passphrase=dict(type="str", no_log=True),
222 privacy_protocol=dict(type="str", choices=["AES", "DES"]),
223 version=dict(type="str", default="v2c", choices=["v2c", "v3"]),
224 community=dict(type="str"),
225 )
226 )
227
228 required_together = [
229 ["auth_passphrase", "auth_protocol"],
230 ["privacy_passphrase", "privacy_protocol"],
231 ]
232
233 module = AnsibleModule(
234 argument_spec,
235 required_together=required_together,
236 supports_check_mode=True,
237 )
238
239 array = get_array(module)
240 api_version = array.get_rest_version()
241
242 if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
243 module.fail_json(
244 msg="FlashArray REST version not supported. "
245 "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
246 )
247 array = get_array(module)
248
249 if module.params["version"] == "v3":
250 if not module.params["user"]:
251 module.fail_json(msg="version is v3 but the following is missing: user")
252 if module.params["auth_passphrase"] and (
253 8 > len(module.params["auth_passphrase"]) > 32
254 ):
255 module.fail_json(msg="auth_password must be between 8 and 32 characters")
256 if (
257 module.params["privacy_passphrase"]
258 and 8 > len(module.params["privacy_passphrase"]) > 63
259 ):
260 module.fail_json(msg="privacy_password must be between 8 and 63 characters")
261 update_agent(module, array)
262 module.exit_json(changed=False)
263
264
265 if __name__ == "__main__":
266 main()