
    VhH&                         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 d dlmZ d dlmZmZ d d	lmZ  G d
 de      Zd Zedk(  r e        yy# e$ r Y =w xY w)    )absolute_importdivisionprint_functionaW  
module: vcenter_license
short_description: Manage VMware vCenter license keys
description:
- Add and delete vCenter, ESXi server license keys.
author:
- Dag Wieers (@dagwieers)
options:
  labels:
    description:
    - The optional labels of the license key to manage in vSphere vCenter.
    - This is dictionary with key/value pair.
    default: {
        'source': 'ansible'
    }
    type: dict
  license:
    description:
    - The license key to manage in vSphere vCenter.
    required: true
    type: str
  state:
    description:
    -  Whether to add (V(present)) or remove (V(absent)) the license key.
    choices: [absent, present]
    default: present
    type: str
  esxi_hostname:
    description:
    - The hostname of the ESXi server to which the specified license will be assigned.
    - This parameter is optional.
    type: str
  datacenter:
    description:
    - The datacenter name to use for the operation.
    type: str
  cluster_name:
    description:
    - Name of the cluster to apply vSAN license.
    type: str
notes:
- This module will also auto-assign the current vCenter to the license key
  if the product matches the license key, and vCenter us currently assigned
  an evaluation license only.
- The evaluation license (00000-00000-00000-00000-00000) is not listed
  when unused.
- If O(esxi_hostname) is specified, then will assign the O(license) key to
  the ESXi host.
- If O(esxi_hostname) is not specified, then will just register the O(license) key to
  vCenter inventory without assigning it to an ESXi host.
extends_documentation_fragment:
- community.vmware.vmware.documentation

a  
- name: Add a new vCenter license
  community.vmware.vcenter_license:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    license: f600d-21ae3-5592b-249e0-cc341
    state: present
  delegate_to: localhost

- name: Remove an (unused) vCenter license
  community.vmware.vcenter_license:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    license: f600d-21ae3-5592b-249e0-cc341
    state: absent
  delegate_to: localhost

- name: Add ESXi license and assign to the ESXi host
  community.vmware.vcenter_license:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    esxi_hostname: '{{ esxi_hostname }}'
    license: f600d-21ae3-5592b-249e0-dd502
    state: present
  delegate_to: localhost

- name: Add vSAN license and assign to the given cluster
  community.vmware.vcenter_license:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: '{{ datacenter_name }}'
    cluster_name: '{{ cluster_name }}'
    license: f600d-21ae3-5592b-249e0-dd502
    state: present
  delegate_to: localhost
z
licenses:
    description: list of license keys after module executed
    returned: always
    type: list
    sample:
    - f600d-21ae3-5592b-249e0-cc341
    - 143cc-0e942-b2955-3ea12-d006f
)vim)AnsibleModule)	to_native)PyVmomifind_hostsystem_by_name)base_argument_specc                   *     e Zd Z fdZd Zd Z xZS )VcenterLicenseMgrc                 ,    t         t        |   |       y N)superr   __init__)selfmodule	__class__s     t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/vmware/plugins/modules/vcenter_license.pyr   zVcenterLicenseMgr.__init__   s    /7    c                 8    |D ]  }|j                   |k(  s|c S  y r   )
licenseKey)r   licenseslicenseitems       r   find_keyzVcenterLicenseMgr.find_key   s'     	D')	 r   c                 h    g }|D ]*  }|j                   |j                  |j                         , |S r   )usedappendr   )r   r   keysr   s       r   	list_keyszVcenterLicenseMgr.list_keys   s:     	)Dyy KK(		)
 r   )__name__
__module____qualname__r   r   r!   __classcell__)r   s   @r   r   r      s    8r   r   c                     t               } | j                  t        t        dt        d            t        dd      t        ddd	dg
      t        d      t        d      t        d                   t        | d      }|j                  d   }|j                  d   }g }|j                  d   D ]E  }t        j                         }||_        |j                  d   |   |_        |j                  |       G t        dt                     }t        |      }|j                         s.|j                  d|j                  j                  d      z         |j                  j                  }	|j!                  |	j"                        |d<   |j$                  rdj'                  |d         dz   |d   d<   |dk(  r||d   vr8d|d<   |j(                  r|d   j                  |       n|	j+                  ||       |j-                  |	j"                  |      }
d }|
|	j.                  }d }|j                  d   }d }|r2|j1                  |      }|s|j                  d|j                  z         |j                  d   }|rJ|j3                  ||      }|s(d}|r|d z  }|j                  ||j                  z         |j4                  }n|j                  d!   rt7        |j                  |j                  d!         }|"|j                  d"|j                  d!   z         |j4                  }d#|
j8                  vr}|j;                  d$|d%|
j8                  d&       nZ|j                  j<                  j>                  |
j>                  v sd'|
j>                  v r |j                  j<                  j@                  }|r	 |jC                  |(      }|r*tI        |      d,k7  r5|d,   jJ                  jL                  |k7  r	 |jO                  ||-       d|d<   |j!                  |	j"                        |d<   n|j                  d1|z         |j$                  rdj'                  |d         dz   |d   d2<   n|d	k(  r||d   v r|j-                  |	j"                  |      }
|
jP                  d,kD  r#|j                  d3|d4|
jP                  d5       d|d<   |j(                  r|d   jS                  |       n/|	jU                  |       |j!                  |	j"                        |d<   |j$                  rdj'                  |d         dz   |d   d2<    |jV                  d6i | y # tD        $ r-}|j                  d)|d*tG        |      d+       Y d }~d }~ww xY w# tD        $ r' |j                  d.|d/|
j>                  d0       Y w xY w)7Ndictansible)source)typedefaultstrT)r*   requiredpresentabsent)r*   r+   choices)r*   )labelsr   stateesxi_hostname
datacentercluster_name)argument_specsupports_check_moder   r2   r1   F)changeddiffzHvcenter_license is meant for vCenter, hostname %s is not vCenter server.hostname)msgr   
r9   beforer8   r4   z,Unable to find the datacenter %(datacenter)sr5   )r5   datacenter_namez+Unable to find the cluster %(cluster_name)sz in datacenter %(datacenter)sr3   z)Cannot find the specified ESXi host "%s".esxzLicense key "z" edition "z!" is not suitable for ESXi serverzvCenter Server)entityIdzCould not query vCenter "z" assigned license info due to .r   )entityr   zCould not assign "z" (z) to vCenter.z0License "%s" is not existing or can not be addedafterzCannot remove key "z", still in use z	 time(s). ),r   updater'   r   paramsr   KeyValuekeyvaluer   r   
is_vcenter	fail_jsongetcontentlicenseManagerr!   r   _diffjoin
check_mode
AddLicenser   licenseAssignmentManagerfind_datacenter_by_namefind_cluster_by_name_moIdr
   
editionKeywarnaboutnameinstanceUuidQueryAssignedLicenses	Exceptionr   lenassignedLicenser   UpdateAssignedLicenser   removeRemoveLicense	exit_json)r6   r   r   r2   r1   kkvresultpyvlmrH   r@   lamassigned_licenser4   datacenter_objclustercluster_objr;   	esxi_hostes                        r   mainrp      s   &(MY)?@%$/y8Y:OP&U#u%  # F
 mmI&GMM'"E F]]8$ \\^==*1-b	 VF
 F
#C>> 68>8I8I*8UV 	W 
	#	#Br{{3F:||#'99VJ-?#@4#Gvx 	&,, $F9  z"))'2gv.ll2;;0?--C#|4J!N!$!<!<Z!H%$$)WZ`ZgZg)g$hmmN3G!66G]k6l"GC!>>$$v}})<$=&,,/3CKKA_`	$$$)TW]WdWdetWu)u$v$??.KKcjlolzlz {|""''38837G3887S;;,,99 E'*'@'@('@'S$ (C0@,AQ,FK[\]K^KnKnKyKy  ~E  LEm11g1V )-F9%%(]]2;;%?z"!SV]!]^<<&*iiz0B&Cd&JF6N7#	(	w&*<< ll2;;088a<W^`c`h`h!ij y:%%g.W%!$r{{!;F:<<&*iiz0B&Cd&JF6N7#Fv? ! E$$ltv  AB  wC  *D$  E  EE % m((Y`bebjbj-k(lms*   :U 9V 	V#"VV,WW__main__N)
__future__r   r   r   r*   __metaclass__DOCUMENTATIONEXAMPLESRETURNpyVmomir   ImportErroransible.module_utils.basicr   ansible.module_utils._textr   @ansible_collections.community.vmware.plugins.module_utils.vmwarer	   r
   Hansible_collections.community.vmware.plugins.module_utils._argument_specr   r   rp   r"   rD   r   r   <module>r}      s|    A @5n'R
	 5 0 m g (yx zF s  		s   A AA