feat(network): add expose_routes_to_vswitch field (#232)

This commit is contained in:
Jonas L 2023-06-22 13:09:10 +02:00 committed by GitHub
parent 02516d9a7b
commit 81129f373d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 96 additions and 7 deletions

View file

@ -0,0 +1,3 @@
minor_changes:
- hcloud_network_info Return expose_routes_to_vswitch for network.
- hcloud_network Add expose_routes_to_vswitch field.

View file

@ -38,6 +38,11 @@ options:
- IP range of the Network.
- Required if Network does not exist.
type: str
expose_routes_to_vswitch:
description:
- Indicates if the routes from this network should be exposed to the vSwitch connection.
- The exposing only takes effect if a vSwitch connection is active.
type: bool
labels:
description:
- User-defined labels (key-value pairs).
@ -95,6 +100,11 @@ hcloud_network:
type: str
returned: always
sample: 10.0.0.0/8
expose_routes_to_vswitch:
description: Indicates if the routes from this network should be exposed to the vSwitch connection.
type: bool
returned: always
sample: false
delete_protection:
description: True if Network is protected for deletion
type: bool
@ -125,6 +135,7 @@ class AnsibleHcloudNetwork(Hcloud):
"id": to_native(self.hcloud_network.id),
"name": to_native(self.hcloud_network.name),
"ip_range": to_native(self.hcloud_network.ip_range),
"expose_routes_to_vswitch": self.hcloud_network.expose_routes_to_vswitch,
"delete_protection": self.hcloud_network.protection["delete"],
"labels": self.hcloud_network.labels,
}
@ -152,6 +163,11 @@ class AnsibleHcloudNetwork(Hcloud):
"ip_range": self.module.params.get("ip_range"),
"labels": self.module.params.get("labels"),
}
expose_routes_to_vswitch = self.module.params.get("expose_routes_to_vswitch")
if expose_routes_to_vswitch is not None:
params["expose_routes_to_vswitch"] = expose_routes_to_vswitch
try:
if not self.module.check_mode:
self.client.networks.create(**params)
@ -179,6 +195,12 @@ class AnsibleHcloudNetwork(Hcloud):
self.hcloud_network.change_ip_range(ip_range=ip_range).wait_until_finished()
self._mark_as_changed()
expose_routes_to_vswitch = self.module.params.get("expose_routes_to_vswitch")
if expose_routes_to_vswitch is not None and expose_routes_to_vswitch != self.hcloud_network.expose_routes_to_vswitch:
if not self.module.check_mode:
self.hcloud_network.update(expose_routes_to_vswitch=expose_routes_to_vswitch)
self._mark_as_changed()
delete_protection = self.module.params.get("delete_protection")
if delete_protection is not None and delete_protection != self.hcloud_network.protection["delete"]:
if not self.module.check_mode:
@ -213,6 +235,7 @@ class AnsibleHcloudNetwork(Hcloud):
id={"type": "int"},
name={"type": "str"},
ip_range={"type": "str"},
expose_routes_to_vswitch={"type": "bool"},
labels={"type": "dict"},
delete_protection={"type": "bool"},
state={

View file

@ -110,6 +110,11 @@ hcloud_network_info:
returned: always
type: str
sample: 10.0.0.1
expose_routes_to_vswitch:
description: Indicates if the routes from this network should be exposed to the vSwitch connection.
returned: always
type: bool
sample: false
servers:
description: Servers attached to the network
returned: always
@ -239,6 +244,7 @@ class AnsibleHcloudNetworkInfo(Hcloud):
"ip_range": to_native(network.ip_range),
"subnetworks": subnets,
"routes": routes,
"expose_routes_to_vswitch": network.expose_routes_to_vswitch,
"servers": servers,
"labels": network.labels,
"delete_protection": network.protection["delete"],

View file

@ -3,3 +3,5 @@
---
hcloud_prefix: "tests"
hcloud_network_name: "{{hcloud_prefix}}-i"
hcloud_network_name_with_vswitch: "{{hcloud_prefix}}-i-vswitch"

View file

@ -1,15 +1,13 @@
# Copyright: (c) 2019, Hetzner Cloud GmbH <info@hetzner-cloud.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
- name: setup
- name: setup ensure network is absent
hcloud_network:
name: "{{ hcloud_network_name }}"
name: "{{ item }}"
state: absent
register: result
- name: verify setup
assert:
that:
- result is success
with_items:
- "{{ hcloud_network_name }}"
- "{{ hcloud_network_name_with_vswitch }}"
- name: test missing ip_range parameter on create Network
hcloud_network:
@ -55,6 +53,31 @@
that:
- network is not changed
- name: test create Network with expose_routes_to_vswitch
hcloud_network:
name: "{{hcloud_network_name_with_vswitch}}"
ip_range: "10.0.0.0/16"
expose_routes_to_vswitch: true
register: network
- name: verify test create Network with vSwitch result
assert:
that:
- network is changed
- network.hcloud_network.name == "{{hcloud_network_name_with_vswitch}}"
- network.hcloud_network.ip_range == "10.0.0.0/16"
- network.hcloud_network.expose_routes_to_vswitch is true
- name: test create Network with expose_routes_to_vswitch idempotence
hcloud_network:
name: "{{hcloud_network_name_with_vswitch}}"
ip_range: "10.0.0.0/16"
expose_routes_to_vswitch: true
register: network
- name: verify test create network idempotency
assert:
that:
- network is not changed
- name: test update Network label
hcloud_network:
name: "{{hcloud_network_name}}"
@ -99,6 +122,27 @@
that:
- network is not changed
- name: test update Network expose_routes_to_vswitch
hcloud_network:
name: "{{hcloud_network_name_with_vswitch}}"
expose_routes_to_vswitch: false
register: network
- name: verify test update Network expose_routes_to_vswitch
assert:
that:
- network is changed
- network.hcloud_network.expose_routes_to_vswitch is false
- name: test update Network expose_routes_to_vswitch idempotency
hcloud_network:
name: "{{hcloud_network_name_with_vswitch}}"
expose_routes_to_vswitch: false
register: network
- name: verify test update Network expose_routes_to_vswitch idempotency
assert:
that:
- network is not changed
- name: test update Network delete protection
hcloud_network:
name: "{{hcloud_network_name}}"
@ -212,3 +256,13 @@
assert:
that:
- result is success
- name: test delete Network with expose_routes_to_vswitch
hcloud_network:
name: "{{hcloud_network_name_with_vswitch}}"
state: absent
register: result
- name: verify delete Network with expose_routes_to_vswitch
assert:
that:
- result is success

View file

@ -20,6 +20,7 @@
- main_network is changed
- main_network.hcloud_network.name == "{{ hcloud_network_name }}"
- main_network.hcloud_network.ip_range == "10.0.0.0/16"
- main_network.hcloud_network.expose_routes_to_vswitch is false
- name: create subnetwork
hcloud_subnetwork:
network: "{{ hcloud_network_name }}"