Skip to content

Commit

Permalink
instance: add vpc2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
resmo committed Mar 10, 2024
1 parent d3dae8b commit 6f5df5e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 22 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/instance_vpc2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- instance - Implemented ``vpc2`` support (https://github.com/vultr/ansible-collection-vultr/pull/118).
48 changes: 47 additions & 1 deletion plugins/modules/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@
vpcs:
description:
- A list of VPCs identified by their description to be assigned to the instance.
- Mutually exclusive with I(vpc2s).
type: list
elements: str
version_added: "1.5.0"
vpc2s:
description:
- A list of VPCs (VPC 2.0) identified by their description to be assigned to the bare metal machine.
- Mutually exclusive with I(vpcs).
type: list
elements: str
version_added: "1.13.0"
state:
description:
- State of the instance.
Expand Down Expand Up @@ -420,6 +428,37 @@
returned: success
type: str
sample: "5a:01:04:3d:5e:72"
vpc2s:
description: List of VPC2s attached.
returned: success
type: list
version_added: "1.13.0"
contains:
id:
description: ID of the VPC.
returned: success
type: str
sample: 5536d2a4-66fd-4dfb-b839-7672fd5bc116
description:
description: Description of the VPC.
returned: success
type: str
sample: my vpc
ip_address:
description: IP assigned from the VPC.
returned: success
type: str
sample: "192.168.23.3"
mac_address:
description: MAC address of the network interface.
returned: success
type: str
sample: "5a:01:04:3d:5e:72"
node_status:
description: Node status network interface.
returned: success
type: str
sample: "active"
"""


Expand Down Expand Up @@ -498,6 +537,7 @@ def main():
enable_ipv6=dict(type="bool"),
tags=dict(type="list", elements="str"),
vpcs=dict(type="list", elements="str"),
vpc2s=dict(type="list", elements="str"),
reserved_ipv4=dict(type="str"),
firewall_group=dict(type="str"),
startup_script=dict(type="str"),
Expand All @@ -522,7 +562,10 @@ def main():
module = AnsibleModule(
argument_spec=argument_spec,
required_if=(("state", "present", ("plan",)),),
mutually_exclusive=(("os", "app", "image", "snapshot"),),
mutually_exclusive=(
("os", "app", "image", "snapshot"),
("vpcs", "vpc2s"),
),
supports_check_mode=True,
)

Expand Down Expand Up @@ -552,6 +595,7 @@ def main():
"sshkey_id",
"backups",
"attach_vpc",
"attach_vpc2",
"user_scheme",
],
resource_update_param_keys=[
Expand All @@ -564,6 +608,8 @@ def main():
"user_data",
"attach_vpc",
"detach_vpc",
"attach_vpc2",
"detach_vpc2",
],
resource_key_name="label",
)
Expand Down
21 changes: 15 additions & 6 deletions tests/integration/targets/instance/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ vultr_instance_firewall_group: "{{ vultr_resource_prefix }}_instance_fw_group"
vultr_instance_ssh_key_name: "{{ vultr_resource_prefix }}_instance_sshkey"
vultr_instance_ssh_key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAgEAyWYItY+3w5b8PdGRoz0oY5mufqydW96naE+VM3JSvJFAUS08rAjQQpQ03ymoALeHQy6JVZbcgecxn6p0pAOINQdqufn4udPtOPCtMjNiPGpkSM9ah/6X5+kvyWMNrvlf+Ld4OOoszP5sAkgQzIbrFQAm41XknBUha0zkewZwfrVhain4pnDjV7wCcChId/Q/Gbi4xMtXkisznWcAJcueBs3EEZDKhJ5q0VeWSJEhYJDLFN1sOxF0AIUnMrOhfKQ/LjgREXPB6uCl899INUTXRNNjRpeMXyJ2wMMmOAbua2qEd1r13Bu1n+6A823Hzb33fyMXuqWnJwBJ4DCvMlGuEsfuOK+xk7DaBfLHbcM6fsPk0/4psTE6YLgC41remr6+u5ZWsY/faMtSnNPie8Z8Ov0DIYGdhbJjUXk1HomxRV9+ZfZ2Ob8iCwlaAQAyEUM6fs3Kxt8pBD8dx1HOkhsfBWPvuDr5y+kqE7H8/MuPDTc0QgH2pjUMpmw/XBwNDHshVEjrZvtICOjOLUJxcowLO1ivNYwPwowQxfisMy56LfYdjsOslBiqsrkAqvNGm1zu8wKHeqVN9w5l3yUELpvubfm9NKIvYcl6yWF36T0c5vE+g0DU/Jy4XpTj0hZG9QV2mRQcLJnd2pxQtJT7cPFtrn/+tgRxzjEtbDXummDV4sE= [email protected]"

vutr_instance_vpc2s:
- description: "{{ vultr_resource_prefix }}_instance_vpc2_1"
ip_block: 192.168.22.0
prefix_length: 24
region: ams
- description: "{{ vultr_resource_prefix }}_instance_vpc2_2"
ip_block: 192.168.99.0
prefix_length: 24
region: ams

vutr_instance_vpcs:
- description: "{{ vultr_resource_prefix }}_instance_vpc_1"
v4_subnet: 192.168.24.0
Expand Down Expand Up @@ -91,9 +101,8 @@ vultr_instances:
enable_ipv6: true
# API does not disable IPv6 once enabled.
enable_ipv6_update: true
vpcs:
- "{{ vultr_resource_prefix }}_instance_vpc_1"
- "{{ vultr_resource_prefix }}_instance_vpc_2"
vpcs_update:
- "{{ vultr_resource_prefix }}_instance_vpc_1"
- "{{ vultr_resource_prefix }}_instance_vpc_3"
vpc2s:
- "{{ vultr_resource_prefix }}_instance_vpc2_1"
vpc2s_update:
- "{{ vultr_resource_prefix }}_instance_vpc2_1"
- "{{ vultr_resource_prefix }}_instance_vpc2_2"
3 changes: 3 additions & 0 deletions tests/integration/targets/instance/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
- ansible.builtin.import_role:
name: cleanup
tasks_from: cleanup_vpc
- ansible.builtin.import_role:
name: cleanup
tasks_from: cleanup_vpc2
31 changes: 16 additions & 15 deletions tests/integration/targets/instance/tasks/present.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
vpc2s: "{{ instance.vpc2s | default(omit) }}"
register: result
check_mode: true
- name: verify test create instance in check mode
Expand All @@ -46,6 +47,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
vpc2s: "{{ instance.vpc2s | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
- name: verify test create instance
Expand All @@ -57,9 +59,8 @@
# - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'"
- result.vultr_instance.ddos_protection == instance.ddos_protection
- result.vultr_instance.enable_ipv6 == instance.enable_ipv6
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_2') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.vpcs | list | count == (instance.vpcs | default([])) | list | count
- result.vultr_instance.vpc2s | list | count == (instance.vpc2s | default([])) | list | count
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test create instance idempotence
Expand All @@ -80,6 +81,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
vpc2s: "{{ instance.vpc2s | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
- name: verify test create instance idempotence
Expand All @@ -91,9 +93,8 @@
# - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'"
- result.vultr_instance.ddos_protection == instance.ddos_protection
- result.vultr_instance.enable_ipv6 == instance.enable_ipv6
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_2') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.vpcs | list | count == (instance.vpcs | default([])) | list | count
- result.vultr_instance.vpc2s | list | count == (instance.vpc2s | default([])) | list | count
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test update instance in check mode
Expand All @@ -114,6 +115,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
vpc2s: "{{ instance.vpc2s_update | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
check_mode: true
Expand All @@ -126,9 +128,8 @@
# - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'"
- result.vultr_instance.ddos_protection == instance.ddos_protection
- result.vultr_instance.enable_ipv6 == instance.enable_ipv6
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_2') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.vpcs | list | count == (instance.vpcs | default([])) | list | count
- result.vultr_instance.vpc2s | list | count == (instance.vpc2s | default([])) | list | count
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test update instance
Expand All @@ -149,6 +150,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
vpc2s: "{{ instance.vpc2s_update | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
- name: verify test update instance
Expand All @@ -160,9 +162,8 @@
# - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'"
- result.vultr_instance.ddos_protection == instance.ddos_protection_update
- result.vultr_instance.enable_ipv6 == instance.enable_ipv6_update
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_3') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.vpcs | list | count == (instance.vpcs_update | default([])) | list | count
- result.vultr_instance.vpc2s | list | count == (instance.vpc2s_update | default([])) | list | count
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test update instance idempotence
Expand All @@ -183,6 +184,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
vpc2s: "{{ instance.vpc2s_update | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
- name: verify test update instance idempotence
Expand All @@ -194,7 +196,6 @@
# - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'"
- result.vultr_instance.ddos_protection == instance.ddos_protection_update
- result.vultr_instance.enable_ipv6 == instance.enable_ipv6_update
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_3') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.vpcs | list | count == (instance.vpcs_update | default([])) | list | count
- result.vultr_instance.vpc2s | list | count == (instance.vpc2s_update | default([])) | list | count
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')
19 changes: 19 additions & 0 deletions tests/integration/targets/instance/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
region: "{{ item.region }}"
with_items: "{{ vutr_instance_vpcs }}"

- name: setup vpc2s
vultr.cloud.vpc2:
description: "{{ item.description }}"
ip_block: "{{ item.ip_block }}"
prefix_length: "{{ item.prefix_length }}"
region: "{{ item.region }}"
with_items: "{{ vutr_instance_vpc2s }}"

- ansible.builtin.include_tasks: present.yml
with_items: "{{ vultr_instances }}"
loop_control:
Expand Down Expand Up @@ -68,3 +76,14 @@
delay: 3
register: result
until: result is not failed

- name: cleanup vpc2s
vultr.cloud.vpc2:
description: "{{ item.description }}"
region: "{{ item.region }}"
state: absent
with_items: "{{ vutr_instance_vpc2s }}"
retries: 20
delay: 3
register: result
until: result is not failed

0 comments on commit 6f5df5e

Please sign in to comment.