
    BBj                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dl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mZmZmZmZmZmZ  G d d          ZdS )	    N)render_template)	APILogger)AthenzClient)S3Client)MySQLHandlerPostgreSQLHandlerMongoDBHandlerRedisHandlerCassandraHandlerOpenSearchHandlerc                       e Zd Zd'dZd Zd Zd Z	 d(d	Zd
 Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd)dZd Zd Zd*d Zd)d!Zd)d"Z d# Z!d$ Z"d% Z#d& Z$dS )+APIFunctionsprodc                    || _         t          d| j                   | _        d| _        d| _        t          | j                  | _        t          | j                  | _        t          | j                  | _
        t          | j                  | _        t          | j                  | _        t          | j                  | _        i | _        d S )Nzcqa-test-app.log)log_fileenvironmentz/swaggerz/swagger.json)envr   
api_loggerswagger_pathapi_spec_pathr   mysql_handlerr   postgresql_handlerr	   mongodb_handlerr
   redis_handlerr   cassandra_handlerr   opensearch_handler_athenz_clientsselfr   s     /app/functions/APIFunctions.py__init__zAPIFunctions.__init__   s    #-?TXVVV&, *$/::"3DO"D"D-do>>)$/::!1$/!B!B"3DO"D"D  "    c                 8   || _         | j         dk    r4t          j        d          | _        t          j        d          | _        d S | j         dv r4t          j        d          | _        t          j        d          | _        d S t          d| j          d          )	NdevDEV_S3_ACCESS_KEYDEV_S3_SECRET_KEY)r   prod_secretprod_top_secretssk_prodPROD_S3_ACCESS_KEYPROD_S3_SECRET_KEYzInvalid environment: zT. Must be either 'dev' or 'prod' or 'prod_secret' or 'prod_top_secret' or 'ssk_prod')r   osgetenv
access_key
secret_key
ValueErrorr   s     r    set_envzAPIFunctions.set_env&   s    8u i(;<<DO i(;<<DOOOXOOO i(<==DO i(<==DOOO  TTX  T  T  T  U  U  Ur"   c                 f   |                                 }|                    ddt          d           |                                }|d         }|s|                     ddi|d          S t          j        |                                          }|                     dd	|z   d
z   |z   i|          S )NcmdTzneed to input cmd..requiredtypehelpmsgz!Bad Request, cmd is empty or None  res_msgrequestres_codez[CMD]:z, [Response]:r;   r<   )RequestParseradd_argumentstr
parse_argsmake_common_responser,   popenread)r   reqparser<   parserargsr3   ress          r    run_cmdzAPIFunctions.run_cmd1   s    ''))EDsAVWWW  "" 5k 	,, CD -    hsmm  "" ((HsN_<sBC ) 
 
 	
r"   c                    |                                  du r|                     ddi|d          S |                                }|                    ddt          d	           |                    d
dt          d	           |                    ddt
          d	           |                    ddt          d	           |                                }|d         r|d
         r|d         r|d         s|                     ddi|d          S |d         dk    r&|d         dk    r|                     ddi|d          S t          j                    }|d         |_	        |d
         |_
        |d         |_        |d         |_        |                                }|j        r"|                     dd|j         i|d          S |j        dk    r|                     |j                  }|                     |j                  }|                     |j                  }	|                     |j                  }
dd|j	         d|j
         d|j         d| d| d|	 d|
 di}no|                     |j                  }|                     |j                  }t-          |j        d          }dd|j	         d|j
         d|j         d | d!| d"| d#i}|                     ||$          S )%NFr8   zQInternal Server Error, There is no "iperf3" in server, cannot run iperf testing..  r:   host_ipTz)need to input "host_ip" for running iperfr4   	host_portz+need to input "host_port" for running iperfdurationz3need to input "duration"(sec) for setting test timeprotocolz;need to input "protocol"(tcp/udp) for setting test protocolz,Bad Request, some params are empty or None..r9   tcpudpz7Bad Request, protocol param must input "tcp" or "udp"..z$Internal Server Error, iperf3 Error=z[TCP Result]: Host=:z, Duration=z sec, Transfer=(Sent)z MBytes, (Recv)z MBytes, Bitrate=(Sent)z Mbit/sec, (Recv)z
 Mbit/sec    z[UDP Result]: Host=z sec, Transfer=z MBytes, Bitrate=z Mbit/sec, LostPacket=z % r>   )_is_iperf_installedrC   r?   r@   rA   intrB   iperf3Clientserver_hostnameportrO   rP   runerror_cal_to_mega
sent_bytesreceived_bytessent_bpsreceived_bpsbytesbpsroundlost_percent)r   rF   r<   rG   paramsclientrI   sent_transferrecv_transfersent_bitraterecv_bitrater;   transferbitratelost_packet_percents                  r    	run_iperfzAPIFunctions.run_iperfH   s   ##%%..,, st -    ''))I3EpqqqK$SGtuuuJC!V 	 	X 	X 	XJC!^ 	 	` 	` 	`""$$ i  	{(; 	6*CU 	]cdn]o 	,, NO -   
 *&&6*+=+F+F,, YZ -    !'	!2[) , ,jjll 9 	,, Rsy R RS -    ?e## --cn==M --c.@AAM,,S\::L,,S-=>>L  9V-C 9 9fk 9 9#&<9 9)69 9 !.9 9 )5	9 9
 !-9 9 9GG ((33H''00G"'(8!"<"<  >V-C > >fk > >#&<> >#+> > #*> > &9	> > >G (('(JJJr"   N   Tc           	         |ddi}|||ddidS ||d<   |j         |d<   |j        |d<   i |d	<   t          j        |d	         d
<   |j        |d	         d<   |                                 \  |d	         d<   |d	         d<   t          j                            d|d	         d                   |d	         d<   i |d<   | 	                    |          |d         d<   |
                                }|sd|d<   d|d          d|d          d|d          d|d          }d|d	         d
          dt          j                            d|d	         d                    d|d	         d          d|d	         d          }d|d         d          }| j        j                            | d| d|            |                     |           t          j        |d          }	|	|fS )Nr8   zTest OKzContent-Typez	text/json)statusbodyheaderscodemethodpathserver_infoapp_versionserver_hostcontainer_id	server_ipHOST_IPclient_info	client_ipz***zcode=z	, method=z, path=z, msg=zapp_version=z, server_ip=z, server_host=z, container_id=z
client_ip=z || )r<      )indent)rv   rw   versionVERSIONhost_get_server_infor,   environget_get_client_ipcopyr   loggerinfomake_request_header_logjsondumps)
r   r<   r;   r=   log_sensitivelog_msgrequest_logsserver_logsclient_logsjson_ress
             r    rC   z!APIFunctions.make_common_response   s{    ?i(G?"*K8   ##N!,!#07}-07}-VZVkVkVmVmS~.0F{0S.0jnnYP]H^_jHk.l.l{+!#.2.A.A'.J.J{+ ,,.. 	#"GENzwvzz9JzzSZ[aSbzzjqrwjxzzDW]%;M%J D DXZXbXfXfgpry  {H  sI  JU  sV  YW  YW D D%,]%;M%JD D[bcp[q  sA  \BD DH7=#9+#FHH##|$W$W$W$W+$W$WXXX$$W$555:ga000!!r"   c                     |                     | j        | j        ddd          }|                    || j                   d S )NnonezCQA Test Application)validatorUrlapp_name)config)
url_prefix)get_swaggerui_blueprintr   r   register_blueprint)r   appflask_swagger_uiswaggerui_blueprints       r    set_swagger_uizAPIFunctions.set_swagger_ui   s_    .FF &2  G 
 
 	2t?PQQQQQr"   c                 (    t          |dz  d          S )Ni@B rT   )rd   )r   vals     r    r]   zAPIFunctions._cal_to_mega   s    S7]A&&&r"   c                     t          j        d                                          }|                    d          dk    rdS dS )Nz	iperf3 -vziperf 3TF)r,   rD   rE   find)r   rI   s     r    rU   z APIFunctions._is_iperf_installed   s>    h{##((**88I##4ur"   c                    t           j                            d|          }t           j                            |          r|                    d          s |                     dd|z   dz   i|d          S t          |dd	
          5 }|                                }d d d            n# 1 swxY w Y   t          j	        |g d          }d}t           j                            |          s|                     ddi|d          S t          d|                    dd                                          |          }|                     ddi|           |S )Nz./resources/wikiz.mdr8   zError: z: File not exists  r:   rutf-8)encoding)tableszfenced-code-blockszcuddled-lists)extrasz./static/css/md_file.csszError: CSS file not foundz
basic.html )titlecontentzConverted to HTML successfullyr>   )r,   rw   joinisfileendswithrC   openrE   	markdown2markdownr   replace
capitalize)	r   r<   	file_path	full_pathmd_filemarkdown_contenthtml_contentcss_file_pathrendered_html_contents	            r    render_markdownzAPIFunctions.render_markdown   s   GLL!3Y??	 w~~i(( 		0B0B50I0I 	,,	I 58K KL -    )S7333 	.w&||~~	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. !)%V%V%V
 
 

 3w~~m,, 	,, ;< -    !0##E2..99;; !
 !
 !
 	!!<= 	" 	
 	
 	
 %$s   B((B,/B,c                 (   d}d}|j                                         D ]}|d| d|j         |          dz  }|dk    rTd                    |                    d          d d                   }| j        j                            ||z              d S d S )Nzrequest headers : r   [z]=, r   )rt   keysr   splitr   r   debug)r   r<   
log_prefixlog_contentskeys        r    r   z$APIFunctions.make_request_header_log  s    )
?'')) 	@ 	@C???ws';????LL299\%7%7%=%=crc%BCCLO"((l)BCCCCC r"   c                 f   |j                             d|j                  }|rF|                                dk    r.|                    d          d                                         }n?g d}|D ]8}|j                             |          }|r|                                dk    r n9|s|j        }|S )NzX-Forwarded-Forunknown,r   )zProxy-Client-IPzWL-Proxy-Client-IPHTTP_CLIENT_IPHTTP_X_FORWARDED_FOR)rt   r   remote_addrlowerr   strip)r   r<   r   headers_to_checkheaders        r    r   zAPIFunctions._get_client_ip  s    O''(97;NOO	 	**i77!,,Q/5577II      +  #O//77	 !2!2i!?!?E 	,+Ir"   c                     t          j                    }|sdS 	 t          j        |          }n=# t          $ r0}| j        j                            d|            |dfcY d }~S d }~ww xY w||fS )N)r   r   z&Failed to get server ip from hostname=r   )socketgethostnamegethostbyname	Exceptionr   r   r   )r   hostnamer|   es       r    r   zAPIFunctions._get_server_info)  s    %'' 	(''	',X66II 	' 	' 	'O"(()\RZ)\)\]]]Y&&&&&&&	' ""s   . 
A(%A#A(#A(c                 .   t          j        ||f          5 }	 |                    d           n:# t           j        $ r(}t	          d|            Y d }~d d d            dS d }~ww xY wt          j        |           d d d            d S # 1 swxY w Y   d S )Ns$   Establishing connection for durationz&An error occurred while sending data: F)r   create_connectionsendallr\   printtimesleep)r   r   rZ   rO   sockr   s         r    establish_connectionz!APIFunctions.establish_connection7  s   %tTl33 		!tDEEEE<   BqBBCCCuuu		! 		! 		! 		! 		! 		! 		! 		!
 Jx   		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		!s6   B
/B
A&A!B
!A&&B

BBc                 d    t          j                    }t          fd|D                       }|S )Nc              3   f   K   | ]+}|j         t          j        k    r|j        j        k    'd V  ,dS )   N)rr   psutilCONN_ESTABLISHEDladdrrZ   ).0connrZ   s     r    	<genexpr>zAAPIFunctions.get_established_connections_count.<locals>.<genexpr>G  sL        *  *dT[FLc=c=c
4'' !"'''' *  *r"   )r   net_connectionssum)r   rZ   connectionsestablished_counts    `  r    !get_established_connections_countz.APIFunctions.get_established_connections_countC  sP    ,..    *  *  *  *k  *  *  * * * ! r"   c                    |                     d          }|r"| j        j                            d|            |                                }|                    ddt          d           |                    ddt          d           |                                }	 |d          d	|d          }|| j        vr$t          |d         |d                   | j        |<   | j        |         }|
                                }|                     d
|i|d          S # t          $ rc}	| j        j                            dt          |	                      |                     d
dt          |	           i|d          cY d }	~	S d }	~	ww xY w)NTsilentRequest body: account_namezneed to input 'account_name'r4   provider_domainzneed to input 'provider_domain'rS   r8   F)r;   r<   r   zFailed to get access token: z$Internal Server Error, Athenz Error=rL   r:   )get_jsonr   r   r   r?   r@   rA   rB   r   r   get_access_tokenrC   r   r\   )
r   rF   r<   request_bodyrG   rH   	cache_keyathenz_clientaccess_tokenr   s
             r    get_athenz_access_tokenz$APIFunctions.get_athenz_access_tokenL  s   ''t'44 	IO"''(G(G(GHHH''))/	 	 	
 	
 	
 	2	 	 	
 	
 	
   ""	/KK$7H2IKKI 4442>($/@*A3 3$Y/ !0;M(99;;L,,-we -     	 	 	O"(()PA)P)PQQQ,, Os1vv O OP -        	s     A:D 
F%AF=FFc                    |                      ||          \  }}|s|S |r|s|                     ddi|d          S |                    d          }|r"| j        j                            d|            |                                }|                    ddt          d	
           |	                                d         }	t          | j        | j        | j                  }
|
                    |||          \  }}|s3|                     |d         d         d|d         d          i|          S |                     ||	|||
|          \  }}|s|S |
                    ||          \  }}|                     |d         d         dd| d| d|d                                                             d           i|          S )Nr8   %Bad Request, invalid bucket or objectr9   r:   Tr   r   data!need to put the content in 'data'r4   bucket_nameobj_namedst_nameResponseMetadataHTTPStatusCodeErrorMessager=   r;   r<   	file_name
input_datadest_bucketdest_objrg   r<   r  r  z*Successfully put the data into FOS Bucket=	, Object=
, Content=Bodyr   )_check_fos_path_and_permissionrC   r   r   r   r   r?   r@   rA   rB   r   r.   r/   r   download_object_file_write_and_upload_fos
get_objectrE   decode)r   rF   r<   r  r  
is_successresponser   rG   r  	s3_client_s               r    put_test_foszAPIFunctions.put_test_foss  s   #BB;PXYY
H 	O  	( 	,, GH -    ''t'44 	IO"''(G(G(GHHH''))4	 	 	
 	
 	
 &&((0
 T_dotxHH	(88[[cnv8ww
H 	,,!"456FG8G#4Y#? AB -     $>>:#hg  ?  
  

H
  	O  **{X*VV8((012BC  XK  X  XZb  X  Xnvw}n~  oD  oD  oF  oF  oM  oM  NU  oV  oV  X  X  ) 
 
 	
r"   c           	      z   |                      ||          \  }}|s|S t          | j        | j        | j                  }|                    ||          \  }}|s|d         d         |d         d          dS |d         d         d| d| d	|d
                                                             d           dS )Nr  r  r  r  r  ru   r8   z*Successfully get the data into FOS Bucket=r  r  r  r   )r  r   r.   r/   r   r  rE   r  )r   r<   r  r  r  r  r  s          r    get_fos_objectzAPIFunctions.get_fos_object  s&   #BB;PXYY
H 	O T_dotxHH	(33V^3__
H 	 !345EF"7+I68  
 /01AB T  T  TV^  T  Tjrsyjzjj  kB  kB  kI  kI  JQ  kR  kR  T  T
 
 	
r"   c                 2   |                     d          }|                    dd          }|                     |           t          | j        | j        |          }|                     ||          \  }}	|s|	S d}
|                     d          }|r"| j        j        	                    d|            |
                                }|                    ddt          d	           |                                d         }d
}|                     ||||||          \  }}	|s|	S |                     ||||          \  }}	|s|	S |
dz  }
t!          j        d           |                     ||||||          \  }}	|s|	S |                     ||||          \  }}	|s|	S |
dz  }
t!          j        d           |                    ||          \  }}	|s|	d         d         |	d         d          dS |
dz  }
t!          j        d           dd|
 d| d| dS )NTr   r   r   r   r   r   r   r4   z(This is new object made by cqa-test-app.r
  zCREATE/GET object-PASS, rT   zUPDATE/GET object-PASS, r  r  r  r  r  r  DELETE object-PASSrp   zAll Passed CRUD Test: [
], Bucket=r  )r   r   r1   r   r.   r/   r  r   r   r   r?   r@   rA   rB   r  _validate_get_fos_objectr   r   delete_object)r   rF   r<   r  r  r   r   r  r  r  
all_resultrG   r  	init_datas                 r    test_fos_crudzAPIFunctions.test_fos_crud  s   ''t'44uf--S T_dosCC	#BB;PXYY
H 	O
 ''t'44 	IO"''(G(G(GHHH''))4	 	 	
 	
 	
 &&((0
 ?	#>>9#hg  ?  
  

H
  	O  $<<YU]_hii
H 	O00

1  $>>:#hg  ?  
  

H
  	O  $<<YU]_ijj
H 	O00

1  )66;Ya6bb
H 	 !345EF"7+I68   	**

1 cZcc;ccYacc
 
 	
r"   c                    |                      |||           d}|                    d          }|r"| j        j                            d|            |                                }|                    ddt          d           |                                d         }| 	                    ||||| j
        |          \  }	}
|	s|
S |                     | j
        |||          \  }	}
|	s|
S |d	z  }d
d| d| d| dS )Nr   Tr   r   r   r   r4   r
  zUpload object-PASSrp   zPassed Upload Test: [r"  r  r  )_initialize_s3_clientr   r   r   r   r?   r@   rA   rB   r  r  r#  )r   rF   r<   r  r  r%  r   rG   r  r  r  s              r    create_fos_objectzAPIFunctions.create_fos_object  s_   ""7KBBB
''t'44 	IO"''(G(G(GHHH''))4	 	 	
 	
 	
 &&((0
  $>>:#h>7  ?  
  

H
  	O  $<<T^[Zbdnoo
H 	O**
 a:aaaaW_aa
 
 	
r"   c                     |j                             dd          }|                     |           t          | j        | j        |          | _        d}| j                            ||          \  }}|sdd|d         d          d	S | j                            ||          \  }}|s|d
         d         |d         d          d	S |dz  }	 t          j
        |           n# t          $ r Y nw xY wdd| d| d| d	S )Nr   r   r   r  r   zObject not found: r  r  r  r  r  r!  rp   zPassed Delete Test: [r"  r  )rH   r   r1   r   r.   r/   r  r  r$  r,   removeFileNotFoundErrorr   r<   r  r  r   r%  r  r  s           r    delete_fos_objectzAPIFunctions.delete_fos_object1  se   luf--S!$/4?CHH
  $~88[[c8dd
H 	JHW,=i,HJJ    $~;;^f;gg
H 	 !345EF"7+I68   	**
	Ih  	 	 	D	 a:aaaaW_aa
 
 	
s   C" "
C/.C/c                    |j                             dd          }|                     |           t          | j        | j        |          | _        |                     |d           \  }}|s|S d}| j                            |          \  }}|s|d         d         |d         d          dS |d	z  }d
 |D             }dd| d| d| dS )Nr   r   r   r  r  r  r  r  zList object-PASSc                     g | ]
}|d          S Key r   objs     r    
<listcomp>z0APIFunctions.get_list_object.<locals>.<listcomp>i  s    666cs5z666r"   rp   zPassed List Test: [r"  r  )	rH   r   r1   r   r.   r/   r  r  get_list_object)r   r<   r  r   r  r  r%  object_keyss           r    r8  zAPIFunctions.get_list_objectU  s   luf--S "$/4?CHH#BB;PTUU
H 	O
#~==kJJ
H 	 !345EF"7+I68   	((
 76X666 bbb{bbU`bb
 
 	
r"   c                     |                      ||d            d}|dvrdddS | j                            ||          \  }}|s|d         d         |d         d	          dS |d
z  }dd| d| d| dS )Nr   )enabledisabler9   z1Bad Request, action must be 'enable' or 'disable'r  r  r  r  r  zUpdate bucket-PASSrp   zPassed Update Bucket Test: [r"  z	, Action=)r)  r  update_bucket)r   r<   r  actionr%  r  r  s          r    r=  zAPIFunctions.update_bucketq  s    ""7K>>>
 ...J  
  $~;;KPP
H 	 !345EF"7+I68   	**
 f*ffff^dff
 
 	
r"   c                    |j                             dd          }|                     |           t          | j        | j        |          | _        |r|s|                     ddi|d          S d}| j                            |||          \  }}|s3|                     |d	         d
         d|d         d          i|          S |dz  }dd| d| d| dS )Nr   r   r8   r   r9   r:   r   r  r  r  r  r  r	  zDownload object-PASSrp   zPassed Download Test: [r"  r  r  )	rH   r   r1   r   r.   r/   r  rC   r  r.  s           r    r  zAPIFunctions.download_object  s4   luf--S "$/4?CHH  	( 	,, GH -    
#~==+`hs{=||
H 	,,!"456FG8G#4Y#? AB -   
 	,,
 cZcc;ccYacc
 
 	
r"   c                     |                     d          pi }|                    dd          }|                     |           t          | j        | j        |          | _        |                     ||           d S )NTr   r   r   )r   r   r1   r   r.   r/   r  r  )r   r<   r  r  r   r   s         r    r)  z"APIFunctions._initialize_s3_client  sw    ''t'44:uf--S!$/4?CHH++KBBBBBr"   c                 X    |sddddfS |	|sddddfS | j         r| j        sddddfS dS )	NFr9   zBad Request, invalid bucketr  zBad Request, invalid objecti  z3Forbidden, need to access key and secret key on envTN)r.   r/   )r   r  r  s      r    r  z+APIFunctions._check_fos_path_and_permission  s     	4   
 4   
  	do 	L    zr"   c                     |                     ||          \  }}|s d|d         d         |d         d          dfS |d                                                             d	          }||k    rdd
d| d| dfS dS )Nr  Fr  r  r  r  r  r  r   rp   z5Failed to validate FOS Object data [Expected Result]=z, [Actual Result]=rB  )r  rE   r  )r   r  r  r  expected_datar  r  object_datas           r    r#  z%APIFunctions._validate_get_fos_object  s    (33V^3__
H 	 !345EF"7+I68   
 v&++--44W==-''}}}}p{}}    zr"   c                 l   	 t          |d          5 }|                    |           d d d            n# 1 swxY w Y   n7# t          $ r*}|                     dd| d| i|d          cY d }~S d }~ww xY w|                    |||          \  }	}
|	sd|
j        |
j         d	fS |                    ||
          \  }	}
|	s d|
d         d         |
d         d          d	fS 	 t          j	        |           n<# t          $ r/}| j
        j                            d| d|            Y d }~nd }~ww xY wdS )Nwr8   z,Internal Server Error, Failed to write file r   rL   r:   )r  r  src_nameFr  r  r  r  r  r  zFailed to delete file : rB  )r   writer   rC   upload_objectstatus_codetextset_object_aclr,   r,  r   r   r\   )r   r  r  r  r  rg   r<   filer   r  r  s              r    r  z'APIFunctions._file_write_and_upload_fos  s*   	i%% '

:&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 	 	 	,, _y _ _\] _ _` -        	  &33#h  4  
  

H  	 ,"-)   
  &44#h  5  
  

H  	 !345EF"7+I68   
	TIi     	T 	T 	TO"(()R))R)Rq)R)RSSSSSSSS	T zsP   A  4A  8A  8A   
A4
A/)A4/A4#C8 8
D1%D,,D1c                    |dk    r| j                             ||||          S |dk    r| j                            ||||          S |dk    r| j                            ||||          S |dk    r| j                            ||||          S |dk    r| j                            ||||          S |dk    r| j                            ||||          S ddd	S )
Nmysql
postgresqlmongodbredis	cassandra
opensearchr9   zBad Request, invalid productr  )r   connectr   r   r   r   r   )r   productserviceuserpassworddatabases         r    connect_to_dbzAPIFunctions.connect_to_db  s	   g%--gtXxPPP$$*227D(HUUU	!!'//xRRR%--gtXxPPP##)11'48TTT$$*227D(HUUU 5  r"   c                    |dk    r| j                             ||          S |dk    r| j                            ||          S |dk    r| j                            ||          S |dk    r| j                            ||          S |dk    r| j                            ||          S |dk    r| j                            ||          S ddd	S )
NrQ  rR  rS  rT  rU  rV  r9   z$Invalid product or connection failedr  )r   execute_queryr   r   r   r   r   )r   rX  queryr\  s       r    r_  zAPIFunctions.execute_query  s    g%33E8DDD$$*88III	!!'55eXFFF%33E8DDD##)77xHHH$$*88III =  r"   c                     	 t          | j                  }|                    |||          }|                    d          dk    r|S |                    |||          S # t
          $ r}	dd|	 dcY d }	~	S d }	~	ww xY w)Nru   rp   rL   zInternal Server Error: r  )r   r   rW  r   execute_api_queryr   )
r   rY  rZ  r[  rv   endpointpayloadtemp_handlerconnect_resultr   s
             r    execute_opensearch_api_queryz)APIFunctions.execute_opensearch_api_query&  s    	,T_==L *11'4JJN!!&))S00%%  11&(GLLL 	 	 	444       	s$   AA A 
A;)A60A;6A;c           
         |                     d          }|r"| j        j                            d|            |                                }|                    ddt          d           |                                d         }ddg}i |D ]}		 |                     |	           | 	                    ||          \  }
}|
s2d	|
                    d
d          |
                    dd          d|	<   ft          | j        | j        |	          }|                     | d|	 |||||          \  }
}|
s2d	|
                    d
d          |
                    dd          d|	<   |                     ||||          \  }
}|
s3d	|
                    d
d          |
                    dd          d|	<   (ddd|||d|	<   6# t           $ rV}| j        j                            d|	 dt          |                      d	ddt          |           d|	<   Y d }~d }~ww xY wt%                    dk    o,t'          fd                                D                       }|rdnd|dS )NTr   r   r   r   r4   r)   r   Fru   rL   r8   Unknown errorsuccessru   r8   r  r
  zUpload failedzValidation failedrp   z#Successfully uploaded and validated)rk  ru   r8   bucketobjectr   Unexpected error for rI  Unexpected error: r   c              3   4   K   | ]}|         d          V  dS rk  Nr4  r   r   resultss     r    r   z1APIFunctions.write_to_dual_env.<locals>.<genexpr>  ,      2e2es73<	3J2e2e2e2e2e2er"   ru   overall_successenvironments)r   r   r   r   r?   r@   rA   rB   r1   r  r   r   r.   r/   r  r#  r   r\   lenallr   )r   rF   r<   r  r  r   rG   r  envsenv_namer  r  r  r   rv  rs  s                  @r    write_to_dual_envzAPIFunctions.write_to_dual_env9  sE   ''t'44 	IO"''(G(G(GHHH''))4	 	 	
 	
 	
 &&((0
 F# 8	 8	H7X&&&'+'J'J;X`'a'a$
H! #( (VS 9 9'||E?CC) )GH%
  %T_doxPP	 (,'F'F!)66H66: +h$g (G ( ($
H
 " #( (VS 9 9'||E?CC) )GH%
  (,'D'DYP[]egq'r'r$
H! #( (VS 9 9'||E3FGG) )GH%
   $A)&&% %!!    &,,-YX-Y-YQTUVQWQW-Y-YZZZ$8A88% %!!!!!! g,,*es2e2e2e2eV]VbVbVdVd2e2e2e/e/e +3CC.#
 
 	
s.   A!G6A1G(AG8G
H&AH!!H&c           
         ddg}i |D ]}	 |                      |           |                     |d           \  }}|s3d|                    dd          |                    dd          g d|<   gt          | j        | j        |          }|                    |          }t          |t                    rt          |          d	k    r|\  }}n!t          |t                    rd}|}nd}dd
d}|sd}	d}
	 t          |t                    rRd|v rd|d         v r|d         d         }	d|v rd|d         v r|d         d         }
nd|v r	|d         }
nd|v r|d         }	n<# t          $ r/}| j        j                            d| d|            Y d }~nd }~ww xY wd|	|
g d|<   g }t          |t                    re	 d |D             }nW# t          $ rJ}| j        j                            d| d|            dddt!          |           g d|<   Y d }~d }~ww xY wdddt          |           d||t          |          d|<   E# t          $ rW}| j        j                            d| dt!          |                      dddt!          |           g d|<   Y d }~d }~ww xY wt                    dk    o,t#          fd                                D                       }|rdnd|dS ) Nr)   r   Fru   rL   r8   ri  )rk  ru   r8   objectsrT   zUnexpected response formatr  zFailed to list objectsr  r  r  r  zError parsing response for rI  c                 P    g | ]#}t          |t                    d |v |d          $S r2  )
isinstancedictr5  s     r    r7  z3APIFunctions.list_from_dual_env.<locals>.<listcomp>  s9    &p&p&pcTWY]I^I^&pchlococos5zcococor"   z!Error extracting object keys for zError processing object list: Trp   zSuccessfully listed z
 object(s))rk  ru   r8   rl  r~  countrn  ro  r   c              3   4   K   | ]}|         d          V  dS rq  r4  rr  s     r    r   z2APIFunctions.list_from_dual_env.<locals>.<genexpr>  rt  r"   ru  )r1   r  r   r   r.   r/   r8  r  tuplerx  r  r   r   r   r\   listrA   ry  r   )r   r<   r  rz  r{  r  r  r  list_result
error_code	error_msgr   r9  rv  rs  s                 @r    list_from_dual_envzAPIFunctions.list_from_dual_env  sL   F# Z	 Z	HYX&&&'+'J'J;X\']']$
H! #( (VS 9 9'||E?CC#%	) )GH%  %T_doxPP	 (77DD k511 	Rc+6F6F!6K6K+6(JT22 R!&J*HH "'J(+4PQQH! !$J 8Id%h55 >1X==BRV^_qVrBrBr-56H-IJZ-[
&(22yHWDU7U7U,4W,=i,H		!&(!2!2,4UO		!'8!3!3-5f-=
$ d d d.445bS[5b5b_`5b5bccccccccd $) *(#%	) )GH%  !h-- !
!&p&pX&p&p&p$ ! ! !.445hYa5h5hef5h5hiii',$'#LCFF#L#L')	- -) !!  $N#k2B2BNNN)* --% %!!    &,,-YX-Y-YQTUVQWQW-Y-YZZZ$8A88!	% %!!!!!! g,,*es2e2e2e2eV]VbVbVdVd2e2e2e/e/e +3CC.#
 
 	
s{   A"I1BI7A'EI
F)%FIFI'I?GI
H ?HIH  -I
J0AJ++J0c           
         |                     d          }|sddddS |                    di           }|                    di           }||d	}i |                                D ]V\  }}	 |                    d
          }|                    d          }	|                    d          }
|                    d          }|r|	r|
s
dddd|<   k| j                            ||	|
|          }|                    d          dk    r"dd|                    dd          ||pdd|<   n1d|                    dd          |                    dd          d|<   # t
          $ rV}| j        j                            d| dt          |                      dddt          |           d|<   Y d }~Pd }~ww xY wt                    dk    o,t          fd                                D                       }|rdnd|dS )NTr   r9   F!Bad Request, missing request bodyru   rv  r8   sskkksr  r  rY  rZ  r[  r\  z1Missing required fields (service, user, password)rj  ru   rp   r8   zConnected successfullyN/A)rk  ru   r8   rY  r\  rL   zConnection failedzConnection error for rI  zConnection error: r   c              3   4   K   | ]}|         d          V  dS rq  r4  rr  s     r    r   z5APIFunctions.connect_to_dual_mysql.<locals>.<genexpr>6  rt  r"   ru  )r   r   itemsr   rW  r   r   r   r\   rA   rx  ry  r   )r   r<   r   ssk_infokks_inforz  r{  db_inforY  rZ  r[  r\  resultr   rv  rs  s                  @r    connect_to_dual_mysqlz"APIFunctions.connect_to_dual_mysql  s   ''t'44 	#(:    ##E2..##E2.. 
 
 !% &	 &	Hg%!++i00{{6**";;z22";;z22  d ( #( #R) )GH%
  +33GT8XVV::f%%,,#' #%zz%1IJJ#*$,$5) )GH%% $) &

63 7 7%zz%1DEE) )GH%
    &,,-YX-Y-YQTUVQWQW-Y-YZZZ$8A88% %!!!!!! g,,*es2e2e2e2eV]VbVbVdVd2e2e2e/e/e +3CC.#
 
 	
s!   .A#EB	E
F<&AF77F<c                    |                     d          }|sddddS |                    d          }|                    d          }|                    d	i           }|                    d
i           }|sddddS ||d}i |                                D ]*\  }}		 |	r
|	                    d          }
|	                    d          }|	                    d          }|	                    d          p|}|
r|r|rt          | j                  }|                    |
|||          }|                    d          dk    r6d|                    dd          d|                    dd           |d|<   |                    ||          }n7| j                            ||          }n| j                            ||          }|                    d          dk    rLdd|                    dd          ||pd|                    dg           |                    dg           d|<   n2d|                    dd          |                    dd          |d|<   # t          $ rW}| j        j	        
                    d| dt          |                      dddt          |           |d|<   Y d }~$d }~ww xY wt                    d k    o,t          fd!                                D                       }|rdnd|d"S )#NTr   r9   Fr  r  r`  r\  r  r  zBad Request, missing queryr  rY  rZ  r[  ru   rp   rL   zReconnection failed: r8   ri  )rk  ru   r8   r`  zQuery executed successfullyr  columns_namesrs  )rk  ru   r8   r`  r\  r  rs  zQuery execution failedzQuery execution error for rI  zQuery execution error: r   c              3   4   K   | ]}|         d          V  dS rq  r4  rr  s     r    r   z8APIFunctions.execute_dual_mysql_query.<locals>.<genexpr>  rt  r"   ru  )r   r   r  r   r   rW  r_  r   r   r   r\   rA   rx  ry  r   )r   r<   r   r`  r\  r  r  rz  r{  r  rY  rZ  r[  dbre  rf  r  r   rv  rs  s                      @r    execute_dual_mysql_queryz%APIFunctions.execute_dual_mysql_query?  s   ''t'44 	#(:     ))##J//  ##E2..##E2.. 	#(3   
 
 !% 8	 8	Hg7 O%kk)44G";;v..D&{{:66H Z00<HB S4 SH S'3DO'D'D)5)=)=gtXWY)Z)Z)--f55<<+0(6(:(:63(G(G'k~?Q?QRWYh?i?i'k'k).	1 1GH- % ".!;!;E8!L!L "&!3!A!A%!R!R "/==eXNNF::f%%,,#' #%zz%1NOO!&$,$5)/OR)H)H#)::i#<#<) )GH%% $) &

63 7 7%zz%1IJJ!&	) )GH%    &,,-^(-^-^VYZ[V\V\-^-^___$=SVV=="	% %!!!!!! g,,*es2e2e2e2eV]VbVbVdVd2e2e2e/e/e +3CC.#
 
 	
s!    CI!:C%I!!
K+AJ==K)r   )Nrp   T)N)NNN)%__name__
__module____qualname__r!   r1   rJ   ro   rC   r   r]   rU   r   r   r   r   r   r   r   r  r  r'  r*  r/  r8  r=  r  r)  r  r#  r  r]  r_  rg  r|  r  r  r  r4  r"   r    r   r      sg       " " " "$	U 	U 	U
 
 
.NK NK NKb BF'" '" '" '"R
R 
R 
R' ' '  ,% ,% ,%^	D 	D 	D  2# # #
! 
! 
!! ! !% % %N5
 5
 5
n
 
 
$L
 L
 L
^#
 #
 #
L!
 !
 !
H
 
 
8
 
 
4
 
 
@C C C   *  "# # #L   (   (   &S
 S
 S
lh
 h
 h
VB
 B
 B
J^
 ^
 ^
 ^
 ^
r"   r   )r,   r   r   rW   r   r   r   flaskr   functions.versionr   lib.api_loggerr   lib.athenz_clientr   lib.s3_clientr   products.dbsr   r   r	   r
   r   r   r   r4  r"   r    <module>r     s   				          ! ! ! ! ! ! # # # # # # $ $ $ $ $ $ * * * * * * " " " " " " { { { { { { { { { { { { { { { {K
 K
 K
 K
 K
 K
 K
 K
 K
 K
r"   