
    Vh=R                        d 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 ddlmZ  ej                   d	      Z G d
 de      Zd Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Zd Zedk(  r e        yy)zF Ansible module for managing NVMe hosts on Dell Technologies PowerFlex    )absolute_importdivisionprint_functiona  
module: nvme_host
version_added: '2.6.0'
short_description: Manage NVMe hosts on Dell PowerFlex
description:
- Managing NVMe hosts on PowerFlex storage system includes creating, getting details of NVMe hosts
  , modifying and deleting NVMe hosts.

author:
- Peter Cao (@P-Cao) <ansible.team@dell.com>

extends_documentation_fragment:
  - dellemc.powerflex.powerflex

options:
  max_num_paths:
    description:
    - Maximum number of paths per volume. Used to create or modify the NVMe host.
    type: str
  max_num_sys_ports:
    description:
    - Maximum number of ports per protection domain. Used to create or modify the NVMe host.
    type: str
  nqn:
    description:
    - NQN of the NVMe host. Used to create, get or modify the NVMe host.
    - To retrieve NQN of NVMe host, use command :command:`cat /etc/nvme/hostnqn`
    type: str
  nvme_host_name:
    description:
    - Name of the NVMe host.
    - Specify either I(nvme_host_name), I(nqn) for create, get or rename operation.
    type: str
  nvme_host_new_name:
    description:
    - New name of the NVMe host. Used to rename the NVMe host.
    - Only used for updates. Ignored during creation.
    type: str
  state:
    description:
    - State of the NVMe host.
    choices: ['present', 'absent']
    default: present
    type: str
attributes:
  check_mode:
    description: Runs task to validate without performing action on the target
                 machine.
    support: full
  diff_mode:
    description: Runs the task to report the changes made or to be made.
    support: full
a  
- name: Create NVMe host
  dellemc.powerflex.nvme_host:
    hostname: "{{ hostname }}"
    username: "{{ username }}"
    password: "{{ password }}"
    validate_certs: "{{ validate_certs }}"
    nqn: "{{ nqn }}"
    nvme_host_name: "{{ nvme_host_name }}"
    state: "present"

- name: Rename nvme_host using NVMe host id
  dellemc.powerflex.nvme_host:
    hostname: "{{ hostname }}"
    username: "{{ username }}"
    password: "{{ password }}"
    validate_certs: "{{ validate_certs }}"
    nvme_host_name: "{{ nvme_host_name }}"
    nvme_host_new_name: "{{ nvme_host_new_name }}"
    state: "present"

- name: Set maximum number of paths per volume and maximum Number of Ports Per Protection Domain of nvme_host
  dellemc.powerflex.nvme_host:
    hostname: "{{ hostname }}"
    username: "{{ username }}"
    password: "{{ password }}"
    validate_certs: "{{ validate_certs }}"
    nvme_host_name: "{{ nvme_host_name }}"
    max_num_paths: "{{ max_num_paths }}"
    max_num_sys_ports: "{{ max_num_sys_ports }}"
    state: "present"

- name: Remove nvme_host
  dellemc.powerflex.nvme_host:
    hostname: "{{ hostname }}"
    username: "{{ username }}"
    password: "{{ password }}"
    validate_certs: "{{ validate_certs }}"
    nvme_host_name: "{{ nvme_host_name }}"
    state: "absent"
a  
changed:
    description: Whether or not the resource has changed.
    returned: always
    type: bool
    sample: 'false'

nvme_host_details:
    description: Details of the NVMe host.
    returned: When NVMe host exists
    type: dict
    contains:
        hostOsFullType:
            description: Full type of the host OS.
            type: str
        hostType:
            description: Type of the host.
            type: str
        id:
            description: ID of the NVMe host.
            type: str
        installedSoftwareVersionInfo:
            description: Installed software version information.
            type: str
        kernelBuildNumber:
            description: Kernel build number.
            type: str
        kernelVersion:
            description: Kernel version.
            type: str
        links:
            description: Links related to the NVMe host.
            type: list
            contains:
                href:
                    description: Hyperlink reference.
                    type: str
                rel:
                    description: Relation type.
                    type: str
        max_num_paths:
            description: Maximum number of paths per volume. Used to create or modify the NVMe host.
            type: int
        max_num_sys_ports:
            description: Maximum number of ports per protection domain. Used to create or modify the NVMe host.
            type: int
        mdmConnectionState:
            description: MDM connection state.
            type: str
        mdmIpAddressesCurrent:
            description: Current MDM IP addresses.
            type: list
        name:
            description: Name of the NVMe host.
            type: str
        nqn:
            description: NQN of the NVMe host. Used to create, get or modify the NVMe host.
            type: str
        osType:
            description: OS type.
            type: str
        peerMdmId:
            description: Peer MDM ID.
            type: str
        perfProfile:
            description: Performance profile.
            type: str
        sdcAgentActive:
            description: Whether the SDC agent is active.
            type: bool
        sdcApproved:
            description: Whether an SDC has approved access to the system.
            type: bool
        sdcApprovedIps:
            description: SDC approved IPs.
            type: list
        sdcGuid:
            description: SDC GUID.
            type: str
        sdcIp:
            description: SDC IP address.
            type: str
        sdcIps:
            description: SDC IP addresses.
            type: list
        sdcType:
            description: SDC type.
            type: str
        sdrId:
            description: SDR ID.
            type: str
        sdtId:
            description: SDT ID.
            type: str
        softwareVersionInfo:
            description: Software version information.
            type: str
        systemId:
            description: ID of the system.
            type: str
        versionInfo:
            description: Version information.
            type: str
    sample: {
        "hostOsFullType": "Generic",
        "systemId": "264ec85b3855280f",
        "name": "name",
        "sdcApproved": null,
        "sdcAgentActive": null,
        "mdmIpAddressesCurrent": null,
        "sdcIp": null,
        "sdcIps": null,
        "osType": null,
        "perfProfile": null,
        "peerMdmId": null,
        "sdtId": null,
        "mdmConnectionState": null,
        "softwareVersionInfo": null,
        "socketAllocationFailure": null,
        "memoryAllocationFailure": null,
        "versionInfo": null,
        "sdcType": null,
        "nqn": "nqn.org.nvmexpress:uuid",
        "maxNumPaths": 6,
        "maxNumSysPorts": 10,
        "sdcGuid": null,
        "installedSoftwareVersionInfo": null,
        "kernelVersion": null,
        "kernelBuildNumber": null,
        "sdcApprovedIps": null,
        "hostType": "NVMeHost",
        "sdrId": null,
        "id": "1040d67200010000",
        "links": [
            {
                "rel": "self",
                "href": "/api/instances/Host::1040d67200010000"
            },
            {
                "rel": "/api/Host/relationship/Volume",
                "href": "/api/instances/Host::1040d67200010000/relationships/Volume"
            },
            {
                "rel": "/api/Host/relationship/NvmeController",
                "href": "/api/instances/Host::1040d67200010000/relationships/NvmeController"
            },
            {
                "rel": "/api/parent/relationship/systemId",
                "href": "/api/instances/System::264ec85b3855280f"
            }
        ]
    }
)AnsibleModule)PowerFlexBase)utilsnvmec                   N     e Zd ZdZ fdZd Zd
dZd Zd Zd Z	d Z
d	 Z xZS )PowerFlexNVMeHostzClass with NVMe host operationsc                 z    ddgg}t               d|d}t        | 	  t        |       t	        di i       | _        y)z-Define all parameters required by this modulenvme_host_namenqnT)argument_specsupports_check_moderequired_one_ofF)changednvme_host_detailsdiffN)"get_powerflex_nvme_host_parameterssuper__init__r   dictresult)selfr   ansible_module_params	__class__s      o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/powerflex/plugins/modules/nvme_host.pyr   zPowerFlexNVMeHost.__init__  sN    ,e45 @A#'.!

 	(=> 
    c                     g d}|D ]^  }||   	t        ||   j                               dk(  s)d| }t        j                  |       | j                  j                  |       ` y)zValidate the input parameters
        :param nvme_host_params: The dict of NVMe host parameters
        :type nvme_host_params: dict
        )r   r   max_num_pathsmax_num_sys_portsNr   zProvide valid msg)lenstripLOGerrormodule	fail_json)r   nvme_host_paramshost_identifiersparamr#   s        r   validate_parametersz%PowerFlexNVMeHost.validate_parameters$  sm    B% 	/E '3(/5578A=&ug.		#%%#%.	/r   c                 F   |||d}t        d |j                         D        d      }	 d}d}|r|\  }}| j                  j                  j	                         }|D ]  }	|	j	                  d      d|	d    |	d<   ! ||	|r|d   S dS |D 
cg c],  }
|
j	                  |      |k(  r|
j	                  d      d	k(  r|
. }}
|r|d   S dS c c}
w # t
        $ rI}d
t        |      z  }t        j                  |       | j                  j                  |       Y d}~yd}~ww xY w)zGet the NVMe host Details
        :param nvme_host_name: The name of the NVMe host
        :param nvme_host_di: The ID of the NVMe host
        :return: The dict containing NVMe host details
        )idr   namec              3   0   K   | ]  \  }}|s	||f  y w)N ).0keyvalues      r   	<genexpr>z2PowerFlexNVMeHost.get_nvme_host.<locals>.<genexpr>@  s     TeeeTs   
	Nr0   z	NVMeHost:r/   r   hostTypeNVMeHostz&Failed to get NVMe host with error: %sr"   )nextitemspowerflex_connsdcget	Exceptionstrr&   r'   r(   r)   )r   nvme_host_idr   r   id_name_mapid_namefilter_fieldfilter_valueall_host_details	nvme_hosthost_entityr   eerrormsgs                 r   get_nvme_hostzPowerFlexNVMeHost.get_nvme_host5  sa    "

 T{7H7H7JTVZ[	0LL-4*l#2266::<- F	==(0*3IdO3D(EIf%F #|';.>'*HDH 0@! +??<0L@[__U_E`dnEn ! !
 ,=$Q'F$F!  	0?#a&HHIIhKK!!h!//	0sB   AC /C C 	C 1C	>C C 	C 	D ?DD c           
         	 |d   s3d}t         j                  |       | j                  j                  |       | j                  j                  ra| j
                  j                  di |j                  d      |j                  d      |j                  d      |j                  d      dd	i       | j                  j                  sRd
|d    }t         j                  |       | j                  j                  j                  |d   |d   |d   |d          | j                  |d         S # t        $ rI}dt        |      z  }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)zCreate the NVMe host
        :param nvme_host_params: The dict of NVMe host parameters
        :type nvme_host_params: dict
        :return: The dict containing NVMe host details
        r   z&nqn is required for creating NVMe hostr"   r   r   r    r!   )r   r   r    r!   beforeafterzCreating NVMe host with nqn: )r   r0   r    r!   )r   z/Create NVMe host operation failed with error %sN)r&   r'   r(   r)   _diffr   updater=   
check_modeinfor;   hostcreaterJ   r>   r?   )r   r*   rI   r#   rH   s        r   create_nvme_hostz"PowerFlexNVMeHost.create_nvme_host^  ss   	0#E*C		(#%%(%3{{  ""Fr5E5I5I%5P@P@T@TUe@f?O?S?STc?dCSCWCWXkCl.n-o $p q
 ;;))67G7N6OP##((//(/)*:;"2?"C&67J&K	 0  %%*:5*A%BB 	0KcRSfTHIIhKK!!h!//	0s   D+D. .	F 7?E;;F c                    	 |d   }| j                   j                  r | j                  j                  d|i di       | j                   j                  s?t
        j                  d|        | j                  j                  j                  |       y| j                  |      S # t        $ rL}d d	t        |       }t
        j                  |       | j                   j                  |       Y d}~yd}~ww xY w)
zRemove the NVMe host
        :param nvme_host_id: The ID of the NVMe host
        :type nvme_host_id: str
        :return: The dict containing NVMe host details
        r/   r   rL   zDeleting NVMe host r"   Nr@   zFailed to remove NVMe host  with error )r(   rO   r   rP   rQ   r&   rR   r;   r<   deleterJ   r>   r?   r'   r)   )r   r   r@   rH   rI   s        r   delete_nvme_hostz"PowerFlexNVMeHost.delete_nvme_host  s    	0,T2L{{  ""F7HSU,V#WX;;))2<.AB##''..|<%%<%@@ 	04\N,sSTvhWHIIhKK!!h!//	0s   BB$ B$ $	C9-AC44C9c           
          dg i i f\  }}}} j                         }d fdd   xr d   d   k7  dd fdd   xr d   t        d         k7  dd	 fd
d   xr d   t        d	         k7  dd}|j                         D ][  \  }	}
|
d   s|
d      ||
d   <   |	   ||
d   <   	  j                  j                  s
 |
d           |j                  |
d          d}]  j                  j                  r"|r  j                  j                  d||di       | j                  d         fS # t        $ r#} j                  d|
d   |||       Y d}~d}~ww xY w)a  
        Modifies the NVMe host with the given details.

        Args:
            nvme_host_details (dict): The details of the NVMe host.
            nvme_host_params (dict): The parameters for modification.

        Returns:
            bool: True if the NVMe host was modified, False otherwise.

        Raises:
            Exception: If there was an error renaming the NVMe host or modifying its properties.
        Fr0   c                  ^    j                   j                  j                   d   d         S )Nr/   nvme_host_new_name)sdc_idr0   )r;   r<   renamer   r*   r   s   r   <lambda>z4PowerFlexNVMeHost.modify_nvme_host.<locals>.<lambda>  s<    t':':'>'>'E'EM^_cMdk{  }Q  lR'E  (S r   r]   )fieldmodify_func	conditionmaxNumPathsc                  ^    j                   j                  j                   d   d         S )Nr/   r    )host_idr    )r;   rS   modify_max_num_pathsr`   s   r   ra   z4PowerFlexNVMeHost.modify_nvme_host.<locals>.<lambda>  s?    t':':'?'?'T'T]nos]tcs  uD  dE (U (F r   r    maxNumSysPortsc                  ^    j                   j                  j                   d   d         S )Nr/   r!   )rg   r!   )r;   rS   modify_max_num_sys_portsr`   s   r   ra   z4PowerFlexNVMeHost.modify_nvme_host.<locals>.<lambda>  s?    t':':'?'?'X'Xarswaxk{  }P  lQ (Y (R r   r!   )r]   r    r!   rd   rb   rc   TmodifyNr   rL   r/   rW   )_get_api_version_and_checkr?   r:   r(   rQ   appendr>   handle_exceptionrO   r   rP   rJ   )r   r   r*   modifiedmodified_fieldsbefore_dict
after_dictversion_checkmodification_funcskvrH   s   ```         r   modify_nvme_hostz"PowerFlexNVMeHost.modify_nvme_host  s    >CBB=N:/;
779    S-.BC  LHXYmHn  sD  EK  sL  IL# ' F-o>  NCSTcCdhkl}  L  mM  iN  DN	 * R-.AB  YGWXkGlps  uF  GW  uX  qY  HY	"
( ',,. 
	vDAq~*;AgJ*GAgJ')9!)<
1W:&v;;11(-(*#**1W:6#H
	v ;;KK;(TUV++9J49P+QQQ ! v))(AgJ=Rcetuuvs   86D??	E+E&&E+c           
          d| d}|r|dk7  r|n
t        |      }|rddj                  |       nd}	d| d|d	    d
| d| d|	 
}
t        j                  |
       | j                  j                  |
       y)aG  
        Handles exceptions that occur during the modification of NVMe host fields.

        Args:
            operation (str): The operation being performed.
            field (str): The field being modified.
            ex (Exception): The exception that occurred.
            version_check (bool): Whether the version check is enabled.
            nvme_host_details (dict): The details of the NVMe host.
            modified_fields (list): The list of modified fields.

        Returns:
            None

        Raises:
            Exception: If the modification fails.

        zUpdating the NVMe host z8 is not supported in PowerFlex versions earlier than 4.6r0   z,Successfully modified the following fields: z,  z
Failed to z NVMe host r/    rX   z. r"   N)r?   joinr&   r'   r(   r)   )r   	operationrb   exrt   r   rq   version_support_errex_msgr#   rI   s              r   ro   z"PowerFlexNVMeHost.handle_exception  s    & !8w>vw(5%6/$sSUw]l<TYY=W<XYrt	{+6G6M5NaPUwVbcibjjlmplqr		((+r   c                     | j                   j                  j                         d   d   d   d   }t        j                  |      }|S )z
        Get the API version and check if it is less than version 4.6.

        Returns:
            bool: True if the API version is less than version 4.6, False otherwise.
        r   
mdmClustermasterversionInfo)r;   systemr=   r   is_version_less_than_4_6)r   api_versionrt   s      r   rm   z,PowerFlexNVMeHost._get_api_version_and_check  sI     ))00446q9,GQR_`66{Cr   )NNN)__name__
__module____qualname____doc__r   r-   rJ   rU   rZ   rx   ro   rm   __classcell__)r   s   @r   r   r     s2    )
"/"'0R 0D0,4Rl,4	r   r   c                      t        t        d      t        d      t        d      t        d      t        d      t        ddddg            S )zPThis method provide parameters for the Ansible NVMe host module on
    PowerFlexr?   )typepresentabsent)defaultr   choices)r   r   r]   r    r!   state)r   r2   r   r   r   r     sJ     e'U+&E*959h:OP r   c                       e Zd Zd Zy)NVMeHostExitHandlerc                     |r||j                   d<   ni |j                   d<    |j                  j                  di |j                    y )Nr   r2   )r   r(   	exit_json)r   nvme_host_objr   s      r   handlezNVMeHostExitHandler.handle   sF    8IM  !458:M  !45&&&>)=)=>r   Nr   r   r   r   r2   r   r   r   r     s    ?r   r   c                       e Zd Zd Zy)NVMeHostDeleteHandlerc                     |d   dk(  r"|r |j                  |      }d|j                  d<   t               j                  ||       y )Nr   r   Tr   )rZ   r   r   r   r   r   r*   r   s       r   r   zNVMeHostDeleteHandler.handle	  sH    G$05F - > >?P Q.2M  +$$]4EFr   Nr   r2   r   r   r   r     s    Gr   r   c                       e Zd ZddZy)NVMeHostModifyHandlerc                     |s6|d   dk(  r.|r,|j                  ||      \  }}|j                  dxx   |z  cc<   t               j                  |||       y )Nr   r   r   )rx   r   r   r   )r   r   r*   r   create_flagr   s         r   r   zNVMeHostModifyHandler.handle  s\    /8IEJ[)6)G)GHY[k)l&G&  +w6+&&}6FHYZr   N)Fr   r2   r   r   r   r     s    [r   r   c                       e Zd Zd Zy)NVMeHostCreateHandlerc                     d}|d   dk(  r$|s"|j                  |      }d|j                  d<   d}t               j                  ||||       y )NFr   r   Tr   )rU   r   r   r   )r   r   r*   r   r   s        r   r   zNVMeHostCreateHandler.handle  sX    G$	1:K - > >?O P.2M  +K&&}6FHY[fgr   Nr   r2   r   r   r   r     s    hr   r   c                       e Zd Zd Zy)NVMeHostHandlerc                     |j                  |       |j                  |d   |d         }t               j                  |||       y )N)r*   r   r   )r   r   )r-   rJ   r   r   r   s       r   r   zNVMeHostHandler.handle'  sS    ));K)L)77GWXhGi<LU<S 8 U&&}6FHYZr   Nr   r2   r   r   r   r   &  s    [r   r   c                  t    t               } t               j                  | | j                  j                         y)zZCreate PowerFlex NVMe host and perform actions on it
    based on user input from playbookN)r   r   r   r(   params)objs    r   mainr   .  s)     
CS#**"3"34r   __main__N)r   
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   `ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_baser   Gansible_collections.dellemc.powerflex.plugins.module_utils.storage.dellr   
get_loggerr&   r   r   r   r   r   r   r   r   r   r2   r   r   <module>r      s    M @ @4l(TX
t 5 ev_ _D
? ?G G[ [h h[ [5 zF r   