
    Vh*                        d dl mZmZmZ e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 d dlmZ d dlmZ dZ	 d dlmZmZmZ d dlmZmZ d d	lmZ d d
lmZ d dlZdZ  ejF                  d      Z$ G d de%      Z& G d de'      Z( G d de'      Z) G d de'      Z* G d de'      Z+d Z,ddZ-y# e!$ r  ejD                         ZdZ Y kw xY w)    )absolute_importdivisionprint_functionN)AnsibleModulemissing_required_lib)configparser)
expanduser)UUID)API_ENDPOINTSDimensionDataAPIExceptionDimensionDataStatus)NodeNodeLocation)
get_driver)ProviderTFz.*MCP\s?2.*c                   ^    e Zd ZdZd Zd Zd Zd Zd Ze	d        Z
e	d        Ze	d	        Zy
)DimensionDataModulezd
    The base class containing common functionality used by Dimension Data modules for Ansible.
    c                    || _         t        s*| j                   j                  t        d      t               | j                         }|d   | _        |d   | _        | j                   j                  d   }dj                  |      | _
        | j                   j                  d   | _        | j                   j                  d   t        j                  _         t        t         j"                        | j                  | j                  | j                  	      | _        | j'                  | j                        | _        d
| j                   j                  v r[| j                   j                  d
   | _        | j                   j                  d   | _        | j                   j                  d   | _        yd| _        d| _        d| _        y)z
        Create a new DimensionDataModule.

        Will fail if Apache libcloud is not present.

        :param module: The underlying Ansible module.
        :type module: AnsibleModule
        libcloud)msg	exceptionuser_idkeyregionzdd-{0}locationvalidate_certs)r   wait	wait_timewait_poll_intervalFr   N)moduleHAS_LIBCLOUD	fail_jsonr   LIBCLOUD_IMP_ERRget_credentialsr   r   paramsformatr   r   r   securityVERIFY_SSL_CERTr   r   DIMENSIONDATAdriverget_mcp_versionmcp_versionr   r   r   )selfr    credentialsr   s       x/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/module_utils/dimensiondata.py__init__zDimensionDataModule.__init__8   sc    KK!!&::&FRb!c **,"9-u% ##H-oof-**:6,0KK,>,>?O,P)8j!7!78LLHH;;
  //> T[['''**62DI![[//<DN&*kk&8&89M&ND#DIDN&'D#    c                 *   t         s| j                  j                  d       d}d}d| j                  j                  v rfd| j                  j                  vr| j                  j                  d       | j                  j                  d   }| j                  j                  d   }|r|s@t        j
                  j                  dd      }t        j
                  j                  dd      }|r|sXt        d	      }t        j                         }|j                  d
|z         	 |j                  dd      }|j                  dd      }|st        d      |st        d      t        ||      S # t        j                  t        j                  f$ r Y Lw xY w)a  
        Get user_id and key from module configuration, environment, or dotfile.
        Order of priority is module, environment, dotfile.

        To set in environment:

            export MCP_USER='myusername'
            export MCP_PASSWORD='mypassword'

        To set in dot file place a file at ~/.dimensiondata with
        the following contents:

            [dimensiondatacloud]
            MCP_USER: myusername
            MCP_PASSWORD: mypassword
        z%libcloud is required for this module.)r   Nmcp_usermcp_passwordzg"mcp_user" parameter was specified, but not "mcp_password" (either both must be specified, or neither).MCP_USERMCP_PASSWORD~z%s/.dimensiondatadimensiondatacloudz Dimension Data user id not foundzDimension Data key not found)r   r   )r!   r    r"   r%   osenvirongetr	   r   RawConfigParserreadNoSectionErrorNoOptionErrorMissingCredentialsErrordict)r-   r   r   homeconfigs        r/   r$   z#DimensionDataModule.get_credentialsf   sn   $ KK!!&M!N +++T[[%7%77%% B &  kk((4G++$$^4C cjjnnZ6G**..6C cc?D!113FKK+d23 **%9:Fjj!5~F )*LMM)*HII G-- !//1K1KL s   !$E, ,#FFc                 z    | j                   j                  |      }t        j                  |j                        ryy)zA
        Get the MCP version for the specified location.
        z2.0z1.0)r*   ex_get_location_by_idMCP_2_LOCATION_NAME_PATTERNmatchname)r-   r   s     r/   r+   z#DimensionDataModule.get_mcp_version   s0    
 ;;44X>&,,X]];r1   c                    t        |      r| j                  j                  |      }nB| j                  j                  |      D cg c]  }|j                  |k(  r| }}|r|d   }nd}|r|S t        d|z        c c}w )z>
        Retrieve a network domain by its name or Id.
        )r   r   NzNetwork '%s' could not be found)is_uuidr*   ex_get_network_domainex_list_network_domainsrH   UnknownNetworkError)r-   locatorr   network_domainmatching_network_domainss        r/   get_network_domainz&DimensionDataModule.get_network_domain   s    
 7![[>>wGN 6:[[5X5Xbj5X5k(#1!&&'1 ($ (
 (!9!!<!%!!!"Cg"MNN(   A<c                    t        |      r| j                  j                  |      }nB| j                  j                  ||      D cg c]  }|j                  |k(  r| }}|r|d   }nd}|r|S t        d|z        c c}w )z5
        Get a VLAN object by its name or id
        r   NzVLAN '%s' could not be found)rJ   r*   ex_get_vlanex_list_vlansrH   UnknownVLANError)r-   rN   r   rO   vlanmatching_vlanss         r/   get_vlanzDimensionDataModule.get_vlan   s     7;;**73D "&!:!:8^!T99' N 
 %a(K=GHHrR   c                      t        t        dd      t        dd      t        ddd      t        dd      t        ddd	      
      }| r|j                  |        |S )a  
        Build an argument specification for a Dimension Data module.
        :param additional_argument_spec: An optional dictionary representing the specification for additional module arguments (if any).
        :return: A dict containing the argument specification.
        strna)typedefaultF)r]   requiredT)r]   r_   no_logboolr]   r_   r^   )r   r3   r4   r   r   )rA   updateadditional_argument_specspecs     r/   argument_specz!DimensionDataModule.argument_spec   s[     UD1uu555Fut4VeTJ
 $KK01r1   c            	          t         j                  t        ddd      t        ddd      t        ddd            }| r|j                  |        |S )aD  
        Build an argument specification for a Dimension Data module that includes "wait for completion" arguments.
        :param additional_argument_spec: An optional dictionary representing the specification for additional module arguments (if any).
        :return: A dict containing the argument specification.
        ra   Frb   intiX     )r   r   r   )r   rg   rA   rc   rd   s     r/   argument_spec_with_waitz+DimensionDataModule.argument_spec_with_wait   sT     #006E5AsC#J 1 
 $KK01r1   c                  6    ddgg}| r|j                  |        |S )al  
        Get the basic argument specification for Dimension Data modules indicating which arguments are must be specified together.
        :param additional_required_together: An optional list representing the specification for additional module arguments that must be specified together.
        :return: An array containing the argument specifications.
        r3   r4   )extend)additional_required_togetherrequired_togethers     r/   ro   z%DimensionDataModule.required_together  s.     (
 ($$%AB  r1   N)__name__
__module____qualname____doc__r0   r$   r+   rQ   rY   staticmethodrg   rk   ro    r1   r/   r   r   3   s`    ,(\;.z	O.I,  (  $ ! !r1   r   c                       e Zd ZdZy)LibcloudNotFoundz@
    Exception raised when Apache libcloud cannot be found.
    Nrp   rq   rr   rs   ru   r1   r/   rw   rw          	r1   rw   c                       e Zd ZdZy)r@   z\
    Exception raised when credentials for Dimension Data CloudControl cannot be found.
    Nrx   ru   r1   r/   r@   r@     ry   r1   r@   c                       e Zd ZdZy)rM   zL
    Exception raised when a network or network domain cannot be found.
    Nrx   ru   r1   r/   rM   rM   %  ry   r1   rM   c                       e Zd ZdZy)rV   z7
    Exception raised when a VLAN cannot be found.
    Nrx   ru   r1   r/   rV   rV   -  ry   r1   rV   c                      t        j                         } | D cg c]  }|j                  d      s|dd  }}|S c c}w )zK
    Get the list of available regions whose vendor is Dimension Data.
    zdd-   N)r   keys
startswith)all_regionsr   regionss      r/   get_dd_regionsr   5  sE      $$&K )4Pfv7H7H7OvabzPGPN Qs   ;;c                 X    	 t        | |      }t        |      | k(  S # t        $ r Y yw xY w)z
    Test if valid v4 UUID
    )versionF)r
   r[   
ValueError)ur   uuid_objs      r/   rJ   rJ   C  s4    7+8}!! s    	)))   ).
__future__r   r   r   r]   __metaclass__r9   re	tracebackansible.module_utils.basicr   r   ansible.module_utils.six.movesr   os.pathr	   uuidr
   r#   libcloud.common.dimensiondatar   r   r   libcloud.compute.baser   r   libcloud.compute.providersr   libcloud.compute.typesr   libcloud.securityr   r!   ImportError
format_exccompilerF   objectr   	Exceptionrw   r@   rM   rV   r   rJ   ru   r1   r/   <module>r      s    C B 	 	  K 7   kk85/L )bjj8 _!& _!D	y 		i 		) 		y 		{  +y++-Ls   $B+ +CC