| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
) |