Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_snmp_agent.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2020, 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: purefb_snmp_agent
20 version_added: '1.0.0'
21 short_description: Configure the FlashBlade SNMP Agent
22 description:
23 - Configure the management SNMP Agent on a Pure Storage FlashBlade.
24 - This module is not idempotent and will always modify the
25 existing management SNMP agent due to hidden parameters that cannot
26 be compared to the play parameters.
27 author:
28 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
29 options:
30 auth_passphrase:
31 type: str
32 description:
33 - SNMPv3 only. Passphrase of 8 - 32 characters.
34 auth_protocol:
35 type: str
36 description:
37 - SNMP v3 only. Hash algorithm to use
38 choices: [ MD5, SHA ]
39 community:
40 type: str
41 description:
42 - SNMP v2c only. Manager community ID. Between 1 and 32 characters long.
43 user:
44 type: str
45 description:
46 - SNMP v3 only. User ID recognized by the specified SNMP agent.
47 Must be between 1 and 32 characters.
48 version:
49 type: str
50 description:
51 - Version of SNMP protocol to use for the agent.
52 choices: [ v2c, v3 ]
53 privacy_passphrase:
54 type: str
55 description:
56 - SNMPv3 only. Passphrase to encrypt SNMP messages.
57 Must be between 8 and 63 non-space ASCII characters.
58 privacy_protocol:
59 type: str
60 description:
61 - SNMP v3 only. Encryption protocol to use
62 choices: [ AES, DES ]
63 extends_documentation_fragment:
64 - purestorage.flashblade.purestorage.fb
65 """
66
67 EXAMPLES = r"""
68 - name: Update v2c SNMP agent
69 purestorage.flashblade.purefb_snmp_agent:
70 community: public
71 fb_url: 10.10.10.2
72 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
73 - name: Update v3 SNMP agent
74 purestorage.flashblade.purefb_snmp_agent:
75 version: v3
76 auth_protocol: MD5
77 auth_passphrase: password
78 fb_url: 10.10.10.2
79 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
80 """
81
82 RETURN = r"""
83 """
84
85
86 HAS_PURITY_FB = True
87 try:
88 from pypureclient.flashblade import SnmpAgent, SnmpV2c, SnmpV3
89 except ImportError:
90 HAS_PURITY_FB = False
91
92
93 from ansible.module_utils.basic import AnsibleModule
94 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
95 get_system,
96 purefb_argument_spec,
97 )
98
99
100 def update_agent(module, blade):
101 """Update SNMP Agent"""
102 changed = False
103 res = blade.get_snmp_agents()
104 if res.status_code != 200:
105 module.fail_json(msg="Failed to get configuration for SNMP agent.")
106 agent = list(res.items)[0]
107 current_attr = {
108 "community": agent.v2c.community,
109 "version": agent.version,
110 "auth_passphrase": agent.v3.auth_passphrase,
111 "auth_protocol": agent.v3.auth_protocol,
112 "privacy_passphrase": agent.v3.privacy_passphrase,
113 "privacy_protocol": agent.v3.privacy_protocol,
114 "user": agent.v3.user,
115 }
116 new_attr = {
117 "community": module.params["community"],
118 "version": module.params["version"],
119 "auth_passphrase": module.params["auth_passphrase"],
120 "auth_protocol": module.params["auth_protocol"],
121 "privacy_passphrase": module.params["privacy_passphrase"],
122 "privacy_protocol": module.params["privacy_protocol"],
123 "user": module.params["user"],
124 }
125 if current_attr != new_attr:
126 changed = True
127 if not module.check_mode:
128 if new_attr["version"] == "v2c":
129 updated_v2c_attrs = SnmpV2c(community=new_attr["community"])
130 updated_v2c_agent = SnmpAgent(version="v2c", v2c=updated_v2c_attrs)
131 res = blade.patch_snmp_agents(snmp_agent=updated_v2c_agent)
132 if res.status_code != 200:
133 module.fail_json(
134 msg="Failed to update v2c SNMP agent. Error: {0}".format(
135 res.errors[0].message
136 )
137 )
138 else:
139 updated_v3_attrs = SnmpV3(
140 auth_protocol=new_attr["auth_protocol"],
141 auth_passphrase=new_attr["auth_passphrase"],
142 privacy_protocol=new_attr["privacy_protocol"],
143 privacy_passphrase=new_attr["privacy_passphrase"],
144 user=new_attr["user"],
145 )
146 updated_v3_agent = SnmpAgent(version="v3", v3=updated_v3_attrs)
147 res = blade.patch_snmp_agents(snmp_agent=updated_v3_agent)
148 if res.status_code != 200:
149 module.fail_json(
150 msg="Failed to update v3 SNMP agent. Error: {0}".format(
151 res.errors[0].message
152 )
153 )
154
155 module.exit_json(changed=changed)
156
157
158 def main():
159 argument_spec = purefb_argument_spec()
160 argument_spec.update(
161 dict(
162 user=dict(type="str"),
163 auth_passphrase=dict(type="str", no_log=True),
164 auth_protocol=dict(type="str", choices=["MD5", "SHA"]),
165 privacy_passphrase=dict(type="str", no_log=True),
166 privacy_protocol=dict(type="str", choices=["AES", "DES"]),
167 version=dict(type="str", choices=["v2c", "v3"]),
168 community=dict(type="str"),
169 )
170 )
171
172 required_together = [
173 ["auth_passphrase", "auth_protocol"],
174 ["privacy_passphrase", "privacy_protocol"],
175 ]
176 required_if = [["version", "v2c", ["community"]], ["version", "v3", ["user"]]]
177
178 module = AnsibleModule(
179 argument_spec,
180 required_together=required_together,
181 required_if=required_if,
182 supports_check_mode=True,
183 )
184
185 blade = get_system(module)
186
187 if not HAS_PURITY_FB:
188 module.fail_json(msg="py-pure-client SDK is required for this module")
189
190 if module.params["version"] == "v3":
191 if module.params["auth_passphrase"] and (
192 8 > len(module.params["auth_passphrase"]) > 32
193 ):
194 module.fail_json(msg="auth_password must be between 8 and 32 characters")
195 if (
196 module.params["privacy_passphrase"]
197 and 8 > len(module.params["privacy_passphrase"]) > 63
198 ):
199 module.fail_json(msg="privacy_password must be between 8 and 63 characters")
200
201 update_agent(module, blade)
202
203 module.exit_json(changed=False)
204
205
206 if __name__ == "__main__":
207 main()