mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Reformat everything.
This commit is contained in:
@@ -99,6 +99,7 @@ from ansible.module_utils.common.text.converters import to_native
|
||||
import_nomad = None
|
||||
try:
|
||||
import nomad
|
||||
|
||||
import_nomad = True
|
||||
except ImportError:
|
||||
import_nomad = False
|
||||
@@ -107,69 +108,62 @@ except ImportError:
|
||||
def run():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
host=dict(required=True, type='str'),
|
||||
port=dict(type='int', default=4646),
|
||||
state=dict(required=True, choices=['present', 'absent']),
|
||||
use_ssl=dict(type='bool', default=True),
|
||||
timeout=dict(type='int', default=5),
|
||||
validate_certs=dict(type='bool', default=True),
|
||||
client_cert=dict(type='path'),
|
||||
client_key=dict(type='path'),
|
||||
namespace=dict(type='str'),
|
||||
name=dict(type='str'),
|
||||
content_format=dict(choices=['hcl', 'json'], default='hcl'),
|
||||
content=dict(type='str'),
|
||||
force_start=dict(type='bool', default=False),
|
||||
token=dict(type='str', no_log=True)
|
||||
host=dict(required=True, type="str"),
|
||||
port=dict(type="int", default=4646),
|
||||
state=dict(required=True, choices=["present", "absent"]),
|
||||
use_ssl=dict(type="bool", default=True),
|
||||
timeout=dict(type="int", default=5),
|
||||
validate_certs=dict(type="bool", default=True),
|
||||
client_cert=dict(type="path"),
|
||||
client_key=dict(type="path"),
|
||||
namespace=dict(type="str"),
|
||||
name=dict(type="str"),
|
||||
content_format=dict(choices=["hcl", "json"], default="hcl"),
|
||||
content=dict(type="str"),
|
||||
force_start=dict(type="bool", default=False),
|
||||
token=dict(type="str", no_log=True),
|
||||
),
|
||||
supports_check_mode=True,
|
||||
mutually_exclusive=[
|
||||
["name", "content"]
|
||||
],
|
||||
required_one_of=[
|
||||
['name', 'content']
|
||||
]
|
||||
mutually_exclusive=[["name", "content"]],
|
||||
required_one_of=[["name", "content"]],
|
||||
)
|
||||
|
||||
if not import_nomad:
|
||||
module.fail_json(msg=missing_required_lib("python-nomad"))
|
||||
|
||||
certificate_ssl = (module.params.get('client_cert'), module.params.get('client_key'))
|
||||
certificate_ssl = (module.params.get("client_cert"), module.params.get("client_key"))
|
||||
|
||||
nomad_client = nomad.Nomad(
|
||||
host=module.params.get('host'),
|
||||
port=module.params.get('port'),
|
||||
secure=module.params.get('use_ssl'),
|
||||
timeout=module.params.get('timeout'),
|
||||
verify=module.params.get('validate_certs'),
|
||||
host=module.params.get("host"),
|
||||
port=module.params.get("port"),
|
||||
secure=module.params.get("use_ssl"),
|
||||
timeout=module.params.get("timeout"),
|
||||
verify=module.params.get("validate_certs"),
|
||||
cert=certificate_ssl,
|
||||
namespace=module.params.get('namespace'),
|
||||
token=module.params.get('token')
|
||||
namespace=module.params.get("namespace"),
|
||||
token=module.params.get("token"),
|
||||
)
|
||||
|
||||
if module.params.get('state') == "present":
|
||||
|
||||
if module.params.get('name') and not module.params.get('force_start'):
|
||||
module.fail_json(msg='For start job with name, force_start is needed')
|
||||
if module.params.get("state") == "present":
|
||||
if module.params.get("name") and not module.params.get("force_start"):
|
||||
module.fail_json(msg="For start job with name, force_start is needed")
|
||||
|
||||
changed = False
|
||||
if module.params.get('content'):
|
||||
|
||||
if module.params.get('content_format') == 'json':
|
||||
|
||||
job_json = module.params.get('content')
|
||||
if module.params.get("content"):
|
||||
if module.params.get("content_format") == "json":
|
||||
job_json = module.params.get("content")
|
||||
try:
|
||||
job_json = json.loads(job_json)
|
||||
except ValueError as e:
|
||||
module.fail_json(msg=to_native(e))
|
||||
job = dict()
|
||||
job['job'] = job_json
|
||||
job["job"] = job_json
|
||||
try:
|
||||
job_id = job_json.get('ID')
|
||||
job_id = job_json.get("ID")
|
||||
if job_id is None:
|
||||
module.fail_json(msg="Cannot retrieve job with ID None")
|
||||
plan = nomad_client.job.plan_job(job_id, job, diff=True)
|
||||
if not plan['Diff'].get('Type') == "None":
|
||||
if not plan["Diff"].get("Type") == "None":
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
result = nomad_client.jobs.register_job(job)
|
||||
@@ -180,20 +174,19 @@ def run():
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e))
|
||||
|
||||
if module.params.get('content_format') == 'hcl':
|
||||
|
||||
if module.params.get("content_format") == "hcl":
|
||||
try:
|
||||
job_hcl = module.params.get('content')
|
||||
job_hcl = module.params.get("content")
|
||||
job_json = nomad_client.jobs.parse(job_hcl)
|
||||
job = dict()
|
||||
job['job'] = job_json
|
||||
job["job"] = job_json
|
||||
except nomad.api.exceptions.BadRequestNomadException as err:
|
||||
msg = f"{err.nomad_resp.reason} {err.nomad_resp.text}"
|
||||
module.fail_json(msg=to_native(msg))
|
||||
try:
|
||||
job_id = job_json.get('ID')
|
||||
job_id = job_json.get("ID")
|
||||
plan = nomad_client.job.plan_job(job_id, job, diff=True)
|
||||
if not plan['Diff'].get('Type') == "None":
|
||||
if not plan["Diff"].get("Type") == "None":
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
result = nomad_client.jobs.register_job(job)
|
||||
@@ -204,21 +197,20 @@ def run():
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e))
|
||||
|
||||
if module.params.get('force_start'):
|
||||
|
||||
if module.params.get("force_start"):
|
||||
try:
|
||||
job = dict()
|
||||
if module.params.get('name'):
|
||||
job_name = module.params.get('name')
|
||||
if module.params.get("name"):
|
||||
job_name = module.params.get("name")
|
||||
else:
|
||||
job_name = job_json['Name']
|
||||
job_name = job_json["Name"]
|
||||
job_json = nomad_client.job.get_job(job_name)
|
||||
if job_json['Status'] == 'running':
|
||||
if job_json["Status"] == "running":
|
||||
result = job_json
|
||||
else:
|
||||
job_json['Status'] = 'running'
|
||||
job_json['Stop'] = False
|
||||
job['job'] = job_json
|
||||
job_json["Status"] = "running"
|
||||
job_json["Stop"] = False
|
||||
job["job"] = job_json
|
||||
if not module.check_mode:
|
||||
result = nomad_client.jobs.register_job(job)
|
||||
else:
|
||||
@@ -230,20 +222,19 @@ def run():
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e))
|
||||
|
||||
if module.params.get('state') == "absent":
|
||||
|
||||
if module.params.get("state") == "absent":
|
||||
try:
|
||||
if not module.params.get('name') is None:
|
||||
job_name = module.params.get('name')
|
||||
if not module.params.get("name") is None:
|
||||
job_name = module.params.get("name")
|
||||
else:
|
||||
if module.params.get('content_format') == 'hcl':
|
||||
job_json = nomad_client.jobs.parse(module.params.get('content'))
|
||||
job_name = job_json['Name']
|
||||
if module.params.get('content_format') == 'json':
|
||||
job_json = module.params.get('content')
|
||||
job_name = job_json['Name']
|
||||
if module.params.get("content_format") == "hcl":
|
||||
job_json = nomad_client.jobs.parse(module.params.get("content"))
|
||||
job_name = job_json["Name"]
|
||||
if module.params.get("content_format") == "json":
|
||||
job_json = module.params.get("content")
|
||||
job_name = job_json["Name"]
|
||||
job = nomad_client.job.get_job(job_name)
|
||||
if job['Status'] == 'dead':
|
||||
if job["Status"] == "dead":
|
||||
changed = False
|
||||
result = job
|
||||
else:
|
||||
@@ -259,7 +250,6 @@ def run():
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
run()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user