Add hcloud_load_balancer_info module (#41)

Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
This commit is contained in:
Lukas Kämmerling 2020-11-27 12:25:06 +01:00 committed by GitHub
parent 8bb449d3de
commit 0617bc65f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 584 additions and 29 deletions

View file

@ -4,7 +4,7 @@ stages:
ansible-devel-1/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
allow_failure: true
except:
- tags
@ -15,7 +15,7 @@ ansible-devel-1/4:
ansible-devel-2/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
allow_failure: true
except:
- tags
@ -26,7 +26,7 @@ ansible-devel-2/4:
ansible-devel-3/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
allow_failure: true
except:
- tags
@ -37,7 +37,7 @@ ansible-devel-3/4:
ansible-devel-4/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
allow_failure: true
except:
- tags
@ -48,7 +48,7 @@ ansible-devel-4/4:
ansible-210-1/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -58,7 +58,7 @@ ansible-210-1/4:
ansible-210-2/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -68,7 +68,7 @@ ansible-210-2/4:
ansible-210-3/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -78,7 +78,7 @@ ansible-210-3/4:
ansible-210-4/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -88,7 +88,7 @@ ansible-210-4/4:
ansible-29-1/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -98,7 +98,7 @@ ansible-29-1/4:
ansible-29-2/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -108,7 +108,7 @@ ansible-29-2/4:
ansible-29-3/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -118,7 +118,7 @@ ansible-29-3/4:
ansible-29-4/4:
stage: sanity
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
@ -126,27 +126,39 @@ ansible-29-4/4:
tags:
- hc-bladerunner
ansible-devel-1/2:
ansible-devel-1/3:
stage: integration
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
- echo "$HCLOUD_TOKEN" >> "$(pwd)/hcloud_token.txt"
- echo "py38-$CI_JOB_ID" >> "$(pwd)/prefix.txt"
- bash tests/utils/gitlab/gitlab.sh devel/hcloud/3.8/1
- bash tests/utils/gitlab/gitlab.sh devel/hcloud/3.6/1
tags:
- hc-bladerunner
ansible-devel-2/2:
ansible-devel-2/3:
stage: integration
image: python:3.8-buster
image: python:3.6-buster
except:
- tags
script:
- echo "$HCLOUD_TOKEN" >> "$(pwd)/hcloud_token.txt"
- echo "py39-$CI_JOB_ID" >> "$(pwd)/prefix.txt"
- bash tests/utils/gitlab/gitlab.sh devel/hcloud/3.8/2
- bash tests/utils/gitlab/gitlab.sh devel/hcloud/3.6/2
tags:
- hc-bladerunner
ansible-devel-3/3:
stage: integration
image: python:3.6-buster
except:
- tags
script:
- echo "$HCLOUD_TOKEN" >> "$(pwd)/hcloud_token.txt"
- echo "py39-$CI_JOB_ID" >> "$(pwd)/prefix.txt"
- bash tests/utils/gitlab/gitlab.sh devel/hcloud/3.6/3
tags:
- hc-bladerunner

View file

@ -1,3 +1,4 @@
title: Hetzner Cloud Ansible Collection
changelog_filename_template: ../CHANGELOG.rst
changelog_filename_version_depth: 0
changes_file: changelog.yaml
@ -25,4 +26,3 @@ sections:
- Bugfixes
- - known_issues
- Known Issues
title: Hetzner Cloud Ansible Collection

View file

@ -231,12 +231,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
def verify_file(self, path):
"""Return the possibly of a file being consumable by this plugin."""
return (
super(InventoryModule, self).verify_file(path) and
super().verify_file(path) and
path.endswith(("hcloud.yaml", "hcloud.yml"))
)
def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path, cache)
super().parse(inventory, loader, path, cache)
self._read_config_data(path)
self._configure_hcloud_client()
self._test_hcloud_token()

View file

@ -177,7 +177,7 @@ except ImportError:
class AnsibleHcloudFloatingIP(Hcloud):
def __init__(self, module):
super(AnsibleHcloudFloatingIP, self).__init__(module, "hcloud_floating_ip")
Hcloud.__init__(self, module, "hcloud_floating_ip")
self.hcloud_floating_ip = None
def _prepare_result(self):

View file

@ -0,0 +1,403 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2020, 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)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: hcloud_load_balancer_info
short_description: Gather infos about your Hetzner Cloud load_balancers.
description:
- Gather infos about your Hetzner Cloud load_balancers.
author:
- Lukas Kaemmerling (@LKaemmerling)
options:
id:
description:
- The ID of the Load Balancers you want to get.
type: int
name:
description:
- The name of the Load Balancers you want to get.
type: str
label_selector:
description:
- The label selector for the Load Balancers you want to get.
type: str
extends_documentation_fragment:
- hetzner.hcloud.hcloud
'''
EXAMPLES = """
- name: Gather hcloud load_balancer infos
hcloud_load_balancer_info:
register: output
- name: Print the gathered infos
debug:
var: output
"""
RETURN = """
hcloud_load_balancer_info:
description: The load_balancer infos as list
returned: always
type: complex
contains:
id:
description: Numeric identifier of the Load Balancer
returned: always
type: int
sample: 1937415
name:
description: Name of the Load Balancer
returned: always
type: str
sample: my-Load-Balancer
status:
description: Status of the Load Balancer
returned: always
type: str
sample: running
load_balancer_type:
description: Name of the Load Balancer type of the Load Balancer
returned: always
type: str
sample: cx11
ipv4_address:
description: Public IPv4 address of the Load Balancer
returned: always
type: str
sample: 116.203.104.109
ipv6_address:
description: Public IPv6 address of the Load Balancer
returned: always
type: str
sample: 2a01:4f8:1c1c:c140::1
location:
description: Name of the location of the Load Balancer
returned: always
type: str
sample: fsn1
labels:
description: User-defined labels (key-value pairs)
returned: always
type: dict
delete_protection:
description: True if Load Balancer is protected for deletion
type: bool
returned: always
sample: false
disable_public_interface:
description: True if Load Balancer public interface is disabled
type: bool
returned: always
sample: false
targets:
description: The targets of the Load Balancer
returned: always
type: complex
contains:
type:
description: Type of the Load Balancer Target
type: str
returned: always
sample: server
load_balancer:
description: Name of the Load Balancer
type: str
returned: always
sample: my-LoadBalancer
server:
description: Name of the Server
type: str
returned: if I(type) is server
sample: my-server
label_selector:
description: Label Selector
type: str
returned: if I(type) is label_selector
sample: application=backend
ip:
description: IP of the dedicated server
type: str
returned: if I(type) is ip
sample: 127.0.0.1
use_private_ip:
description:
- Route the traffic over the private IP of the Load Balancer through a Hetzner Cloud Network.
- Load Balancer needs to be attached to a network. See M(hetzner.hcloud.hcloud.hcloud_load_balancer_network)
type: bool
sample: true
returned: always
services:
description: all services from this Load Balancer
returned: Always
type: complex
contains:
listen_port:
description: The port the service listens on, i.e. the port users can connect to.
returned: always
type: int
sample: 443
protocol:
description: Protocol of the service
returned: always
type: str
sample: http
destination_port:
description:
- The port traffic is forwarded to, i.e. the port the targets are listening and accepting connections on.
returned: always
type: int
sample: 80
proxyprotocol:
description:
- Enable the PROXY protocol.
returned: always
type: bool
sample: false
http:
description: Configuration for HTTP and HTTPS services
returned: always
type: complex
contains:
cookie_name:
description: Name of the cookie which will be set when you enable sticky sessions
returned: always
type: str
sample: HCLBSTICKY
cookie_lifetime:
description: Lifetime of the cookie which will be set when you enable sticky sessions, in seconds
returned: always
type: int
sample: 3600
certificates:
description: List of Names or IDs of certificates
returned: always
type: list
elements: str
sticky_sessions:
description: Enable or disable sticky_sessions
returned: always
type: bool
sample: true
redirect_http:
description: Redirect Traffic from Port 80 to Port 443, only available if protocol is https
returned: always
type: bool
sample: false
health_check:
description: Configuration for health checks
returned: always
type: complex
contains:
protocol:
description: Protocol the health checks will be performed over
returned: always
type: str
sample: http
port:
description: Port the health check will be performed on
returned: always
type: int
sample: 80
interval:
description: Interval of health checks, in seconds
returned: always
type: int
sample: 15
timeout:
description: Timeout of health checks, in seconds
returned: always
type: int
sample: 10
retries:
description: Number of retries until a target is marked as unhealthy
returned: always
type: int
sample: 3
http:
description: Additional Configuration of health checks with protocol http/https
returned: always
type: complex
contains:
domain:
description: Domain we will set within the HTTP HOST header
returned: always
type: str
sample: example.com
path:
description: Path we will try to access
returned: always
type: str
sample: /
response:
description: Response we expect, if response is not within the health check response the target is unhealthy
returned: always
type: str
status_codes:
description: List of HTTP status codes we expect to get when we perform the health check.
returned: always
type: list
elements: str
sample: ["2??","3??"]
tls:
description: Verify the TLS certificate, only available if health check protocol is https
returned: always
type: bool
sample: false
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
try:
from hcloud import APIException
except ImportError:
APIException = None
class AnsibleHcloudLoadBalancerInfo(Hcloud):
def __init__(self, module):
Hcloud.__init__(self, module, "hcloud_load_balancer_info")
self.hcloud_load_balancer_info = None
def _prepare_result(self):
tmp = []
for load_balancer in self.hcloud_load_balancer_info:
if load_balancer is not None:
services = [self._prepare_service_result(service) for service in load_balancer.services]
targets = [self._prepare_target_result(target) for target in load_balancer.targets]
private_ipv4_address = None if len(load_balancer.private_net) == 0 else to_native(
load_balancer.private_net[0].ip)
tmp.append({
"id": to_native(load_balancer.id),
"name": to_native(load_balancer.name),
"ipv4_address": to_native(load_balancer.public_net.ipv4.ip),
"ipv6_address": to_native(load_balancer.public_net.ipv6.ip),
"private_ipv4_address": private_ipv4_address,
"load_balancer_type": to_native(load_balancer.load_balancer_type.name),
"location": to_native(load_balancer.location.name),
"labels": load_balancer.labels,
"delete_protection": load_balancer.protection["delete"],
"disable_public_interface": False if load_balancer.public_net.enabled else True,
"targets": targets,
"services": services
})
return tmp
@staticmethod
def _prepare_service_result(service):
http = None
if service.protocol != "tcp":
http = {
"cookie_name": to_native(service.http.cookie_name),
"cookie_lifetime": service.http.cookie_name,
"redirect_http": service.http.redirect_http,
"sticky_sessions": service.http.sticky_sessions,
"certificates": [to_native(certificate.name) for certificate in
service.http.certificates],
}
health_check = {
"protocol": to_native(service.health_check.protocol),
"port": service.health_check.port,
"interval": service.health_check.interval,
"timeout": service.health_check.timeout,
"retries": service.health_check.retries,
}
if service.health_check.protocol != "tcp":
health_check["http"] = {
"domain": to_native(service.health_check.http.domain),
"path": to_native(service.health_check.http.path),
"response": to_native(service.health_check.http.response),
"certificates": [to_native(status_code) for status_code in
service.health_check.http.status_codes],
"tls": service.health_check.http.tls,
}
return {
"protocol": to_native(service.protocol),
"listen_port": service.listen_port,
"destination_port": service.destination_port,
"proxyprotocol": service.proxyprotocol,
"http": http,
"health_check": health_check,
}
@staticmethod
def _prepare_target_result(target):
result = {
"type": to_native(target.type),
"use_private_ip": target.use_private_ip
}
if target.type == "server":
result["server"] = to_native(target.server.name)
elif target.type == "label_selector":
result["label_selector"] = to_native(target.label_selector.selector)
elif target.type == "ip":
result["ip"] = to_native(target.ip.ip)
return result
def get_load_balancers(self):
try:
if self.module.params.get("id") is not None:
self.hcloud_load_balancer_info = [self.client.load_balancers.get_by_id(
self.module.params.get("id")
)]
elif self.module.params.get("name") is not None:
self.hcloud_load_balancer_info = [self.client.load_balancers.get_by_name(
self.module.params.get("name")
)]
else:
params = {}
label_selector = self.module.params.get("label_selector")
if label_selector:
params["label_selector"] = label_selector
self.hcloud_load_balancer_info = self.client.load_balancers.get_all(**params)
except APIException as e:
self.module.fail_json(msg=e.message)
@staticmethod
def define_module():
return AnsibleModule(
argument_spec=dict(
id={"type": "int"},
name={"type": "str"},
label_selector={"type": "str"},
**Hcloud.base_module_arguments()
),
supports_check_mode=True,
)
def main():
module = AnsibleHcloudLoadBalancerInfo.define_module()
hcloud = AnsibleHcloudLoadBalancerInfo(module)
hcloud.get_load_balancers()
result = hcloud.get_result()
ansible_info = {
'hcloud_load_balancer_info': result['hcloud_load_balancer_info']
}
module.exit_json(**ansible_info)
if __name__ == "__main__":
main()

View file

@ -108,7 +108,7 @@ except ImportError:
class AnsibleHcloudLoadBalancerNetwork(Hcloud):
def __init__(self, module):
super(AnsibleHcloudLoadBalancerNetwork, self).__init__(module, "hcloud_load_balancer_network")
Hcloud.__init__(self, module, "hcloud_load_balancer_network")
self.hcloud_network = None
self.hcloud_load_balancer = None
self.hcloud_load_balancer_network = None

View file

@ -154,7 +154,7 @@ except ImportError:
class AnsibleHcloudLoadBalancerTarget(Hcloud):
def __init__(self, module):
super(AnsibleHcloudLoadBalancerTarget, self).__init__(module, "hcloud_load_balancer_target")
Hcloud.__init__(self, module, "hcloud_load_balancer_target")
self.hcloud_load_balancer = None
self.hcloud_load_balancer_target = None
self.hcloud_server = None

View file

@ -122,7 +122,7 @@ except ImportError:
class AnsibleHcloudNetwork(Hcloud):
def __init__(self, module):
super(AnsibleHcloudNetwork, self).__init__(module, "hcloud_network")
Hcloud.__init__(self, module, "hcloud_network")
self.hcloud_network = None
def _prepare_result(self):

View file

@ -119,7 +119,7 @@ except ImportError:
class AnsibleHcloudReverseDNS(Hcloud):
def __init__(self, module):
super(AnsibleHcloudReverseDNS, self).__init__(module, "hcloud_rdns")
Hcloud.__init__(self, module, "hcloud_rdns")
self.hcloud_resource = None
self.hcloud_rdns = None

View file

@ -105,7 +105,7 @@ except ImportError:
class AnsibleHcloudRoute(Hcloud):
def __init__(self, module):
super(AnsibleHcloudRoute, self).__init__(module, "hcloud_route")
Hcloud.__init__(self, module, "hcloud_route")
self.hcloud_network = None
self.hcloud_route = None

View file

@ -127,7 +127,7 @@ except ImportError:
class AnsibleHcloudServerNetwork(Hcloud):
def __init__(self, module):
super(AnsibleHcloudServerNetwork, self).__init__(module, "hcloud_server_network")
Hcloud.__init__(self, module, "hcloud_server_network")
self.hcloud_network = None
self.hcloud_server = None
self.hcloud_server_network = None

View file

@ -142,7 +142,7 @@ except ImportError:
class AnsibleHcloudSubnetwork(Hcloud):
def __init__(self, module):
super(AnsibleHcloudSubnetwork, self).__init__(module, "hcloud_subnetwork")
Hcloud.__init__(self, module, "hcloud_subnetwork")
self.hcloud_network = None
self.hcloud_subnetwork = None

View file

@ -0,0 +1,2 @@
cloud/hcloud
shippable/hcloud/group1

View file

@ -0,0 +1,6 @@
# 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)
---
hcloud_prefix: "tests"
hcloud_load_balancer_name: "{{hcloud_prefix}}-integration"
hcloud_server_name: "{{hcloud_prefix}}-lb-info"

View file

@ -0,0 +1,3 @@
collections:
- community.general.ipfilter
- hetzner.cloud

View file

@ -0,0 +1,128 @@
# 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 ensure Load Balancer is absent
hcloud_load_balancer:
name: "{{ hcloud_load_balancer_name }}"
state: absent
- name: setup server
hcloud_server:
name: "{{hcloud_server_name}}"
server_type: cx11
image: ubuntu-20.04
state: started
register: server
- name: verify setup server
assert:
that:
- server is success
- name: setup Load Balancer
hcloud_load_balancer:
name: "{{ hcloud_load_balancer_name }}"
load_balancer_type: lb11
network_zone: eu-central
labels:
key: value
register: test_load_balancer
- name: verify setup Load Balancer
assert:
that:
- test_load_balancer is changed
- name: test create load_balancer target
hcloud_load_balancer_target:
type: "server"
load_balancer: "{{hcloud_load_balancer_name}}"
server: "{{hcloud_server_name}}"
state: present
register: load_balancer_target
- name: verify create load_balancer target
assert:
that:
- load_balancer_target is success
- name: test create load_balancer service
hcloud_load_balancer_service:
load_balancer: "{{hcloud_load_balancer_name}}"
protocol: "http"
listen_port: 80
state: present
register: load_balancer_service
- name: verify create load_balancer service
assert:
that:
- load_balancer_service is success
- name: test gather hcloud Load Balancer infos
hcloud_load_balancer_info:
id: "{{test_load_balancer.hcloud_load_balancer.id}}"
register: hcloud_load_balancers
- name: verify test gather hcloud Load Balancer infos
assert:
that:
- hcloud_load_balancers.hcloud_load_balancer_info| list | count >= 1
- hcloud_load_balancers.hcloud_load_balancer_info[0].targets | list | count == 1
- hcloud_load_balancers.hcloud_load_balancer_info[0].targets | selectattr('type','equalto','server') | list | count == 1
- hcloud_load_balancers.hcloud_load_balancer_info[0].targets | selectattr('server','equalto','{{ hcloud_server_name }}') | list | count == 1
- hcloud_load_balancers.hcloud_load_balancer_info[0].services | list | count == 1
- hcloud_load_balancers.hcloud_load_balancer_info[0].services | selectattr('protocol','equalto','http') | list | count == 1
- hcloud_load_balancers.hcloud_load_balancer_info[0].services | selectattr('listen_port','equalto',80) | list | count == 1
- hcloud_load_balancers.hcloud_load_balancer_info[0].services | selectattr('destination_port','equalto',80) | list | count == 1
- name: test gather hcloud Load Balancer infos in check mode
hcloud_load_balancer_info:
check_mode: yes
register: hcloud_load_balancers
- name: verify test gather hcloud Load Balancer infos in check mode
assert:
that:
- hcloud_load_balancers.hcloud_load_balancer_info| list | count >= 1
- name: test gather hcloud Load Balancer infos with correct label selector
hcloud_load_balancer_info:
label_selector: "key=value"
register: hcloud_load_balancers
- name: verify test gather hcloud Load Balancer with correct label selector
assert:
that:
- hcloud_load_balancers.hcloud_load_balancer_info|selectattr('name','equalto','{{ test_load_balancer.hcloud_load_balancer.name }}') | list | count == 1
- name: test gather hcloud Load Balancer infos with wrong label selector
hcloud_load_balancer_info:
label_selector: "key!=value"
register: hcloud_load_balancers
- name: verify test gather hcloud Load Balancer with wrong label selector
assert:
that:
- hcloud_load_balancers.hcloud_load_balancer_info | list | count == 0
- name: test gather hcloud Load Balancer infos with correct id
hcloud_load_balancer_info:
id: "{{test_load_balancer.hcloud_load_balancer.id}}"
register: hcloud_load_balancers
- name: verify test gather hcloud Load Balancer with correct id
assert:
that:
- hcloud_load_balancers.hcloud_load_balancer_info|selectattr('name','equalto','{{ test_load_balancer.hcloud_load_balancer.name }}') | list | count == 1
- name: test gather hcloud Load Balancer infos with wrong id
hcloud_load_balancer_info:
id: "{{test_load_balancer.hcloud_load_balancer.id}}1"
register: result
ignore_errors: yes
- name: verify test gather hcloud Load Balancer with wrong id
assert:
that:
- result is failed
- name: cleanup
hcloud_load_balancer:
id: "{{ test_load_balancer.hcloud_load_balancer.id }}"
state: absent
register: result
- name: verify cleanup
assert:
that:
- result is success

View file

@ -57,6 +57,7 @@ retry ansible-galaxy -vvv collection install ansible.netcommon
retry ansible-galaxy -vvv collection install community.internal_test_tools
retry pip install netaddr --disable-pip-version-check
retry pip install hcloud
retry pip install rstcheck
# END: HACK
export PYTHONIOENCODING='utf-8'