Anons79 Mini Shell

Directory : /lib/python2.7/site-packages/ansible/module_utils/
Upload File :
Current File : //lib/python2.7/site-packages/ansible/module_utils/gcp.pyc

�
�Udac@s6ddlZddlZddlZddlZddlmZyddlZeZWne	k
roe
ZnXy&ddlZddl
mZeZWne	k
r�e
ZnXyVddlZddlmZddlmZddlmZddlmZeZWne	k
re
ZnXddljjjjjZ dgZ!d	�Z"d
�Z#d�Z$ee
d�Z%ee
d
�Z&d�Z'e(d�Z)e(ddd�Z*e(dd�Z+d�Z,d�Z-dd�Z.d�Z/de0fd��YZ1de2fd��YZ3de3fd��YZ4d e3fd!��YZ5dS("i����N(tLooseVersion(tservice_account(tHttp(tset_user_agent(t	HttpError(tbuilds.https://www.googleapis.com/auth/cloud-platformcCsa|jjdd�}|jjdd�p<|jjdd�}|jjdd�}|||fS(s0Helper to fetch creds from AnsibleModule object.tservice_account_emailtpem_filetcredentials_filet
project_idN(tparamstgettNone(tmoduleRRR	((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyt_get_gcp_ansible_credentialsDs
cCstjj||�S(s#Wrapper around os.environ.get call.(tostenvironR(tvar_namet
default_value((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyt_get_gcp_environ_varOs	cCs�|stdd�}n|sNtdd�pHtdd�pHtdd�}n|sutdd�potdd�}n|||fS(s8Helper to look in environment variables for credentials.t	GCE_EMAILtGCE_CREDENTIALS_FILE_PATHtGOOGLE_APPLICATION_CREDENTIALStGCE_PEM_FILE_PATHtGCE_PROJECTtGOOGLE_CLOUD_PROJECTN(RR(RRR	((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyt _get_gcp_environment_credentialsUsc	Cst|�\}}}t|||�\}}}|d
ksT|d
ksT|d
kr�|tkr�|d
kr�|jdd�q�n)|d
kr�|jdd||f�n|d
kr�d}n|d
kr�d}q�n|r�t||d|d|�ni|d6|d6|d	6S(s�
    Obtain GCP credentials by trying various methods.

    There are 3 ways to specify GCP credentials:
    1. Specify via Ansible module parameters (recommended).
    2. Specify via environment variables.  Two sets of env vars are available:
       a) GOOGLE_CLOUD_PROJECT, GOOGLE_CREDENTIALS_APPLICATION (preferred)
       b) GCE_PROJECT, GCE_CREDENTIAL_FILE_PATH, GCE_EMAIL (legacy, not recommended; req'd if
          using p12 key)
    3. Specify via libcloud secrets.py file (deprecated).

    There are 3 helper functions to assist in the above.

    Regardless of method, the user also has the option of specifying a JSON
    file or a p12 file as the credentials file.  JSON is strongly recommended and
    p12 will be removed in the future.

    Additionally, flags may be set to require valid json and check the libcloud
    version.

    AnsibleModule.fail_json is called only if the project_id cannot be found.

    :param module: initialized Ansible module object
    :type module: `class AnsibleModule`

    :param require_valid_json: If true, require credentials to be valid JSON.  Default is True.
    :type require_valid_json: ``bool``

    :params check_libcloud: If true, check the libcloud version available to see if
                            JSON creds are supported.
    :type check_libcloud: ``bool``

    :return:  {'service_account_email': service_account_email,
               'credentials_file': credentials_file,
                'project_id': project_id}
    :rtype: ``dict``
    tmsgs;Missing GCE connection parameters in libcloud secrets file.sOGCP connection error: unable to determine project (%s) or credentials file (%s)ttrequire_valid_jsontcheck_libcloudRRR	N(RRRtTruet	fail_jsont_validate_credentials_file(R
RRRRR	((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyt_get_gcp_credentialsfs*($	

cCs�y^t|��L}tj|j��|rSttj�dkrS|jdd�ntSWdQXWnVt	k
r�}|jdd|dt
�t
Stk
r�}|jdddt
�nXdS(s�
    Check for valid credentials file.

    Optionally check for JSON format and if libcloud supports JSON.

    :param module: initialized Ansible module object
    :type module: `class AnsibleModule`

    :param credentials_file: path to file on disk
    :type credentials_file: ``str``.  Complete path to file on disk.

    :param require_valid_json: This argument is ignored as of Ansible 2.7.
    :type require_valid_json: ``bool``

    :params check_libcloud: If true, check the libcloud version available to see if
                            JSON creds are supported.
    :type check_libcloud: ``bool``

    :returns: True
    :rtype: ``bool``
    s0.17.0RsYUsing JSON credentials but libcloud minimum version not met. Upgrade to libcloud>=0.17.0.Ns"GCP Credentials File %s not found.tchangeds`Non-JSON credentials file provided. Please generate a new JSON key from the Google Cloud console(topentjsontloadstreadRtlibcloudt__version__R RtIOErrortFalset
ValueError(R
RRRtcredentialste((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR!�s	cCs�ts|jdd�nt|dtdt�}yZ||�|d|dd|jjdd
�d	|d
�}|jj	d||f�Wnct
tfk
r�}|jdt|�dt�n/t
k
r�}|jdt|�dt�nX|S(s+Return a Google libcloud driver connection.Rs-libcloud must be installed to use this moduleRRRRt
datacentertzonetprojectR	s%s/%sR#N(tHAS_LIBCLOUD_BASER R"R+RR
RRt
connectiontuser_agent_appendtRuntimeErrorR,tstrt	Exceptiontunexpected_error_msg(R
tprovidert
get_drivertuser_agent_producttuser_agent_versiontcredstgcpR.((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytgcp_connect�s 		
 cCs�|d	krgn|}ts1|jdd�nt|dtdt�}y}|dr�tjj|d�}|r�|j	|�}q�n4t
jjd|�\}}|d	k	r�||d<n||fSWn0t
k
r�}|jdt|�dt�d
SXd	S(s�
    Get credentials object for use with Google Cloud client.

    Attempts to obtain credentials by calling _get_gcp_credentials. If those are
    not present will attempt to connect via Application Default Credentials.

    To connect via libcloud, don't use this function, use gcp_connect instead.  For
    Google Python API Client, see get_google_api_auth for how to connect.

    For more information on Google's client library options for Python, see:
    U(https://cloud.google.com/apis/docs/client-libraries-explained#google_api_client_libraries)

    Google Cloud example:
      creds, params = get_google_cloud_credentials(module, scopes, user_agent_product, user_agent_version)
      pubsub_client = pubsub.Client(project=params['project_id'], credentials=creds)
      pubsub_client.user_agent = 'ansible-pubsub-0.1'
      ...

    :param module: initialized Ansible module object
    :type module: `class AnsibleModule`

    :param scopes: list of scopes
    :type module: ``list`` of URIs

    :returns: A tuple containing (google authorized) credentials object and
              params dict {'service_account_email': '...', 'credentials_file': '...', 'project_id': ...}
    :rtype: ``tuple``
    RsPlease install google-auth.RRRtscopesR	R#N(NN(RtHAS_GOOGLE_AUTHR R"RR+RtCredentialstfrom_service_account_filetwith_scopestgoogletauthtdefaultR7R8(R
R@tconn_paramsR-R	R.((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytget_google_cloud_credentials�s(		
	

sansible-python-apitNAc	Cs�|dkrgn|}ts1|jdd�n|s@t}nyTt||�\}}tt�d||f�}tj|d|�}||fSWn0t	k
r�}|jdt
|�dt�dSXdS(s�
    Authentication for use with google-python-api-client.

    Function calls get_google_cloud_credentials, which attempts to assemble the credentials
    from various locations.  Next it attempts to authenticate with Google.

    This function returns an httplib2 (compatible) object that can be provided to the Google Python API client.

    For libcloud, don't use this function, use gcp_connect instead.  For Google Cloud, See
    get_google_cloud_credentials for how to connect.

    For more information on Google's client library options for Python, see:
    U(https://cloud.google.com/apis/docs/client-libraries-explained#google_api_client_libraries)

    Google API example:
      http_auth, conn_params = get_google_api_auth(module, scopes, user_agent_product, user_agent_version)
      service = build('myservice', 'v1', http=http_auth)
      ...

    :param module: initialized Ansible module object
    :type module: `class AnsibleModule`

    :param scopes: list of scopes
    :type scopes: ``list`` of URIs

    :param user_agent_product: User agent product.  eg: 'ansible-python-api'
    :type user_agent_product: ``str``

    :param user_agent_version: Version string to append to product.  eg: 'NA' or '0.1'
    :type user_agent_version: ``str``

    :returns: A tuple containing (google authorized) httplib2 request object and a
              params dict {'service_account_email': '...', 'credentials_file': '...', 'project_id': ...}
    :rtype: ``tuple``
    Rs/Please install google-api-python-client librarys%s-%sthttpR#N(NN(RtHAS_GOOGLE_API_LIBR tGCP_DEFAULT_SCOPESRIRRtgoogle_auth_httplib2tAuthorizedHttpR7R8R+(	R
R@R;R<R-RHRKt	http_authR.((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytget_google_api_auth-s$	tv1c	CsR|st}nt|d|d|d|�\}}t||d|�}||fS(s�
    Get the discovery-based python client. Use when a cloud client is not available.

    client = get_google_api_client(module, 'compute', user_agent_product=USER_AGENT_PRODUCT,
                                   user_agent_version=USER_AGENT_VERSION)

    :returns: A tuple containing the authorized client to the specified service and a
              params dict {'service_account_email': '...', 'credentials_file': '...', 'project_id': ...}
    :rtype: ``tuple``
    R@R;R<RK(RMRQR(	R
tserviceR;R<R@tapi_versionRPRHtclient((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytget_google_api_clientcs	cCsNddlm}y#||�j}t|�|kSWntk
rI}tSXdS(s1Minimum required version is >= installed version.i����(tget_distributionN(t
pkg_resourcesRWtversionRR7R+(tpkg_nametminimum_versionRWtinstalled_versionR.((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytcheck_min_pkg_versionzscCsdt|�tj�fS(s0Create an error string based on passed in error.s%Unexpected response: (%s). Detail: %s(R6t	tracebackt
format_exc(terror((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR8�sR0cCsl|dkr|j|�}n|j|�}|dkrhd}|jdd||||fdt�n|S(NR0sKhttps://cloud.google.com/compute/docs/regions-zones/regions-zones#availableRs;%s %s is invalid. Please see the list of available %s at %sR#(tex_get_zonet
ex_get_regionRR R+(R
tdrivertlocationt
location_typetltlink((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytget_valid_location�s
c
Cs�x�|D]�}|d|krcd|kr�|dtkr�td|dt|d�f��q�qt||d|d�s�td|dt|d�||dt||d�f��nd|kr||d|dkrtd|ddj|d�f��qnt||dt�rd	|krq||d|d	krqtd
|d|d	f��qqnd|kr�||d|dkr�td|d|df��q�q�qqWtS(
s�
    Helper to validate params.

    Use this in function definitions if they require specific fields
    to be present.

    :param params: structure that contains the fields
    :type params: ``dict``

    :param field_list: list of dict representing the fields
                       [{'name': str, 'required': True/False', 'type': cls}]
    :type field_list: ``list`` of ``dict``

    :return True or raises ValueError
    :rtype: ``bool`` or `class:ValueError`
    tnametrequireds&%s is required and must be of type: %sttypes)%s must be of type: %s. %s (%s) provided.tvaluess%s must be one of: %st,tmins'%s must be greater than or equal to: %stmaxs$%s must be less than or equal to: %s(RR,R6t
isinstanceRktjointint(R
t
field_listtd((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytcheck_params�s.
$'%tGCPUtilscBs�eZdZed��Zed��Zedd��Zededded��Z	eddd��Z
ed��Zed	��Zed
��Z
ed��Zedd
d��Zedd��Zed��ZRS(s#
    Helper utilities for GCP.
    cCs5|jd�ddjd�|jd�dD��S(Nt_iRcss!|]}|j�pdVqdS(RwN(t
capitalize(t.0tx((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pys	<genexpr>�si(tsplitRq(ttxt((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytunderscore_to_camel�scCs4dg}x$|D]}||kr||=qqW|S(s)
        Remove params if found.
        tstate((R
tparams_to_removetp((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytremove_non_gcp_params�s
	
cCs�t|t�s|Si}tj|�}x�|j�D]�\}}tj|�}t|t�r{tj|�}|||<q5t|t�r�g|D]}tj|�^q�||<q5|r�||kr�||d<q5|||<q5W|S(s
        Recursively convert ansible params to GCP Params.

        Keys are converted from snake to camelCase
        ex: default_service to defaultService

        Handles lists, dicts and strings

        special provision for the resource name
        Ri(RptdictRvR�titemsR}tparams_to_gcp_dicttlist(R
t
resource_nametgcp_dicttktvtgcp_keytretvalRz((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR��s
)
i�icCs�yj|j�}|sdS|r#|S|ddkrQtj|||||�}nd|kre|dS|SWnFtk
r�}|jjdkr�|r�dS�ntk
r��nXdS(s�
        General python api client interaction function.

        For use with google-api-python-client, or clients created
        with get_google_api_client function
        Not for use with Google Cloud client libraries

        For long-running operations, we make an immediate query and then
        sleep poll_interval before re-querying.  After the request is done
        we rebuild the request with a get method and return the result.

        tkindscompute#operationR�i�N(texecuteRRvt execute_api_client_operation_reqRtresptstatusR7(treqRUtrawtoperation_timeoutt
poll_intervalt	raise_404R�th((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytexecute_api_client_req�s(
cCs�tj|j�}|d}tj|j�}tj||�}tj�}	t}
d}x[|
s�|	|tj�kr�|j�j	d|d|d�}|j
�}|ddkr�tj|�|d7}q�t}
|ddkr�tS|ddkrytj
|�}
|dd	ks$d|krBtj|d
�d|d<ni|d6|d|
6}|j	|�}|j
�}|S|jd|�}|j
�}|Sq[td|d
��q[WdS(s1
        Poll an operation for a result.
        R1it	operationRiR�tDONEt
operationTypetdeletetinserttupdatetpatchtentity_namet
targetLinksOperation timed out: %sN(sinsertsupdateR�(Rvt
parse_gcp_urlturitget_gcp_resource_from_methodIdtmethodIdtbuild_resource_from_namettimeR+tglobalOperationsRR�tsleepRt"get_entity_name_from_resource_nameR�tGCPOperationTimeoutError(torig_reqtop_respRUR�R�t
parsed_urlR	R�tresourcet
start_timetcompletetattemptstop_reqtresource_name_singulartargstnew_reqR�((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�#sF
	

	
cCsHyt||�}|�SWn'tk
rCtd||f��nXdS(Ns%s is not an attribute of %s(tgetattrtAttributeErrortNotImplementedError(RUR�tmethod((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�Vs
	cCsJy1|jd�}t|�dkr(dS|dSWntk
rEdSXdS(Nt.ii(R{tlenRR�(R�tparts((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�_s
cCs�|s
dSy�|jd�r1|jdd�}n$|jd�rU|jdd�}n|dj�|d}|ddkr�|j|dd	�S|d
dkr�|d
 S|SWntk
r�dSXdS(NtglobalRtregionaltregioniii����tiestyi����ts(Rt
startswithtreplacetlowerR�(R�((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�js 
cCshtj|�}|sdSi}|j|d<|j|d<|j|d<|jjd�dkrr|jd|d<n|j|d<|j|d<|j|d	<d|d
<d|d<d|d<|dj	d�}|d|d<|d|d<|d
dkr|d|d
<nt
d|��d|krt|d<|jd�}t
|�|dkr�||d|d<||d
|d<||d|d<nt
|�|dkr�||d|d<||d
|d<nt
|�|d
kr�||d|d<nt
|�|d
kr`t
d|��q`nAd|ks7d|krqd}d|krm|jd�}||d|d<n!|jd�}||d|d<t
|�|dkr�||d
|d<||d|d<||d|d<nt
|�|dkr||d
|d<||d|d<nt
|�|dkrE||d
|d<nt
|�|dkr`t
d|��q`n�|jd�}t
|�|dkr�||d
|d<||d|d<||d|d<nt
|�|dkr||d
|d<||d|d<nt
|�|dkr7||d
|d<nt
|�|dkr`t
d|��n|SdS(s�
        Parse GCP urls and return dict of parts.

        Supported URL structures:
        /SERVICE/VERSION/'projects'/PROJECT_ID/RESOURCE
        /SERVICE/VERSION/'projects'/PROJECT_ID/RESOURCE/ENTITY_NAME
        /SERVICE/VERSION/'projects'/PROJECT_ID/RESOURCE/ENTITY_NAME/METHOD_NAME
        /SERVICE/VERSION/'projects'/PROJECT_ID/'global'/RESOURCE
        /SERVICE/VERSION/'projects'/PROJECT_ID/'global'/RESOURCE/ENTITY_NAME
        /SERVICE/VERSION/'projects'/PROJECT_ID/'global'/RESOURCE/ENTITY_NAME/METHOD_NAME
        /SERVICE/VERSION/'projects'/PROJECT_ID/LOCATION_TYPE/LOCATION/RESOURCE
        /SERVICE/VERSION/'projects'/PROJECT_ID/LOCATION_TYPE/LOCATION/RESOURCE/ENTITY_NAME
        /SERVICE/VERSION/'projects'/PROJECT_ID/LOCATION_TYPE/LOCATION/RESOURCE/ENTITY_NAME/METHOD_NAME

        :param url: GCP-generated URL, such as a selflink or resource location.
        :type url: ``str``

        :return: dictionary of parts. Includes stanard components of urlparse, plus
                 GCP-specific 'service', 'api_version', 'project' and
                 'resource_name' keys. Optionally, 'zone', 'region', 'entity_name'
                 and 'method_name', if applicable.
        :rtype: ``dict``
        tschemethosttpatht/iiR
tfragmenttqueryR1RSRTitprojectsisunable to parse: %sR�iR�R�tmethod_nametregionstzonesi����R�R0iN(turlparseRR�tnetlocR�tfindR
R�R�R{tGCPInvalidURLErrorRtindexR�(turlR�t	url_partst
path_partstidx((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR��s�









RRtcomputecCsd|||fS(Ns,https://www.googleapis.com/%s/%s/projects/%s((R1RTRS((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytbuild_googleapi_urlscCs�i}|s'ddddddg}nt|t�r^g|D]}tj||�^q=}nXt|t�r�xF|j�D]/}||krztj|||�||<qzqzWn|}|S(NtcreationTimestamptidR�tselfLinktfingerprinttdescription(RpR�Rvtfilter_gcp_fieldsR�tkeys(R
texcluded_fieldst
new_paramsRzR�((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�s	%cCs2tj|�}tj|�}||kr.tStS(s�
        Check if two params dicts are equal.
        TODO(supertom): need a way to filter out URLs, or they need to be built
        (RvR�R+R(tp1tp2tfiltered_p1tfiltered_p2((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pytare_params_equals
N(t__name__t
__module__t__doc__tstaticmethodR}R�RR�RR�R�R�R�R�R�R�R�R�(((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyRv�s(+1	}tGCPErrorcBseZRS((R�R�(((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�&sR�cBseZRS((R�R�(((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�*sR�cBseZRS((R�R�(((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyR�.s(6R%RR�R^tdistutils.versionRR(RR2tImportErrorR+tgoogle.authREt
google.oauth2RRARNthttplib2Rtgoogleapiclient.httpRtgoogleapiclient.errorsRtapiclient.discoveryRRLt+ansible.module_utils.six.moves.urllib.parsetmodule_utilstsixtmovesturllibtparseR�RMRRRR"R!R?RRIRQRVR]R8RhRutobjectRvR7R�R�R�(((s</usr/lib/python2.7/site-packages/ansible/module_utils/gcp.pyt<module>sX








				O+	77	
		+�e

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]