Add vswitch Integration (#35)

Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
This commit is contained in:
Lukas Kämmerling 2020-11-06 11:00:16 +01:00 committed by GitHub
parent 45ced2b206
commit 207d68677a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 17 deletions

View file

@ -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",

View file

@ -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

View file

@ -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

View file

@ -1,2 +1,2 @@
cloud/hcloud
shippable/hcloud/group2
shippable/hcloud/group3

View file

@ -3,3 +3,4 @@
---
hcloud_prefix: "tests"
hcloud_network_name: "{{hcloud_prefix}}-subnet"
hetzner_vswitch_id: 15311

View file

@ -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}}"