
    Vh\)                         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mZmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlZd dlZ e       Z G d de      Z  G d de!      Z" G d de      Z#y)    )absolute_importdivisionprint_functiona  
    author:
        - Kyrylo Galanov (!UNKNOWN) <galanoff@gmail.com>
    name: manifold
    short_description: get credentials from Manifold.co
    description:
        - Retrieves resources' credentials from Manifold.co
    deprecated:
        removed_in: 11.0.0
        why: Manifold (the company) has been acquired in 2021 and the services used by this plugin are no longer operational.
        alternative: There is none.
    options:
        _terms:
            description:
                - Optional list of resource labels to lookup on Manifold.co. If no resources are specified, all
                  matched resources will be returned.
            type: list
            elements: string
            required: false
        api_token:
            description:
                - manifold API token
            type: string
            required: true
            env:
              - name: MANIFOLD_API_TOKEN
        project:
            description:
                - The project label you want to get the resource for.
            type: string
            required: false
        team:
            description:
                - The team label you want to get the resource for.
            type: string
            required: false
a
  
    - name: all available resources
      ansible.builtin.debug:
        msg: "{{ lookup('community.general.manifold', api_token='SecretToken') }}"
    - name: all available resources for a specific project in specific team
      ansible.builtin.debug:
        msg: "{{ lookup('community.general.manifold', api_token='SecretToken', project='poject-1', team='team-2') }}"
    - name: two specific resources
      ansible.builtin.debug:
        msg: "{{ lookup('community.general.manifold', 'resource-1', 'resource-2') }}"
a  
    _raw:
        description:
            - dictionary of credentials ready to be consumed as environment variables. If multiple resources define
              the same environment variable(s), the last one returned by the Manifold API will take precedence.
        type: dict
)AnsibleError)
LookupBase)open_urlConnectionErrorSSLValidationError)	HTTPErrorURLError)	urlencode)six)Display)format_exceptionNc                       e Zd Zy)ApiErrorN)__name__
__module____qualname__     m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/manifold.pyr   r   P   s    r   r   c                   @    e Zd ZdZd Zd Zd Zd
dZddZddZ	d	 Z
y)ManifoldApiClientzpython-manifold-ansible-1.0.0c                     || _         y N)_token)selftokens     r   __init__zManifoldApiClient.__init__W   s	    r   c                     d| d| S )Nzhttps://api.z.manifold.co/v1/r   )r   apiendpoints      r   	_make_urlzManifoldApiClient._make_urlZ   s    cU"28*==r   c           
         d| j                    dd}| j                  ||      }|}|j                  dd      }|r|j                  |       	 t        j                  d|        t        |g||| j                  d|}	|	j                         }
|	j                  j                  d      d	k(  rt        j                  |
      }
|
S # t        $ r t        d
| d
       t        $ r'}t        d| d| d|j                                d}~wt         $ r}t        d| d|       d}~wt"        $ r}t        d| d|       d}~wt$        $ r}t        d| d|       d}~ww xY w)a  
        Send a request to API backend and pre-process a response.
        :param api: API to send a request to
        :type api: str
        :param endpoint: API endpoint to fetch data from
        :type endpoint: str
        :param args: other args for open_url
        :param kwargs: other kwargs for open_url
        :return: server response. JSON response is automatically deserialized.
        :rtype: dict | list | str
        zBearer z*/*)AuthorizationAcceptheadersNzmanifold lookup connecting to )r(   
http_agentzcontent-typezapplication/jsonz/JSON response can't be parsed while requesting z:
zServer returned: z while requesting zFailed lookup url for z : z.Error validating the server's certificate for z: zError connecting to )r   r$   popupdatedisplayvvvvr   r)   readr(   getjsonloads
ValueErrorr   r   r   r
   r	   )r   r"   r#   argskwargsdefault_headersurlr(   arg_headersresponsedataes               r   requestzManifoldApiClient.request]   s     't{{m4

 nnS(+!jjD1NN;'	>LL9#?@bSWbWb[abH==?D##N37IIzz$'K 	^McURUVZU[\]] 	X.qc1CC5AFFH:VWW 	A3C5A3?@@! 	YLSEQSTUSVWXX 	>1#b<==	>s=   A8C "E&("D

E&D''E&3EE&E!!E&Nc                     d}d}i }|r||d<   |r||d<   |r||d<   |r|dt        |       z  }| j                  ||      S )a  
        Get resources list
        :param team_id: ID of the Team to filter resources by
        :type team_id: str
        :param project_id: ID of the project to filter resources by
        :type project_id: str
        :param label: filter resources by a label, returns a list with one or zero elements
        :type label: str
        :return: list of resources
        :rtype: list
        marketplace	resourcesteam_id
project_idlabel?r   r;   )r   r?   r@   rA   r"   r#   query_paramss          r   get_resourceszManifoldApiClient.get_resources   se     &-L#)3L&$)L!!Il3455H||C**r   c                 h    d}d}| j                  ||      }rt        t        fd|            }|S )z
        Get teams list
        :param label: filter teams by a label, returns a list with one or zero elements
        :type label: str
        :return: list of teams
        :rtype: list
        identityteamsc                     | d   d   k(  S NbodyrA   r   )xrA   s    r   <lambda>z-ManifoldApiClient.get_teams.<locals>.<lambda>   s    67);u)D r   )r;   listfilter)r   rA   r"   r#   r9   s    `   r   	get_teamszManifoldApiClient.get_teams   s9     ||C*DdKLDr   c                 f    d}d}i }|r||d<   |r|dt        |       z  }| j                  ||      S )z
        Get projects list
        :param label: filter projects by a label, returns a list with one or zero elements
        :type label: str
        :return: list of projects
        :rtype: list
        r=   projectsrA   rB   rC   )r   rA   r"   r#   rD   s        r   get_projectszManifoldApiClient.get_projects   sK     $)L!!Il3455H||C**r   c                 J    d}dt        d|i       }| j                  ||      S )z
        Get resource credentials
        :param resource_id: ID of the resource to filter credentials by
        :type resource_id: str
        :return:
        r=   zcredentials?resource_idrC   )r   rU   r"   r#   s       r   get_credentialsz!ManifoldApiClient.get_credentials   s1     !)]K,H"I!JK||C**r   )NNNr   )r   r   r   r)   r    r$   r;   rE   rP   rS   rV   r   r   r   r   r   T   s+    0J>)>V+8 +(	+r   r   c                       e Zd ZddZy)LookupModuleNc                    | j                  ||       | j                  d      }| j                  d      }| j                  d      }	 |t        |      }|r7|j                  |      }t	        |      dk(  rt        d| d      |d   d   }	nd	}	|r7|j                  |      }
t	        |
      dk(  rt        d
| d      |
d   d   }nd	}t	              dk(  r|j                  |	|d         }n-|j                  |	|      }rt        t        fd|            }r^t	        |      t	              k  rG|D cg c]
  }|d   d    }}D cg c]	  }||vs| }}t        ddj                  |       d      i }i }|D ]  }|j                  |d         }t	        |      s#|d   d   d   s/t        j                  |d   d   d         D ]=  \  }}|d   d   }||v r"t        j                  d| d||    d| d       |||<   |||<   ?  |g}|S c c}w c c}w # t         $ r}t        d|       d	}~wt
        $ r}|d	}~wt"        $ r/ t%        j&                         \  }}}t        t)        |||            w xY w)aC  
        :param terms: a list of resources lookups to run.
        :param variables: ansible variables active at the time of the lookup
        :param api_token: API token
        :param project: optional project label
        :param team: optional team label
        :return: a dictionary of resources credentials
        )var_optionsdirect	api_tokenprojectteamr   zTeam 'z' does not existidNz	Project '   )r?   r@   rA   )r?   r@   c                     | d   d   v S rJ   r   )rL   labelss    r   rM   z"LookupModule.run.<locals>.<lambda>   s    1V9W;MQW;W r   rK   rA   zResource(s) z, z do not existvalues'z' with label 'z,' was replaced by resource data with label 'zAPI Error: )set_options
get_optionr   rP   lenr   rS   rE   rN   rO   joinrV   r   	iteritemsr,   warningr   	Exceptionsysexc_infor   )r   terms	variablesr4   r\   r]   r^   client	team_datar?   project_datar@   resources_datarfetched_labelsrA   not_found_labelscredentialscred_mapresourceresource_credentialscred_keycred_valretr:   exc_type	exc_valueexc_tracebackrb   s                               @r   runzLookupModule.run   s    	Yv>OOK0	//),v&4	UF&y1F",,T2	y>Q&&v5E'FGG#A,t,%227;|$)&7);K'LMM)!_T2
!
6{a!'!5!5gR\djkldm!5!n!'!5!5gR\!5!]%)&1WYg*h%iN#n-F;>L!M!F)G"4!M!M7=#]enA\E#] #]"\$))<L2M1Nm#\]]KH* 3'-'='=htn'M$+,1Ea1H1PQY1Z.1mm<PQR<STZ<[\d<e.f 3*( ( 0 9#{2#OOazQYHZG[  \H  IN  HO  OP  -Q  R08H--2*33 -CJ# "N#]"  	2QC011 	G 	U14.Hi/)]STT	UsW   	C3H# <HH# 	HHAH# (H# 4A$H# 
H# #	J,H::JI;Jr   )r   r   r   r   r   r   r   rX   rX      s	    DUr   rX   )$
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNansible.errorsr   ansible.plugins.lookupr   ansible.module_utils.urlsr   r	   r
   +ansible.module_utils.six.moves.urllib.errorr   r   +ansible.module_utils.six.moves.urllib.parser   ansible.module_utilsr   ansible.utils.displayr   	tracebackr   r0   rl   r,   rk   r   objectr   rX   r   r   r   <module>r      s~    C B$L

 ( - S S K A $ ) &  

)	y 	}+ }+@FU: FUr   