#!/bin/bash -x

echo '---
interfaces:
- name: br0
  type: ovs-bridge
  state: absent
- name: eth1
  type: ethernet
  state: absent
' | sudo nmstatectl set -


sudo ip link del eth1
sudo ip netns add tmp
sudo ip link add eth1 type veth peer name eth1peer
sudo ip link set eth1 up
sudo ip link set eth1peer up
sudo ip link set eth1peer netns tmp
sudo ip netns exec tmp ip link set eth1peer up
sudo nmcli device set eth1 managed yes

echo '---
interfaces:
- name: br0
  type: ovs-bridge
  state: up
  bridge:
    port:
    - name: ovs0
    - name: eth1
      vlan:
        mode: access
        tag: 100
' | sudo nmstatectl set -

sleep 1

sudo nmcli c

sleep 5

# To reproduce the issue, we need to make sure
#  1. OVS bridge, OVS port and OVS system interface connections are all changed
#  2. The failure(no vlan ID) is trigger at `connection-add` state with no new
#     activation/reapply to OVS bridge/port/interface
echo '
interfaces:
- name: eth1
  type: ethernet
  ovs-db:
    external_ids:
      gris: eth1-if
- name: br0
  type: ovs-bridge
  state: up
  ovs-db:
    external_ids:
      gris: br0-br
  bridge:
    port:
    - name: ovs0
    - name: eth1
      vlan:
        mode: access
        tag: 101
- name: vlan9
  type: vlan
' | sudo nmstatectl set - --no-commit

sleep 5
sudo ovs-vsctl show
if [ "CHK$(nmcli c show --active eth1)" == "CHK" ];then
    sudo nmcli c show
    echo "FAILED"
    exit 1
fi
