Compare commits

..

No commits in common. 'cb75e33eb3be0ae61140a15e0379a13b21183326' and '894d68d1610c97076b67e4ba719fde2a2e42a855' have entirely different histories.

@ -5,7 +5,6 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible.module_utils.basic import AnsibleModule
import json
try:
# FIXME: Hack to make imports work with IDE. The ansible import path is not valid for a regular python
@ -96,6 +95,7 @@ docker_compose_file_path:
sample: ''
"""
COMPOSE_STACK = 2
STRING_METHOD = "string"
@ -115,19 +115,29 @@ def _create_stack(client, module, file_contents):
return client.post("stacks", body=body, query_params=query_params)
def _update_stack(contents, client, module, stack_id):
def _update_stack(client, module, stack_id):
target_stack_name = module.params["stack_name"]
with open(module.params["docker_compose_file_path"]) as f:
file_contents = f.read()
return client.put(
f"stacks/{stack_id}?&endpointId={client.endpoint}",
body={
"name": target_stack_name,
"stackFileContent": json.dumps(contents),
"stackFileContent": file_contents,
},
)
def _handle_state_present(contents, stacks, result, client, module):
def handle_state_present(client, module):
result = dict(changed=False, stack_name=module.params["stack_name"])
already_exists = False
stacks = client.get("stacks")
result["stacks"] = stacks
with open(module.params["docker_compose_file_path"]) as f:
file_contents = f.read()
target_stack_name = module.params["stack_name"]
for stack in stacks:
if stack["Name"] == target_stack_name and int(stack["EndpointId"]) == module.params["endpoint_id"]:
@ -136,7 +146,7 @@ def _handle_state_present(contents, stacks, result, client, module):
break
if not already_exists:
stack = _create_stack(client, module, contents)
stack = _create_stack(client, module, file_contents)
result["changed"] = True
result["stack_id"] = stack["Id"]
module.exit_json(**result)
@ -148,36 +158,18 @@ def _handle_state_present(contents, stacks, result, client, module):
)
result["are_equal"] = (
current_file_contents_resp["StackFileContent"] == contents
current_file_contents_resp["StackFileContent"] == file_contents
)
if result["are_equal"]:
module.exit_json(**result)
return
# the stack exists and we have a new config.
_update_stack(contents, client, module, stack_id)
_update_stack(client, module, stack_id)
result["changed"] = True
module.exit_json(**result)
def handle_state_present(client, module):
result = dict(changed=False, stack_name=module.params["stack_name"])
stacks = client.get("stacks")
result["stacks"] = stacks
contents = ""
if module.params.get("docker_compose_file_path"):
with open(module.params["docker_compose_file_path"]) as f:
contents = f.read()
elif module.params.get("stack_definition"):
contents = json.dumps(module.params["stack_definition"], indent=4)
else:
raise ValueError("Should not be able to be here!")
_handle_state_present(contents, stacks, result, client, module)
def handle_state_absent(client, module):
result = dict(changed=False, stack_name=module.params["stack_name"])
already_exists = False
@ -206,7 +198,6 @@ def run_module():
module_args = dict(
stack_name=dict(type="str", required=True),
docker_compose_file_path=dict(type="str"),
stack_definition=dict(type=str),
username=dict(type="str", default="admin"),
password=dict(type="str", required=True, no_log=True),
endpoint_id=dict(type="int", required=True),
@ -214,9 +205,9 @@ def run_module():
state=dict(type="str", default="present", choices=["present", "absent"]),
)
required_one_of = [
required_if = [
# docker compose file is only required if we are ensuring the stack is present.
["state", "present", ("docker_compose_file_path", "stack_definition")],
["state", "present", ("docker_compose_file_path",)],
]
state_fns = {"present": handle_state_present, "absent": handle_state_absent}
@ -227,7 +218,7 @@ def run_module():
# supports check mode
module = AnsibleModule(
argument_spec=module_args,
required_one_of=required_one_of,
required_if=required_if,
# TODO: support check mode
supports_check_mode=False,
)

Loading…
Cancel
Save