---
# Copyright 2017-present Open Networking Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# interface-config/tasks/main.yml

- name: Configure management network if management_net_ip_cidr is set/valid
  when: management_net_ip_cidr | ipaddr
  set_fact:
    configure_management_interfaces: true

- name: Configure fabric network if fabric_net_ip_cidr is set/valid
  when: fabric_net_ip_cidr | ipaddr
  set_fact:
    configure_fabric_interfaces: true

- name: Install bridging/bonding utilities
  apt:
    name: "{{ item }}"
    update_cache: yes
    cache_valid_time: 3600
  with_items:
    - bridge-utils
    - ifenslave
  register: task_result
  until: task_result is success
  retries: 3
  delay: 5

# management network
- name: Find management interfaces given management_net_kmods
  when: ( configure_management_interfaces ) and ( management_net_kmods | length > 0 )
  set_fact:
    management_net_interfaces: "{{ [ hostvars[inventory_hostname]['ansible_' ~ item[0]] ] | selectattr('module', 'defined') | selectattr('module', 'equalto', item[1]) |  map(attribute='device') | list | union(management_net_interfaces) }}"
  with_nested:
    - "{{ ansible_interfaces }}"
    - "{{ management_net_kmods }}"

- name: Find management interfaces given management_net_hwaddrs
  when: ( configure_management_interfaces ) and ( management_net_hwaddrs | length > 0 )
  set_fact:
    management_net_interfaces: "{{ [ hostvars[inventory_hostname]['ansible_' ~ item[0]] ] | selectattr('macaddress', 'defined') | selectattr('macaddress', 'equalto', item[1]) |  map(attribute='device') | list | union(management_net_interfaces) }}"
  with_nested:
    - "{{ ansible_interfaces }}"
    - "{{ management_net_hwaddrs }}"

# fabric network
- name: Find fabric interfaces given fabric_net_kmods
  when: ( configure_fabric_interfaces ) and ( fabric_net_kmods | length > 0 )
  set_fact:
    fabric_net_interfaces: "{{ [ hostvars[inventory_hostname]['ansible_' ~ item[0]] ] | selectattr('module', 'defined') | selectattr('module', 'equalto', item[1]) |  map(attribute='device') | list | union(fabric_net_interfaces) }}"
  with_nested:
    - "{{ ansible_interfaces }}"
    - "{{ fabric_net_kmods }}"

- name: Find fabric interfaces given fabric_net_hwaddrs
  when: ( configure_fabric_interfaces ) and ( fabric_net_hwaddrs | length > 0 )
  set_fact:
    fabric_net_interfaces: "{{ [ hostvars[inventory_hostname]['ansible_' ~ item[0]] ] | selectattr('macaddress', 'defined') | selectattr('macaddress', 'equalto', item[1]) |  map(attribute='device') | list | union(fabric_net_interfaces) }}"
  with_nested:
    - "{{ ansible_interfaces }}"
    - "{{ fabric_net_hwaddrs }}"

# all interfaces discovered
- name: Print lists of interfaces
  debug:
    msg: "{{ item }}"
  with_items:
    - "Ansible detected interfaces: {{ ansible_interfaces | join(', ') }}"
    - "Proposed management network interfaces: {{ management_net_interfaces | join(', ') }}"
    - "Proposed fabric network interfaces: {{ fabric_net_interfaces | join(', ') }}"

- name: Make sure all interfaces exist, and aren't shared between lists
  assert:
    that:
      - fabric_net_interfaces | difference(ansible_interfaces) | length == 0
      - management_net_interfaces | difference(ansible_interfaces) | length == 0
      - management_net_interfaces | intersect(fabric_net_interfaces) | length == 0

- name: Configure management network interfaces
  when: configure_management_interfaces | default(false)
  template:
    src: management.cfg.j2
    dest: "/etc/network/interfaces.d/management.cfg"
    owner: root
    group: root
    mode: 0644
  register: management_net_config

- name: Configure fabric network interfaces
  when: configure_fabric_interfaces | default(false)
  template:
    src: fabric.cfg.j2
    dest: "/etc/network/interfaces.d/fabric.cfg"
    owner: root
    group: root
    mode: 0644
  register: fabric_net_config

- name: Bring up management network interfaces, if reconfigured
  when: management_net_config.changed
  command: "ifup {{ item }}"
  with_flattened:
    - mgmtbridge
    - mgmtbond
    - "{{ management_net_interfaces }}"
  tags:
    - skip_ansible_lint # needs to be run before next steps

- name: Bring up fabric network interfaces, if reconfigured
  when: fabric_net_config.changed
  command: "ifup {{ item }}"
  with_flattened:
    - fabricbridge
    - fabricbond
    - "{{ fabric_net_interfaces }}"
  tags:
    - skip_ansible_lint # needs to be run before next steps

