Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/module_utils/purefb.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 flashblade
50 except ImportError:
51 PYPURECLIENT = False
52
53 from os import environ
54 import platform
55
56 VERSION = "1.5"
57 USER_AGENT_BASE = "Ansible"
58 API_AGENT_VERSION = "1.5"
59
60
61 def get_system(module):
62 """Return System Object or Fail"""
63 if module.params["disable_warnings"]:
64 urllib3.disable_warnings()
65 if HAS_DISTRO:
66 user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % {
67 "base": USER_AGENT_BASE,
68 "class": __name__,
69 "version": VERSION,
70 "platform": distro.name(pretty=True),
71 }
72 else:
73 user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % {
74 "base": USER_AGENT_BASE,
75 "class": __name__,
76 "version": VERSION,
77 "platform": platform.platform(),
78 }
79 blade_name = module.params["fb_url"]
80 api = module.params["api_token"]
81
82 if HAS_PYPURECLIENT:
83 if blade_name and api:
84 system = flashblade.Client(
85 target=blade_name,
86 api_token=api,
87 user_agent=user_agent,
88 )
89 elif environ.get("PUREFB_URL") and environ.get("PUREFB_API"):
90 system = flashblade.Client(
91 target=(environ.get("PUREFB_URL")),
92 api_token=(environ.get("PUREFB_API")),
93 user_agent=user_agent,
94 )
95 else:
96 module.fail_json(
97 msg="You must set PUREFB_URL and PUREFB_API environment variables "
98 "or the fb_url and api_token module arguments"
99 )
100 res = system.get_hardware()
101 if res.status_code != 200:
102 module.fail_json(
103 msg="Pure Storage FlashBlade authentication failed. Error: {0}".format(
104 res.errors[0].message
105 )
106 )
107 else:
108 module.fail_json(msg="pypureclient SDK not installed.")
109 return system
110
111
112 def purefb_argument_spec():
113 """Return standard base dictionary used for the argument_spec argument in AnsibleModule"""
114
115 return dict(
116 fb_url=dict(),
117 api_token=dict(no_log=True),
118 disable_warnings=dict(type="bool", default=False),
119 )