Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/module_utils/purefa.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # This code is part of Ansible, but is an independent component.
4 # This particular file snippet, and this file snippet only, is BSD licensed.
5 # Modules you write using this snippet, which is embedded dynamically by Ansible
6 # still belong to the author of the module, and may assign their own license
7 # to the complete work.
8 #
9 # Copyright (c), Simon Dodsley <[email protected]>,2017
10 # All rights reserved.
11 #
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
14 #
15 # * Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 # * Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the following disclaimer in the documentation
19 # and/or other materials provided with the distribution.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 from __future__ import absolute_import, division, print_function
32
33 __metaclass__ = type
34
35 HAS_URLLIB3 = True
36 try:
37 import urllib3
38 except ImportError:
39 HAS_URLLIB3 = False
40
41 HAS_DISTRO = True
42 try:
43 import distro
44 except ImportError:
45 HAS_DISTRO = False
46
47 HAS_PYPURECLIENT = True
48 try:
49 from pypureclient import flasharray
50 except ImportError:
51 HAS_PYPURECLIENT = False
52
53 from os import environ
54 import platform
55
56 VERSION = 1.5
57 USER_AGENT_BASE = "Ansible"
58
59
60 def get_array(module):
61 """Return System Object or Fail"""
62 if HAS_URLLIB3 and module.params["disable_warnings"]:
63 urllib3.disable_warnings()
64 if HAS_DISTRO:
65 user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % {
66 "base": USER_AGENT_BASE,
67 "class": __name__,
68 "version": VERSION,
69 "platform": distro.name(pretty=True),
70 }
71 else:
72 user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % {
73 "base": USER_AGENT_BASE,
74 "class": __name__,
75 "version": VERSION,
76 "platform": platform.platform(),
77 }
78 array_name = module.params["fa_url"]
79 api = module.params["api_token"]
80 if HAS_PYPURECLIENT:
81 if array_name and api:
82 system = flasharray.Client(
83 target=array_name,
84 api_token=api,
85 user_agent=user_agent,
86 )
87 elif environ.get("PUREFA_URL") and environ.get("PUREFA_API"):
88 system = flasharray.Client(
89 target=(environ.get("PUREFA_URL")),
90 api_token=(environ.get("PUREFA_API")),
91 user_agent=user_agent,
92 )
93 else:
94 module.fail_json(
95 msg="You must set PUREFA_URL and PUREFA_API environment variables "
96 "or the fa_url and api_token module arguments"
97 )
98 try:
99 system.get_hardware()
100 except Exception:
101 module.fail_json(
102 msg="Pure Storage FlashArray authentication failed. Check your credentials"
103 )
104 else:
105 module.fail_json(msg="py-pure-client and/or requests are not installed.")
106 return system
107
108
109 def purefa_argument_spec():
110 """Return standard base dictionary used for the argument_spec argument in AnsibleModule"""
111
112 return dict(
113 fa_url=dict(),
114 api_token=dict(no_log=True),
115 disable_warnings=dict(type="bool", default=False),
116 )