Compare commits

..

12 Commits

Author SHA1 Message Date
chatton cb75e33eb3 wip 3 years ago
chatton 185b620d78 wip 3 years ago
chatton fbd2a49753 wip 3 years ago
chatton 65e0375550 wip 3 years ago
chatton 9d5ebd4def wip 3 years ago
chatton 0b60111d5e wip 3 years ago
chatton 0a8dd345b4 wip 3 years ago
chatton ee03f03aed wip 3 years ago
chatton 2f15f878f8 wip 3 years ago
chatton c6c8b65a55 wip 3 years ago
chatton 6c9c8e72ea wip 3 years ago
chatton 9f92cd06f7 wip 3 years ago

@ -5,6 +5,7 @@ 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
@ -95,7 +96,6 @@ docker_compose_file_path:
sample: ''
"""
COMPOSE_STACK = 2
STRING_METHOD = "string"
@ -115,29 +115,19 @@ def _create_stack(client, module, file_contents):
return client.post("stacks", body=body, query_params=query_params)
def _update_stack(client, module, stack_id):
def _update_stack(contents, 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": file_contents,
"stackFileContent": json.dumps(contents),
},
)
def handle_state_present(client, module):
result = dict(changed=False, stack_name=module.params["stack_name"])
def _handle_state_present(contents, stacks, result, client, module):
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"]:
@ -146,7 +136,7 @@ def handle_state_present(client, module):
break
if not already_exists:
stack = _create_stack(client, module, file_contents)
stack = _create_stack(client, module, contents)
result["changed"] = True
result["stack_id"] = stack["Id"]
module.exit_json(**result)
@ -158,18 +148,36 @@ def handle_state_present(client, module):
)
result["are_equal"] = (
current_file_contents_resp["StackFileContent"] == file_contents
current_file_contents_resp["StackFileContent"] == contents
)
if result["are_equal"]:
module.exit_json(**result)
return
# the stack exists and we have a new config.
_update_stack(client, module, stack_id)
_update_stack(contents, 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
@ -198,6 +206,7 @@ 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),
@ -205,9 +214,9 @@ def run_module():
state=dict(type="str", default="present", choices=["present", "absent"]),
)
required_if = [
required_one_of = [
# docker compose file is only required if we are ensuring the stack is present.
["state", "present", ("docker_compose_file_path",)],
["state", "present", ("docker_compose_file_path", "stack_definition")],
]
state_fns = {"present": handle_state_present, "absent": handle_state_absent}
@ -218,7 +227,7 @@ def run_module():
# supports check mode
module = AnsibleModule(
argument_spec=module_args,
required_if=required_if,
required_one_of=required_one_of,
# TODO: support check mode
supports_check_mode=False,
)

Loading…
Cancel
Save