---
- name: setup
  cs_loadbalancer_rule:
    name: "{{ cs_resource_prefix }}_lb"
    public_ip: "{{ test_cs_lb_public_ip }}"
    state: absent
  register: lb
- name: verify setup
  assert:
    that:
    - lb|success

- name: setup instance
  cs_instance:
    name: "{{ test_cs_lb_member }}"
    template: "{{ test_cs_instance_template }}"
    service_offering: "{{ test_cs_instance_offering_1 }}"
  register: instance
- name: verify create instance
  assert:
    that:
    - instance|success

- name: test rule requires params
  cs_loadbalancer_rule:
  ignore_errors: true
  register: lb
- name: verify test rule requires params
  assert:
    that:
    - lb|failed
    - "'ip_address,name' in lb.msg"

- name: test create rule
  cs_loadbalancer_rule:
    name: "{{ cs_resource_prefix }}_lb"
    public_ip: "{{ test_cs_lb_public_ip }}"
    algorithm: roundrobin
    public_port: 80
    private_port: 8080
  register: lb
- name: verify test create rule
  assert:
    that:
    - lb|success
    - lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "roundrobin"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080

- name: test create rule idempotence
  cs_loadbalancer_rule:
    name: "{{ cs_resource_prefix }}_lb"
    public_ip: "{{ test_cs_lb_public_ip }}"
    algorithm: roundrobin
    public_port: 80
    private_port: 8080
  register: lb
- name: verify  test create rule idempotence
  assert:
    that:
    - lb|success
    - not lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "roundrobin"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080

- name: test update rule
  cs_loadbalancer_rule:
    name: "{{ cs_resource_prefix }}_lb"
    public_ip: "{{ test_cs_lb_public_ip }}"
    algorithm: source
    public_port: 80
    private_port: 8080
  register: lb
- name: verify  test update rule
  assert:
    that:
    - lb|success
    - lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "source"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080

- name: test update rule idempotence
  cs_loadbalancer_rule:
    name: "{{ cs_resource_prefix }}_lb"
    public_ip: "{{ test_cs_lb_public_ip }}"
    algorithm: source
    public_port: 80
    private_port: 8080
  register: lb
- name: verify  test update rule idempotence
  assert:
    that:
    - lb|success
    - not lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "source"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080

- name: test rule member requires params
  cs_loadbalancer_rule_member:
  ignore_errors: true
  register: lb
- name: verify test rule requires params
  assert:
    that:
    - lb|failed
    - "'vms,name' in lb.msg"

- name: test add members to rule
  cs_loadbalancer_rule_member:
    name: "{{ cs_resource_prefix }}_lb"
    vm: "{{ test_cs_lb_member }}"
  register: lb
- name: verify add members to rule
  assert:
    that:
    - lb|success
    - lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "source"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080
    - "'{{ test_cs_lb_member }}' in lb.vms"

- name: test add members to rule idempotence
  cs_loadbalancer_rule_member:
    name: "{{ cs_resource_prefix }}_lb"
    vm: "{{ test_cs_lb_member }}"
  register: lb
- name: verify add members to rule idempotence
  assert:
    that:
    - lb|success
    - not lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "source"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080
    - "'{{ test_cs_lb_member }}' in lb.vms"

- name: test remove members to rule
  cs_loadbalancer_rule_member:
    name: "{{ cs_resource_prefix }}_lb"
    vm: "{{ test_cs_lb_member }}"
    state: absent
  register: lb
- name: verify remove members to rule
  assert:
    that:
    - lb|success
    - lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "source"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080
    - "'{{ test_cs_lb_member }}' not in lb.vms"

- name: test remove members to rule idempotence
  cs_loadbalancer_rule_member:
    name: "{{ cs_resource_prefix }}_lb"
    vm: "{{ test_cs_lb_member }}"
    state: absent
  register: lb
- name: verify remove members to rule
  assert:
    that:
    - lb|success
    - not lb|changed

- name: test remove rule
  cs_loadbalancer_rule:
    name: "{{ cs_resource_prefix }}_lb"
    public_ip: "{{ test_cs_lb_public_ip }}"
    state: absent
  register: lb
- name: verify remove rule
  assert:
    that:
    - lb|success
    - lb|changed
    - lb.name == "{{ cs_resource_prefix }}_lb"
    - lb.algorithm == "source"
    - lb.public_ip == "{{ test_cs_lb_public_ip }}"
    - lb.public_port == 80
    - lb.private_port == 8080

- name: test remove rule idempotence
  cs_loadbalancer_rule:
    name: "{{ cs_resource_prefix }}_lb"
    public_ip: "{{ test_cs_lb_public_ip }}"
    state: absent
  register: lb
- name: verify remove rule idempotence
  assert:
    that:
    - lb|success
    - not lb|changed
