---

- name: "Reading hetzner server infos for stage <{{ stage }}>"
  uri:
    url: "https://api.hetzner.cloud/v1/servers?label_selector=stage={{ stage }}"
    method: GET
    headers:
      authorization: Bearer {{ hetzner_authentication_ansible }}
  register: hetzner_servers_result
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Setting hetzner server pagination count: <{{ hetzner_servers_result.json.meta.pagination.last_page }}>"
  set_fact:
    total_server_pages: "{{ hetzner_servers_result.json.meta.pagination.last_page }}"
  tags:
    - always

- name: "Reading hetzner server infos for stage <{{ stage }}> without pagination"
  set_fact:
    hetzner_servers: "{{ hetzner_servers_result.json.servers }}"
  tags:
    - always
  when:
    - total_server_pages == '1'

- name: "Block << WITH >> pagination"
  block:
  - name: "Reading hetzner server infos for stage <{{ stage }}> with pagination"
    uri:
      url: "https://api.hetzner.cloud/v1/servers?label_selector=stage={{ stage }}&page={{ item }}"
      method: GET
      headers:
        authorization: Bearer {{ hetzner_authentication_ansible }}
    register: hetzner_servers_results
    with_sequence: start=1 end={{ total_server_pages }}
    delegate_to: 127.0.0.1

  - name: "Reading hetzner server infos for stage <{{ stage }}> with pagination"
    set_fact:
      hetzner_servers: "{{ hetzner_servers_results.results | json_query(querystr1) | first | json_query(querystr2) | json_query(querystr2) }}"
    vars:
      querystr1: "[[*].json.servers]"
      querystr2: "[]"
    delegate_to: 127.0.0.1
  when:
    - total_server_pages != '1'
  tags:
    - always

- name: "Printing hetzner server infos for stage <{{ stage }}>"
  debug:
    msg: "{{ hetzner_servers }}"
  delegate_to: 127.0.0.1
  tags:
    - always
  when:
    - debug

- name: "Reading hetzner private network infos for stage <{{ stage }}>"
  uri:
    url: "https://api.hetzner.cloud/v1/networks?label_selector=stage={{ stage }}"
    method: GET
    headers:
      authorization: Bearer {{ hetzner_authentication_ansible }}
  register: hetzner_networks
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Printing hetzner private network infos for stage <{{ stage }}>"
  debug:
    msg: "{{ hetzner_networks.json.networks }}"
  delegate_to: 127.0.0.1
  tags:
    - always
  when:
    - debug

- name: "Reading hetzner private network id for <{{ stage }}>"
  set_fact:
    stage_private_network_id: "{{ hetzner_networks.json.networks | map(attribute='id') | first | default(0) }}"
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Printing hetzner private network id for <{{ stage }}>"
  debug:
    msg: "{{ stage_private_network_id }}"
  delegate_to: 127.0.0.1
  tags:
    - always
  when:
    - debug

- name: "Reading hetzner loadbalancer infos for stage <{{ stage_kube }}>"
  uri:
    url: "https://api.hetzner.cloud/v1/load_balancers"
    method: GET
    headers:
      authorization: Bearer {{ hetzner_authentication_ansible }}
  register: hetzner_loadbalancers
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Printing hetzner loadbalancer infos for stage <{{ stage_kube }}>"
  debug:
    msg: "{{ hetzner_loadbalancers.json.load_balancers }}"
  delegate_to: 127.0.0.1
  tags:
    - always
  when:
    - debug

- name: "Reading hetzner loadbalancer infos for stage <{{ stage_kube }}> with pagination"
  set_fact:
    shared_service_kube_loadbalancer_private_ip: "{{
       hetzner_loadbalancers.json.load_balancers
       | json_query(querystr1)
       | first
       | default([])
       | first
       | default(shared_service_kube_loadbalancer_private_ip_not_available) }}"
  vars:
    querystr1: "[?name=='{{ stage_kube_load_balancer }}'].private_net[*].ip"
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Reading hetzner loadbalancer infos for stage <{{ stage_kube }}> with pagination"
  set_fact:
    shared_service_kube_loadbalancer_public_ip: "{{
       hetzner_loadbalancers.json.load_balancers
       | json_query(querystr1)
       | first
       | default(shared_service_kube_loadbalancer_public_ip_not_available) }}"
  vars:
    querystr1: "[?name=='{{ stage_kube_load_balancer }}'].public_net.ipv4.ip"
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Printing hetzner loadbalancer infos for stage <{{ stage_kube }}>"
  debug:
    msg: "<public={{ shared_service_kube_loadbalancer_public_ip }}:private={{ shared_service_kube_loadbalancer_private_ip }}>"
  delegate_to: 127.0.0.1
  tags:
    - always
  when:
    - debug

# due to a hetzner api bug for label selector: only last given selector is used - label_selector=stage=XXX,!manual not working correctly
- name: "Reading hetzner server infos for stage <{{ stage }}/{{ stage_kube }}>"
  set_fact:
    stage_server_infos: "\
      {% set  list= [] %}\
      {% for server in hetzner_servers %}\
        {% if server.labels.manual is not defined %}\
          {% if (server.private_net|length) > 0 %}\
            {% for private_net in server.private_net %}\
              {% if private_net.network == stage_private_network_id|int %}\
                {{ list.append({'service': server.labels.service | default(''), 'private_ip': private_net.ip, 'public_ip': server.public_net.ipv4.ip, 'name': server.name, 'id': server.id}) }}\
              {% endif %}\
            {% endfor %}\
          {% else %}\
            {{ list.append({'service': server.labels.service | default(''), 'private_ip': '-', 'public_ip': server.public_net.ipv4.ip, 'name': server.name, 'id': server.id}) }}\
          {% endif %}\
        {% endif %}\
      {% endfor %}\
      {{ list|list }}"
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Printing hetzner server infos for stage <{{ stage }}/{{ stage_kube }}>"
  debug:
    msg: "{{ stage_server_infos }}"
  delegate_to: 127.0.0.1
  tags:
    - always
  when:
    - debug

- name: "Reading server id for {{ inventory_hostname }}"
  set_fact:
    stage_server_id: "{{ stage_server_infos | json_query(querystr) | first | default('') }}"
  vars:
    querystr: "[?name=='{{ inventory_hostname }}'].id"
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Reading public ip address for {{ inventory_hostname }}"
  set_fact:
    stage_server_ip: "{{ stage_server_infos | json_query(querystr) | first | default('') }}"
  vars:
    querystr: "[?name=='{{ inventory_hostname }}'].public_ip"
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Reading private ip address for {{ inventory_hostname }}"
  set_fact:
    stage_private_server_ip: "{{ stage_server_infos | json_query(querystr) | first | default('') }}"
  vars:
    querystr: "[?name=='{{ inventory_hostname }}'].private_ip"
  delegate_to: 127.0.0.1
  tags:
    - always

- name: "Printing ip addresses for {{ inventory_hostname }}"
  debug:
    msg: "{{ stage_server_id }} / {{ stage_server_ip }} / {{ stage_private_server_ip }}"
  delegate_to: 127.0.0.1
  tags:
    - always
#  when:
#    - debug

- name: "Printing groups for {{ inventory_hostname }}"
  debug:
    msg: "{{ group_names }}"
  delegate_to: 127.0.0.1
  tags:
    - always
#  when:
#    - debug
