
    Vh1                     j    d dl mZmZmZ eZdZdZdZd dl	m
Z
  G d de
      Zd Zed	k(  r e        y
y
)    )absolute_importdivisionprint_functiona  
module: one_vnet
short_description: Manages OpenNebula virtual networks
version_added: 9.4.0
author: "Alexander Bakanovskii (@abakanovskii)"
requirements:
  - pyone
description:
  - Manages virtual networks in OpenNebula.
attributes:
  check_mode:
    support: partial
    details:
      - Note that check mode always returns C(changed=true) for existing networks, even if the network would not actually
        change.
  diff_mode:
    support: none
options:
  id:
    description:
      - A O(id) of the network you would like to manage.
      - If not set then a new network will be created with the given O(name).
    type: int
  name:
    description:
      - A O(name) of the network you would like to manage. If a network with the given name does not exist it will be created,
        otherwise it will be managed by this module.
    type: str
  template:
    description:
      - A string containing the network template contents.
    type: str
  state:
    description:
      - V(present) - state that is used to manage the network.
      - V(absent) - delete the network.
    choices: ["present", "absent"]
    default: present
    type: str

extends_documentation_fragment:
  - community.general.opennebula
  - community.general.attributes
a  
- name: Make sure the network is present by ID
  community.general.one_vnet:
    id: 0
    state: present
  register: result

- name: Make sure the network is present by name
  community.general.one_vnet:
    name: opennebula-bridge
    state: present
  register: result

- name: Create a new or update an existing network
  community.general.one_vnet:
    name: bridge-network
    template: |
      VN_MAD  = "bridge"
      BRIDGE  = "br0"
      BRIDGE_TYPE  = "linux"
      AR=[
        TYPE  = "IP4",
        IP    = 192.0.2.50,
        SIZE  = "20"
      ]
      DNS     = 192.0.2.1
      GATEWAY = 192.0.2.1

- name: Delete the network by ID
  community.general.one_vnet:
    id: 0
    state: absent
a  
id:
  description: The network ID.
  type: int
  returned: when O(state=present)
  sample: 153
name:
  description: The network name.
  type: str
  returned: when O(state=present)
  sample: app1
template:
  description: The parsed network template.
  type: dict
  returned: when O(state=present)
  sample:
    BRIDGE: onebr.1000
    BRIDGE_TYPE: linux
    DESCRIPTION: sampletext
    PHYDEV: eth0
    SECURITY_GROUPS: 0
    VLAN_ID: 1000
    VN_MAD: 802.1Q
user_id:
  description: The network's user name.
  type: int
  returned: when O(state=present)
  sample: 1
user_name:
  description: The network's user ID.
  type: str
  returned: when O(state=present)
  sample: oneadmin
group_id:
  description: The network's group ID.
  type: int
  returned: when O(state=present)
  sample: 1
group_name:
  description: The network's group name.
  type: str
  returned: when O(state=present)
  sample: one-users
owner_id:
  description: The network's owner ID.
  type: int
  returned: when O(state=present)
  sample: 143
owner_name:
  description: The network's owner name.
  type: str
  returned: when O(state=present)
  sample: ansible-test
permissions:
  description: The network's permissions.
  type: dict
  returned: when O(state=present)
  contains:
    owner_u:
      description: The network's owner USAGE permissions.
      type: str
      sample: 1
    owner_m:
      description: The network's owner MANAGE permissions.
      type: str
      sample: 0
    owner_a:
      description: The network's owner ADMIN permissions.
      type: str
      sample: 0
    group_u:
      description: The network's group USAGE permissions.
      type: str
      sample: 0
    group_m:
      description: The network's group MANAGE permissions.
      type: str
      sample: 0
    group_a:
      description: The network's group ADMIN permissions.
      type: str
      sample: 0
    other_u:
      description: The network's other users USAGE permissions.
      type: str
      sample: 0
    other_m:
      description: The network's other users MANAGE permissions.
      type: str
      sample: 0
    other_a:
      description: The network's other users ADMIN permissions.
      type: str
      sample: 0
  sample:
    owner_u: 1
    owner_m: 0
    owner_a: 0
    group_u: 0
    group_m: 0
    group_a: 0
    other_u: 0
    other_m: 0
    other_a: 0
clusters:
  description: The network's clusters.
  type: list
  returned: when O(state=present)
  sample: [0, 100]
bridge:
  description: The network's bridge interface.
  type: str
  returned: when O(state=present)
  sample: br0
bridge_type:
  description: The network's bridge type.
  type: str
  returned: when O(state=present)
  sample: linux
parent_network_id:
  description: The network's parent network ID.
  type: int
  returned: when O(state=present)
  sample: 1
vn_mad:
  description: The network's VN_MAD.
  type: str
  returned: when O(state=present)
  sample: bridge
phydev:
  description: The network's physical device (NIC).
  type: str
  returned: when O(state=present)
  sample: eth0
vlan_id:
  description: The network's VLAN tag.
  type: int
  returned: when O(state=present)
  sample: 1000
outer_vlan_id:
  description: The network's outer VLAN tag.
  type: int
  returned: when O(state=present)
  sample: 1000
vrouters:
  description: The network's list of virtual routers IDs.
  type: list
  returned: when O(state=present)
  sample: [0, 1]
ar_pool:
  description: The network's list of ar_pool.
  type: list
  returned: when O(state=present)
  sample:
    - ar_id: 0
      ip: 192.0.2.1
      mac: 6c:1e:46:01:cd:d1
      size: 20
      type: IP4
    - ar_id: 1
      allocated: 0
      ip: 198.51.100.1
      mac: 5d:9b:c0:9e:f6:e5
      size: 20
      type: IP4
)OpenNebulaModulec                   N    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)NetworksModulec           	          t        t        dd      t        dd      t        dddgd      t        dd            }d	d
gg}dg}dddggg}t        j                  | |d|||       y )NintF)typerequiredstrpresentabsent)r   choicesdefault)idnamestatetemplater   r   )r   r   r   r   T)supports_check_modemutually_exclusiverequired_one_ofrequired_if)dictr   __init__)selfargument_specr   r   r   s        n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/one_vnet.pyr   zNetworksModule.__init__  s    /551EIx+@)Tuu5	
 6N
 ** i*.
 	!!$"/6:5G2A.9	;    c                    |j                   }|j                  d      }|j                  d      }|j                  d      }|j                  d      }i | _        | j                  ||      }	d}
|	s(|dk7  r#|r|j	                  dt        |      z          nd	}
|dk(  r| j                  |	      | _        n1|
r| j                  ||      | _        n| j                  |	|      | _        | j                          y )
Nr   r   r   r   Fr   zThere is no template with id=)msgT)
paramsgetresultget_template_instance	fail_jsonr   delete_templatecreate_templateupdate_templateexit)r   onemoduler$   r"   r   r   desired_statetemplate_datar   needs_creations              r   runzNetworksModule.run!  s    ZZzz&!

7+

:.--b$7MX5  %Ds2w%N O!%H$..x8DK"224G"228]K		r   c                     | j                   j                  j                  ddd      }|j                  D ]  } ||      s|c S  y )N)r+   vnpoolinfoVNET)r   	predicatepoolr   s       r   get_templatezNetworksModule.get_template<  sE     xx##BB/		 	 H"	  r   c                 ,    | j                  fd      S )Nc                 "    | j                   k(  S N)ID)r   template_ids    r   <lambda>z3NetworksModule.get_template_by_id.<locals>.<lambda>H  s    8;;+3M r   r9   )r   r>   s    `r   get_template_by_idz!NetworksModule.get_template_by_idG  s      !NOOr   c                 ,    | j                  fd      S )Nc                 "    | j                   k(  S r<   )NAME)r   r   s    r   r?   z5NetworksModule.get_template_by_name.<locals>.<lambda>K  s    8==D3H r   r@   )r   r   s    `r   get_template_by_namez#NetworksModule.get_template_by_nameJ  s      !IJJr   c                 J    |r| j                  |      S | j                  |      S r<   )rA   rE   )r   requested_idrequested_names      r   r%   z$NetworksModule.get_template_instanceM  s'    **<88,,^<<r   c                 ~   g }|j                   j                  }t        t        |            D ]  }||   }|j	                  |j
                  |j                  |j                  |j                  t        |dd      t        |dd      t        |dd      t        |dd      t        |dd      t        |dd      d
        |S )	N	ALLOCATEDNullIPGLOBAL_PREFIXPARENT_NETWORK_AR_ID
ULA_PREFIXVN_MAD)
ar_idmacsizer   	allocatedipglobal_prefixparent_network_ar_id
ula_prefixvn_mad)
AR_POOLARrangelenappendAR_IDMACSIZETYPEgetattr)r   r   ar_pooltemplate_poolartemplate_params         r   get_networks_ar_poolz#NetworksModule.get_networks_ar_poolS  s     ((++M*+ 	B*2.NNN'--%))&++&++ %^[&IndF;!(&!Q(/@VX^(_%nlFK!.(FC 	" r   c                    i d|j                   d|j                  d|j                  d|j                  d|j                  d|j
                  d|j                  d|j                  j                  |j                  j                  |j                  j                  |j                  j                  |j                  j                  |j                  j                  |j                  j                  |j                  j                  |j                  j                   d		d
|j"                  j                   d|j$                  d|j&                  d|j(                  d|j*                  d|j,                  d|j.                  d|j0                  d|j2                  |j4                  j                   | j7                  |      d}|S )Nr   r   r   	user_nameuser_id
group_namegroup_idpermissions)	owner_uowner_mowner_agroup_ugroup_mgroup_aother_uother_mother_aclustersbridge
bride_typeparent_network_idrY   phydevvlan_idouter_vlan_idused_leases)vroutersrd   )r=   rD   TEMPLATEUNAMEUIDGNAMEGIDPERMISSIONSOWNER_UOWNER_MOWNER_AGROUP_UGROUP_MGROUP_AOTHER_UOTHER_MOTHER_ACLUSTERSBRIDGEBRIDGE_TYPEPARENT_NETWORK_IDrP   PHYDEVVLAN_IDOUTER_VLAN_IDUSED_LEASESVROUTERSrh   )r   r   r5   s      r   get_template_infoz NetworksModule.get_template_infoi  s   
(++
HMM
 ))
 	

 x||
 (..
 
 #//77#//77#//77#//77#//77#//77#//77#//77#//77

& )),,'
( hoo)
* (..+
,  !;!;-
. hoo/
0 hoo1
2 x''3
4 X335
6 8//7
8 !)),,00:;
@ r   c                     | j                   j                  s/| j                  j                  j	                  d|z   dz   |z   d       | j                  | j                  |            }d|d<   |S )NzNAME = "z"
r3   Tchanged)r,   
check_moder+   vnallocater   rE   )r   r   r.   r$   s       r   r(   zNetworksModule.create_template  s`    {{%%HHKK  t!3f!<}!LbQ''(A(A$(GH yr   c                 L   | j                   j                  s1| j                  j                  j	                  |j
                  |d       | j                  | j                  |j
                              }| j                   j                  rd|d<   |S |j                  |d   k7  |d<   |S )Nr   Tr   r   )	r,   r   r+   r   updater=   r   rA   r   )r   r   r.   r$   s       r   r)   zNetworksModule.update_template  s    {{%%HHKKx{{M1=''(?(?(LM;;!! $F9
  !) 1 1VJ5G GF9r   c                     |sddiS | j                   j                  s/| j                  j                  j	                  |j
                         ddiS )Nr   FT)r,   r   r+   r   deleter=   )r   r   s     r   r'   zNetworksModule.delete_template  sB    u%%{{%%HHKKx{{+4  r   N)__name__
__module____qualname__r   r0   r9   rA   rE   r%   rh   r   r(   r)   r'    r   r   r   r     s<    ;26	PK=,!F!r   r   c                  4    t               j                          y r<   )r   
run_moduler   r   r   mainr     s    !r   __main__N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNEansible_collections.community.general.plugins.module_utils.opennebular   r   r   r   r   r   r   <module>r      s[    C B+Z De
P cf!% f!R" zF r   