- block:
  - name: Include prompts
    import_tasks: prompts.yml

  - name: Set disk size
    set_fact:
      server_disk_size: 50000000000

  - name: Check server size
    set_fact:
      server_disk_size: 25000000000
    when: cloud_providers.scaleway.size == "START1-XS"

  - name: Check if server exists
    uri:
      url: "https://cp-{{ algo_region }}.scaleway.com/servers"
      method: GET
      headers:
        Content-Type: 'application/json'
        X-Auth-Token: "{{ algo_scaleway_token }}"
      status_code: 200
    register: scaleway_servers

  - name: Set server id as a fact
    set_fact:
      server_id: "{{ item.id }}"
    no_log: true
    when: algo_server_name == item.name
    with_items: "{{ scaleway_servers.json.servers }}"

  - name: Create a server if it doesn't exist
    block:
    - name: Get the organization id
      uri:
        url: https://account.cloud.online.net/organizations
        method: GET
        headers:
          Content-Type: 'application/json'
          X-Auth-Token: "{{ algo_scaleway_token }}"
        status_code: 200
      register: scaleway_organizations

    - name: Set organization id as a fact
      set_fact:
        organization_id: "{{ item.id }}"
      no_log: true
      when: algo_scaleway_org == item.name
      with_items: "{{ scaleway_organizations.json.organizations }}"

    - name: Get total count of images
      uri:
        url: "https://cp-{{ algo_region }}.scaleway.com/images"
        method: GET
        headers:
          Content-Type: 'application/json'
          X-Auth-Token: "{{ algo_scaleway_token }}"
        status_code: 200
      register: scaleway_pages

    - name: Get images
      uri:
        url: "https://cp-{{ algo_region }}.scaleway.com/images?per_page=100&page={{ item }}"
        method: GET
        headers:
          Content-Type: 'application/json'
          X-Auth-Token: "{{ algo_scaleway_token }}"
        status_code: 200
      register: scaleway_images
      with_sequence: start=1 end={{ ((scaleway_pages.x_total_count|int / 100)| round )|int }}

    - name: Set image id as a fact
      include_tasks: image_facts.yml
      with_items: "{{ scaleway_images['results'] }}"
      loop_control:
        loop_var: outer_item

    - name: Create a server
      uri:
        url: "https://cp-{{ algo_region }}.scaleway.com/servers/"
        method: POST
        headers:
          Content-Type: 'application/json'
          X-Auth-Token: "{{ algo_scaleway_token }}"
        body:
          organization: "{{ organization_id }}"
          name: "{{ algo_server_name }}"
          image: "{{ image_id }}"
          commercial_type: "{{cloud_providers.scaleway.size }}"
          enable_ipv6: true
          boot_type: local
          tags:
            - Environment:Algo
            - AUTHORIZED_KEY={{ lookup('file', SSH_keys.public)|regex_replace(' ', '_') }}
        status_code: 201
        body_format: json
      register: algo_instance

    - name: Set server id as a fact
      set_fact:
        server_id: "{{ algo_instance.json.server.id }}"
    when: server_id is not defined

  - name: Power on the server
    uri:
      url: https://cp-{{ algo_region }}.scaleway.com/servers/{{ server_id }}/action
      method: POST
      headers:
        Content-Type: application/json
        X-Auth-Token: "{{ algo_scaleway_token }}"
      body:
        action: poweron
      status_code: 202
      body_format: json
    ignore_errors: true
    no_log: true

  - name: Wait for the server to become running
    uri:
      url: "https://cp-{{ algo_region }}.scaleway.com/servers/{{ server_id }}"
      method: GET
      headers:
        Content-Type: 'application/json'
        X-Auth-Token: "{{ algo_scaleway_token }}"
      status_code: 200
    until:
      - algo_instance.json.server.state is defined
      - algo_instance.json.server.state == "running"
    retries: 20
    delay: 30
    register: algo_instance

  - set_fact:
      cloud_instance_ip: "{{ algo_instance['json']['server']['public_ip']['address'] }}"
      ansible_ssh_user: root

  rescue:
  - debug: var=fail_hint
    tags: always
  - fail:
    tags: always
