loadbalancer_: improve error handlings (#138)

* loadbalancer_: improve error handlings

* streamline a bit to make ansible 2.9 pass

* handle race condition
This commit is contained in:
René Moser 2022-06-20 06:26:26 +02:00 committed by GitHub
parent a0a5da3f46
commit 1d568f3ac7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 10 deletions

View file

@ -122,8 +122,18 @@ class AnsibleHcloudLoadBalancerNetwork(Hcloud):
def _get_load_balancer_and_network(self):
try:
self.hcloud_network = self.client.networks.get_by_name(self.module.params.get("network"))
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(self.module.params.get("load_balancer"))
network = self.module.params.get("network")
self.hcloud_network = self.client.networks.get_by_name(network)
if not self.hcloud_network:
self.module.fail_json(msg="Network does not exist: %s" % network)
load_balancer_name = self.module.params.get("load_balancer")
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(
load_balancer_name
)
if not self.hcloud_load_balancer:
self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name)
self.hcloud_load_balancer_network = None
except Exception as e:
self.module.fail_json(msg=e.message)

View file

@ -340,9 +340,13 @@ class AnsibleHcloudLoadBalancerService(Hcloud):
def _get_load_balancer(self):
try:
load_balancer_name = self.module.params.get("load_balancer")
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(
self.module.params.get("load_balancer")
load_balancer_name
)
if not self.hcloud_load_balancer:
self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name)
self._get_load_balancer_service()
except Exception as e:
self.module.fail_json(msg=e.message)

View file

@ -177,9 +177,19 @@ class AnsibleHcloudLoadBalancerTarget(Hcloud):
def _get_load_balancer_and_target(self):
try:
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(self.module.params.get("load_balancer"))
load_balancer_name = self.module.params.get("load_balancer")
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(
load_balancer_name
)
if not self.hcloud_load_balancer:
self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name)
if self.module.params.get("type") == "server":
self.hcloud_server = self.client.servers.get_by_name(self.module.params.get("server"))
server_name = self.module.params.get("server")
self.hcloud_server = self.client.servers.get_by_name(server_name)
if not self.hcloud_server:
self.module.fail_json(msg="Server not found: %s" % server_name)
self.hcloud_load_balancer_target = None
except Exception as e:
self.module.fail_json(msg=e.message)

View file

@ -46,7 +46,33 @@
assert:
that:
- result is failed
- 'result.msg == "missing required arguments: load_balancer, network"'
- '"missing required arguments:" in result.msg'
- name: test fail load balancer does not exist
hetzner.hcloud.hcloud_load_balancer_network:
network: "{{ hcloud_network_name }}"
load_balancer: does-not-exist
state: present
register: result
ignore_errors: true
- name: verify test fail load_balancer does not exist
assert:
that:
- result is failed
- "result.msg == 'Load balancer does not exist: does-not-exist'"
- name: test fail network does not exist
hetzner.hcloud.hcloud_load_balancer_network:
network: does-not-exist
load_balancer: "{{ hcloud_load_balancer_name }}"
state: present
register: result
ignore_errors: true
- name: verify test fail network does not exist
assert:
that:
- result is failed
- "result.msg == 'Network does not exist: does-not-exist'"
- name: test create load_balancer network with checkmode
hcloud_load_balancer_network:

View file

@ -13,6 +13,20 @@
that:
- load_balancer is success
- name: test fail load balancer does not exist
hetzner.hcloud.hcloud_load_balancer_service:
load_balancer: does-not-exist
protocol: http
listen_port: 80
state: present
register: result
ignore_errors: true
- name: verify test fail load_balancer does not exist
assert:
that:
- result is failed
- "result.msg == 'Load balancer does not exist: does-not-exist'"
- name: test create load_balancer service with checkmode
hcloud_load_balancer_service:
load_balancer: "{{hcloud_load_balancer_name}}"

View file

@ -26,6 +26,32 @@
that:
- load_balancer is success
- name: test fail load balancer does not exist
hetzner.hcloud.hcloud_load_balancer_target:
type: server
load_balancer: does-not-exist
server: "{{ hcloud_server_name }}"
register: result
ignore_errors: true
- name: verify test fail load_balancer does not exist
assert:
that:
- result is failed
- "result.msg == 'Load balancer does not exist: does-not-exist'"
- name: test fail server does not exist
hetzner.hcloud.hcloud_load_balancer_target:
type: server
load_balancer: "{{ hcloud_load_balancer_name }}"
server: does-not-exist
register: result
ignore_errors: true
- name: verify test fail server does not exist
assert:
that:
- result is failed
- "result.msg == 'Server not found: does-not-exist'"
- name: test create load_balancer target with checkmode
hcloud_load_balancer_target:
type: "server"
@ -113,10 +139,9 @@
name: "{{ hcloud_load_balancer_name }}"
state: absent
register: result
- name: verify cleanup load_balancer
assert:
that:
- result is success
until: result is not failed
retries: 5
delay: 2
- name: cleanup
hcloud_server: