
    5                        S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	J
r
  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SS0rSr\R2                  \R4                  " \R6                  R8                  \R6                  R:                  5       " S S\R<                  5      5       5       r\R2                  \R4                  " \R6                  R@                  5       " S S\R<                  5      5       5       r!g)z&recommender API insights list command.    )absolute_import)division)unicode_literalsN)
exceptions)client_util)insight)insight_types)	locations)arg_parsers)base)flags)logEXAMPLESz
        To list all insights for a billing account:

          $ {command} --project=project-id --location=global --insight-type=google.compute.firewall.Insight
        Q  
        table(
          name.basename(): label=INSIGHT_ID,
          category: label=CATEGORY,
          stateInfo.state: label=INSIGHT_STATE,
          lastRefreshTime: label=LAST_REFRESH_TIME,
          severity: label=SEVERITY,
          insightSubtype: label=INSIGHT_SUBTYPE,
          description: label=DESCRIPTION
        )
    c                   d    \ rS rSrSr\r\S 5       rS r	S r
S\4S jrS rS	 rS
 rS rS rSrg)List8   a  List insights for Google Cloud resources.

This command lists all insights for the specified Google Cloud resource,
location, and insight type. If an insight type or location is not specified,
insights for all supported insight types or locations, respectively, are
listed. If the `--recursive` flag is set, insights for child resources and
projects are also listed. Supported insight types can be found here:
https://cloud.google.com/recommender/docs/insights/insight-types.
c                     [         R                  " U 5        U R                  SSSSS9  U R                  SSSSS9  U R                  S	[        R                  SS
S9  U R
                  R                  [        5        g)Args is called by calliope to gather arguments for this command.

Args:
  parser: An argparse parser that you can use to add arguments that go on
    the command line after this command.

--locationLOCATIONFzLocation to list insights for. If no location is specified, insights for all supported locations are listed. Not specifying a location can add 15-20 seconds to the runtime.metavarrequiredhelp--insight-typeINSIGHT_TYPEa$  Insight type to list insights for. If no insight type is specified, insights for all supported insight types are listed. Supported insight types can be found here: https://cloud.google.com/recommender/docs/insights/insight-types Not specifying an insight-type can add a minute to the runtime.z--recursivea  
            In addition to listing the insights for the specified organization
            or folder, recursively list all of
            the insights for the resource's child resources, including
            their descendants (for example, a folder's sub-folders), and for
            the resource's child projects. For example, when using the
            `--recursive` flag and specifying an organization, the response
            lists all of the insights associated with that
            organization, all of the insights associated with that
            organization's folders and sub-folders, and all of the
            insights associated with that organization's child
            projects. The maximum number of resources (organization,
            folders, projects, and descendant resources) that can be accessed at
            once with the `--recursive` flag is 100. For a larger
            number of nested resources, use
            [BigQuery export](https://cloud.google.com/recommender/docs/bq-export/export-recommendations-to-bq).
            Using `--recursive` can add 15-20 seconds per resource to the
            runtime.
            )actionr   r   N)r   AddParentFlagsToParseradd_argumentr   StoreTrueFalseActiondisplay_info	AddFormatDISPLAY_FORMATparsers    (lib/surface/recommender/insights/list.pyArgs	List.ArgsG   s     
  (
O  	 O   //	  0 !!.1    c                     SUl         SS/Ul        SUl        SUl        UR                  (       a  SUR                  -   Ul        UR                  (       a  SUR                  -   Ul        UR                  (       a  SUR                  -   Ul        U$ )zSetups up args to search all resources under a project, folder, or organization.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
    with.

Returns:
  (argparse.Namespace) args with additional parameters setup
*+cloudresourcemanager.googleapis.com/Project*cloudresourcemanager.googleapis.com/Folder
createTimez	projects/zorganizations/zfolders/)	read_maskasset_typesorder_byqueryprojectscopeorganizationfolder)selfargss     r'   setArgsList.setArgs   s{     DN 	64	D !DMDJ||-dj#d&7&77dj{{+djKr*   c                 :    [        U[        5      (       a  US   $ U$ )Nr   )
isinstancelist)r8   asset_ins     r'   read	List.read   s    (D!!a[or*   returnc                 Z    XR                   ;  a  U R                   R                  U5        gg)NTF)resource_locationsappend)r8   resource_locations     r'   AddResourceList.AddResource   s)     7 77
$$%67r*   c                 j   U R                  U5      n[        R                  " [        R                  5      n[	        UR                  U5      5      n/ U l        U GHK  nU R                  UR                  5       SUR                   3nSU;  a  U R                  U5        UR                  S:X  a9  U R                  U R                  UR                  5       SUR                   35        UR                  S:X  a~  U R                  U R                  UR                  5       SUR                   35      (       a@  U R                  UR                  5      Ul        UR                  UR                  U5      5        [        U R                  5      S:  d  GM8  [         R"                  " S5      e   U R                  $ )zSearch all nested resources under a project, folder, or organization.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
    with.

Returns:
  (List): a list of all Google Cloud resource,location pairs
/locations/r4   r-   r.   d   zThe maximum number of resources (organizations, folders, projects, and descendant resources) that can be accessed to list insights is 100. To access a larger number of resources, use BigQuery Export.)r:   r   AssetSearchClientDEFAULT_API_VERSIONr>   SearchAllResourcesrD   r@   r5   locationrG   	assetTyper4   foldersextendlenr   UnsupportedOperationError)r8   r9   client	resourcesrparent_resources         r'   searchAllResourcesList.searchAllResources   se    <<D**;+J+JKFV..t45I D99TZZ01QZZLIo	/	)) 
E	EDIIaii01QZZLIJ ++E
E$))AII"6!7{1::,OPPYYqyy)
22489	T$$	%	+22B
 	
% 2 """r*   c                    [         R                  R                  S5        UR                  (       a  U R	                  U5      nU$ UR
                  c  [        R                  " U R                  5       5      nUR                  UR                  UR                  UR                  UR                  UR                  S9 Vs/ s H  nUR                  PM     nnU$ [         R"                  " U5      SUR
                   3-   /nU$ s  snf )a  Iterate through search all resources response and collects unique Google Cloud resouce,location pairs.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
    with.

Returns:
  (List): a list of all Google Cloud resource,location pairs
z2Collecting Resources... This may take some time...)r4   r6   r7   billing_accountrJ   )r   statusPrint	recursiverY   rO   r
   CreateClientReleaseTrackr   	page_sizer4   r6   r7   r\   namer   GetResourceSegment)r8   r9   rD   
loc_clientlocs        r'   CollectAssetsList.CollectAssets   s     JJIJ~~2248" ! 
	))$*;*;*=>j  __nnll,,[["22 % 	c ((  	  
"
"4
([+H
H 	s   <C?c                     [         R                  " U R                  5       5      nUR                  UR                  5      nU Vs/ s H  oDR
                  PM     sn$ s  snf )zList all Insight Types.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
    with.

Returns:
  (list) all insight-types in a list of strings
)r	   r`   ra   r   rb   rc   )r8   r9   insighttypes_clientinsighttypes_responseresponses        r'   ListInsightTypesList.ListInsightTypes   sO     (44T5F5F5HI/44T^^D*?@*?hMM*?@@@s   Ac                    / n[         R                  " U R                  5       5      nSnSnU H  u  pxn	Xu:w  d  X:w  a%  [        R                  R                  SU SU 35        UnUnSR                  XxU	/5      n
UR                  XR                  5      n [        U5      n[        R                  " X<4U5      nM     U$ ! [        R                  [        R                  [        4 a     M  f = f)a7  Collects all insights for a given Google Cloud Resource.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
    with.
  asset_insight_types: list, The list of Google Cloud resource insight types
    URL to collect insights

Returns:
  (Insights) a iterator for all returned insights
NzReading Insights for:  /)r   r`   ra   r   r]   r^   joinr   rb   nextapitools_exceptionsHttpBadRequestErrorBadStatusCodeErrorStopIteration	itertoolschain)r8   r9   asset_insight_typesinsightsinsights_clientresource_prevlocation_prevresourcerO   insight_typeparent_namenew_insightspeeks                r'   GetInsightsList.GetInsights  s     H**4+<+<+>?OMM,?(L		"h&?

1(1XJGHmmHHh,?@k$))
~~lL! 
G\h# -@* O 
1
1

0
0
 
 	s   B>>)C+*C+c           
         U R                  U5      n/ nU H  nUR                  S5      nSR                  USS 5      nSR                  USS 5      nUR                  b"  UR	                  XgSUR                   345        Mk  UR                  U R                  U5       Vs/ s H
  nXgSU 34PM     sn5        M     U R                  X5      $ s  snf )zRun 'gcloud recommender insights list'.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
    with.

Returns:
  The list of insights for this Google Cloud Resource.
rq   N      zinsightTypes/)rg   splitrr   r   rE   rR   rm   r   )	r8   r9   rD   rz   assettokensr   rO   rc   s	            r'   RunList.Run+  s     ++D1 #{{3f&!*%h&1+&h				&""=1B1B0C!DE	
 	""--d3$
3 =!783$
 	 $ D66$
s   C
)rD   N)__name__
__module____qualname____firstlineno____doc__DETAILED_HELPdetailed_helpstaticmethodr(   r:   r@   boolrG   rY   rg   rm   r   r   __static_attributes__ r*   r'   r   r   8   sT      -62 62p:d )#VBA%N7r*   r   c                   2    \ rS rSrSr\r\S 5       rS r	Sr
g)ListOriginaliL  ai  List insights for a Google Cloud entity.

This command lists all insights for a given Google Cloud entity, location, and
insight type. Supported insight-types can be found here:
https://cloud.google.com/recommender/docs/insights/insight-types. Currently
the following Google Cloud entity types are supported: project,
billing_account, folder, and organization.
c                     [         R                  " U 5        U R                  SSSSS9  U R                  SSSSS9  U R                  R	                  S	5        g
)r   r   r   TzLocation to list insights for. r   r   r   zInsight type to list insights for. Supported insight-types can be found here: https://cloud.google.com/recommender/docs/insights/insight-typesr   N)r   r   r    r"   r#   r%   s    r'   r(   ListOriginal.ArgsZ  sp     
  (
.	   O  	 !! 
# 
	r*   c                     [         R                  " U R                  5       5      n[        R                  " U5      nUR                  X1R                  5      $ )zRun 'gcloud recommender insights list'.

Args:
  args: argparse.Namespace, The arguments that this command was invoked
    with.

Returns:
  The list of insights for this project.
)r   r`   ra   r   GetInsightTypeNamer   rb   )r8   r9   r|   r   s       r'   r   ListOriginal.Run  sB     **4+<+<+>?O**40K^^<<r*   r   N)r   r   r   r   r   r   r   r   r(   r   r   r   r*   r'   r   r   L  s&      -"	 "	H=r*   r   )"r   
__future__r   r   r   rx   apitools.base.pyr   rt   googlecloudsdk.api_lib.assetr   "googlecloudsdk.api_lib.recommenderr   r	   r
   googlecloudsdk.callioper   r   &googlecloudsdk.command_lib.recommenderr   googlecloudsdk.command_lib.rungooglecloudsdk.corer   r   r$   DefaultUniverseOnlyReleaseTracksra   ALPHABETAListCommandr   GAr   r   r*   r'   <module>r      s    - &  '  > 4 6 < 8 / ( 8 5 #  
 D%%++T->->-C-CDO74 O7 E O7d D%%(()?=4## ?= * ?=r*   