
    Vh                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlmZmZ d dlmZmZ d dlmZmZ d d	lmZ d d
lmZmZmZmZmZm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)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2d Z3d!Z4d"Z5d#Z6d$Z7d%d&d'd(Z8 G d) d*      Z9 G d+ d,e9      Z: G d- d.e9      Z; G d/ d0e9      Z< G d1 d2      Z=d3 Z>d4 Z?e@d5k(  r e>        yy)6    )absolute_importdivisionprint_functiona  
---
module: idrac_license
short_description: Configure iDRAC licenses
version_added: "8.7.0"
description:
  - This module allows to import, export and delete licenses on iDRAC.
extends_documentation_fragment:
  - dellemc.openmanage.idrac_x_auth_options
options:
  license_id:
    description:
      - Entitlement ID of the license that is to be imported, exported or deleted.
      - I(license_id) is required when I(delete) is C(true) or I(export) is C(true).
    type: str
    aliases: ['entitlement_id']
  delete:
    description:
      - Delete the license from the iDRAC.
      - When I(delete) is C(true), then I(license_id) is required.
      - I(delete) is mutually exclusive with I(export) and I(import).
    type: bool
    default: false
  export:
    description:
      - Export the license from the iDRAC.
      - When I(export) is C(true), I(license_id) and I(share_parameters) is required.
      - I(export) is mutually exclusive with I(delete) and I(import).
    type: bool
    default: false
  import:
    description:
      - Import the license from the iDRAC.
      - When I(import) is C(true), I(share_parameters) is required.
      - I(import) is mutually exclusive with I(delete) and I(export).
    type: bool
    default: false
  share_parameters:
    description:
      - Parameters that are required for the import and export operation of a license.
      - I(share_parameters) is required when I(export) or I(import) is C(true).
    type: dict
    suboptions:
      share_type:
        description:
          - Share type of the network share.
          - C(local) uses local path for I(import) and I(export) operation.
          - C(nfs) uses NFS share for I(import) and I(export) operation.
          - C(cifs) uses CIFS share for I(import) and I(export) operation.
          - C(http) uses HTTP share for I(import) and I(export) operation.
          - C(https) uses HTTPS share for I(import) and I(export) operation.
        type: str
        choices: [local, nfs, cifs, http, https]
        default: local
      file_name:
        description:
          - License file name for I(import) and I(export) operation.
          - I(file_name) is required when I(import) is C(true).
          - For the I(import) operation, when I(share_type) is C(local), the supported extensions for I(file_name) are '.txt' and '.xml'.
            For other share types, the supported extension is '.xml'
        type: str
      ip_address:
        description:
          - IP address of the network share.
          - I(ip_address) is required when I(share_type) is C(nfs), C(cifs), C(http) or C(https).
        type: str
      share_name:
        description:
          - Network share or local path of the license file.
        type: str
      workgroup:
        description:
          - Workgroup of the network share.
          - I(workgroup) is applicable only when I(share_type) is C(cifs).
        type: str
      username:
        description:
          - Username of the network share.
          - I(username) is required when I(share_type) is C(cifs).
        type: str
      password:
        description:
          - Password of the network share.
          - I(password) is required when I(share_type) is C(cifs).
        type: str
      ignore_certificate_warning:
        description:
          - Ignores the certificate warning while connecting to Share and is only applicable when I(share_type) is C(https).
          - C(on) ignores the certificate warning.
          - C(off) does not ignore the certificate warning.
        type: str
        choices: ["off", "on"]
        default: "off"
      proxy_support:
        description:
          - Specifies if proxy is to be used or not.
          - C(off) does not use proxy settings.
          - C(default_proxy) uses the default proxy settings.
          - C(parameters_proxy) uses the specified proxy settings. I(proxy_server) is required when I(proxy_support) is C(parameters_proxy).
          - I(proxy_support) is only applicable when I(share_type) is C(https) or C(https).
        type: str
        choices: ["off", "default_proxy", "parameters_proxy"]
        default: "off"
      proxy_type:
        description:
          - The proxy type of the proxy server.
          - C(http) to select HTTP proxy.
          - C(socks) to select SOCKS proxy.
          - I(proxy_type) is only applicable when I(share_type) is C(https) or C(https) and when I(proxy_support) is C(parameters_proxy).
        type: str
        choices: [http, socks]
        default: http
      proxy_server:
        description:
          - The IP address of the proxy server.
          - I(proxy_server) is required when I(proxy_support) is C(parameters_proxy).
          - I(proxy_server) is only applicable when I(share_type) is C(https) or C(https) and when I(proxy_support) is C(parameters_proxy).
        type: str
      proxy_port:
        description:
          - The port of the proxy server.
          - I(proxy_port) is only applicable when I(share_type) is C(https) or C(https) and when I(proxy_support) is C(parameters_proxy).
        type: int
        default: 80
      proxy_username:
        description:
          - The username of the proxy server.
          - I(proxy_username) is only applicable when I(share_type) is C(https) or C(https) and when I(proxy_support) is C(parameters_proxy).
        type: str
      proxy_password:
        description:
          - The password of the proxy server.
          - I(proxy_password) is only applicable when I(share_type) is C(https) or C(https) and when I(proxy_support) is C(parameters_proxy).
        type: str
  resource_id:
    type: str
    description:
      - Id of the resource.
      - If the value for resource ID is not provided, the module picks the first resource ID available from the list of system resources returned by the iDRAC.
requirements:
  - "python >= 3.9.6"
author:
  - "Rajshekar P(@rajshekarp87)"
notes:
    - Run this module from a system that has direct access to Dell iDRAC.
    - This module supports only iDRAC9 and above.
    - This module supports IPv4 and IPv6 addresses.
    - This module does not support C(check_mode).
    - When I(share_type) is C(local) for I(import) and I(export) operations, job_details are not displayed.
a  
---
- name: Export a license from iDRAC to local
  dellemc.openmanage.idrac_license:
    idrac_ip: "192.168.0.1"
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    license_id: "LICENSE_123"
    export: true
    share_parameters:
      share_type: "local"
      share_name: "/path/to/share"
      file_name: "license_file"

- name: Export a license from iDRAC to NFS share
  dellemc.openmanage.idrac_license:
    idrac_ip: "192.168.0.1"
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    license_id: "LICENSE_123"
    export: true
    share_parameters:
      share_type: "nfs"
      share_name: "/path/to/share"
      file_name: "license_file"
      ip_address: "192.168.0.1"

- name: Export a license from iDRAC to CIFS share
  dellemc.openmanage.idrac_license:
    idrac_ip: "192.168.0.1"
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    license_id: "LICENSE_123"
    export: true
    share_parameters:
      share_type: "cifs"
      share_name: "/path/to/share"
      file_name: "license_file"
      ip_address: "192.168.0.1"
      username: "username"
      password: "password"
      workgroup: "workgroup"

- name: Export a license from iDRAC to HTTP share via proxy
  dellemc.openmanage.idrac_license:
    idrac_ip: "192.168.0.1"
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    license_id: "LICENSE_123"
    export: true
    share_parameters:
      share_type: "http"
      share_name: "/path/to/share"
      file_name: "license_file"
      ip_address: "192.168.0.1"
      username: "username"
      password: "password"
      proxy_support: "parameters_proxy"
      proxy_type: socks
      proxy_server: "192.168.0.2"
      proxy_port: 1080
      proxy_username: "proxy_username"
      proxy_password: "proxy_password"

- name: Export a license from iDRAC to HTTPS share
  dellemc.openmanage.idrac_license:
    idrac_ip: "192.168.0.1"
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    license_id: "LICENSE_123"
    export: true
    share_parameters:
      share_type: "https"
      share_name: "/path/to/share"
      file_name: "license_file"
      ip_address: "192.168.0.1"
      username: "username"
      password: "password"
      ignore_certificate_warning: "on"

- name: Import a license to iDRAC from local
  dellemc.openmanage.idrac_license:
    idrac_ip: 198.162.0.1
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    import: true
    share_parameters:
      file_name: "license_file_name.xml"
      share_type: local
      share_name: "/path/to/share"

- name: Import a license to iDRAC from NFS share
  dellemc.openmanage.idrac_license:
    idrac_ip: 198.162.0.1
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    import: true
    share_parameters:
      file_name: "license_file_name.xml"
      share_type: nfs
      ip_address: "192.168.0.1"
      share_name: "/path/to/share"

- name: Import a license to iDRAC from CIFS share
  dellemc.openmanage.idrac_license:
    idrac_ip: 198.162.0.1
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    import: true
    share_parameters:
      file_name: "license_file_name.xml"
      share_type: cifs
      ip_address: "192.168.0.1"
      share_name: "/path/to/share"
      username: "username"
      password: "password"

- name: Import a license to iDRAC from HTTP share
  dellemc.openmanage.idrac_license:
    idrac_ip: 198.162.0.1
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    import: true
    share_parameters:
      file_name: "license_file_name.xml"
      share_type: http
      ip_address: "192.168.0.1"
      share_name: "/path/to/share"
      username: "username"
      password: "password"

- name: Import a license to iDRAC from HTTPS share via proxy
  dellemc.openmanage.idrac_license:
    idrac_ip: 198.162.0.1
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    import: true
    share_parameters:
      file_name: "license_file_name.xml"
      share_type: https
      ip_address: "192.168.0.1"
      share_name: "/path/to/share"
      username: "username"
      password: "password"
      proxy_support: "parameters_proxy"
      proxy_server: "192.168.0.2"
      proxy_port: 808
      proxy_username: "proxy_username"
      proxy_password: "proxy_password"

- name: Delete a License from iDRAC
  dellemc.openmanage.idrac_license:
    idrac_ip: 198.162.0.1
    idrac_user: "username"
    idrac_password: "password"
    ca_path: "/path/to/ca_cert.pem"
    license_id: "LICENCE_123"
    delete: true
a  
---
msg:
  type: str
  description: Status of the license operation.
  returned: always
  sample: "Successfully exported the license."
job_details:
    description: Returns the output for status of the job.
    returned: For import and export operations
    type: dict
    sample: {
        "ActualRunningStartTime": "2024-01-09T05:16:19",
        "ActualRunningStopTime": "2024-01-09T05:16:19",
        "CompletionTime": "2024-01-09T05:16:19",
        "Description": "Job Instance",
        "EndTime": null,
        "Id": "JID_XXXXXXXXX",
        "JobState": "Completed",
        "JobType": "LicenseExport",
        "Message": "The command was successful.",
        "MessageArgs": [],
        "MessageId": "LIC900",
        "Name": "Export: License",
        "PercentComplete": 100,
        "StartTime": "2024-01-09T05:16:19",
        "TargetSettingsURI": null
    }
error_info:
  description: Details of the HTTP Error.
  returned: on HTTP error
  type: dict
  sample: {
    "error": {
      "code": "Base.1.8.GeneralError",
      "message": "A general error has occurred. See ExtendedInfo for more information.",
      "@Message.ExtendedInfo": [
        {
          "MessageId": "Base.1.8.AccessDenied",
          "Message": "The authentication credentials included with this request are missing or invalid.",
          "MessageArgs": [],
          "RelatedProperties": [],
          "Severity": "Critical",
          "Resolution": "Attempt to ensure that the URI is correct and that the service has the appropriate credentials."
        }
      ]
    }
  }
N)	HTTPErrorURLError)iDRACRedfishAPIIdracAnsibleModule)ConnectionErrorSSLValidationError)LooseVersion)get_idrac_firmware_versionget_dynamic_uriget_manager_res_id&validate_and_get_first_resource_id_uri
remove_keyidrac_redfish_job_trackingz/redfish/v1z/redfish/v1/Managersz{res_uri}/Jobs/{job_id}OemDellDellLicenseManagementServiceActionsz+#DellLicenseManagementService.ExportLicensez9#DellLicenseManagementService.ExportLicenseToNetworkSharez+#DellLicenseManagementService.ImportLicensez;#DellLicenseManagementService.ImportLicenseFromNetworkSharez	@odata.idz(.*?)@odataz;License with ID '{license_id}' does not exist on the iDRAC.z"Successfully exported the license.z!Successfully deleted the license.z"Successfully imported the license.zPUnable to '{operation}' the license with id '{license_id}' as it does not exist.zUnable to import the license.zLicense file not found.z(iDRAC firmware version is not supported.zATask is skipped as none of import, export or delete is specified.zFile extension is invalid. Supported extensions for local 'share_type' are: .txt and .xml, and for network 'share_type' is: .xml.z.Provided directory path '{path}' is not valid.zkProvided directory path '{path}' is not writable. Please check if the directory has appropriate permissionsz'Missing required parameter 'file_name'.OffDefaultProxyParametersProxyoffdefault_proxyparameters_proxyc                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	Licensec                      || _         || _        y)z
        Initializes the class instance with the provided idrac and module parameters.

        :param idrac: The idrac parameter.
        :type idrac: Any
        :param module: The module parameter.
        :type module: Any
        N)idracmodule)selfr!   r"   s      t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/openmanage/plugins/modules/idrac_license.py__init__zLicense.__init__  s     
    c                      y)z
        Executes the function with the given module.

        :param module: The module to execute.
        :type module: Any
        :return: None
        N )r#   s    r$   executezLicense.execute  s    r&   c                     | j                         }|d| z   }	 | j                  j                  |d      }|S # t        $ r4 | j                  j                  t        j                  |      d       Y yw xY w)a6  
        Check the license ID for a given operation.

        :param self: The object instance.
        :param module: The Ansible module.
        :param license_id: The ID of the license to check.
        :param operation: The operation to perform.
        :return: The response from the license URL.
        /GET
license_idTmsgskippedN)get_license_urlr!   invoke_request	Exceptionr"   	exit_jsonINVALID_LICENSE_MSGformat)r#   r.   license_urilicense_urlresponses        r$   check_license_idzLicense.check_license_id  s|     **,!a
|$44	gzz00eDHO 	gKK!!&9&@&@J&@&Wae!f	gs   8 :A54A5c                     t        | j                  t              }|j                  di       j                  t        i       }t        | j                  |      }|j                  di       j                  t        i       }|S )zp
        Retrieves the license URL for the current user.

        :return: The license URL as a string.
        LicenseServiceLicenses)r   r!   REDFISHgetODATA)r#   v1_resplicense_service_urllicense_service_respr9   s        r$   r2   zLicense.get_license_url  sj     "$**g6%kk*:B?CCE2N.tzz;NO*..z2>BB5"Mr&   c                    t        | j                  | j                  t              }|j                  j                  d      }|j                  d      d   }t        j                  ||d         }t        | j                  |      \  }}}}	t        |t              }|r-| j                  j                  |j                  d      d|	       |S )
z
        Get the status of a job.

        Args:
            module (object): The module object.
            license_job_response (object): The response object for the license job.

        Returns:
            dict: The job details.
        Locationr+   r   job_idres_uriregex_patternMessageTr0   failedjob_detailsr   r"   r!   MANAGERS_URIheadersr@   splitIDRAC_JOB_URIr7   r   r   ODATA_REGEXr5   
r#   license_job_responserJ   job_tracking_urirI   job_uri
job_failedr0   job_dict	wait_times
             r$   get_job_statuszLicense.get_job_status  s     9djjR^_/77;;JG!'',R0&&fgaj&I/I$**V]/^,
C9hkBKK!!LL+$ " & r&   c                    i }| j                   j                  j                  d      j                  d      |d<   | j                   j                  j                  d      j                  d      |d<   | j                   j                  j                  d      j                  d      |d<   | j                   j                  j                  d      j                  d      |d	<   |S )
a  
        Retrieves the share details from the given module.

        Args:
            module (object): The module object containing the share parameters.

        Returns:
            dict: A dictionary containing the share details with the following keys:
                - IPAddress (str): The IP address of the share.
                - ShareName (str): The name of the share.
                - UserName (str): The username for accessing the share.
                - Password (str): The password for accessing the share.
        share_parameters
ip_address	IPAddress
share_name	ShareNameusernameUserNamepasswordPassword)r"   paramsr@   )r#   share_detailss     r$   get_share_detailszLicense.get_share_details  s     %)[[%7%7%;%;<N%O%S%ST`%ak"%)[[%7%7%;%;<N%O%S%ST`%ak"$(KK$6$6$:$:;M$N$R$RS]$^j!$(KK$6$6$:$:;M$N$R$RS]$^j!r&   c                 p   i }| j                   j                  j                  d      j                  d      j                         |d<   | j	                         }|j                  |       | j                   j                  j                  d      j                  d      j                         |d<   | j                   j                  j                  d      j                  d      dk(  rt        | j                   j                  j                  d      j                  d         |d<   | j                   j                  j                  d      j                  d	      j                         |d
<   | j                   j                  j                  d      j                  d      |d<   t        | j                   j                  j                  d      j                  d            |d<   | j                   j                  j                  d      j                  d      r| j                   j                  j                  d      j                  d      rn| j                   j                  j                  d      j                  d      |d<   | j                   j                  j                  d      j                  d      |d<   |S )a"  
        Retrieves the proxy details based on the provided module parameters.

        Args:
            self: The instance of the class.
            module: The module object containing the parameters.

        Returns:
            dict: A dictionary containing the proxy details.
        r`   
share_type	ShareTypeignore_certificate_warningIgnoreCertWarningproxy_supportr   ProxySupport
proxy_type	ProxyTypeproxy_serverProxyServer
proxy_port	ProxyPortproxy_usernameproxy_password
ProxyUnameProxyPasswd)	r"   ri   r@   upperrk   update
capitalizePROXY_SUPPORTstr)r#   proxy_detailsrj   s      r$   get_proxy_detailszLicense.get_proxy_details  s@    %)[[%7%7%;%;<N%O%S%ST`%a%g%g%ik"..0]+-1[[-?-?-C-CDV-W-[-[\x-y  .E  .E  .G)*;;!!"4599/JN``,9$++:L:L:P:PQc:d:h:hix:y,zM.))-););)?)?@R)S)W)WXd)e)k)k)mM+&+/;;+=+=+A+ABT+U+Y+YZh+iM-(),T[[-?-?-C-CDV-W-[-[\h-i)jM+&{{!!%%&89==>NOTXT_T_TfTfTjTjk}T~  UC  UC  DT  UU.2kk.@.@.D.DEW.X.\.\]m.nl+/3{{/A/A/E/EFX/Y/]/]^n/om,r&   N)
__name__
__module____qualname__r%   r)   r;   r2   r^   rk   r   r(   r&   r$   r   r     s&    
g$
0*r&   r   c                       e Zd Zd Zy)DeleteLicensec                    | j                   j                  j                  d      }| j                  |       | j	                         }|d| z   }| j
                  j                  |d      }|j                  }|dk(  r"| j                   j                  t        d       y
| j                   j                  t        j                  d|      d	       y
)z
        Executes the delete operation for a given license ID.

        Args:
            module (object): The Ansible module object.

        Returns:
            object: The response object from the delete operation.
        r.   r+   DELETE   T)r0   changeddelete	operationr.   r0   rO   N)r"   ri   r@   r;   r2   r!   r3   status_coder5   SUCCESS_DELETE_MSGFAILURE_MSGr7   )r#   r.   r9   delete_license_urldelete_license_responsestatuss         r$   r)   zDeleteLicense.execute/  s     [[''++L9
j)**,(Qzl+;;"&**";";<NPX"Y(44S=KK!!&8$!GKK!!k&8&88Xb&8&clp!qr&   N)r   r   r   r)   r(   r&   r$   r   r   .  s    rr&   r   c                   >    e Zd ZddgZd Zd Zd Zd Zd Zd Z	d	 Z
y
)ExportLicense      c                    | j                   j                  j                  d      j                  d      }| j                   j                  j                  d      }| j                  |       | j	                         }i }|dk(  r| j                  |      }nv|dv r#| j                  |      }| j                  |      }nO|dk(  r#| j                  |      }| j                  |      }n'|dk(  r"| j                  |      }| j                  |      }j                  }|| j                  v r#| j                   j                  t        d|	       y| j                   j                  t        j                  d
|      d|       y)z
        Executes the export operation for a given license ID.

        :param module: The Ansible module object.
        :type module: AnsibleModule

        :return: The response from the export operation.
        :rtype: Response
        r`   rm   r.   localhttphttpscifsnfsTr0   r   rP   exportr   rN   N)r"   ri   r@   r;   &_ExportLicense__get_export_license_url$_ExportLicense__export_license_local#_ExportLicense__export_license_httpr^   #_ExportLicense__export_license_cifs"_ExportLicense__export_license_nfsr   STATUS_SUCCESSr5   SUCCESS_EXPORT_MSGr   r7   )r#   rm   r.   export_license_url
job_statusexport_license_responser   s          r$   r)   zExportLicense.executeH  si    [[''++,>?CCLQ
[[''++L9
j)!::<
 &*&A&ABT&U#,,&*&@&@AS&T#,,-DEJ6!&*&@&@AS&T#,,-DEJ5 &*&?&?@R&S#,,-DEJ(44T(((KK!!&8$T^!_KK!!k&8&88Xb&8&clp  I!  Jr&   c                 V   i }| j                   j                  j                  d      |d<   | j                   j                  j                  d      j                  d      }t        j                  j                  |      sPt        j                  j                  |      s1| j                   j                  t        j                  |      d       t        j                  |t        j                        s1| j                   j                  t        j                  |      d       | j                   j                  j                  d      j                  d      }|r| }n| j                   j                  d    d	}| j                  j                  |d
|      }|j                  }t!        j"                  |j                  d            j%                  d      }t        j                  j'                  ||      }	t)        |	d      5 }
|
j+                  |       ddd       |S # 1 sw Y   |S xY w)a  
        Export the license to a local directory.

        Args:
            module (object): The Ansible module object.
            export_license_url (str): The URL for exporting the license.

        Returns:
            object: The license status after exporting.
        r.   EntitlementIDr`   rc   pathTr   	file_name_iDRAC_license.xmlPOSTdataLicenseFilezutf-8wN)r"   ri   r@   osr   existsisdirr5   INVALID_DIRECTORY_MSGr7   accessW_OK%INSUFFICIENT_DIRECTORY_PERMISSION_MSGr!   r3   	json_database64	b64decodedecodejoinopenwrite)r#   r   payloadr   license_namelicense_file_namelicense_statuslicense_datalicense_filer   fps              r$   __export_license_localz$ExportLicense.__export_license_localh  s    #';;#5#5#9#9,#G {{!!%%&89==lKt$d(;KK!!&;&B&B&B&MVZ![yyrww'KK!!&K&R&RX\&R&]fj!k{{))--.@AEEkR#/.#';;#5#5l#C"DDV W223EvT[2\%//''(8(8(GHOOPWXGGLL'89	)S! 	#RHH\"	#	#s   HH(c                     i }| j                   j                  j                  d      |d<   | j                         }|j	                  |       | j                  ||      }|S )z
        Export the license using the HTTP protocol.

        Args:
            module (object): The module object.
            export_license_url (str): The URL for exporting the license.

        Returns:
            str: The export status.
        r.   r   )r"   ri   r@   r   r~   _ExportLicense__export_license)r#   r   r   r   export_statuss        r$   __export_license_httpz#ExportLicense.__export_license_http  s[     #';;#5#5#9#9,#G ..0}%--g7IJr&   c                    i }| j                   j                  j                  d      |d<   d|d<   | j                   j                  j                  d      j                  d      r7| j                   j                  j                  d      j                  d      |d<   | j                         }|j	                  |       | j                  ||      }|S )a  
        Export the license using CIFS share type.

        Args:
            module (object): The Ansible module object.
            export_license_url (str): The URL for exporting the license.

        Returns:
            str: The export status.
        r.   r   CIFSrn   r`   	workgroup	Workgroup)r"   ri   r@   rk   r~   r   )r#   r   r   rj   r   s        r$   __export_license_cifsz#ExportLicense.__export_license_cifs  s     #';;#5#5#9#9,#G %;;!!"4599+F#';;#5#5#9#9:L#M#Q#QR]#^GK ..0}%--g7IJr&   c                 d   i }| j                   j                  j                  d      |d<   d|d<   | j                   j                  j                  d      j                  d      |d<   | j                   j                  j                  d      j                  d      |d	<   | j                  ||      }|S )
a  
        Export the license using NFS share type.

        Args:
            module (object): The Ansible module object.
            export_license_url (str): The URL for exporting the license.

        Returns:
            dict: The export status of the license.
        r.   r   NFSrn   r`   ra   rb   rc   rd   )r"   ri   r@   r   )r#   r   r   r   s       r$   __export_license_nfsz"ExportLicense.__export_license_nfs  s     #';;#5#5#9#9,#G $#{{11556HIMMl[#{{11556HIMMl[--g7IJr&   c                    t        | j                  | j                  t              \  }}|r| j                  j	                  |d       t        | j                  |      }|j                  di       j                  t        i       j                  t        i       j                  t        i       j                  t        i       }t        | j                  |      }| j                  j                  j                  d      j                  d      dk(  rt        nt        }|j                  t        i       j                  |i       j                  di       }|S )z
        Get the export license URL.

        :param module: The module object.
        :type module: object
        :return: The export license URL.
        :rtype: str
        Tr   Linksr`   rm   r   target)r   r"   r!   rR   r5   r   r@   OEMMANUFACTURERLICENSE_MANAGEMENT_SERVICErA   ri   EXPORT_LOCALEXPORT_NETWORK_SHAREACTIONS)r#   uri	error_msgrespurlaction_resplicense_service
export_urls           r$   __get_export_license_urlz&ExportLicense.__get_export_license_url      @KK\3YKK!!i!=tzz3/hhw#''R044\2FJJKegijnnotvxy%djj#6*.++*<*<*@*@AS*T*X*XYe*fjq*q,  xL __Wb155orJNNxY[\
r&   c                     | j                   j                  j                  d      j                  d      }|r| }n| j                   j                  d    d}||d<   | j                  j	                  |d|      }|S )aS  
        Export the license to a file.

        Args:
            module (object): The Ansible module object.
            payload (dict): The payload containing the license information.
            export_license_url (str): The URL for exporting the license.

        Returns:
            dict: The license status after exporting.
        r`   r   r.   r   FileNamer   r   r"   ri   r@   r!   r3   )r#   r   r   r   r   r   s         r$   __export_licensezExportLicense.__export_license  s     {{))--.@AEEkR#/.#';;#5#5l#C"DDV W/
223EvT[2\r&   N)r   r   r   r   r)   r   r   r   r   r   r   r(   r&   r$   r   r   E  s0    3ZNJ@>$*&(r&   r   c                   D    e Zd ZddgZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zy)ImportLicenser   r   c                 d   | j                   j                  j                  d      j                  d      s!| j                   j                  t        d       | j                   j                  j                  d      j                  d      }| j                          | j                         }t        | j                        }i }|dk(  r| j                  ||      }ny|dv r$| j                  ||      }| j                  |      }nQ|dk(  r$| j                  ||      }| j                  |      }n(|d	k(  r#| j                  ||      }| j                  |      }j                  }|| j                  v r#| j                   j                  t         d|
       y| j                   j                  t"        d|       y)z
        Executes the import license process based on the given module parameters.

        Args:
            module (object): The Ansible module object.

        Returns:
            object: The response object from the import license API call.
        r`   r   Tr   rm   r   r   r   r   r   rN   N)r"   ri   r@   r5   MISSING_FILE_NAME_PARAMETER_MSG$_ImportLicense__check_file_extension&_ImportLicense__get_import_license_urlr   r!   $_ImportLicense__import_license_local#_ImportLicense__import_license_httpr^   #_ImportLicense__import_license_cifs"_ImportLicense__import_license_nfsr   r   SUCCESS_IMPORT_MSGFAILURE_IMPORT_MSG)r#   rm   import_license_urlresource_idr   import_license_responser   s          r$   r)   zImportLicense.execute  s    {{!!%%&89==kJKK!!&Ed!S[[''++,>?CCLQ
##%!::<(4
 &*&A&ABTVa&b#,,&*&@&@ASU`&a#,,-DEJ6!&*&@&@ASU`&a#,,-DEJ5 &*&?&?@RT_&`#,,-DEJ(44T(((KK!!&8$T^!_KK!!&8S]!^r&   c                 N   i }| j                   j                  j                  d      j                  d      }t        j                  j                  |      sPt        j                  j                  |      s1| j                   j                  t        j                  |      d       | j                   j                  j                  d      j                  d      dz   | j                   j                  j                  d      j                  d      z   }t        j                  j                  |      }|rJt        |d      5 }|j                         }t        j                  |      j                  d	      }	d
d
d
       n!| j                   j                  t        d       	|d<   ||d<   d|d<   	 | j                   j#                  |d|      }
|
S # 1 sw Y   9xY w# t$        $ r}t'        t)        j*                  |      t,              }|j                  d      j                  d      d   }|j                  d      }d|v r-| j                   j                  |j                  d      d       n-| j                   j                  |j                  d      |d       Y d
}~
S d
}~ww xY w)a8  
        Import a license locally.

        Args:
            module (object): The Ansible module object.
            import_license_url (str): The URL for importing the license.
            resource_id (str): The ID of the resource.

        Returns:
            dict: The import status of the license.
        r`   rc   r   Tr   r+   r   rbasciiNr   FQDDForceImportOptionsr   r   rK   errorz@Message.ExtendedInfor   	MessageIdLIC018rM   r/   r0   
error_inforO   )r"   ri   r@   r   r   r   r   r5   r   r7   r   readr   encodebytesr   NO_FILE_MSGr!   r3   r   r   jsonloadrV   )r#   r   r   r   r   	file_path
file_exitscertcert_content	read_fileimport_statuserr
filter_errmessage_details
message_ids                  r$   __import_license_localz$ImportLicense.__import_license_local  s\    {{!!%%&89==lKt$d(;KK!!&;&B&B&B&MVZ![KK&&**+=>BB<PSVVY]YdYdYkYkYoYo  qC  ZD  ZH  ZH  IT  ZU  U	WW^^I.
i& M$#yy{"..|<CCGL	M M KK!!k$!?!*%#* 		n JJ556H&W^5_M %M M  	n#DIIcN+NJ(nnW599:QRSTUO(,,[9J:%%%/*=*=i*HRV%W%%/*=*=i*HU_hl%m	ns%   ?5G.G G	J$#B6JJ$c                 
   i }| j                   j                  j                  d      j                  d      |d<   ||d<   d|d<   | j                         }|j	                  |       | j
                  j                  |d|      }|S )	a/  
        Imports a license using HTTP.

        Args:
            module (object): The Ansible module object.
            import_license_url (str): The URL for importing the license.
            resource_id (str): The ID of the resource.

        Returns:
            object: The import status.
        r`   r   LicenseNamer  r  r  r   r   )r"   ri   r@   r   r~   r!   r3   )r#   r   r   r   r   r  s         r$   __import_license_httpz#ImportLicense.__import_license_http7  s     !%!3!3!7!78J!K!O!OP[!\%#* ..0}%

112DfSZ1[r&   c                    i }d|d<   | j                   j                  j                  d      j                  d      |d<   ||d<   d|d<   | j                   j                  j                  d      j                  d	      r7| j                   j                  j                  d      j                  d	      |d
<   | j                         }|j	                  |       | j
                  j                  |d|      }|S )a  
        Imports a license using CIFS share type.

        Args:
            self (object): The instance of the class.
            module (object): The Ansible module object.
            import_license_url (str): The URL for importing the license.
            resource_id (str): The ID of the resource.

        Returns:
            object: The import status of the license.
        r   rn   r`   r   r  r  r  r  r   r   r   r   )r"   ri   r@   rk   r~   r!   r3   )r#   r   r   r   rj   r  s         r$   __import_license_cifsz#ImportLicense.__import_license_cifsL  s     %!%!3!3!7!78J!K!O!OP[!\%#* ;;!!"4599+F#';;#5#5#9#9:L#M#Q#QR]#^GK ..0}%

112DfSZ1[r&   c                    i }d|d<   | j                   j                  j                  d      j                  d      |d<   | j                   j                  j                  d      j                  d      |d<   | j                   j                  j                  d      j                  d      |d	<   ||d
<   d|d<   | j                  j	                  |d|      }|S )aB  
        Import a license from an NFS share.

        Args:
            module (object): The Ansible module object.
            import_license_url (str): The URL for importing the license.
            resource_id (str): The ID of the resource.

        Returns:
            dict: The import status of the license.
        r   rn   r`   ra   rb   rc   rd   r   r  r  r  r  r   r   r   )r#   r   r   r   r  s        r$   __import_license_nfsz"ImportLicense.__import_license_nfse  s     $#{{11556HIMMl[#{{11556HIMMl[!%!3!3!7!78J!K!O!OP[!\%#* 

112DfSZ1[r&   c                 ^   | j                   j                  j                  d      j                  d      }| j                   j                  j                  d      j                  d      |dk(  rddhndh}t        fd|D              }|s"| j                   j	                  t
        d	       y
y
)z
        Check if the file extension of the given file name is valid.

        :param module: The Ansible module object.
        :type module: AnsibleModule

        :return: None
        r`   rm   r   r   z.txtz.xmlc              3   \   K   | ]#  }j                         j                  |       % y wN)lowerendswith).0extr   s     r$   	<genexpr>z7ImportLicense.__check_file_extension.<locals>.<genexpr>  s#     YY__.77<Ys   ),Tr   N)r"   ri   r@   anyr5   INVALID_FILE_MSG)r#   rm   valid_extensionsfile_extensionr   s       @r$   __check_file_extensionz$ImportLicense.__check_file_extension{  s     [[''++,>?CCLQ
KK&&**+=>BB;O	/9W/DFF+6(YHXYYKK!!&6t!D r&   c                    t        | j                  | j                  t              \  }}|r| j                  j	                  |d       t        | j                  |      }|j                  di       j                  t        i       j                  t        i       j                  t        i       j                  t        i       }t        | j                  |      }| j                  j                  j                  d      j                  d      dk(  rt        nt        }|j                  t        i       j                  |i       j                  di       }|S )z
        Get the import license URL.

        :param module: The module object.
        :type module: object
        :return: The import license URL.
        :rtype: str
        Tr   r   r`   rm   r   r   )r   r"   r!   rR   r5   r   r@   r   r   r   rA   ri   IMPORT_LOCALIMPORT_NETWORK_SHAREr   )r#   r   r   r   r   r   r   
import_urls           r$   __get_import_license_urlz&ImportLicense.__get_import_license_url  r   r&   c                    t        | j                  | j                  t              }|j                  j                  d      }|j                  d      d   }t        j                  ||d         }t        | j                  |      \  }}}}	t        |t              }|rp|j                  d      dk(  r/| j                  j                  |j                  d	      d
|       |S | j                  j                  |j                  d	      d
|       |S )NrF   r+   rG   r   rH   rK   r  r  rM   T)r0   r1   rP   rN   rQ   rW   s
             r$   r^   zImportLicense.get_job_status  s    8djjR^_/77;;JG!'',R0&&fgaj&I/I$**V]/^,
C9hkB||K(H4%%(,,y*A4]e%f 	 %% Y/ ( & * r&   N)r   r   r   r   r)   r   r   r   r   r   r   r^   r(   r&   r$   r   r     s7    3ZN _D%N*2,E (r&   r   c                   (    e Zd ZeeedZed        Zy)LicenseTypeimportr   r   c                     t        fddD        d      }|sj                  t        d       t        j                  j                  |      } ||       S )aG  
        Perform a license operation based on the given parameters.

        :param idrac: The IDRAC object.
        :type idrac: IDRAC
        :param module: The Ansible module object.
        :type module: AnsibleModule
        :return: The license class object based on the license type.
        :rtype: LicenseType
        c              3   B   K   | ]  }j                   |   s|  y wr$  )ri   )r'  paramr"   s     r$   r)  z0LicenseType.license_operation.<locals>.<genexpr>  s      guRXR_R_`eRfUgs   r7  NTr/   )nextr5   NO_OPERATION_SKIP_MSGr6  _license_classesr@   )r!   r"   license_typelicense_classs    `  r$   license_operationzLicenseType.license_operation  sS     g0Ngimn!6E#4488FUF++r&   N)	r   r   r   r   r   r   r>  staticmethodrA  r(   r&   r$   r6  r6    s'     , ,r&   r6  c                     t               } t        | dgg dg dg dgd      }	 t        |j                        5 }t	        |      }t        |      dk  r|j                  t        d	       t        j                  ||      }|r|j                          d
d
d
       y
# 1 sw Y   y
xY w# t        $ rK}t        t        j                  |      t              }|j                  t!        |      |d       Y d
}~y
d
}~wt"        $ r&}|j                  t!        |      d       Y d
}~y
d
}~wt$        t&        t(        t*        t,        f$ r&}|j                  t!        |      d	       Y d
}~y
d
}~ww xY w)a  
    Main function that serves as the entry point for the program.

    This function retrieves the argument specification using the `get_argument_spec` function and updates it with the `idrac_auth_params`.
    It then creates an `AnsibleModule` object with the updated argument specification, specifying the mutually exclusive arguments,
    required arguments if conditions are met, and setting `supports_check_mode` to `False`.

    The function then attempts to establish a connection with the iDRAC Redfish API using the `iDRACRedfishAPI` class.
    It retrieves the iDRAC firmware version using the `get_idrac_firmware_version` function and checks if it is less than or equal to '3.0'.
    If it is, the function exits with a message indicating that the iDRAC firmware version is not supported and sets `failed` to `True`.

    If the iDRAC firmware version is supported, the function creates a `LicenseType` object using the `license_operation` method of the
    `LicenseType` class and calls the `execute` method on the `license_obj` object, passing in the `module` object.

    If an `HTTPError` occurs, the function loads the error response as JSON, removes a specific key using a regular expression pattern,
    and exits with the error message, the filtered error information, and sets `failed` to `True`.

    If a `URLError` occurs, the function exits with the error message and sets `unreachable` to `True`.

    If any of the following errors occur: `SSLValidationError`, `ConnectionError`, `TypeError`, `ValueError`, or `OSError`, the function
    exits with the error message and sets `failed` to `True`.

    Parameters:
        None

    Returns:
        None
    r7  )r8  T)r`   )r   T)r.   r`   )r   Tr-   F)argument_specmutually_exclusiverequired_ifsupports_check_modez3.0Tr   NrK   r	  )r0   unreachable)get_argument_specr	   r   ri   r   r   r5   UNSUPPORTED_FIRMWARE_MSGr6  rA  r)   r   r   r  r  rV   r   r   r   r
   	TypeError
ValueErrorOSError)specsr"   r!   idrac_firmware_versionlicense_objr  r  s          r$   mainrQ    s;   : E:;3A-

 "	F4V]]+ 	&u%?%F"23u<  %=d K%77vFK##%	& 	& 	&  K		#kJ
SX*TJJ 9SX488JP 4SXd334sN   B( ABB( B%!B( %B( (	E+1AC77E+D$$!E+E&&E+c                      ddgdddddddddddddg d	d
ddiddiddiddiddidddddddgd
ddg dd
ddddgd
ddidddddidddddddggddddggddg dgddddggddddggdddgggd d!gd"ddid#S )$a"  
    Returns a dictionary containing the argument spec for the get_argument_spec function.
    The argument spec is a dictionary that defines the parameters and their types and options for the function.
    The dictionary has the following keys:
        - "license_id": A string representing the license ID.
        - "delete": A boolean representing whether to delete the license.
        - "export": A boolean representing whether to export the license.
        - "import": A boolean representing whether to import the license.
        - "share_parameters": A dictionary representing the share parameters.
            - "type": A string representing the share type.
            - "options": A dictionary representing the options for the share parameters.
                - "share_type": A string representing the share type.
                - "file_name": A string representing the file name.
                - "ip_address": A string representing the IP address.
                - "share_name": A string representing the share name.
                - "workgroup": A string representing the workgroup.
                - "username": A string representing the username.
                - "password": A string representing the password.
                - "ignore_certificate_warning": A string representing whether to ignore certificate warnings.
                - "proxy_support": A string representing the proxy support.
                - "proxy_type": A string representing the proxy type.
                - "proxy_server": A string representing the proxy server.
                - "proxy_port": A integer representing the proxy port.
                - "proxy_username": A string representing the proxy username.
                - "proxy_password": A string representing the proxy password.
            - "required_if": A list of lists representing the required conditions for the share parameters.
            - "required_together": A list of lists representing the required conditions for the share parameters.
        - "resource_id": A string representing the resource ID.
    r   entitlement_id)typealiasesboolF)rT  defaultdictr   )r   r   r   r   r   )rT  rW  choicesrT  T)rT  no_logr   onr   r   socksintP   )rm   r   ra   rc   r   re   rg   ro   rq   rs   ru   rw   ry   rz   rm   rc   r   ra   r   )ra   rc   re   rg   r   rq   r   ru   )re   rg   )ry   rz   )rT  optionsrF  required_together)r.   r   r   r8  r`   r   r(   r(   r&   r$   rI  rI    sF   >  %2B1CD!e4!e4!e4 "&H
 %e_%uo%uo$e_#UO%*d;!$ %t}/ "$K" "% &0
 "(',<#)5/+0D"A=B w7u|\&BCv'[\vl'CDw|(DE "4~6FG )4"U.
^ i5 5r&   __main__)A
__future__r   r   r   rT  __metaclass__DOCUMENTATIONEXAMPLESRETURNr  r   r   urllib.errorr   r   Iansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfishr   r	   ansible.module_utils.urlsr
   r   #ansible.module_utils.compat.versionr   Aansible_collections.dellemc.openmanage.plugins.module_utils.utilsr   r   r   r   r   r   r?   rR   rU   r   r   r   r   r   r   r0  r1  rA   rV   r6   r   r   r   r   r   r  rJ  r=  r+  r   r   r   r   r   r   r   r   r6  rQ  rI  r   r(   r&   r$   <module>rl     sY   C BUnhT0
f  	  , y I <T T %); 
<R <T S 9 8 9 `4 'E [ P H )d %"K Tefy yxrG r.cG cLCG CL, ,484vSl zF r&   