From d7c429bfd143524c76a4993120bb7cc0017bcba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 24 Jun 2020 08:37:29 +0200 Subject: [PATCH] Add hcloud_load_balancer_type_info module --- plugins/modules/hcloud_certificate.py | 1 - .../modules/hcloud_load_balancer_type_info.py | 169 ++++++++++++++++++ plugins/modules/hcloud_server_network.py | 1 - plugins/modules/hcloud_subnetwork.py | 1 - .../hcloud_load_balancer_type_info/aliases | 2 + .../defaults/main.yml | 5 + .../meta/main.yml | 3 + .../tasks/main.yml | 38 ++++ 8 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 plugins/modules/hcloud_load_balancer_type_info.py create mode 100644 tests/integration/targets/hcloud_load_balancer_type_info/aliases create mode 100644 tests/integration/targets/hcloud_load_balancer_type_info/defaults/main.yml create mode 100644 tests/integration/targets/hcloud_load_balancer_type_info/meta/main.yml create mode 100644 tests/integration/targets/hcloud_load_balancer_type_info/tasks/main.yml diff --git a/plugins/modules/hcloud_certificate.py b/plugins/modules/hcloud_certificate.py index 919471e..e6571b2 100644 --- a/plugins/modules/hcloud_certificate.py +++ b/plugins/modules/hcloud_certificate.py @@ -136,7 +136,6 @@ from ansible.module_utils._text import to_native from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud try: - from hcloud.volumes.domain import Volume from hcloud.certificates.domain import Certificate from hcloud.certificates.domain import Server from hcloud import APIException diff --git a/plugins/modules/hcloud_load_balancer_type_info.py b/plugins/modules/hcloud_load_balancer_type_info.py new file mode 100644 index 0000000..3d81b95 --- /dev/null +++ b/plugins/modules/hcloud_load_balancer_type_info.py @@ -0,0 +1,169 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright: (c) 2019, Hetzner Cloud GmbH +# 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 + +ANSIBLE_METADATA = { + "metadata_version": "1.1", + "status": ["preview"], + "supported_by": "community", +} + +DOCUMENTATION = ''' +--- +module: hcloud_load_balancer_type_info + +short_description: Gather infos about the Hetzner Cloud Load Balancer types. + + +description: + - Gather infos about your Hetzner Cloud Load Balancer types. + +author: + - Lukas Kaemmerling (@LKaemmerling) + +options: + id: + description: + - The ID of the Load Balancer type you want to get. + type: int + name: + description: + - The name of the Load Balancer type you want to get. + type: str +extends_documentation_fragment: +- hetzner.hcloud.hcloud + +''' + +EXAMPLES = """ +- name: Gather hcloud Load Balancer type infos + hcloud_load_balancer_type_info: + register: output + +- name: Print the gathered infos + debug: + var: output.hcloud_load_balancer_type_info +""" + +RETURN = """ +hcloud_load_balancer_type_info: + description: The Load Balancer type infos as list + returned: always + type: complex + contains: + id: + description: Numeric identifier of the Load Balancer type + returned: always + type: int + sample: 1937415 + name: + description: Name of the Load Balancer type + returned: always + type: str + sample: lb11 + description: + description: Description of the Load Balancer type + returned: always + type: str + sample: LB11 + max_connections: + description: Number of maximum simultaneous open connections + returned: always + type: int + sample: 1 + max_services: + description: Number of services a Load Balancer of this type can have + returned: always + type: int + sample: 1 + max_targets: + description: Number of targets a single Load Balancer can have + returned: always + type: int + sample: 25 + max_assigned_certificates: + description: Number of SSL Certificates that can be assigned to a single Load Balancer + returned: always + type: int + sample: 5 +""" + +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: + pass + + +class AnsibleHcloudLoadBalancerTypeInfo(Hcloud): + def __init__(self, module): + Hcloud.__init__(self, module, "hcloud_load_balancer_type_info") + self.hcloud_load_balancer_type_info = None + + def _prepare_result(self): + tmp = [] + + for load_balancer_type in self.hcloud_load_balancer_type_info: + if load_balancer_type is not None: + tmp.append({ + "id": to_native(load_balancer_type.id), + "name": to_native(load_balancer_type.name), + "description": to_native(load_balancer_type.description), + "max_connections": load_balancer_type.max_connections, + "max_services": load_balancer_type.max_services, + "max_targets": load_balancer_type.max_targets, + "max_assigned_certificates": load_balancer_type.max_assigned_certificates + }) + return tmp + + def get_load_balancer_types(self): + try: + if self.module.params.get("id") is not None: + self.hcloud_load_balancer_type_info = [self.client.load_balancer_types.get_by_id( + self.module.params.get("id") + )] + elif self.module.params.get("name") is not None: + self.hcloud_load_balancer_type_info = [self.client.load_balancer_types.get_by_name( + self.module.params.get("name") + )] + else: + self.hcloud_load_balancer_type_info = self.client.load_balancer_types.get_all() + + 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"}, + **Hcloud.base_module_arguments() + ), + supports_check_mode=True, + ) + + +def main(): + module = AnsibleHcloudLoadBalancerTypeInfo.define_module() + + hcloud = AnsibleHcloudLoadBalancerTypeInfo(module) + hcloud.get_load_balancer_types() + result = hcloud.get_result() + ansible_info = { + 'hcloud_load_balancer_type_info': result['hcloud_load_balancer_type_info'] + } + module.exit_json(**ansible_info) + + +if __name__ == "__main__": + main() diff --git a/plugins/modules/hcloud_server_network.py b/plugins/modules/hcloud_server_network.py index 545c2ed..e876834 100644 --- a/plugins/modules/hcloud_server_network.py +++ b/plugins/modules/hcloud_server_network.py @@ -25,7 +25,6 @@ options: network: description: - The name of the Hetzner Cloud Networks. - type: str required: true server: diff --git a/plugins/modules/hcloud_subnetwork.py b/plugins/modules/hcloud_subnetwork.py index 111db86..b3123f2 100644 --- a/plugins/modules/hcloud_subnetwork.py +++ b/plugins/modules/hcloud_subnetwork.py @@ -25,7 +25,6 @@ options: network: description: - The ID or Name of the Hetzner Cloud Networks. - type: str required: true ip_range: diff --git a/tests/integration/targets/hcloud_load_balancer_type_info/aliases b/tests/integration/targets/hcloud_load_balancer_type_info/aliases new file mode 100644 index 0000000..55ec821 --- /dev/null +++ b/tests/integration/targets/hcloud_load_balancer_type_info/aliases @@ -0,0 +1,2 @@ +cloud/hcloud +shippable/hcloud/group2 diff --git a/tests/integration/targets/hcloud_load_balancer_type_info/defaults/main.yml b/tests/integration/targets/hcloud_load_balancer_type_info/defaults/main.yml new file mode 100644 index 0000000..b7fd863 --- /dev/null +++ b/tests/integration/targets/hcloud_load_balancer_type_info/defaults/main.yml @@ -0,0 +1,5 @@ +# Copyright: (c) 2020, Hetzner Cloud GmbH +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +--- +hcloud_load_balancer_type_name: "lb11" +hcloud_load_balancer_type_id: 1 diff --git a/tests/integration/targets/hcloud_load_balancer_type_info/meta/main.yml b/tests/integration/targets/hcloud_load_balancer_type_info/meta/main.yml new file mode 100644 index 0000000..407c901 --- /dev/null +++ b/tests/integration/targets/hcloud_load_balancer_type_info/meta/main.yml @@ -0,0 +1,3 @@ +collections: + - community.general.ipfilter + - hetzner.cloud diff --git a/tests/integration/targets/hcloud_load_balancer_type_info/tasks/main.yml b/tests/integration/targets/hcloud_load_balancer_type_info/tasks/main.yml new file mode 100644 index 0000000..bcd805a --- /dev/null +++ b/tests/integration/targets/hcloud_load_balancer_type_info/tasks/main.yml @@ -0,0 +1,38 @@ +# Copyright: (c) 2020, Hetzner Cloud GmbH +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +--- +- name: test gather hcloud Load Balancer type infos + hcloud_load_balancer_type_info: + register: hcloud_load_balancer_types +- name: verify test gather hcloud Load Balancer type infos + assert: + that: + - hcloud_load_balancer_types.hcloud_load_balancer_type_info| list | count >= 1 + +- name: test gather hcloud Load Balancer type infos in check mode + hcloud_load_balancer_type_info: + check_mode: yes + register: hcloud_load_balancer_types + +- name: verify test gather hcloud Load Balancer type infos in check mode + assert: + that: + - hcloud_load_balancer_types.hcloud_load_balancer_type_info| list | count >= 1 + +- name: test gather hcloud Load Balancer type infos with name + hcloud_load_balancer_type_info: + name: "{{hcloud_load_balancer_type_name}}" + register: hcloud_load_balancer_types +- name: verify test gather hcloud Load Balancer type with name + assert: + that: + - hcloud_load_balancer_types.hcloud_load_balancer_type_info|selectattr('name','equalto','{{ hcloud_load_balancer_type_name }}') | list | count == 1 + +- name: test gather hcloud Load Balancer type infos with correct id + hcloud_load_balancer_type_info: + id: "{{hcloud_load_balancer_type_id}}" + register: hcloud_load_balancer_types +- name: verify test gather hcloud Load Balancer type with correct id + assert: + that: + - hcloud_load_balancer_types.hcloud_load_balancer_type_info|selectattr('name','equalto','{{ hcloud_load_balancer_type_name }}') | list | count == 1