---
- name: "Clear facts"
  set_fact:
    host_id: ""
    location_id: ""
    host_grp_id: ""
    compute_resource_id: ""
    compute_profile_id: ""
    host_fqdn: ""
    host_exists: ""
    partition_table_id: ""
    subnet_id: ""
    domain_id: ""
    failed_host: []
    created_host: []
    updated_host: []

- name: "Obtain Satellite6 Host Group ID"
  block:
    - name: "Obtain Satellite6 Host Group ID"
      uri:
        body: "{\"search\": 'title=\"{{ host.host_group }}\"'}"
      vars:
        sat6_url_path: "api/v2/hostgroups"
      register: "host_grp_qry"

    - debug:
        var: "host_grp_qry.json.results[0]"
        verbosity: "1"

    - name: "Set Satellite6 Host Group ID"
      set_fact:
        host_grp_id: "{{ host_grp_qry.json.results[0].id }}"
      when: "host_grp_qry.json.results[0].title == host.host_group"

    - debug:
        var: "host_grp_id"
        verbosity: "1"
  tags:
    - "host_group_id"

- name: "Check if Satellite6 host exists"
  block:
    - name: "Set Satellite6 host fqdn | Domain Default from Host Group"
      set_fact:
        host_fqdn: "{{ host.name }}.{{ host_grp_qry.json.results[0].domain_name }}"
      when:
        - "not host.domain | default('')"
        - "host_grp_qry.json.results[0].domain_name"

    - name: "Set Satellite6 host fqdn | Domain Specified"
      set_fact:
        host_fqdn: "{{ host.name }}.{{ host.domain }}"
      when: "host.domain | default('')"

    - debug:
        var: "host_fqdn"
        verbosity: "1"

    - name: "Check if Satellite6 host {{ host_fqdn }} exists"
      uri:
        body: "{\"search\": 'name=\"{{ host_fqdn }}\"'}"
      vars:
        sat6_url_path: "api/v2/hosts"
      register: "host_qry"

    - debug:
        var: "host_qry.json.results[0]"
        verbosity: "1"

    - name: "Set Satellite6 Host {{ host_fqdn }} Exists | True"
      set_fact:
        host_exists: True
      when:
        - "host_qry.json.results | length > 0"
        - "host_qry.json.results[0].name == host_fqdn"

    - name: "Set Satellite6 Host {{ host_fqdn }} Exists | False"
      set_fact:
        host_exists: False
      when:
        - "host_qry.json.results | length < 1"

    - name: "Set Satellite6 Host id"
      set_fact:
        host_id: "{{ host_qry.json.results[0].id }}"
      when:
        - "not sat6_fail_on_existing"
        - "host.force_host_update | default(False)"
        - "host.register_only | default(False)"
        - "host_exists"

    - debug:
        var: "host_exists"
        verbosity: "1"

    - debug:
        var: "host_id"
        verbosity: "1"
      when:
        - "not sat6_fail_on_existing"
        - "host.force_host_update | default(False)"
        - "host.register_only | default(False)"
        - "host_exists"
  tags:
    - "host_exists_check"

- name: "Obtain Satellite6 Domain ID"
  block:
    - name: "Obtain Satellite6 Domain ID"
      uri:
        body: "{\"search\": 'name=\"{{ host.domain }}\"'}"
      vars:
        sat6_url_path: "api/v2/domains"
      register: "domain_qry"

    - debug:
        var: "domain_qry.json.results[0]"
        verbosity: "1"

    - name: "Set Satellite6 Domain ID"
      set_fact:
        domain_id: "{{ domain_qry.json.results[0].id }}"
      when: "domain_qry.json.results[0].name == host.domain"

    - debug:
        var: "domain_id"
        verbosity: "1"
  when: (not host_exists and host.domain is defined) or
        (host_exists and
         host.force_host_update | default(False) and
         host.domain is defined and
         host.register_only | default(False) and
         not sat6_fail_on_existing)

- name: "Obtain Satellite6 Location ID"
  block:
    - name: "Obtain Satellite6 Location ID"
      uri:
        body: "{\"search\": 'name=\"{{ host.location }}\"'}"
      vars:
        sat6_url_path: "api/v2/locations"
      register: "loc_qry"

    - debug:
        var: "loc_qry.json.results[0]"
        verbosity: "1"

    - name: "Set Satellite6 Location ID"
      set_fact:
        location_id: "{{ loc_qry.json.results[0].id }}"
      when: "loc_qry.json.results[0].name == host.location"

    - debug:
        var: "location_id"
        verbosity: "1"
  when: (not host_exists) or
        (host_exists and host.force_host_update | default(False) and host.register_only | default(False) and not sat6_fail_on_existing)
  tags:
    - "location_id"

- name: "Obtain Satellite6 Compute Resource ID | Specified"
  block:
    - name: "Obtain Satellite6 Compute Resource ID"
      uri:
        body: "{\"search\": 'name=\"{{ host.compute_resource }}\"'}"
      vars:
        sat6_url_path: "api/v2/compute_resources"
      register: "compute_resource_qry"

    - debug:
        var: "compute_resource_qry.json.results[0]"
        verbosity: "1"

    - name: Set Satellite6 Compute Resource ID
      set_fact:
        compute_resource_id: "{{ compute_resource_qry.json.results[0].id }}"
      when: "compute_resource_qry.json.results[0].name == host.compute_resource"

    - debug:
        var: "compute_resource_id"
        verbosity: "1"
  when: (not host_exists and host.compute_resource is defined and host.mac is undefined) or
        (host_exists and
         host.force_host_update | default(False) and
         host.compute_resource is defined and
         host.mac is undefined and
         host.register_only | default(False) and
         not sat6_fail_on_existing)
  tags:
    - "compute_resource_id"

- name: "Obtain Satellite6 Compute Profile ID | Specified"
  block:
    - name: "Obtain Satellite6 Compute Profile ID"
      uri:
        body: "{\"search\": 'name=\"{{ host.compute_profile | truncate(20, True, '') }}\"'}"
      vars:
        sat6_url_path: "api/v2/compute_profiles"
      register: "compute_profile_qry"

    - debug:
        var: "compute_profile_qry.json.results[0]"
        verbosity: "1"

    - name: "Set Satellite6 Compute Profile ID | Specified"
      set_fact:
        compute_profile_id: "{{ compute_profile_qry.json.results[0].id }}"
      when: "compute_profile_qry.json.results[0].name == host.compute_profile"

    - debug:
        var: "compute_profile_id"
        verbosity: "1"
  when: (not host_exists and host.compute_profile is defined) or
        (host_exists and host.force_host_update | default(False) and host.register_only | default(False) and host.compute_profile is defined)
  tags:
    - "compute_profile_id"

- name: "Obtain Satellite6 Compute Profile ID | Default from Host Group"
  block:
    - name: "Obtain Compute Profile"
      uri:
        body: "{\"search\": 'name=\"{{ host_grp_qry.json.results[0].compute_profile_name }}\"', \"per_page\": \"1000\"}"
      vars:
        sat6_url_path: "api/v2/compute_profiles"
      register: "inherit_compute_profile_qry"

    - debug:
        var: "inherit_compute_profile_qry.json.results[0]"
        verbosity: "1"

    - name: "Set Compute Profile ID"
      set_fact:
        compute_profile_id: "{{ inherit_compute_profile_qry.json.results[0].id }}"
  when:
    - "not host_exists"
    - "host.compute_profile is undefined"
    - "host.mac is undefined"
    - "not host.register_only | default(False)"
  tags:
    - "compute_profile_id"

- name: "Obtain Satellite6 Subnet ID"
  block:
    - name: "Obtain Satellite6 Subnet ID"
      uri:
        body: "{\"search\": 'name=\"{{ host.subnet }}\"'}"
      vars:
        sat6_url_path: "api/v2/subnets"
      register: "subnet_qry"

    - debug:
        var: "subnet_qry.json.results[0]"
        verbosity: "1"

    - name: "Set Satellite6 Subnet ID"
      set_fact:
        subnet_id: "{{ subnet_qry.json.results[0].id }}"
      when: "subnet_qry.json.results[0].name == host.subnet"

    - debug:
        var: "subnet_id"
        verbosity: "1"
  when: (not host_exists and host.subnet is defined) or
        (host_exists and
         host.force_host_update | default(False) and
         host.register_only | default(False) and
         host.subnet is defined and
         not sat6_fail_on_existing)
  tags:
    - "subnet_id"

- name: "Obtain Satellite6 partition table ID"
  block:
    - name: "Obtain Satellite6 partition table ID"
      uri:
        body: "{\"search\": 'name=\"{{ host.partition_table }}\"'}"
      vars:
        sat6_url_path: "api/v2/ptables"
      register: "partition_table_qry"

    - debug:
        var: "partition_table_qry.json.results[0]"
        verbosity: "1"

    - name: Set Satellite6 partition table ID
      set_fact:
        partition_table_id: "{{ partition_table_qry.json.results[0].id }}"
      when: "partition_table_qry.json.results[0].name == host.partition_table"

    - debug:
        var: "partition_table_id"
        verbosity: "1"
  when: (not host_exists and host.partition_table is defined) or
        (host_exists and
         host.force_host_update | default(False) and
         host.register_only | default(False) and
         host.partition_table is defined and
         not sat6_fail_on_existing)
  tags:
    - "partition_table_id"

# Virtual Host Creation and Updating
- name: "Create new Satellite 6 host"
  block:
    - name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual"
      block:
        - name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual"
          uri:
            method: "POST"
            body: "{{ lookup('template', 'new_host_virtual.json.j2') }}"
            status_code: "201"
            timeout: 180
          vars:
            sat6_url_path: "api/v2/hosts"
          register: "create_host_results"

        - debug:
            var: "create_host_results"
            verbosity: "1"

        - name: "Wait for host creation to complete"
          pause:
            seconds: "{{ sat6_power_on_delay | default('30') }}"

        # - name: "Power on Satellite6 host {{ host_fqdn }}"
        #   uri:
        #     body: "{\"power_action\": \"start\"}"
        #     method: "PUT"
        #   vars:
        #     sat6_url_path: "api/v2/hosts/{{ create_host_results.json.id }}/power"
        #   register: "power_result"
        #   failed_when:
        #     - "'[Cannot run VM because the VM is in Up status.]' not in power_result.json.error.message"

        # - debug:
        #     var: "power_result"
        #     verbosity: "1"

        - set_fact:
            created_host: ["{{ host_fqdn }}"]
          # when:
          #   - "create_host_results.status == 201"
          #   - "power_result.status == 200"
      when:
        - "not host_exists"
        - "compute_profile_id is defined"
        - "compute_resource_id is defined"
        - "domain_id is defined"
        - "host.mac is undefined"
        - "not host.register_only | default(False)"

    - name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual (register_only)"
      block:
        - name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual (register_only)"
          uri:
            method: "POST"
            body: "{{ lookup('template', 'new_host_virtual_register_only.json.j2') }}"
            status_code: "201"
          vars:
            sat6_url_path: "api/v2/hosts"
          register: "create_host_results"

        - debug:
            var: "create_host_results"
            verbosity: "1"

        - set_fact:
            created_host: ["{{ host_fqdn }}"]
          when:
            - "create_host_results.status == 201"
      when:
        - "not host_exists"
        - "host.mac is undefined"
        - "domain_id is defined"
        - "host.register_only | default(False)"

    - name: "Update existing Satellite 6 host {{ host_fqdn }} | Virtual"
      block:
        - name: "Update existing Satellite 6 host {{ host_fqdn }} | Virtual"
          uri:
            method: "PUT"
            body: "{{ lookup('template', 'new_host_virtual_register_only.json.j2') }}"
          vars:
            sat6_url_path: "api/v2/hosts/{{ host_id }}"
          register: "update_host_results"

        - debug:
            var: "update_host_results"
            verbosity: "1"

        - set_fact:
            updated_host: ["{{ host_fqdn }}"]
          when:
            - "update_host_results.status == 200"
      when:
        - "host_exists"
        - "host.force_host_update | default(False)"
        - "host.register_only | default(False)"
        - "host.mac is undefined"
        - "domain_id is defined"
        - "not sat6_fail_on_existing"

# Bare-metal Host Creation and Updating
    - name: "Create new Satellite 6 host {{ host_fqdn }} | Physical"
      block:
        - name: "Create new Satellite 6 host {{ host_fqdn }} | Physical"
          uri:
            method: "POST"
            body: "{{ lookup('template', 'new_host_physical.json.j2') }}"
            status_code: "201"
          vars:
            sat6_url_path: "api/v2/hosts"
          register: "create_host_results"

        - debug:
            var: "create_host_results"
            verbosity: "1"

        - set_fact:
            created_host: ["{{ host_fqdn }}"]
          when: "create_host_results.status == 201"
      when:
        - "not host_exists"
        - "host.mac is defined"
        - "domain_id is defined"
        - "host.compute_profile is undefined"
        - "host.compute_resource is undefined"
        - "not host.register_only | default(False)"

    - name: "Create new Satellite 6 host {{ host_fqdn }} | Physical (register only)"
      block:
        - name: "Create new Satellite 6 host {{ host_fqdn }} | Physical (register only)"
          uri:
            method: "POST"
            body: "{{ lookup('template', 'new_host_physical_register_only.json.j2') }}"
            status_code: "201"
          vars:
            sat6_url_path: "api/v2/hosts"
          register: "create_host_results"

        - debug:
            var: "create_host_results"
            verbosity: "1"

        - set_fact:
            created_host: ["{{ host_fqdn }}"]
          when: "create_host_results.status == 201"
      when:
        - "not host_exists"
        - "host.mac is defined"
        - "domain_id is defined"
        - "host.compute_profile is undefined"
        - "host.compute_resource is undefined"
        - "host.register_only | default(False)"

    - name: "Update existing Satellite 6 host {{ host_fqdn }} | Physical"
      block:
        - name: "Update existing Satellite 6 host {{ host_fqdn }} | Physical"
          uri:
            method: "PUT"
            body: "{{ lookup('template', 'new_host_physical_register_only.json.j2') }}"
          vars:
            sat6_url_path: "api/v2/hosts/{{ host_id }}"
          register: "update_host_results"

        - debug:
            var: "update_host_results"
            verbosity: "1"

        - set_fact:
            updated_host: ["{{ host_fqdn }}"]
          when: "update_host_results.status == 200"
      when:
        - "host_exists"
        - "host.force_host_update | default(False)"
        - "host.register_only | default(False)"
        - "host.mac is defined"
        - "domain_id is defined"
        - "host.compute_profile is undefined"
        - "host.compute_resource is undefined"
        - "not sat6_fail_on_existing"

    - name: "Build list of Created Hosts"
      set_fact:
        created_hosts: "{{ created_hosts | union(created_host) }}"
      when: "created_host | length > 0"

    - name: "Build list of Updated Hosts"
      set_fact:
        updated_hosts: "{{ updated_hosts | union(updated_host) }}"
      when: "updated_host | length > 0"
  when: (not host_exists) or
        (host_exists and host.force_host_update | default(False) and host.register_only | default(False) and not sat6_fail_on_existing)

- name: "Handle existing Satellite6 hosts | Fail on existing"
  block:
    - debug:
        msg: "{{ host_fqdn }} already exists.  Not attempting to create."

    - name: "Failed host"
      set_fact:
        failed_host: ["{{ host_fqdn }}"]

    - name: "Build list of failed hosts | fail"
      set_fact:
        failed_hosts: "{{ failed_hosts | union(failed_host) }}"
        fail_playbook: True
      when: "sat6_fail_on_existing"
  when:
    - "host_exists"
    - "sat6_fail_on_existing"
    - "not host.force_host_update | default(False)"

- name: "Handle existing Satellite6 hosts | No fail"
  block:
    - debug:
        msg: "{{ host_fqdn }} already exists.  Not attempting to create."
      when: "not host.force_host_update"

    - name: "Failed host"
      set_fact:
        failed_host: ["{{ host_fqdn }}"]
      when: "not host.force_host_update"

    - name: "Build list of failed hosts | no fail"
      set_fact:
        failed_hosts: "{{ failed_hosts | union(failed_host) }}"
      when: "not host.force_host_update"

    - debug:
        msg:
          - "{{ host_fqdn }} already exists.  Not attempting to create."
          - "Updated {{ host_fqdn }}"
      when:
        - "host.force_host_update | default(False)"
        - "host.register_only | default(False)"
  when:
    - "host_exists"
    - "not sat6_fail_on_existing"
