Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rebuild server functionality to hcloud_server #54899

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/ansible/modules/cloud/hcloud/hcloud_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
description:
- State of the server.
default: present
choices: [ absent, present, restarted, started, stopped ]
choices: [ absent, present, restarted, started, stopped, rebuild ]
type: str
extends_documentation_fragment: hcloud
"""
Expand Down Expand Up @@ -146,6 +146,12 @@
hcloud_server:
name: my-server
state: restarted

- name: Ensure the server is rebuild
hcloud_server:
name: my-server
image: ubuntu-18.04
state: rebuild
"""

RETURN = """
Expand Down Expand Up @@ -322,6 +328,16 @@ def stop_server(self):
self._mark_as_changed()
self._get_server()

def rebuild_server(self):
self.module.fail_on_missing_params(
required_params=["image"]
)
if not self.module.check_mode:
self.client.servers.rebuild(self.hcloud_server, self.client.images.get_by_name(self.module.params.get("image"))).wait_until_finished()
self._mark_as_changed()

self._get_server()

def present_server(self):
self._get_server()
if self.hcloud_server is None:
Expand Down Expand Up @@ -355,7 +371,7 @@ def define_module():
upgrade_disk={"type": "bool", "default": False},
force_upgrade={"type": "bool", "default": False},
state={
"choices": ["absent", "present", "restarted", "started", "stopped"],
"choices": ["absent", "present", "restarted", "started", "stopped", "rebuild"],
"default": "present",
},
**Hcloud.base_module_arguments()
Expand Down Expand Up @@ -385,6 +401,9 @@ def main():
hcloud.present_server()
hcloud.stop_server()
hcloud.start_server()
elif state == "rebuild":
hcloud.present_server()
hcloud.rebuild_server()

module.exit_json(**hcloud.get_result())

Expand Down
22 changes: 22 additions & 0 deletions test/integration/targets/hcloud_server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,28 @@
- result is not changed
- result.hcloud_server.backup_window != ""

- name: test rebuild server
hcloud_server:
name: "{{ hcloud_server_name }}"
state: rebuild
register: result_after_test
- name: verify rebuild server
assert:
that:
- result is changed
- result.hcloud_server.id == result_after_test.hcloud_server.id

- name: test rebuild server with check mode
hcloud_server:
name: "{{ hcloud_server_name }}"
state: rebuild
register: result_after_test
check_mode: true
- name: verify rebuild server with check mode
assert:
that:
- result is changed

- name: absent server
hcloud_server:
name: "{{ hcloud_server_name }}"
Expand Down