Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, 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_phonehome
20 version_added: '1.0.0'
21 short_description: Enable or Disable Pure Storage FlashBlade Phone Home
22 description:
23 - Enablke or Disable Remote Phone Home for a Pure Storage FlashBlade.
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 state:
28 description:
29 - Define state of phone home
30 type: str
31 default: present
32 choices: [ present, absent, test ]
33 extends_documentation_fragment:
34 - purestorage.flashblade.purestorage.fb
35 """
36
37 EXAMPLES = r"""
38 - name: Enable Remote Phone Home
39 purestorage.flashblade.purefb_phonehome:
40 fb_url: 10.10.10.2
41 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
42 - name: Disable Remote Phone Home
43 purestorage.flashblade.purefb_phonehome:
44 state: absent
45 fb_url: 10.10.10.2
46 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
47 """
48
49 RETURN = r"""
50 """
51
52 HAS_PURITY_FB = True
53 try:
54 from pypureclient.flashblade import Support
55 except ImportError:
56 HAS_PURITY_FB = False
57
58 from ansible.module_utils.basic import AnsibleModule
59 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
60 get_system,
61 purefb_argument_spec,
62 )
63
64
65 def enable_ph(module, blade):
66 """Enable Phone Hone"""
67 changed = True
68 if not module.check_mode:
69 res = blade.patch_support(support=Support(phonehome_enabled=True))
70 if res.status_code != 200:
71 module.fail_json(
72 msg="Enabling Phone Home failed. Error: {0}".format(
73 res.errors[0].message
74 )
75 )
76 module.exit_json(changed=changed)
77
78
79 def disable_ph(module, blade):
80 """Disable Phone Home"""
81 changed = True
82 if not module.check_mode:
83 res = blade.patch_support(support=Support(phonehome_enabled=False))
84 if res.status_code != 200:
85 module.fail_json(
86 msg="Disabling Phone Home failed. Error: {0}".format(
87 res.errors[0].message
88 )
89 )
90 module.exit_json(changed=changed)
91
92
93 def test_ph(module, blade):
94 """Test phonehome configuration"""
95 test_response = []
96 response = list(blade.get_support_test(test_type="phonehome").items)
97 for component in range(len(response)):
98 if response[component].enabled:
99 enabled = "true"
100 else:
101 enabled = "false"
102 if response[component].success:
103 success = "true"
104 else:
105 success = "false"
106 test_response.append(
107 {
108 "component_address": response[component].component_address,
109 "component_name": response[component].component_name,
110 "description": response[component].description,
111 "destination": response[component].destination,
112 "enabled": enabled,
113 "result_details": getattr(response[component], "result_details", ""),
114 "success": success,
115 "test_type": response[component].test_type,
116 }
117 )
118 module.exit_json(changed=False, test_response=test_response)
119
120
121 def main():
122 argument_spec = purefb_argument_spec()
123 argument_spec.update(
124 dict(
125 state=dict(
126 type="str", default="present", choices=["present", "absent", "test"]
127 ),
128 )
129 )
130
131 module = AnsibleModule(argument_spec, supports_check_mode=True)
132
133 blade = get_system(module)
134
135 if not HAS_PURITY_FB:
136 module.fail_json(msg="py-pure-client SDK is required for this module")
137 support = list(blade.get_support().items)[0].phonehome_enabled
138 if module.params["state"] == "present" and not support:
139 enable_ph(module, blade)
140 elif module.params["state"] == "absent" and support:
141 disable_ph(module, blade)
142 elif module.params["state"] == "test":
143 test_ph(module, blade)
144 module.exit_json(changed=False)
145
146
147 if __name__ == "__main__":
148 main()