---
- pause:
    prompt: |
      Enter the local path to your credentials JSON file
      (https://support.google.com/cloud/answer/6158849?hl=en&ref_topic=6262490#serviceaccounts)
  register: _gce_credentials_file
  when:
    - gce_credentials_file is undefined
    - lookup('env', 'GCE_CREDENTIALS_FILE_PATH')|length <= 0
  no_log: true

- set_fact:
    credentials_file_path: >-
      {{ gce_credentials_file | default(_gce_credentials_file.user_input|default(None)) |
         default(lookup('env', 'GCE_CREDENTIALS_FILE_PATH'), true) }}
    ssh_public_key_lookup: "{{ lookup('file', SSH_keys.public) }}"
  no_log: true

- set_fact:
    credentials_file_lookup: "{{ lookup('file', credentials_file_path) | from_json }}"
  no_log: true

- set_fact:
    service_account_email: "{{ credentials_file_lookup.client_email | default(lookup('env', 'GCE_EMAIL'), true) }}"
    project_id: "{{ credentials_file_lookup.project_id | default(lookup('env', 'GCE_PROJECT'), true) }}"
  no_log: true

- when: region is undefined
  block:
    - name: Get regions
      gcp_compute_location_info:
        auth_kind: serviceaccount
        service_account_file: "{{ credentials_file_path }}"
        project: "{{ project_id }}"
        scope: regions
        filters: status=UP
      register: gcp_compute_regions_info

    - name: Set facts about the regions
      set_fact:
        gce_regions: "{{ gcp_compute_regions_info.resources | sort(attribute='name') }}"

    - name: Set facts about the default region
      set_fact:
        default_region: >-
          {% for r in gce_regions -%}
            {% if r.name == "us-east1" %}{{ loop.index }}{% endif %}
          {%- endfor %}

    - pause:
        prompt: |
          What region should the server be located in?
          (https://cloud.google.com/compute/docs/regions-zones/#locations)
            {% for r in gce_regions %}
            {{ loop.index }}. {{ r.name }}
            {% endfor %}

          Enter the number of your desired region
          [{{ default_region }}]
      register: _gce_region

- name: Set region as a fact
  set_fact:
    algo_region: >-
      {% if region is defined %}{{ region -}}
      {% elif _gce_region.user_input %}{{ gce_regions[_gce_region.user_input | int - 1].name -}}
      {% else %}{{ gce_regions[default_region | int - 1].name }}{% endif %}

- name: Get zones
  gcp_compute_location_info:
    auth_kind: serviceaccount
    service_account_file: "{{ credentials_file_path }}"
    project: "{{ project_id }}"
    scope: zones
    filters:
      - name={{ algo_region }}-*
      - status=UP
  register: gcp_compute_zone_info

- name: Set random available zone as a fact
  set_fact:
    algo_zone: "{{ (gcp_compute_zone_info.resources | random(seed=algo_server_name + algo_region + project_id)).name }}"
