
    Vh)                     z    d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functionu
  
module: proxmox_nic
short_description: Management of a NIC of a Qemu(KVM) VM in a Proxmox VE cluster
version_added: 3.1.0
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
  action_group:
    version_added: 9.0.0
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(1 ≤ 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.general.proxmox.actiongroup_proxmox
  - community.general.proxmox.documentation
  - community.general.attributes
a  
- name: Create NIC net0 targeting the vm by name
  community.general.proxmox_nic:
    api_user: root@pam
    api_password: secret
    api_host: proxmoxhost
    name: my_vm
    interface: net0
    bridge: vmbr0
    tag: 3

- name: Create NIC net0 targeting the vm by id
  community.general.proxmox_nic:
    api_user: root@pam
    api_password: secret
    api_host: proxmoxhost
    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.general.proxmox_nic:
    api_user: root@pam
    api_password: secret
    api_host: proxmoxhost
    name: my_vm
    interface: net0
    state: absent
z
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"
)AnsibleModule)proxmox_auth_argument_specProxmoxAnsiblec                       e Zd Zd Zd Zy)ProxmoxNicAnsiblec                    | j                  |      }	 | j                  j                  |d         j                  |      j                  j                         }|v r||   j                  d      }|j                          i }	|D ]  }
|
j                  d      }	 |d   |	|d   <   ! g d
}t        |      t        |	j                               z  }|j                         }|	|   }dj                  ||      }n|}|d   rdj                  ||d         }|d   r|dj                  |d         z  }|d   r|dz  }|d   r|dz  }|d   rG|dj                  |d         z  }|dk7  r+| j                  j!                  dj                  ||             |d   r|dj                  |d         z  }|d   r|dj                  |d         z  }|d   r|dj                  |d         z  }|d   r-|dj                  dj#                  d  |d   D                    z  }||i}| j                  |      }||vs||   |k7  r^| j                  j$                  sG | j                  j                  |d         j                  |      j                  j                  d#i | y!y"# t        $ r-}| j                  j                  d|d|       Y d }~nd }~ww xY w# t        $ r d	|	|d   <   Y ^w xY w)$Nnodez'Getting information for VM with vmid = z failed with exception: )msg,=   r    e1000ze1000-82540emze1000-82544gcze1000-82545emi82551i82557bi82559erne2k_isane2k_pcipcnetrtl8139virtiovmxnet3z{0}={1}macbridgez,bridge={0}firewallz,firewall=1	link_downz,link_down=1mtuz,mtu={0}r   zOIgnoring MTU for nic {0} on VM with vmid {1}, model should be set to 'virtio': queuesz,queues={0}ratez	,rate={0}tagz,tag={0}trunksz,trunks={0};c              3   2   K   | ]  }t        |        y w)N)str).0xs     q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/proxmox_nic.py	<genexpr>z/ProxmoxNicAnsible.update_nic.<locals>.<genexpr>   s     <^SV<^s   TF )get_vmproxmox_apinodesqemuconfigget	Exceptionmodule	fail_jsonsplitsort
IndexErrorsetkeyspopformatwarnjoin
check_mode)selfvmid	interfacemodelkwargsvmvminfoer2   config_currentikvmodelscurrent_modelcurrent_macconfig_providednets                    r+   
update_niczProxmoxNicAnsible.update_nic   s   [[	y%%++BvJ7<<TBIIMMOF I&,,S1FKKMN /WWS\/,.qEN2a5)/cFK#n.A.A.C*DDM)--/M(7K (..ukBO#O%='..ufUmDO(}33F84DEEO*},O+~-O%=z00??O   ::@&D:QS (}33F84DEEO&>{11&.AAO%=z00??O(}33CHH<^VT\M]<^4^__O/*[[f$&*;*N;;))H  &&r&z277=DDHHO3OI  	yKK!!osuv&w!xx	y " /,.N2a5)/s*   AJ K	K"KKK#"K#c                 t   | j                  |      }| j                  j                  |d         j                  |      j                  j                         }||v r^| j                  j                  sG| j                  j                  |d         j                  |      j                  j                  |       yy)Nr   )deleteTF)	r.   r/   r0   r1   r2   r3   r5   r@   r:   )rA   rB   rC   rF   rG   s        r+   
delete_niczProxmoxNicAnsible.delete_nic   s    [[!!''6
388>EEIIK;;))  &&r&z277=DDHHPYHZ    N)__name__
__module____qualname__rQ   rT   r-   rU   r+   r
   r
      s    IV	rU   r
   c                  J   t               } t        t        d      t        dd      t        dd      t        dd      t        d      t        g dd	
      t        d      t        d      t        d      t        d      t        dddg      t        d      t        dd      t        d            }| j                  |       t        | dgddgd      }t	        |      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|s|j                  |      }|j                  |       |dk(  r	 |j                  ||||j
                  d   |j
                  d   |j
                  d   |j
                  d   |j
                  d    |j
                  d!   |j
                  d"   |j
                  d#   |j
                  d$   %      r%|j                  d|d&j                  ||      '       n$|j                  |d(j                  ||      )       y y |dk(  r\	 |j                  ||      r%|j                  d|d+j                  ||      '       y |j                  |d,j                  ||      )       y y # t        $ r9}	|j                  |d*j                  ||      t        |	      z   )       Y d }	~	y d }	~	ww xY w# t        $ r9}	|j                  |d-j                  ||      t        |	      z   )       Y d }	~	y d }	~	ww xY w).Nr(   )typeboolF)rZ   defaultT)rZ   requiredr   r   )choicesr\   intfloatpresentabsent)r\   r^   list)rZ   elements)r   r   rC   r    r   rD   r!   namer"   r#   stater$   r%   rB   )api_token_idapi_token_secret)re   rB   )api_passwordrg   )argument_specrequired_togetherrequired_one_ofsupports_check_moderC   rD   re   rf   rB   r   r   r    r   r!   r"   r#   r$   r%   )	r   r   r    r   r!   r"   r#   r$   r%   z#Nic {0} updated on VM with vmid {1})changedrB   r   z%Nic {0} unchanged on VM with vmid {1})rB   r   z.Unable to change nic {0} on VM with vmid {1}: z#Nic {0} deleted on VM with vmid {1}z*Nic {0} does not exist on VM with vmid {1}z.Unable to delete nic {0} on VM with vmid {1}: )r   dictupdater   r
   paramsget_vmidr.   rQ   	exit_jsonr=   r4   r6   r(   rT   )
module_argsnic_argsr5   proxmoxrC   rD   re   rf   rB   rH   s
             r+   mainrw      s   ,.K651ED1FE2e <EMO euw9y(.CDe%0u!H$ x !?@)+KL 	F  'Gk*IMM'"E== DMM'"E== D % NN4		!!$	5)/x)@+1==+D,2MM+,F&,mmE&:&,mmE&:)/x)@'-}}V'<&,mmE&:)/x)@ " 	B   D>c>j>jktvz>{ |  d0W0^0^_hjn0o p } 
(		!!$	2  D>c>j>jktvz>{ |  d0\0c0cdmos0t u 
  	$,\,c,cdmos,twz{|w},}~~	  	$,\,c,cdmos,twz{|w},}~~	s7   CJ ?6K  6#K  	K$/KK 	L")/LL"__main__N)
__future__r   r   r   rZ   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Bansible_collections.community.general.plugins.module_utils.proxmoxr   r   r
   rw   rV   r-   rU   r+   <module>r      s^    A @Zx@
 5 {U UpEP zF rU   