Skip to content

Commit

Permalink
instance: add create by snapshot (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
resmo committed Jan 20, 2023
1 parent 5ed7df2 commit d9664e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions plugins/doc_fragments/56-instance_snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- instance - Added argument ``snapshot`` to support creation of instances via snapshot (https://github.com/vultr/ansible-collection-vultr/pull/56).
24 changes: 22 additions & 2 deletions plugins/modules/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
- List of SSH key names passed to the instance on creation.
type: list
elements: str
snapshot:
description:
- Description or ID of the snapshot.
- Only considered while creating the instance.
type: str
version_added: "1.7.0"
reserved_ipv4:
description:
- IP address of the floating IP to use as the main IP of this instance.
Expand Down Expand Up @@ -454,6 +460,15 @@ def get_firewall_group(self):
fail_not_found=True,
)

def get_snapshot(self):
return self.query_filter_list_by_name(
key_name="description",
param_key="snapshot",
path="/snapshots",
result_key="snapshots",
fail_not_found=True,
)

def get_startup_script(self):
return self.query_filter_list_by_name(
key_name="name",
Expand Down Expand Up @@ -524,6 +539,9 @@ def configure(self):
if self.module.params["startup_script"] is not None:
self.module.params["script_id"] = self.get_startup_script()["id"]

if self.module.params["snapshot"] is not None:
self.module.params["snapshot_id"] = self.get_snapshot()["id"]

if self.module.params["firewall_group"] is not None:
self.module.params["firewall_group_id"] = self.get_firewall_group()["id"]

Expand Down Expand Up @@ -562,7 +580,7 @@ def handle_power_status(self, resource, state, action, power_status, force=False
return resource

def create(self):
param_keys = ("os", "image", "app")
param_keys = ("os", "image", "app", "snapshot")
if not any(self.module.params.get(x) is not None for x in param_keys):
self.module.fail_json(msg="missing required arguements, one of the following required: %s" % ", ".join(param_keys))
return super(AnsibleVultrInstance, self).create()
Expand Down Expand Up @@ -607,6 +625,7 @@ def main():
hostname=dict(type="str"),
app=dict(type="str"),
image=dict(type="str"),
snapshot=dict(type="str"),
os=dict(type="str"),
plan=dict(type="str"),
activation_email=dict(type="bool", default=False),
Expand Down Expand Up @@ -637,7 +656,7 @@ def main():
module = AnsibleModule(
argument_spec=argument_spec,
required_if=(("state", "present", ("plan",)),),
mutually_exclusive=(("os", "app", "image"),),
mutually_exclusive=(("os", "app", "image", "snapshot"),),
supports_check_mode=True,
)

Expand All @@ -654,6 +673,7 @@ def main():
"os_id",
"iso_id",
"image_id",
"snapshot_id",
"script_id",
"region",
"enable_ipv6",
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/targets/instance/tasks/present.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
os: "{{ instance.os | default(omit) }}"
app: "{{ instance.app | default(omit) }}"
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
register: result
check_mode: true
Expand All @@ -43,6 +44,7 @@
os: "{{ instance.os | default(omit) }}"
app: "{{ instance.app | default(omit) }}"
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
register: result
- name: verify test create instance
Expand Down Expand Up @@ -74,6 +76,7 @@
os: "{{ instance.os | default(omit) }}"
app: "{{ instance.app | default(omit) }}"
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
register: result
- name: verify test create instance idempotence
Expand Down Expand Up @@ -105,6 +108,7 @@
os: "{{ instance.os | default(omit) }}"
app: "{{ instance.app | default(omit) }}"
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
register: result
check_mode: true
Expand Down Expand Up @@ -137,6 +141,7 @@
os: "{{ instance.os | default(omit) }}"
app: "{{ instance.app | default(omit) }}"
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
register: result
- name: verify test update instance
Expand Down Expand Up @@ -168,6 +173,7 @@
os: "{{ instance.os | default(omit) }}"
app: "{{ instance.app | default(omit) }}"
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
register: result
- name: verify test update instance idempotence
Expand Down

0 comments on commit d9664e8

Please sign in to comment.