
    Vh                         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Z ej                  d	      Z G d
 de      Zd Zd Zd Zd Zd Zedk(  r e        yy)z5 Ansible module for managing MDM Cluster on PowerFlex    )absolute_importdivisionprint_functiona  
module: mdm_cluster
version_added: '1.3.0'
short_description: Manage MDM cluster on Dell PowerFlex
description:
- Managing MDM cluster and MDMs on PowerFlex storage system includes
  adding/removing standby MDM, modify MDM name and virtual interface.
- It also includes getting details of MDM cluster, modify MDM cluster
  ownership, cluster mode, and performance profile.
author:
- Bhavneet Sharma (@sharmb5) <ansible.team@dell.com>
extends_documentation_fragment:
  - dellemc.powerflex.powerflex
options:
  mdm_name:
    description:
    - The name of the MDM. It is unique across the PowerFlex array.
    - Mutually exclusive with I(mdm_id).
    - If mdm_name passed in add standby operation, then same name will be
      assigned to the new standby mdm.
    type: str
  mdm_id:
    description:
    - The ID of the MDM.
    - Mutually exclusive with I(mdm_name).
    type: str
  mdm_new_name:
    description:
    - To rename the MDM.
    type: str
  standby_mdm:
    description:
    - Specifies add standby MDM parameters.
    type: dict
    suboptions:
      mdm_ips:
        description:
        - List of MDM IPs that will be assigned to new MDM. It can contain
          IPv4 addresses.
        required: true
        type: list
        elements: str
      role:
        description:
        - Role of new MDM.
        required: true
        choices: ['Manager', 'TieBreaker']
        type: str
      management_ips:
        description:
        - List of management IPs to manage MDM. It can contain IPv4
          addresses.
        type: list
        elements: str
      port:
        description:
        - Specifies the port of new MDM.
        type: int
      allow_multiple_ips:
        description:
        - Allow the added node to have different number of IPs from the
          primary node.
        type: bool
      virtual_interfaces:
        description:
        - List of NIC interfaces that will be used for virtual IP addresses.
        type: list
        elements: str
  is_primary:
    description:
    - Set I(is_primary) as C(true) to change MDM cluster ownership from the current
      master MDM to different MDM.
    - Set I(is_primary) as C(false), will return MDM cluster details.
    - New owner MDM must be an MDM with a manager role.
    type: bool
  cluster_mode:
    description:
    - Mode of the cluster.
    choices: ['OneNode', 'ThreeNodes', 'FiveNodes']
    type: str
  mdm:
    description:
    - Specifies parameters to add/remove MDMs to/from the MDM cluster.
    type: list
    elements: dict
    suboptions:
      mdm_id:
        description:
        - ID of MDM that will be added/removed to/from the cluster.
        type: str
      mdm_name:
        description:
        - Name of MDM that will be added/removed to/from the cluster.
        type: str
      mdm_type:
        description:
        - Type of the MDM.
        - Either I(mdm_id) or I(mdm_name) must be passed with mdm_type.
        required: true
        choices: ['Secondary', 'TieBreaker']
        type: str
  mdm_state:
    description:
    - Mapping state of MDM.
    choices: ['present-in-cluster', 'absent-in-cluster']
    type: str
  virtual_ip_interfaces:
    description:
    - List of interfaces to be used for virtual IPs.
    - The order of interfaces must be matched with virtual IPs assigned to the
      cluster.
    - Interfaces of the primary and secondary type MDMs are allowed to modify.
    - The I(virtual_ip_interfaces) is mutually exclusive with I(clear_interfaces).
    type: list
    elements: str
  clear_interfaces:
    description:
    - Clear all virtual IP interfaces.
    - The I(clear_interfaces) is mutually exclusive with I(virtual_ip_interfaces).
    type: bool
  performance_profile:
    description:
    - Apply performance profile to cluster MDMs.
    choices: ['Compact', 'HighPerformance']
    type: str
  state:
    description:
    - State of the MDM cluster.
    choices: ['present', 'absent']
    required: true
    type: str
notes:
  - Parameters I(mdm_name) or I(mdm_id) are mandatory for rename and modify virtual IP
    interfaces.
  - Parameters I(mdm_name) or I(mdm_id) are not required while modifying performance
    profile.
  - For change MDM cluster ownership operation, only changed as true will be
    returned and for idempotency case MDM cluster details will be returned.
  - Reinstall all SDC after changing ownership to some newly added MDM.
  - To add manager standby MDM, MDM package must be installed with manager
    role.
  - The I(check_mode) is supported.
a  
- name: Add a standby MDM
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    mdm_name: "mdm_1"
    standby_mdm:
      mdm_ips:
        - "10.x.x.x"
      role: "TieBreaker"
      management_ips:
        - "10.x.y.z"
    state: "present"

- name: Remove a standby MDM
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    mdm_name: "mdm_1"
    state: "absent"

- name: Switch cluster mode from 3 node to 5 node MDM cluster
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    cluster_mode: "FiveNodes"
    mdm:
      - mdm_id: "5f091a8a013f1100"
        mdm_type: "Secondary"
      - mdm_name: "mdm_1"
        mdm_type: "TieBreaker"
    sdc_state: "present-in-cluster"
    state: "present"

- name: Switch cluster mode from 5 node to 3 node MDM cluster
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    cluster_mode: "ThreeNodes"
    mdm:
      - mdm_id: "5f091a8a013f1100"
        mdm_type: "Secondary"
      - mdm_name: "mdm_1"
        mdm_type: "TieBreaker"
    sdc_state: "absent-in-cluster"
    state: "present"

- name: Get the details of the MDM cluster
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    state: "present"

- name: Change ownership of MDM cluster
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    mdm_name: "mdm_2"
    is_primary: true
    state: "present"

- name: Modify performance profile
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    performance_profile: "HighPerformance"
    state: "present"

- name: Rename the MDM
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    mdm_name: "mdm_1"
    mdm_new_name: "new_mdm_1"
    state: "present"

- name: Modify virtual IP interface of the MDM
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    mdm_name: "mdm_1"
    virtual_ip_interface:
      - "ens224"
    state: "present"

- name: Clear virtual IP interface of the MDM
  dellemc.powerflex.mdm_cluster:
    hostname: "{{hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    port: "{{port}}"
    mdm_name: "mdm_1"
    clear_interfaces: true
    state: "present"
aA  
changed:
    description: Whether or not the resource has changed.
    returned: always
    type: bool
    sample: 'false'
mdm_cluster_details:
    description: Details of the MDM cluster.
    returned: When MDM cluster exists
    type: dict
    contains:
        id:
            description: The ID of the MDM cluster.
            type: str
        name:
            description: Name of MDM cluster.
            type: str
        clusterMode:
            description: Mode of the MDM cluster.
            type: str
        master:
            description: The details of the master MDM.
            type: dict
            contains:
                id:
                    description: ID of the MDM.
                    type: str
                name:
                    description: Name of the MDM.
                    type: str
                port:
                    description: Port of the MDM.
                    type: str
                ips:
                    description: List of IPs for master MDM.
                    type: list
                managementIPs:
                    description: List of management IPs for master MDM.
                    type: list
                role:
                    description: Role of MDM.
                    type: str
                status:
                    description: Status of MDM.
                    type: str
                versionInfo:
                    description: Version of MDM.
                    type: str
                virtualInterfaces:
                    description: List of virtual interfaces
                    type: list
                opensslVersion:
                    description: OpenSSL version.
                    type: str
        slaves:
            description: The list of the secondary MDMs.
            type: list
            elements: dict
            contains:
                id:
                    description: ID of the MDM.
                    type: str
                name:
                    description: Name of the MDM.
                    type: str
                port:
                    description: Port of the MDM.
                    type: str
                ips:
                    description: List of IPs for secondary MDM.
                    type: list
                managementIPs:
                    description: List of management IPs for secondary MDM.
                    type: list
                role:
                    description: Role of MDM.
                    type: str
                status:
                    description: Status of MDM.
                    type: str
                versionInfo:
                    description: Version of MDM.
                    type: str
                virtualInterfaces:
                    description: List of virtual interfaces
                    type: list
                opensslVersion:
                    description: OpenSSL version.
                    type: str
        tieBreakers:
            description: The list of the TieBreaker MDMs.
            type: list
            elements: dict
            contains:
                id:
                    description: ID of the MDM.
                    type: str
                name:
                    description: Name of the MDM.
                    type: str
                port:
                    description: Port of the MDM.
                    type: str
                ips:
                    description: List of IPs for tie-breaker MDM.
                    type: list
                managementIPs:
                    description: List of management IPs for tie-breaker MDM.
                    type: list
                role:
                    description: Role of MDM.
                    type: str
                status:
                    description: Status of MDM.
                    type: str
                versionInfo:
                    description: Version of MDM.
                    type: str
                opensslVersion:
                    description: OpenSSL version.
                    type: str
        standbyMDMs:
            description: The list of the standby MDMs.
            type: list
            elements: dict
            contains:
                id:
                    description: ID of the MDM.
                    type: str
                name:
                    description: Name of the MDM.
                    type: str
                port:
                    description: Port of the MDM.
                    type: str
                ips:
                    description: List of IPs for MDM.
                    type: list
                managementIPs:
                    description: List of management IPs for MDM.
                    type: list
                role:
                    description: Role of MDM.
                    type: str
                status:
                    description: Status of MDM.
                    type: str
                versionInfo:
                    description: Version of MDM.
                    type: str
                virtualInterfaces:
                    description: List of virtual interfaces.
                    type: list
                opensslVersion:
                    description: OpenSSL version.
                    type: str
        clusterState:
            description: State of the MDM cluster.
            type: str
        goodNodesNum:
            description: Number of Nodes in MDM cluster.
            type: int
        goodReplicasNum:
            description: Number of nodes for Replication.
            type: int
        virtualIps:
            description: List of virtual IPs.
            type: list
    sample: {
        "clusterState": "ClusteredNormal",
        "clusterMode": "ThreeNodes",
        "goodNodesNum": 3,
        "master": {
            "virtualInterfaces": [
                "ens1"
            ],
            "managementIPs": [
                "10.x.y.z"
            ],
            "ips": [
                "10.x.y.z"
            ],
            "versionInfo": "R3_6.0.0",
            "opensslVersion": "OpenSSL 1.0.2k-fips  26 Jan 2017",
            "role": "Manager",
            "status": "Normal",
            "name": "sample_mdm",
            "id": "5908d328581d1400",
            "port": 9011
        },
        "perfProfile": "HighPerformance",
        "slaves": [
            {
                "virtualInterfaces": [
                    "ens1"
                ],
                "managementIPs": [
                    "10.x.x.z"
                ],
                "ips": [
                    "10.x.x.z"
                ],
                "versionInfo": "R3_6.0.0",
                "opensslVersion": "OpenSSL 1.0.2k-fips  26 Jan 2017",
                "role": "Manager",
                "status": "Normal",
                "name": "sample_mdm1",
                "id": "5908d328581d1401",
                "port": 9011
            }
        ],
        "tieBreakers": [
            {
                "virtualInterfaces": [],
                "managementIPs": [],
                "ips": [
                    "10.x.y.y"
                ],
                "versionInfo": "R3_6.0.0",
                "opensslVersion": "N/A",
                "role": "TieBreaker",
                "status": "Normal",
                "id": "5908d328581d1402",
                "port": 9011
            }
        ],
        "standbyMDMs": [
            {
                "virtualInterfaces": [],
                "managementIPs": [
                    "10.x.z.z"
                ],
                "ips": [
                    "10.x.z.z"
                ],
                "versionInfo": "R3_6.0.0",
                "opensslVersion": "N/A",
                "role": "TieBreaker",
                "status": "Normal",
                "id": "5908d328581d1403",
                "port": 9011
            }
        ],
        "goodReplicasNum": 2,
        "id": "cdd883cf00000002"
    }
)AnsibleModule)utilsNmdm_clusterc                       e Zd ZdZd Z	 	 	 	 ddZ	 	 ddZ	 	 ddZd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Z	 	 d dZ	 	 ddZ	 	 ddZ	 	 ddZ	 	 d dZd Zd Zd Zd ZddZd Zd Zd Zy)!PowerFlexMdmClusterz!Class with MDM cluster operationsc                    t        j                         | _        | j                  j                  t	                      ddgddgg}g dg}t        | j                  d||      | _        t        j                  | j                         d| _        d	| _	        	 t        j                  | j                  j                        | _        t        j                  d
       t        j                  d| j                  j                         y# t         $ rM}t        j#                  t%        |             | j                  j'                  t%        |             Y d}~yd}~ww xY w)z. Define all parameters required by this modulemdm_namemdm_idvirtual_ip_interfacesclear_interfaces)cluster_modemdm	mdm_stateT)argument_specsupports_check_modemutually_exclusiverequired_togetherz'MDM {0} does not exists in MDM cluster.z%MDM already exists in the MDM clusterz3Got the PowerFlex system connection object instancezCheck Mode Flag %smsgN)r   %get_powerflex_gateway_host_parametersmodule_paramsupdate$get_powerflex_mdm_cluster_parametersr   moduleensure_required_libsnot_exist_msg	exist_msg%get_powerflex_gateway_host_connectionparamspowerflex_connLOGinfo
check_mode	Exceptionerrorstr	fail_json)selfmut_ex_argsrequired_together_argses       q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/powerflex/plugins/modules/mdm_cluster.py__init__zPowerFlexMdmCluster.__init__  s   "HHJ!!"F"HI"H-/1CDF #G!G $,, $*4	6 	""4;;/F@	."'"M"M""#$DHHJKHH)4;;+A+AB 	.IIc!fKK!!c!f!--	.s   A-D 	EAEENc                    |r|n|}|5|3d}t         j                  |       | j                  j                  |       | j	                  |||      }|7| j
                  j                  |      }| j                  j                  |       |d   }g }	t        |||      \  }	}
|	|
st         j                  d       ydt        |	      d	|
}t         j                  |       | j                  j                  s(| j                  j                  j                  ||	|

       y# t        $ rV}dj                  |t        |            }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)a  Modify the MDM virtual IP interface.
        :param mdm_id: ID of MDM
        :param mdm_name: Name of MDM
        :param virtual_ip_interfaces: List of virtual IP interfaces
        :param clear_interfaces: clear virtual IP interfaces of MDM.
        :param mdm_cluster_details: Details of MDM cluster
        :return: True if modification of virtual interface or clear operation
                 successful
        NzGPlease provide mdm_name/mdm_id to modify virtual IP interfaces the MDM.r   r   r   cluster_detailsidz0No change required in MDM virtual IP interfaces.Fz$Modifying MDM virtual interfaces to z or )r   r   r   TzDFailed to modify the virtual IP interfaces of MDM {0} with error {1})r$   r(   r   r*   is_mdm_name_id_existsr   formatis_modify_mdm_virtual_interfacer%   r)   debugr&   r#   systemmodify_virtual_ip_interfacer'   )r+   r   r   r   r   mdm_cluster_details
name_or_iderr_msgmdm_detailsmodify_listclearlog_msgr.   	error_msgs                 r/   set_mdm_virtual_interfacez-PowerFlexMdmCluster.set_mdm_virtual_interface9  s}     &V8
-GIIgKK!!g!.!!8F2E " G 	 ((//
;GKK!!g!.T"<!#3[BU uHHGH	1!$[!15:GIIg;;))##**FF!%* G ,  	1--3VJA-G IIi KK!!i!00		1s   <A$D! !	F *AE;;F c                    | j                   j                  d   dk(  rI|rG|d   |k7  r>	 | j                   j                  s&| j                  j                  j                  |       yy	y	# t        $ rV}dj                  |t        |            }t        j                  |       | j                   j                  |       Y d}~y	d}~ww xY w)
z Set the performance profile of Cluster MDMs
        :param performance_profile: Specifies the performance profile of MDMs
        :param cluster_details: Details of MDM cluster
        :return: True if updated successfully
        statepresentperfProfile)performance_profileTz;Failed to update performance profile to {0} with error {1}.r   NF)r   r"   r&   r#   r9   #set_cluster_mdm_performance_profiler'   r6   r)   r$   r(   r*   )r+   rH   r3   r.   rB   s        r/   set_performance_profilez+PowerFlexMdmCluster.set_performance_profilel  s     ;;g&)38K}-1DD
9;;11++22??Tg?h  ! 9!228&9L9<Q3A  IIi(KK))i)889s   <A' '	C0ACCc                    |r|n|}| |d}| j                   j                  |       | j                  |||      }|7| j                  j	                  |      }| j                   j                  |       |d   }	 d|v r||d   k7  sd|vr\d|d|d	}t
        j                  |       | j                   j                  s'| j                  j                  j                  ||
       yy# t        $ rV}	dj	                  |t        |	            }
t
        j                  |
       | j                   j                  |
       Y d}	~	yd}	~	ww xY w)a  Rename the MDM
        :param mdm_name: Name of the MDM.
        :param mdm_id: ID of the MDM.
        :param mdm_new_name: New name of the MDM.
        :param cluster_details: Details of the MDM cluster.
        :return: True if successfully renamed.
        Nz1Please provide mdm_name/mdm_id to rename the MDM.r   r2   r4   namezModifying the MDM name from z to .)r   mdm_new_nameTz,Failed to rename the MDM {0} with error {1}.)r   r*   r5   r   r6   r$   r%   r&   r#   r9   
rename_mdmr'   r)   r(   )r+   r   r   rN   r3   r<   r=   r>   rA   r.   rB   s              r/   rO   zPowerFlexMdmCluster.rename_mdm  sQ     &V8
IGKK!!g!.!!8F2A " C 	 ((//
;GKK!!g!.T"	1+%F 33+-#+\;!{{--''..99%L : B .  	1Fz3q6* IIi KK!!i!00		1s   <A+C) )	E2AEEc                 f    |D ],  }|d   s	|d   sd}| j                   j                  |       . y)z7 Check whether mdm dict have mdm_name and mdm_id or notr   r   z2parameters are mutually exclusive: mdm_name|mdm_idr   N)r   r*   )r+   r   noder   s       r/   &is_none_name_id_in_switch_cluster_modez:PowerFlexMdmCluster.is_none_name_id_in_switch_cluster_mode  s;      	/DH~$z"2J%%#%.	/    c                    | j                  |       ||d   k(  rt        j                  d       yg }g }g }g }| j                  j                  d   dk(  r2| j                  j                  d   dk(  r| j                  ||      \  }}nM| j                  j                  d   dk(  r1| j                  j                  d   d	k(  r| j                  ||      \  }}	 | j                  j                  s*| j                  j                  j                  |||||
       y# t        $ rU}dj                  t        |            }	t        j                  |	       | j                  j                  |	       Y d}~yd}~ww xY w)a  change the MDM cluster mode.
        :param cluster_mode: specifies the mode of MDM cluster
        :param mdm: A dict containing parameters to change MDM cluster mode
        :param cluster_details: Details of MDM cluster
        :return: True if mode changed successfully
        )r   clusterModez(MDM cluster is already in required mode.FrE   rF   r   present-in-clusterabsent-in-cluster)r   add_secondaryremove_secondaryadd_tb	remove_tbTz4Failed to change the MDM cluster mode with error {0}r   N)rR   r$   r%   r   r"   cluster_expand_listcluster_reduce_listr&   r#   r9   switch_cluster_moder'   r6   r)   r(   r*   )
r+   r   r   r3   rX   rZ   rY   r[   r.   r=   s
             r/   change_cluster_modez'PowerFlexMdmCluster.change_cluster_mode  sd    	333<?=99HH?@	;;g&)3"";/3GG$($<$<S/$R!M6[[(I5"";/3FF*.##C9 (i	/;;))##**>>!-]%5f' ? )  	/"F3q6N IIgKK!!g!..		/s   A D 	E4AE//E4c                    g }|D ]  }|d   r|d   n|d   }|d   dk(  ri|d   d| j                  |d   |      }|7| j                  j                  |      }| j                  j	                  |       |j                  |d          |d   dk(  s|d   | j                  |d   |      }|8| j                  j                  |      }| j                  j	                  |       |j                  |d           |S )	zO Prepare a list of secondary MDMs for switch cluster mode
            operationr   r   mdm_type	Secondaryr   r3   r   r   r3   r4   )r5   r   r6   r   r*   append)r+   r   r3   
secondarysrQ   r<   r>   r=   s           r/   gather_secondarys_idsz)PowerFlexMdmCluster.gather_secondarys_ids  s:    
 	9D-1*-=j)X  J;.4>3M"))h:I * K  &"0077
CGKK))g)6!!$x.1j![0T*5E5Q"))4
3C:I * K  &"0077
CGKK))g)6%%k$&78+	9, rS   c                 4   g }g }d|vr3d}t         j                  |       | j                  j                  |       | j	                  ||      }|D ]  }|d   r|d   n|d   }|d   dk(  r|d   |j                  |d          4|d   dk(  s=|d   C| j                  |d   |      }|8| j                  j                  |      }| j                  j                  |       |j                  |d	           d
|d|}	t         j                  |	       ||fS )z:Whether MDM cluster expansion is required or not.
        standbyMDMszFNo Standby MDMs found. To expand cluster size, first add standby MDMs.r   r   r   ra   
TieBreakerrd   r4   zexpand List are: , )
r$   r(   r   r*   rg   re   r5   r   r6   r8   )
r+   r   r3   rX   rZ   r=   rQ   r<   r>   rA   s
             r/   r\   z'PowerFlexMdmCluster.cluster_expand_list  sE    /0GIIgKK!!g!.223H 	5D-1*-=j)X  J</N.d8n-j!\1$0"))4
3C:I * K  &"0077
CGKK))g)6MM+d"34%	5( 0=fE		'f$$rS   c                 f   g }g }| j                  ||      }|D ]  }|d   r|d   n|d   }|d   dk(  ri|d   d| j                  |d   |      }|7| j                  j                  |      }| j                  j                  |       |j                  |d          |d   dk(  s|d   | j                  |d   |      }|8| j                  j                  |      }| j                  j                  |       |j                  |d           d	|d
|d}	t        j                  |	       ||fS )z:Whether MDM cluster reduction is required or not.
        r   r   ra   rj   rc   r   r4   rd   zReduce List are: rk   rM   )	rg   r5   r   r6   r   r*   re   r$   r8   )
r+   r   r3   rY   r[   rQ   r<   r>   r=   rA   s
             r/   r]   z'PowerFlexMdmCluster.cluster_reduce_list  sr    	55c?K 	8D-1*-=j)X  J</N."))h:I * K  &"0077
CGKK))g)6  T!23j!\1$0"))4
3C:I * K  &"0077
CGKK))g)6$$[%67/	82 1A)L		'**rS   c           
      p   	 | j                   j                  s>| j                  j                  j	                  |d   |d   |d   ||d   |d   |d          y# t
        $ rU}d	j                  t        |            }t        j                  |       | j                   j                  |
       Y d}~yd}~ww xY w)z Perform SDK call to add a standby MDM

        :param mdm_name: Name of new standby MDM
        :param standby_payload: Parameters dict to add a standby MDM
        :return: True if standby MDM added successfully
        mdm_ipsrolemanagement_ipsportallow_multiple_ipsvirtual_interfaces)rn   ro   rp   r   rq   rr   virtual_interfaceTz+Failed to Add a standby MDM with error {0}.r   N)r   r&   r#   r9   add_standby_mdmr'   r6   r)   r$   r(   r*   )r+   r   standby_payloadr.   r=   s        r/   perform_add_standbyz'PowerFlexMdmCluster.perform_add_standby@  s    	/;;))##**::+I6(0#23C#D%OF,C'67K'L&56J&K ; M  	/CJJAGIIgKK!!g!..		/s   AA 	B5 AB00B5c                     | j                   j                  d   s| j                   j                  d   r4d}t        j                  |       | j                   j	                  |       yy)z@ Check whether mdm_id or mdm_new_name present in Add standby MDMr   rN   zParameters mdm_id/mdm_new_name are not allowed while adding a standby MDM. Please try with valid parameters to add a standby MDM.r   N)r   r"   r$   r(   r*   )r+   r=   s     r/   is_id_new_name_in_add_mdmz-PowerFlexMdmCluster.is_id_new_name_in_add_mdmW  sW     ;;h'4;;+=+=n+M9G IIgKK!!g!. ,NrS   c                    | j                   j                  d   dk(  r|| j                  |d   |      r| j                          | j	                  ||      }|rt
        j                  d|       d|fS t        |      }| j                  ||      }|rC| j                         }dj                  t        |            }t
        j                  |       d|fS d|fS )	z Adding a standby MDMrE   rF   rn   rd   z"Standby MDM %s exits in the systemFz>Fetched the MDM cluster details {0} after adding a standby MDMT)r   r"   check_mdm_existsry   r5   r$   r%   prepare_standby_payloadrw   get_mdm_cluster_detailsr6   r)   )r+   r   standby_mdmr3   r>   rv   standby_addr   s           r/   ru   z#PowerFlexMdmCluster.add_standby_mdma  s     ;;g&)3'&&{9'='68**,%%x6E & G  =xHo--5kBO228_MK"&">">"@$$*F3+?$@ _,,o%%rS   c                     |r|n|}|5|3d}t         j                  |       | j                  j                  |       | j	                  |||      }|t         j                  d|       y|d   }	 | j                  j                  s&| j                  j                  j                  |       y	# t        $ rV}d
j                  |t        |            }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)a   Remove the Standby MDM
        :param mdm_id: ID of MDM that will become owner of MDM cluster
        :param mdm_name: Name of MDM that will become owner of MDM cluster
        :param cluster_details: Details of MDM cluster
        :return: True if MDM removed successful
        NzEEither mdm_name or mdm_id is required while removing the standby MDM.r   r2   z!MDM %s not exists in MDM cluster.Fr4   r   TzHFailed to remove the standby MDM {0} from the MDM cluster with error {1})r$   r(   r   r*   r5   r%   r&   r#   r9   remove_standby_mdmr'   r6   r)   )	r+   r   r   r3   r<   r=   r>   r.   rB   s	            r/   r   z&PowerFlexMdmCluster.remove_standby_mdm{  s     &V8
>h.*GIIgKK!!g!.!!8F2A " C 	 HH8*ET"	1;;))##**==V=L 	1117
CF1K IIi KK!!i!00		1s   1<B. .	D7ADDc                    |r|n|}|5|3d}t         j                  |       | j                  j                  |       | j	                  |||      }|7| j
                  j                  |      }| j                  j                  |       |d   }|d   |d   d   k(  rt         j                  d|       y	 | j                  j                  s&| j                  j                  j                  |	       y
# t        $ rV}dj                  |t        |            }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)a7   Change the ownership of MDM cluster.
        :param mdm_id: ID of MDM that will become owner of MDM cluster
        :param mdm_name: Name of MDM that will become owner of MDM cluster
        :param cluster_details: Details of MDM cluster
        :return: True if Owner of MDM cluster change successful
        NzNEither mdm_name or mdm_id is required while changing ownership of MDM cluster.r   r2   r4   masterz'MDM %s is already Owner of MDM cluster.Fr   TzCFailed to update the Owner of MDM cluster to MDM {0} with error {1})r$   r(   r   r*   r5   r   r6   r%   r&   r#   r9   change_mdm_ownershipr'   r)   )	r+   r   r   r3   r<   r=   r>   r.   rB   s	            r/   change_ownershipz$PowerFlexMdmCluster.change_ownership  sK     &V8
>h.3GIIgKK!!g!.!!8F2A " C 	 ((//
;GKK!!g!.T"t 9$ ??HH>
K
5{{--''..,,F,; 555;VJ<?F6D  		)$%%)%445s   6<C3 3	E<AEEc                 ~    d|v r9|d   D ]0  }d|v r||d   k(  s	||d   k(  st         j                  d|       |c S  yy)z8Whether MDM exists with mdm_name or id in secondary MDMsslavesrL   r4   zMDM %s found in Secondarys MDM.Nr$   r%   r+   r   r   r3   r<   r   s         r/   find_mdm_in_secondarysz*PowerFlexMdmCluster.find_mdm_in_secondarys  sW     &&x0 cMh#f+&=#d)+HH>
KJ	 'rS   c                 ~    d|v r9|d   D ]0  }d|v r||d   k(  s	||d   k(  st         j                  d|       |c S  yy)z:Whether MDM exists with mdm_name or id in tie-breaker MDMstieBreakersrL   r4   z MDM %s found in tieBreakers MDM.Nr   r   s         r/   find_mdm_in_tbz"PowerFlexMdmCluster.find_mdm_in_tb  sW     O+&}5 cMh#f+&=#d)+HH?LJ	 ,rS   c                 ~    d|v r9|d   D ]0  }d|v r||d   k(  s	||d   k(  st         j                  d|       |c S  yy)z6Whether MDM exists with mdm_name or id in standby MDMsri   rL   r4   zMDM %s found in standby MDM.Nr   r   s         r/   find_mdm_in_standbyz'PowerFlexMdmCluster.find_mdm_in_standby  sW     O+&}5 cMh#f+&=#d)+HH;ZHJ	 ,rS   c                 J   |r|n|}d|d   v r||d   d   k(  s||d   d   k(  rt         j                  d|       |d   S g }| j                  ||||      }||S g }| j                  ||||      }||S | j	                  ||||      }||S t         j                  d|       y)z'Whether MDM exists with mdm_name or id rL   r   r4   zMDM %s is master MDM.)r   r   r3   r<   Nz&MDM %s does not exists in MDM Cluster.)r$   r%   r   r   r   )r+   r   r   r3   r<   secondary_mdmtb_mdmr~   s           r/   r5   z)PowerFlexMdmCluster.is_mdm_name_id_exists  s     &V8
oh//HPX@YZ`@a4a_X6t<<HH,j9"8,, ""HV3B.8 # : 	 $   $$hv5D0: % < M ..6<?N:D / F "9:FrS   c                 (   	 | j                   j                  j                         }t        |      dk(  r3d}t        j                  |       | j                  j                  |       | j                         }||d   |d<   | j                   j                  j                         }||d   |d<   |S # t        $ rW}d}|j                  t        |            }t        j                  |       | j                  j                  |       Y d}~yd}~ww xY w)zTGet MDM cluster details
        :return: Details of MDM Cluster if existed.
        r   zMDM cluster not foundr   NrG   mdmAddressesz-Failed to get the MDM cluster with error {0}.)r#   r9   r}   lenr$   r(   r   r*   get_system_details!get_gateway_configuration_detailsr'   r6   r)   )r+   r;   r   respgateway_configuration_detailsr.   rB   s          r/   r}   z+PowerFlexMdmCluster.get_mdm_cluster_details  s   
	1"&"5"5"<"<'')   &'1,-		#%%#%. **,D59-5H#M2 -1,?,?,F,F113 *,86STb6c#N3&& 	1GI!((Q0IIIi KK!!i!00		1s   B.B1 1	D:ADDc                     d|v r<|d   D ]4  }|d   }|D ](  }||v st         j                  | j                           y 6 y)z-whether standby IPs present in secondary MDMsr   ipsFTr$   r%   r    )r+   
standby_ipr3   r   current_secondary_ipsr   s         r/   check_ip_in_secondarysz*PowerFlexMdmCluster.check_ip_in_secondarys-  s[     &!0!: %(5e(<%% %C330$%% rS   c                     d|v r<|d   D ]4  }|d   }|D ](  }||v st         j                  | j                           y 6 y)z/whether standby IPs present in tie-breaker MDMsr   r   FTr   )r+   r   r3   r   current_tb_ipsr   s         r/   check_ip_in_tbsz#PowerFlexMdmCluster.check_ip_in_tbs9  sX     O+)-8 %!'% %Cn,0$%% rS   c                     d|v r<|d   D ]4  }|d   }|D ](  }||v st         j                  | j                           y 6 y)z+whether standby IPs present in standby MDMsri   r   FTr   )r+   r   r3   stb_mdmcurrent_stb_ipsr   s         r/   check_ip_in_standbyz'PowerFlexMdmCluster.check_ip_in_standbyE  sX     O+*=9 %")%.% %Co-0$%% rS   c                    |d   d   }|D ]'  }||v st         j                  | j                          y | j                  ||      }|sy| j	                  ||      }|sy| j                  ||      }|syt         j                  d       y)z/Check whether standby MDM exists in MDM Clusterr   r   F)r   r3   z.New Standby MDM does not exists in MDM clusterT)r$   r%   r    r   r   r   )r+   r   r3   current_master_ipsr   in_secondaryin_tbs
in_standbys           r/   r{   z$PowerFlexMdmCluster.check_mdm_existsQ  s     -X6u= 	C(((	 22jCR 3 T %%6E & G -->M . O
ABrS   c                    	 | j                   j                  j                         }t        |      dk(  r| j                  j                  d       t        |      dkD  r| j                  j                  d       |d   S # t        $ rI}dt        |      z  }t        j                  |       | j                  j                  |       Y d}~yd}~ww xY w)zHGet system details
        :return: Details of PowerFlex system
        r   z"No system exist on the given host.r      z)Multiple systems exist on the given host.z%Failed to get system id with error %sN)
r#   r9   getr   r   r*   r'   r)   r$   r(   )r+   r   r.   r   s       r/   r   z&PowerFlexMdmCluster.get_system_detailsp  s    
	+&&--113D4yA~%% +2% 34y1}%% +8% 97N 	+9CFBCIIcNKK!!c!**	+s   A<A? ?	C?CCc                 n   g d}d}|D ]  }| j                   j                  |   t        | j                   j                  |   j                               xs+ | j                   j                  |   j	                  d      dkD  dk(  s~|j                  |      }| j                   j                  |        y)zValidate the input parameters)r   r   rN   zPlease provide the valid {0}N r   r   )r   r"   r   stripcountr6   r*   )r+   name_paramsr   n_itemr=   s        r/   validate_parametersz'PowerFlexMdmCluster.validate_parameters  s     =,! 	3F{{!!&)5++F399;< ?[[''/55c:Q>1E**V,%%'%2	3rS   c           	      z   | j                   j                  d   }| j                   j                  d   }| j                   j                  d   }t        j                  | j                   j                  d         }| j                   j                  d   }| j                   j                  d   }t        j                  | j                   j                  d         }| j                   j                  d   }| j                   j                  d	   }	| j                   j                  d
   }
| j                   j                  d   }| j                   j                  d   }d}t	        di       }| j                          | j                         }dj                  t        |            }t        j                  |       d}d}d}d}d}d}d}| j                  |||      \  }}| j                  ||      }|dk(  r|r| j                  ||||      }|dk(  r|	s|
r| j                  |||	|
|      }|dk(  r|r|r|r| j                  |||      }|dk(  r| j!                  |||      }|dk(  r|r| j#                  |||      }t%        |||||||      }|ri }n| j                         }||d<   ||d<    | j                   j&                  di | y)zm
        Perform different actions on MDM cluster based on parameters passed in
        the playbook
        r   r   rN   r~   
is_primaryr   r   r   r   r   rH   rE   F)changedr;   z#Fetched the MDM cluster details {0}rF   absentr;   r   N )r   r"   copydeepcopydictr   r}   r6   r)   r$   r%   ru   rJ   rO   rC   r_   r   r   update_change_flag	exit_json)r+   r   r   rN   r~   r   r   r   r   r   r   rH   rE   r   resultr;   r   standby_changedperformance_changedrenamed_changedinterface_changedremove_changedmode_changedowner_changeds                           r/   perform_module_operationz,PowerFlexMdmCluster.perform_module_operation  s   
 ;;%%j1##H-{{)).9mmDKK$6$6}$EF[[''5
{{)).9mmDKK..u56KK&&{3	 $ 2 23J K;;--.@A"kk001FG""7+  "
 	  ""::<3F3*+, 	#! 04OHk3FG 	-, ###$79LM 	 I,"ooh.ACO I#8<L $))&(*?*:*=?  I,3933L#4GIL H!44Xv5HJN I* 11&(2EGM %_6I%46G%1>%24 "$"&">">"@(;$%#y''rS   )NNNNN)NN)NNNN)NNN)__name__
__module____qualname____doc__r0   rC   rJ   rO   rR   r_   rg   r\   r]   rw   ry   ru   r   r   r   r   r   r5   r}   r   r   r   r{   r   r   r   r   rS   r/   r
   r
     s    +.< ?C8<376:11f ;?040 CG#'%1N/$/L<#%J"+H/./&41@ 6:)-'5R <@@D	 488<	 9==A	 ;?.2%N1B


>+&3Y(rS   r
   c                 "    | s|s
|s|s|s|s|ryy)zE Update the changed flag based on the operation performed in the taskTFr   )r   r   r   r   r   r   r   s          r/   r   r     s    
 -rS   c                 >    i }| D ]  }| |   r	| |   ||<   d||<    |S )z'prepare the payload for add standby MDMNr   )r~   payload_dictmdm_keyss      r/   r|   r|     s?    L *x %0%:L"%)L"	*
 rS   c                    g }d}|d   }|Dt        |      t        |       k(  r-t        |      t        |       k(  rt        j                  d       y|r't        |d         dk(  rt        j                  d       y|r.t        |d         dk7  r| t        j                  d       d	}d|fS | r|| D ]  }|j	                  |        ||fS yy)
z;Check if modification in MDM virtual IP interface required.FvirtualInterfacesNz-No changes required for virtual IP interface.)NFr   z'No change required for clear interface.z Clear all interfaces of the MDM.T)r   setr$   r%   re   )r   r   r>   r?   r@   existing_interfaces	interfaces          r/   r7   r7     s     KE%&9: #$,A(BB#$,A(BB@A C,? @AQF:; 
c+.A"BCqH!)34U{!1!9. 	*Iy)	*E!! ":rS   c                  &   t        t               t               t               t        dd      t        d      t        d      t        dt        t        ddd      t        dd	d
g      t        dd      t        d      t        d      t        dd                  t        g d      t        ddt        t               t               t        ddd
g                  t        ddg      t        ddg      t        ddddg            S )zRThis method provide parameter required for the MDM cluster
    module on PowerFlexlistr)   )typeelementsbool)r   r   T)r   r   requiredManagerrj   )r   choicesint)rn   ro   rp   rq   rr   rs   )r   options)OneNode
ThreeNodes	FiveNodes)r   rb   )r   r   ra   )r   r   r   rV   rW   CompactHighPerformancerF   r   )r   r   r   )r   r   rN   r   r   r   r~   r   r   r   rH   rE   )r   r   rS   r/   r   r   $  s     TV"?6*t7HfdfutDti-FGVe<5!d6G#%@/B C "HIfvTVdf'+T5@,4O(QRS  46IJK )5F)GHDuy(6KL# rS   c                  8    t               } | j                          y)zA Perform actions on MDM cluster based on user input from playbookN)r
   r   )objs    r/   mainr   <  s    

C  "rS   __main__)r   
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Gansible_collections.dellemc.powerflex.plugins.module_utils.storage.dellr   r   
get_loggerr$   objectr
   r   r|   r7   r   r   r   r   rS   r/   <module>r      s    < B BN`zxv
p 5 e}%Q(& Q(h	"B0# zF rS   