Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_proxy.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: purefb_proxy
20 version_added: '1.0.0'
21 author:
22 - Pure Storage ansible Team (@sdodsley) <[email protected]>
23 short_description: Configure FlashBlade phonehome HTTPs proxy settings
24 description:
25 - Set or erase configuration for the phonehome proxy settings.
26 options:
27 state:
28 description:
29 - Set or delete proxy configuration
30 default: present
31 type: str
32 choices: [ absent, present ]
33 host:
34 description:
35 - The proxy host name.
36 type: str
37 port:
38 description:
39 - The proxy TCP/IP port number.
40 type: int
41 secure:
42 description:
43 - Use http or https as the proxy protocol.
44 - True uses https, false uses http.
45 default: true
46 type: bool
47 version_added: '1.11.0'
48 extends_documentation_fragment:
49 - purestorage.flashblade.purestorage.fb
50 """
51
52 EXAMPLES = r"""
53 - name: Delete exisitng proxy settings
54 purestorage.flashblade.purefb_proxy:
55 state: absent
56 fb_url: 10.10.10.2
57 api_token: e31060a7-21fc-e277-6240-25983c6c4592
58
59 - name: Set proxy settings
60 purestorage.flashblade.purefb_proxy:
61 host: purestorage.com
62 port: 8080
63 fb_url: 10.10.10.2
64 api_token: e31060a7-21fc-e277-6240-25983c6c4592
65 """
66
67 RETURN = r"""
68 """
69
70 HAS_PYPURECLIENT = True
71 try:
72 from pypureclient.flashblade import Support
73 except ImportError:
74 HAS_PYPURECLIENT = False
75
76
77 from ansible.module_utils.basic import AnsibleModule
78 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
79 get_system,
80 purefb_argument_spec,
81 )
82
83
84 def delete_proxy(module, blade):
85 """Delete proxy settings"""
86 changed = True
87 if not module.check_mode:
88 res = blade.patch_support(support=Support(proxy=""))
89 if res.status_code != 200:
90 module.fail_json(
91 msg="Delete proxy settings failed. Error: {0}".format(
92 res.errors[0].message
93 )
94 )
95 module.exit_json(changed=changed)
96
97
98 def create_proxy(module, blade):
99 """Set proxy settings"""
100 if module.params["secure"]:
101 protocol = "https://"
102 else:
103 protocol = "http://"
104 changed = True
105 new_proxy = protocol + module.params["host"] + ":" + str(module.params["port"])
106 if not module.check_mode:
107 res = blade.patch_support(support=Support(proxy=new_proxy))
108 if res.status_code != 200:
109 module.fail_json(
110 msg="Set phone home proxy failed. Error: {0}".format(
111 res.errors[0].message
112 )
113 )
114
115 module.exit_json(changed=changed)
116
117
118 def update_proxy(module, blade):
119 """Update proxy settings"""
120 changed = False
121 current_proxy = list(blade.get_support().items)[0].proxy
122 if module.params["secure"]:
123 protocol = "https://"
124 else:
125 protocol = "http://"
126 if not module.check_mode:
127 new_proxy = protocol + module.params["host"] + ":" + str(module.params["port"])
128 if new_proxy != current_proxy:
129 changed = True
130 res = blade.patch_support(support=Support(proxy=new_proxy))
131 if res.status_code != 200:
132 module.fail_json(
133 msg="Phone home proxy update failed. Error: {0}".format(
134 res.errors[0].message
135 )
136 )
137
138 module.exit_json(changed=changed)
139
140
141 def main():
142 argument_spec = purefb_argument_spec()
143 argument_spec.update(
144 dict(
145 state=dict(type="str", default="present", choices=["absent", "present"]),
146 secure=dict(type="bool", default=True),
147 host=dict(type="str"),
148 port=dict(type="int"),
149 )
150 )
151
152 required_together = [["host", "port"]]
153
154 module = AnsibleModule(
155 argument_spec, required_together=required_together, supports_check_mode=True
156 )
157
158 if not HAS_PYPURECLIENT:
159 module.fail_json(msg="py-pure-client SDK is required for this module")
160
161 state = module.params["state"]
162 blade = get_system(module)
163 current_proxy = bool(list(blade.get_support().items)[0].proxy)
164
165 if state == "absent" and current_proxy:
166 delete_proxy(module, blade)
167 elif state == "present" and not current_proxy:
168 create_proxy(module, blade)
169 elif state == "present" and current_proxy:
170 update_proxy(module, blade)
171 else:
172 module.exit_json(changed=False)
173
174
175 if __name__ == "__main__":
176 main()