#!/usr/bin/python
#
# Copyright (c) 2021, Lammert Hellinga (@Kogelvis) <lammert@hellinga.it>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later


DOCUMENTATION = r"""
module: proxmox_nic
short_description: Management of a NIC of a Qemu(KVM) VM in a Proxmox VE cluster
description:
  - Allows you to create/update/delete a NIC on Qemu(KVM) Virtual Machines in a Proxmox VE cluster.
author: "Lammert Hellinga (@Kogelvis) <lammert@hellinga.it>"
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  bridge:
    description:
      - Add this interface to the specified bridge device. The Proxmox VE default bridge is called V(vmbr0).
    type: str
  firewall:
    description:
      - Whether this interface should be protected by the firewall.
    type: bool
    default: false
  interface:
    description:
      - Name of the interface, should be V(net[n]) where C(0 ≤ n ≤ 31).
    type: str
    required: true
  link_down:
    description:
      - Whether this interface should be disconnected (like pulling the plug).
    type: bool
    default: false
  mac:
    description:
      - V(XX:XX:XX:XX:XX:XX) should be a unique MAC address. This is automatically generated if not specified.
      - When not specified this module will keep the MAC address the same when changing an existing interface.
    type: str
  model:
    description:
      - The NIC emulator model.
    type: str
    choices: ['e1000', 'e1000-82540em', 'e1000-82544gc', 'e1000-82545em', 'i82551', 'i82557b', 'i82559er', 'ne2k_isa', 'ne2k_pci',
      'pcnet', 'rtl8139', 'virtio', 'vmxnet3']
    default: virtio
  mtu:
    description:
      - Force MTU, for C(virtio) model only, setting will be ignored otherwise.
      - Set to V(1) to use the bridge MTU.
      - Value should be C(1 ≤ n ≤ 65520).
    type: int
  name:
    description:
      - Specifies the VM name. Only used on the configuration web interface.
      - Required only for O(state=present).
    type: str
  queues:
    description:
      - Number of packet queues to be used on the device.
      - Value should be C(0 ≤ n ≤ 16).
    type: int
  rate:
    description:
      - Rate limit in MBps (MegaBytes per second) as floating point number.
    type: float
  state:
    description:
      - Indicates desired state of the NIC.
    type: str
    choices: ['present', 'absent']
    default: present
  tag:
    description:
      - VLAN tag to apply to packets on this interface.
      - Value should be C(1 ≤ n ≤ 4094).
    type: int
  trunks:
    description:
      - List of VLAN trunks to pass through this interface.
    type: list
    elements: int
  vmid:
    description:
      - Specifies the instance ID.
    type: int
extends_documentation_fragment:
  - community.proxmox.proxmox.actiongroup_proxmox
  - community.proxmox.proxmox.documentation
  - community.proxmox.attributes
"""

EXAMPLES = r"""
- name: Create NIC net0 targeting the vm by name
  community.proxmox.proxmox_nic:
    name: my_vm
    interface: net0
    bridge: vmbr0
    tag: 3

- name: Create NIC net0 targeting the vm by id
  community.proxmox.proxmox_nic:
    vmid: 103
    interface: net0
    bridge: vmbr0
    mac: "12:34:56:C0:FF:EE"
    firewall: true

- name: Delete NIC net0 targeting the vm by name
  community.proxmox.proxmox_nic:
    name: my_vm
    interface: net0
    state: absent
"""

RETURN = r"""
vmid:
  description: The VM vmid.
  returned: success
  type: int
  sample: 115
msg:
  description: A short message.
  returned: always
  type: str
  sample: "Nic net0 unchanged on VM with vmid 103"
"""

from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
    ProxmoxAnsible,
    create_proxmox_module,
)


def module_args():
    return dict(
        bridge=dict(type="str"),
        firewall=dict(type="bool", default=False),
        interface=dict(type="str", required=True),
        link_down=dict(type="bool", default=False),
        mac=dict(type="str"),
        model=dict(
            choices=[
                "e1000",
                "e1000-82540em",
                "e1000-82544gc",
                "e1000-82545em",
                "i82551",
                "i82557b",
                "i82559er",
                "ne2k_isa",
                "ne2k_pci",
                "pcnet",
                "rtl8139",
                "virtio",
                "vmxnet3",
            ],
            default="virtio",
        ),
        mtu=dict(type="int"),
        name=dict(type="str"),
        queues=dict(type="int"),
        rate=dict(type="float"),
        state=dict(default="present", choices=["present", "absent"]),
        tag=dict(type="int"),
        trunks=dict(type="list", elements="int"),
        vmid=dict(type="int"),
    )


def module_options():
    return dict(required_one_of=[("name", "vmid")])


class ProxmoxNicAnsible(ProxmoxAnsible):
    def update_nic(self, vmid, interface, model, **kwargs):  # noqa: PLR0912
        vm = self.get_vm(vmid)

        try:
            vminfo = self.proxmox_api.nodes(vm["node"]).qemu(vmid).config.get()
        except Exception as e:
            self.module.fail_json(msg=f"Getting information for VM with vmid = {vmid} failed with exception: {e}")

        if interface in vminfo:
            # Convert the current config to a dictionary
            config = vminfo[interface].split(",")
            config.sort()

            config_current = {}

            for i in config:
                kv = i.split("=")
                try:
                    config_current[kv[0]] = kv[1]
                except IndexError:
                    config_current[kv[0]] = ""

            # determine the current model nic and mac-address
            models = [
                "e1000",
                "e1000-82540em",
                "e1000-82544gc",
                "e1000-82545em",
                "i82551",
                "i82557b",
                "i82559er",
                "ne2k_isa",
                "ne2k_pci",
                "pcnet",
                "rtl8139",
                "virtio",
                "vmxnet3",
            ]
            current_model = set(models) & set(config_current.keys())
            current_model = current_model.pop()
            current_mac = config_current[current_model]

            # build nic config string
            config_provided = f"{model}={current_mac}"
        else:
            config_provided = model

        if kwargs["mac"]:
            config_provided = f"{model}={kwargs['mac']}"

        if kwargs["bridge"]:
            config_provided += f",bridge={kwargs['bridge']}"

        if kwargs["firewall"]:
            config_provided += ",firewall=1"

        if kwargs["link_down"]:
            config_provided += ",link_down=1"

        if kwargs["mtu"]:
            config_provided += f",mtu={kwargs['mtu']}"
            if model != "virtio":
                self.module.warn(
                    f"Ignoring MTU for nic {interface} on VM with vmid {vmid}, model should be set to 'virtio': "
                )

        if kwargs["queues"]:
            config_provided += f",queues={kwargs['queues']}"

        if kwargs["rate"]:
            config_provided += f",rate={kwargs['rate']}"

        if kwargs["tag"]:
            config_provided += f",tag={kwargs['tag']}"

        if kwargs["trunks"]:
            config_provided += f",trunks={';'.join(str(x) for x in kwargs['trunks'])}"

        net = {interface: config_provided}
        vm = self.get_vm(vmid)

        if (interface not in vminfo) or (vminfo[interface] != config_provided):
            if not self.module.check_mode:
                self.proxmox_api.nodes(vm["node"]).qemu(vmid).config.set(**net)
            return True

        return False

    def delete_nic(self, vmid, interface):
        vm = self.get_vm(vmid)
        vminfo = self.proxmox_api.nodes(vm["node"]).qemu(vmid).config.get()

        if interface in vminfo:
            if not self.module.check_mode:
                self.proxmox_api.nodes(vm["node"]).qemu(vmid).config.set(delete=interface)
            return True

        return False


def main():
    module = create_proxmox_module(module_args(), **module_options())
    proxmox = ProxmoxNicAnsible(module)

    interface = module.params["interface"]
    model = module.params["model"]
    name = module.params["name"]
    state = module.params["state"]
    vmid = module.params["vmid"]

    # If vmid is not defined then retrieve its value from the vm name,
    if not vmid:
        vmid = proxmox.get_vmid(name)

    # Ensure VM id exists
    proxmox.get_vm(vmid)

    if state == "present":
        try:
            if proxmox.update_nic(
                vmid,
                interface,
                model,
                bridge=module.params["bridge"],
                firewall=module.params["firewall"],
                link_down=module.params["link_down"],
                mac=module.params["mac"],
                mtu=module.params["mtu"],
                queues=module.params["queues"],
                rate=module.params["rate"],
                tag=module.params["tag"],
                trunks=module.params["trunks"],
            ):
                module.exit_json(changed=True, vmid=vmid, msg=f"Nic {interface} updated on VM with vmid {vmid}")
            else:
                module.exit_json(vmid=vmid, msg=f"Nic {interface} unchanged on VM with vmid {vmid}")
        except Exception as e:
            module.fail_json(vmid=vmid, msg=f"Unable to change nic {interface} on VM with vmid {vmid}: " + str(e))

    elif state == "absent":
        try:
            if proxmox.delete_nic(vmid, interface):
                module.exit_json(changed=True, vmid=vmid, msg=f"Nic {interface} deleted on VM with vmid {vmid}")
            else:
                module.exit_json(vmid=vmid, msg=f"Nic {interface} does not exist on VM with vmid {vmid}")
        except Exception as e:
            module.fail_json(vmid=vmid, msg=f"Unable to delete nic {interface} on VM with vmid {vmid}: " + str(e))


if __name__ == "__main__":
    main()
