� ��fc@s3ddlmZddlmZmZddlmZmZddlm Z ddl m Z ddl m Z ddlmZddlmZdd lZdd ljjZdd ljjZdd ljjZdd ljjZdd lZdd lZdd lZd Zej d �Z!d efd ��YZ"d S(i����(tOption(t RequestErrortConnectionError(tEmptyValueErrort_(t LaunchHelper(t Constants(t GenericPrompt(tPlugin(t AddAttachmentNs&Spenser Shumaker <sshumake@redhat.com>s$redhat_support_tool.plugins.add_casetOpenCasecBs�eZdZdZdZdZdZdZe d��Z e d��Z e d��Z e d��Z d�Zd�Zd�Zd �Zd �Zd �Zd �Zd �Zd�Zd�Zd�Zd�ZRS(topencasesRed Hat Enterprise LinuxcCs td�S(s� The usage statement that will be printed by OptionParser. Example: - %prog -c CASENUMBER [options] <comment text here> Important: %prog is a OptionParser built-in. Use it! s?%prog -s SUMMARY -p PRODUCT -v VERSION -d DESCRIPTION [options](R(tcls((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt get_usage2s cCstd�|jS(s� The description statement that will be printed by OptionParser. Example: - 'Use the '%s' command to add a comment to a case.' % cls.plugin_name s0Use the '%s' command to open a new support case.(Rt plugin_name(R ((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pytget_desc>s cCstd�|jS(s The epilog string that will be printed by OptionParser. Usually used to print an example of how to use the program. Example: Examples: - %s -c 12345678 Lorem ipsum dolor sit amet, consectetur adipisicing - %s -c 12345678 s\Examples: - %s -s "Case Summary" -p "Red Hat Virtualization" -v 3.0 -d "Case description" (RR(R ((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt get_epilogJs cCs*tdddddtd�dd&�tdd dd dtd �dd&�td d dddtd�dd&�tdddddtd�dd&�tdddddtd�dd&�tdddddtd�dd&�tdddddd dtd!�dt�td"d#dd$dtd%�dd&�gS('sO Subclasses that need command line options should override this method and return an array of optparse.Option(s) to be used by the OptionParser. Example: return [Option("-f", "--file", action="store", dest="filename", help='Some file'), Option("-c", "--case", action="store", dest="casenumber", help='A case')] Would produce the following: Command (? for help): help mycommand Usage: mycommand [options] Use the 'mycommand' command to find a knowledge base solution by ID Options: -h, --help show this help message and exit -f, --file Some file -c, --case A case Example: - mycommand -c 12345 -f abc.txt s-ps --producttdesttproductthelps7The product the case will be opened against. (required)tdefaults-vs --versiontversionsFThe version of the product the case will be opened against. (required)s-ss --summarytsummarys!A summary for the case (required)s-ds --descriptiont descriptions&A description for the case. (required)s-Ss --severitytseveritys$The severity of the case. (optional)s-as --attachmentt attachments6Add an attachment to the newly opened case. (optional)s-os --obfuscatet obfuscatetactiont store_trues}Obfuscate the attachment by running it through soscleaner to clean hostnames and IP addresses (requires root user privileges)s-gs --casegroupt casegroups6Add this case to the designated case group. (optional)N(RRtNonetFalse(R ((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt get_optionsYs0        cCs�td�|j}d|_t}|jdsatj�sO|GHt|��n|j |�}ntj �|_|s�t}xS|jD]H}|j �j �|jdj �kr�t }|j �|jd<Pq�q�W|s�td�}|GHt|��q�ndS(NsERROR: %s requires a product.Rs ERROR: Invalid product provided.(RRRt _productsAryRt_optionstcommontis_interactivet Exceptiont_interactive_product_selectiont get_productstget_nametlowertTrue(tselftmsgt been_verifiedtinArrayR((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt_check_product�s*     % cs tj�|_tj�}|jdd��t}�dksK�dkrW|j�nt �fd�|jD�d�}|dk r|dkrt t d���}t |�j �j�dkr�|j�|jd<t}t}n|r|j|||�}qn|S(Ntoptiontdefault_producttc3s'|]}|j��kr|VqdS(N(R((t.0tval(R1(sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pys <genexpr>�ss5Do you want to use the default product - "%s" (y/N)?:tyR(R#R'R!t confighelpertget_config_helpertgetR*Rtdefault_product_nametnextt raw_inputRtstrtstripR)R(R"Rt_prompt_for_product(R+R-tcfgtneeds_user_inputt searched_itemtline((R1sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyR&�s"  "   cCstj|j�x�tr�ttd��}t|�j�dkrOt��nyt |�}Wntd�GHqnX|t dt |j�d�kr�|dkr�|j|dj �|j d<t}Pqtd�GHqW|j d|j �kr |j|j d�n|S(Ns*Please select a product (or 'q' to exit): tqs!ERROR: Invalid product selection.iR2R(R#tprint_productsR!R*R;RR<R=R%tinttrangetlenR(R"t_prompt_for_default_product(R+R-R?RARB((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyR>�s$   .cCs�td�|j}t}d}x:|jD]/}|jd|j�kr)|j�}Pq)q)W|jdsYtj �rEtj |�x�t rAt td��}t |�j�dkr�t��nyt|�}Wntd�GHq�nX|tdt|�d�kr3|dkr3||d|jd<t }Pq�td�GHq�WqY|GHt|��n|s�t}xD|D]<}|j�|jdj�krlt }||jd<PqlqlW|s�td �}|GHt|��q�ndS( NsERROR: %s requires a version.RRs*Please select a version (or 'q' to exit): RCs!ERROR: Invalid version selection.iR2s ERROR: Invalid version provided.(RRRRR!R"R(t get_versionsR#R$tprint_versionsR*R;R<R=R%RERFRGR)(R+R,t beenVerifiedtversionsRRBR.R((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt_check_version�sL         +   cCs�td�|j}|jds�tj�r�x�tr�ttd��}yt|�j�}Wntd�GHnXt|�j�dkr�t ��n|dkr�td�GHq/||jd<Pq/Wq�|GHt |��ndS(NsERROR: %s requires a summary.Rs)Please enter a summary (or 'q' to exit): s!ERROR: Invalid summary selection.RCR2( RRR"R#R$R*R;R<R=R%(R+R,RB((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt_check_summary s$        cCs�td�|j}|jds�tj�r�x�tr�g}y,td�GHxtre|jt��qLWWq/tk r�t dj |��j �}|dkr�td�GHq�|j d�|jd<Pq/Xq/Wq�|GHt |��ndS(Ns!ERROR: %s requires a description.RsCPlease enter a description (Ctrl-D on an empty line when complete):s R2sERROR: Invalid description.tutf_8(RRR"R#R$R*tappendR;tEOFErrorR<tjoinR=tdecodeR%(R+R,t userinputR((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt_check_description#s&          cCsctd�}|jds�tj�r_tj�}tj|�x�tr�ttd��}t|�j �dkr}t ��nyt |�}Wn|GHqAnX|t dt |�d�kr�|dkr�||d|jd<PqA|GHqAWq_nmt}xJtj�D]<}|jdj�|j�krt}||jd<PqqW|s_td�}|GHndS(Ns!ERROR: Invalid severity selectionRs+Please select a severity (or 'q' to exit): RCiR2s"ERROR: Invalid severity specified.(RR"R#R$tget_severitiestprint_severitiesR*R;R<R=R%RERFRGRR)(R+R,t severitiesAryRBtmatchR((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt_check_severity=s6       +  cCs�|jds)tj�r�|j�q�n�t}x�tj�D]�}|jdj�|j�j�kr<t}|j�|jd<|j �|jd<t j t j d|jd|jdf�Pq<q<W|s�td�}|GHndS(NRtcasegroupnumbers!Casegroup(%s) casegroupnumber(%s)s$ERROR: Invalid case group specified.(R"R#R$t_interactive_case_group_actionRt get_groupsR)R(R*t get_groupNumtloggertlogtloggingtINFOR(R+RYtgroupR,((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt_check_case_group_s$     cCs�tj�}td�|D�d�}|dks=|dkrYtd�|D�d�}n|dk sq|dkr�ttd�|j��}t|�j�j �dkr�|j |�q�|j�|j d<|j �|j d<n |j |�dS(Ncss!|]}|j�r|VqdS(N(t get_isDefault(R3R4((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pys <genexpr>wsR2css'|]}|j�dkr|VqdS(s-1N(R^(R3R4((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pys <genexpr>zss:Would you like to use the default (%s) Case Group (y/N)? :tnRR[( R#R]R:RR;RR(R<R=R)t_prompt_for_case_groupR"R^(R+t groupsArytdefault_case_groupRB((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyR\ts  cCstd�}tj|�x�trttd��}t|�j�dkrXt��nyt|�}Wn|GHqnX|t dt |�d�kr |dkr ||dj �|j d<||dj �|j d<tjtjd|j d|j df�Pq|GHqWdS( Ns#ERROR: Invalid case group selections-Please select a case group (or 'q' to exit): RCiR2RR[s!Casegroup(%s) casegroupnumber(%s)(RR#t print_groupsR*R;R<R=R%RERFRGR(R"R^R_R`RaRb(R+RhR,RB((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyRg�s(    +  cCs@|j�|j�|j�|j�|j�|j�dS(N(R/RMRNRURZRd(R+((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt validate_args�s      cCs�d}|jdrPtj|jd�|jd<tj|jd�|jd<ny5tj�}|jj�}|jd|_|jd|_ |jd|_ |jd|_ |jd|_ |jdr�|jd|_ ntj�r�ttd ��}t|�j�j�}|d kr�|jj|�}tj|�\}}tt�}|jd |d td ��ttd��}|j�dkr�td�GHdSq�n|jj|�}|j�dkr�td�} | GHt| ��n|j�|_dt j!tdt j"�t j#fGHtd�|j} | GHt$j%t&j'| �|jdr�tj(t)�tt*�}|jd|jt+j,j-|jd�|jdr�dnd |jdf�n3t+j.�dkr�t/j0j1td�|j�t�}t|�j�}|d kr�t2j3ddgdt2j4dt2j5�} | j6�dj7d�} x|| D]q} d| kr^t| �j�} tj(t)�tt*�}|jd |j|jdr�dnd | f�Pq^q^Wq�ntd!�|j|jfGH|jds�ttd"�|j�}t|�j�}|d kr�tj(t)�tt*�}|jd#|j|jdrsdnd f�q�nWn.t8k r�}td$�t|�} | GHt$j%t&j9| ��n�t:k r'}td%�|j;} | GHt$j%t&j9| �|j;d&kr!tj<�n�n�t=k r~}td'�} | GHt|�j7d(�}|d)GHt$j%t&j9| ��n8tk r�td*�} | GHt$j%t&j9| ��nXdS(+NRRRRRRRR[saWould you like to see if there's a solution to this problem before opening a support case? (y/N) R5R2tprompts-Selection (q returns to case creation menu): s5Would you still like to open the support case? (Y/n) RfsSupport case not created.s.ERROR: There was a problem creating your case.s%s%s%st-s.Support case %s has successfully been opened. Rs -c %s -d '[RHST] File %s ' %s %ss-oisgWould you like Red Hat Support Tool to automatically generate and attach a SoS report to %s now? (y/N) t sosreports--batchtstdouttstderrs s.tar.s -c %s %s %ss�Please attach a SoS report to support case %s. Create a SoS report as the root user and execute the following command to attach the SoS report directly to the case: redhat-support-tool addattachment -c %s <path to sosreport> s:Would you like to attach a file to %s at this time? (y/N) s -c %s %s -rs ERROR: %ss?Unable to fulfill request with support services API. Reason: %st UnauthorizedsZProblem connecting to the support services API. Is the service accessible from this host?s, isUnable to create case.(>RR"R#tobfuscate_textt apihelpertget_apitimtmakeCaseRRRRRt groupNumberR$R;RR<R=R)tproblemst diagnoseCasetrecommendationpromptertgenerate_metadataRRtruntcasestaddtget_caseNumberR%t _caseNumberRtBOLDtMAX_RULEtENDR_R`RaRbtset_interactiveR*R tostpathtbasenametgeteuidtsysRotwritetsubtPopentPIPEtSTDOUTt communicatetsplitRtWARNINGRtreasont remove_userR(R+tapitcaseRBtrecommendationst recprompttrecdoctlhtcsR,tptoutputtoutR�tevetreterrmsgtemg((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pytnon_interactive_action�s�                                 (       N(t__name__t __module__RRtcommentRR!R�R9t classmethodR RRR R/R&R>RMRNRURZRdR\RgRkR�(((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyR *s,  :    0   "    (#toptparseRt(redhat_support_lib.infrastructure.errorsRRt(redhat_support_tool.helpers.confighelperRRt(redhat_support_tool.helpers.launchhelperRt%redhat_support_tool.helpers.constantsRt4redhat_support_tool.helpers.genericinteractivepromptRtredhat_support_tool.pluginsRt*redhat_support_tool.plugins.add_attachmentR R�t%redhat_support_tool.helpers.apihelperthelpersRst"redhat_support_tool.helpers.commonR#t2redhat_support_tool.helpers.recommendationprompterRzR6t subprocessR�RaR�t __author__t getLoggerR_R (((sI/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.pyt<module>s$