# Copyright (c) 2019 Dell Inc. or its subsidiaries. All Rights Reserved.
---

- name: Deploy Gateway and VirtualService for AWX
  k8s:
    state: present
    definition: "{{ lookup('template', '{{ role_path }}/templates/{{ item }}.yml.j2') | from_yaml }}"
  loop:
  - gateway
  - virtualservice
  when: env_setup_type == 'cam'

- import_tasks: fetch-awx-admin-creds.yml

- import_tasks: create-ngci-org.yml

- include_tasks: create-team.yml
  loop: "{{teams | flatten(levels=1)}}"
  
- name: Fetch existing organizations
  shell: >
    kubectl exec -it -n ngci-consul {{ consul_pod_name.stdout }} -c ngci-secret-storage-consul --
    curl -X GET -u {{ awx_admin_username }}:{{ awx_admin_password }} {{ awx_service_address }}/api/v2/organizations/ -H 'Accept: application/json'
  register: fetch_organizations_response
  no_log: "{{ enforce_no_log }}"

- name: Retrieve organization json resp
  set_fact:
    organizations_json_resp: "{{ fetch_organizations_response.stdout | from_json }}"

- name: Retriev organizations list from fetch response
  set_fact:
    organization_list: "{{ organizations_json_resp.results }}"

- name: Fetch existing roles
  shell: >
    kubectl exec -it -n ngci-consul {{ consul_pod_name.stdout }} -c ngci-secret-storage-consul --
    curl -X GET -u {{ awx_admin_username }}:{{ awx_admin_password }} '{{ awx_service_address }}/api/v2/roles/?page=1&page_size=10000' -H 'Accept: application/json'
  register: fetch_roles_response
  no_log: "{{ enforce_no_log }}"

- name: Retrieve roles json resp
  set_fact:
    roles_json_resp: "{{ fetch_roles_response.stdout | from_json }}"

- name: Retrive roles list from fetch response
  set_fact: 
    roles_list: "{{ roles_json_resp.results }}"

- include_tasks: create-awx-users.yml
  loop: "{{ tower_users }}"
  loop_control:
    loop_var: tower_user_info
