---
- name: verify current and desired state
  hosts: localhost
  tasks:
  - name: get current number of master nodes
    command: gcloud --project {{ gcloud_project }} compute instance-groups managed describe {{ prefix }}-master --region {{ gcloud_region }} --format 'value(targetSize)'
    register: masters_nb
    changed_when: false

  - name: get current number of infra nodes
    command: gcloud --project {{ gcloud_project }} compute instance-groups managed describe {{ prefix }}-infra-node --region {{ gcloud_region }} --format 'value(targetSize)'
    register: infra_nodes_nb
    changed_when: false

  - name: get current number of app nodes
    command: gcloud --project {{ gcloud_project }} compute instance-groups managed describe {{ prefix }}-node --region {{ gcloud_region }} --format 'value(targetSize)'
    register: nodes_nb
    changed_when: false

  - name: assert that we are not scaling down
    assert:
      that:
      - "master_instance_group_size >= masters_nb.stdout | int"
      - "infra_node_instance_group_size >= infra_nodes_nb.stdout | int"
      - "node_instance_group_size >= nodes_nb.stdout | int"
      msg: Scaling down is not supported! Desired number of nodes must be equal or bigger than the current state.

  - name: verify that there is something to scale up
    fail:
      msg: To scale up, update 'master_instance_group_size', 'infra_node_instance_group_size' and 'node_instance_group_size' variables to the desired number of nodes in your 'config.yaml' file and rerun this command.
    when:
    - master_instance_group_size == masters_nb.stdout | int
    - infra_node_instance_group_size == infra_nodes_nb.stdout | int
    - node_instance_group_size == nodes_nb.stdout | int

- include: core-infra.yaml

- name: create instance groups
  hosts: localhost
  roles:
  - instance-groups

- include: ../../../../playbooks/prerequisite.yaml
- include: ../../../../../openshift-ansible/playbooks/byo/openshift_facts.yml
- include: ../../../../../openshift-ansible/playbooks/byo/openshift-master/scaleup.yml
  when:
  - groups['new_masters'] is defined
  - groups['new_masters'] | length > 0
- include: ../../../../../openshift-ansible/playbooks/byo/openshift-node/scaleup.yml
  when:
  - groups['new_nodes'] is defined
  - groups['new_nodes'] | length > 0
- include: openshift-post.yaml
