From 2cd2392218d84aad399a7a40961f834691de80df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Tue, 3 Nov 2020 12:18:15 +0100 Subject: [PATCH] Add vswitch Integration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Kämmerling --- plugins/modules/hcloud_subnetwork.py | 51 +++++++++++++++---- shippable.yml | 2 +- .../tasks/main.yml | 2 +- .../targets/hcloud_subnetwork/aliases | 2 +- .../hcloud_subnetwork/defaults/main.yml | 1 + .../targets/hcloud_subnetwork/tasks/main.yml | 41 +++++++++++++-- 6 files changed, 82 insertions(+), 17 deletions(-) diff --git a/plugins/modules/hcloud_subnetwork.py b/plugins/modules/hcloud_subnetwork.py index b3123f2..88ad7a9 100644 --- a/plugins/modules/hcloud_subnetwork.py +++ b/plugins/modules/hcloud_subnetwork.py @@ -36,12 +36,18 @@ options: description: - Type of subnetwork. type: str + choices: [ server, cloud, vswitch ] required: true network_zone: description: - Name of network zone. type: str required: true + vswitch_id: + description: + - ID of the vSwitch you want to couple with your Network. + - Required if type == vswitch + type: int state: description: - State of the subnetwork. @@ -50,7 +56,7 @@ options: type: str requirements: - - hcloud-python >= 1.3.0 + - hcloud-python >= 1.10.0 extends_documentation_fragment: - hetzner.hcloud.hcloud @@ -63,7 +69,16 @@ EXAMPLES = """ network: my-network ip_range: 10.0.0.0/16 network_zone: eu-central - type: server + type: cloud + state: present + +- name: Create a basic subnetwork + hcloud_subnetwork: + network: my-vswitch-network + ip_range: 10.0.0.0/24 + network_zone: eu-central + type: vswitch + vswitch_id: 123 state: present - name: Ensure the subnetwork is absent (remove if needed) @@ -71,7 +86,7 @@ EXAMPLES = """ network: my-network ip_range: 10.0.0.0/8 network_zone: eu-central - type: server + type: cloud state: absent """ @@ -101,6 +116,11 @@ hcloud_subnetwork: type: str returned: always sample: eu-central + vswitch_id: + description: ID of the vswitch, null if not type vswitch + type: int + returned: always + sample: 123 gateway: description: Gateway of the subnetwork type: str @@ -133,6 +153,7 @@ class AnsibleHcloudSubnetwork(Hcloud): "type": to_native(self.hcloud_subnetwork.type), "network_zone": to_native(self.hcloud_subnetwork.network_zone), "gateway": self.hcloud_subnetwork.gateway, + "vswitch_id": self.hcloud_subnetwork.vswitch_id, } def _get_network(self): @@ -149,15 +170,20 @@ class AnsibleHcloudSubnetwork(Hcloud): self.hcloud_subnetwork = subnetwork def _create_subnetwork(self): - subnet = NetworkSubnet( - ip_range=self.module.params.get("ip_range"), - type=self.module.params.get('type'), - network_zone=self.module.params.get('network_zone') - ) + params = { + "ip_range": self.module.params.get("ip_range"), + "type": self.module.params.get('type'), + "network_zone": self.module.params.get('network_zone') + } + if self.module.params.get('type') == NetworkSubnet.TYPE_VSWITCH: + self.module.fail_on_missing_params( + required_params=["vswitch_id"] + ) + params["vswitch_id"] = self.module.params.get('vswitch_id') if not self.module.check_mode: try: - self.hcloud_network.add_subnet(subnet=subnet).wait_until_finished() + self.hcloud_network.add_subnet(subnet=NetworkSubnet(**params)).wait_until_finished() except APIException as e: self.module.fail_json(msg=e.message) @@ -186,8 +212,13 @@ class AnsibleHcloudSubnetwork(Hcloud): argument_spec=dict( network={"type": "str", "required": True}, network_zone={"type": "str", "required": True}, - type={"type": "str", "required": True}, + type={ + "type": "str", + "required": True, + "choices": ["server", "cloud", "vswitch"] + }, ip_range={"type": "str", "required": True}, + vswitch_id={"type": "int"}, state={ "choices": ["absent", "present"], "default": "present", diff --git a/shippable.yml b/shippable.yml index 56021c0..885c381 100644 --- a/shippable.yml +++ b/shippable.yml @@ -1,7 +1,6 @@ language: python python: 3.7 - env: matrix: - T=none @@ -30,6 +29,7 @@ matrix: - env: T=2.10/hcloud/3.8/1 - env: T=2.10/hcloud/3.8/2 + - env: T=2.10/hcloud/3.8/3 - env: T=2.9/sanity/1 diff --git a/tests/integration/targets/hcloud_load_balancer_network/tasks/main.yml b/tests/integration/targets/hcloud_load_balancer_network/tasks/main.yml index 7f16225..76788ed 100644 --- a/tests/integration/targets/hcloud_load_balancer_network/tasks/main.yml +++ b/tests/integration/targets/hcloud_load_balancer_network/tasks/main.yml @@ -135,7 +135,7 @@ hcloud_subnetwork: network: "{{ hcloud_network_name }}" ip_range: "10.0.0.0/16" - type: "load_balancer" + type: "cloud" network_zone: "eu-central" state: absent register: result diff --git a/tests/integration/targets/hcloud_subnetwork/aliases b/tests/integration/targets/hcloud_subnetwork/aliases index 55ec821..af1d98c 100644 --- a/tests/integration/targets/hcloud_subnetwork/aliases +++ b/tests/integration/targets/hcloud_subnetwork/aliases @@ -1,2 +1,2 @@ cloud/hcloud -shippable/hcloud/group2 +shippable/hcloud/group3 diff --git a/tests/integration/targets/hcloud_subnetwork/defaults/main.yml b/tests/integration/targets/hcloud_subnetwork/defaults/main.yml index bba960f..51a123d 100644 --- a/tests/integration/targets/hcloud_subnetwork/defaults/main.yml +++ b/tests/integration/targets/hcloud_subnetwork/defaults/main.yml @@ -3,3 +3,4 @@ --- hcloud_prefix: "tests" hcloud_network_name: "{{hcloud_prefix}}-subnet" +hetzner_vswitch_id: 15311 diff --git a/tests/integration/targets/hcloud_subnetwork/tasks/main.yml b/tests/integration/targets/hcloud_subnetwork/tasks/main.yml index 06bcf83..0453f9d 100644 --- a/tests/integration/targets/hcloud_subnetwork/tasks/main.yml +++ b/tests/integration/targets/hcloud_subnetwork/tasks/main.yml @@ -42,7 +42,7 @@ hcloud_subnetwork: network: "{{ hcloud_network_name }}" ip_range: "10.0.0.0/16" - type: "server" + type: "cloud" network_zone: "eu-central" state: present register: subnet @@ -52,14 +52,14 @@ - subnet is changed - subnet.hcloud_subnetwork.network == "{{ hcloud_network_name }}" - subnet.hcloud_subnetwork.ip_range == "10.0.0.0/16" - - subnet.hcloud_subnetwork.type == "server" + - subnet.hcloud_subnetwork.type == "cloud" - subnet.hcloud_subnetwork.network_zone == "eu-central" - name: test create subnetwork idempotency hcloud_subnetwork: network: "{{ hcloud_network_name }}" ip_range: "10.0.0.0/16" - type: "server" + type: "cloud" network_zone: "eu-central" state: present register: result @@ -72,7 +72,7 @@ hcloud_subnetwork: network: "{{ hcloud_network_name }}" ip_range: "10.0.0.0/16" - type: "server" + type: "cloud" network_zone: "eu-central" state: absent register: result @@ -81,6 +81,39 @@ that: - result is changed +- name: test vswitch subnetwork + hcloud_subnetwork: + network: "{{ hcloud_network_name }}" + ip_range: "10.0.0.0/16" + type: "vswitch" + network_zone: "eu-central" + vswitch_id: "{{ hetzner_vswitch_id }}" + state: present + register: subnet +- name: verify test vswitch subnetwork + assert: + that: + - subnet is changed + - subnet.hcloud_subnetwork.network == "{{ hcloud_network_name }}" + - subnet.hcloud_subnetwork.ip_range == "10.0.0.0/16" + - subnet.hcloud_subnetwork.type == "vswitch" + - subnet.hcloud_subnetwork.network_zone == "eu-central" + - subnet.hcloud_subnetwork.vswitch_id == hetzner_vswitch_id + +- name: test absent subnetwork + hcloud_subnetwork: + network: "{{ hcloud_network_name }}" + ip_range: "10.0.0.0/16" + type: "vswitch" + network_zone: "eu-central" + vswitch_id: "{{ hetzner_vswitch_id }}" + state: absent + register: subnet +- name: verify test absent subnetwork + assert: + that: + - result is changed + - name: cleanup hcloud_network: name: "{{hcloud_network_name}}"