
    v                        d 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	 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 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ZdZdZdZdZ dZ!dddZ"dZ# G d dejH                        Z%efdZ&efdZ'd Z(d Z)efd Z*efd!Z+efd"Z,efd#Z- G d$ d%e.      Z/ G d& d'e.      Z0 G d( d)e.      Z1 G d* d+e.      Z2 G d, d-e.      Z3 G d. d/e.      Z4 G d0 d1e.      Z5 G d2 d3e.      Z6 G d4 d5e.      Z7 G d6 d7e.      Z8 G d8 d9e.      Z9 G d: d;e.      Z: G d< d=e.      Z;y)>z6Shared utilities for access the CloudAsset API client.    )absolute_import)division)unicode_literals)encoding)
exceptions)
list_pager)apis)utils)	arg_utils)labels_util)repeated)log)yaml)timesN
cloudassetv1	v1p1beta1	v1p5beta1	v1p7beta1z!application/x-www-form-urlencodedGET)zContent-TypezX-HTTP-Method-Overridez=HTTP request failed with status code {}. Response content: {}c                       e Zd ZdZy)MessageDecodeErrorz7Error raised when a failure to decode a message occurs.N)__name__
__module____qualname____doc__     /lib/googlecloudsdk/api_lib/asset/client_util.pyr   r   4   s    ?r   r   c                 6    t        j                  t        |       S )zImport and return the cloudasset messages module.

  Args:
    version: the API version

  Returns:
    cloudasset message module.
  )r	   GetMessagesModuleAPI_NAMEversions    r   GetMessagesr%   8        
		'	22r   c                 6    t        j                  t        |       S )zImport and return the cloudasset client module.

  Args:
    version: the API version

  Returns:
    cloudasset API client module.
  )r	   GetClientInstancer"   r#   s    r   	GetClientr)   D   r&   r   c                 L    | dk(  ry| dk(  ry| dk(  ry| dk(  ry| d	k(  ry
| dk(  ryy)zTranslate content type from gcloud format to API format.

  Args:
    content_type: the gcloud format of content_type

  Returns:
    cloudasset API format of content_type.
  resourceRESOURCEz
iam-policy
IAM_POLICYz
org-policy
ORG_POLICYzaccess-policyACCESS_POLICYzos-inventoryOS_INVENTORYrelationshipRELATIONSHIPCONTENT_TYPE_UNSPECIFIEDr   )content_types    r   ContentTypeTranslationr5   P   sI     Z\!\!_$^#^#	#r   c                     | dk(  ry| dk(  ryy)Nz	read-time	READ_TIMEzrequest-timeREQUEST_TIMEPARTITION_KEY_UNSPECIFIEDr   )partition_keys    r   PartitionKeyTranslationr;   h   s    k!n$	$r   c           
   #     K   t        |      }t        j                  | j                  |j                  j
                        }t        j                  | j                  | j                        }t        j                  | j                        }d}| j                  d      rt        j                  | j                        }|j                  |j	                  | j                   | j"                  ||||            }|j$                  D ]  }	|	  yw)z-Manually make the get assets history request.Nend_time)
assetNamesrelationshipTypescontentTypeparentreadTimeWindow_endTimereadTimeWindow_startTime)r%   r   ChoiceToEnumr4   &CloudassetBatchGetAssetsHistoryRequestContentTypeValueValuesEnumasset_utilsGetParentNameForGetHistoryorganizationprojectr   FormatDateTime
start_timeIsSpecifiedr=   BatchGetAssetsHistoryasset_namesrelationship_typesassets)
argsserviceapi_versionmessagesr4   rA   rL   r=   responseassets
             r    MakeGetAssetsHistoryHttpRequestsrX   p   s      %(''
HH!!#, 11$2C2C26,,@&##DOO4*(	j!##DMM2H**55%% 33"!)#- 6 	( e
K s   DDc              #   B  K   | j                   D ]  }i }|j                  |j                  d}||d<   g |d<   |j                  D ]}  }i }|j                  j
                  |d<   |j                  |d<   |j                  |d<   |t        k(  r%|j                  r|j                  j                  |d<   |d   j                  |        |  | j                  D ]  }i }|j                  j                  |j                  j                  |j                  j                   d}||d	<   g |d
<   |j"                  D ]c  }	i }
|	j$                  |
d<   |	j                  |
d<   |	j
                  |
d<   |	j                  |
d<   |	j&                  |
d<   |d
   j                  |
       e |  yw)zGRenders the analysis query and results of the AnalyzeIamPolicy request.)attachedResourcebindingpolicyACLs
identitiesaccesses	resourcesconditionEvaluationValue)r+   accessidentityaccess_tupledeny_details	deny_ruleexception_identitiesN)analysisResultsattachedResourceFullName
iamBindingaccessControlListsidentityListr^   r_   r`   DEFAULT_API_VERSIONconditionEvaluationevaluationValueappenddenyAnalysisResultsaccessTupler+   rb   rc   denyDetailsdenyRuleexceptionIdentities)analysisrT   analysis_resultentryr\   aclaclsdeny_analysis_resultrd   detaildeny_details              r   "_RenderAnalysisforAnalyzeIamPolicyr~      s     "11oE ,DD"--F E(OE&M11d*77BBd<d:--d;	+	+0G0G*-*A*A*Q*Q 	&	(Fm4  2 K) 2, '::E )44==&2299(44==L
 )E.E.&22k!'k+!'!1!1k+"("3"3k, &k*,2,F,Fk()N"";/ 3 K) ;s   FFc              #     K   | j                   rd}nd}d}| j                  j                  rd}|s#|r!| j                  D ]  }|j                  sd} n |s|dz  }n|dz  }t	        | j                  |      D ]  }|  |r8| j                  D ])  }d|j
                  i}| t	        ||      D ]  }|  + t        j                  j                  |       yw)	z5Renders the response of the AnalyzeIamPolicy request.z)Your analysis request is fully explored. zpYour analysis request is NOT fully explored. You can use the --show-response option to see the unexplored part. FTzNo matching ACL is found.zbThe ACLs matching your requests are listed per IAM policy binding, so there could be duplications.z,Service Account Impersonation Analysis QueryN)	fullyExploredmainAnalysisrh   #serviceAccountImpersonationAnalysisr~   analysisQueryr   statusPrint)	rV   %analyze_service_account_impersonationrT   msghas_resultssa_impersonation_analysisrx   rv   titles	            r   "_RenderResponseforAnalyzeIamPolicyr      s     
 
5CAC +**K
@%-%Q%Q!	"	2	2 &R
 
&&C 0 1C 2(2G2G2=?e
K? +@@
8(:P:Pe k5hL% M A **3s   ACBCc                    t        j                  | j                  | j                  | j                        }| j                  d      r| j                  nd}| j                  d      r| j                  nd}| j                  d      r| j                  ng }| j                  d      r| j                  ng }| j                  r| j                  nd}	| j                  r| j                  nd}
| j                  r| j                  nd}| j                  r| j                  nd}| j                  r| j                  nd}| j                  d      r| j                   nd}d}| j"                  r.| j$                  st'        j(                  dd      | j"                  }d}| j*                  r.| j$                  st'        j(                  d	d      | j*                  }d}| j                  d
      rt-        | j.                        dz   }d}| j                  d      rt1        j2                  | j4                        }|j7                  |j9                  |||||	|
|||||||||            }| j$                  st;        |||      S |S )z-Manually make the analyze IAM policy request.full_resource_nameNrc   rolespermissionsinclude_deny_policy_analysisz--output-resource-edgesz9Must be set together with --show-response to take effect.z--output-group-edgesexecution_timeoutsaccess_time)(analysisQuery_accessSelector_permissions"analysisQuery_accessSelector_roles'analysisQuery_identitySelector_identity8analysisQuery_options_analyzeServiceAccountImpersonation"analysisQuery_options_expandGroups%analysisQuery_options_expandResources!analysisQuery_options_expandRoles/analysisQuery_options_includeDenyPolicyAnalysis&analysisQuery_options_outputGroupEdges)analysisQuery_options_outputResourceEdges/analysisQuery_resourceSelector_fullResourceName)analysisQuery_conditionContext_accessTimeexecutionTimeoutscopesavedAnalysisQuery)rG    GetParentNameForAnalyzeIamPolicyrI   rJ   folderrM   r   rc   r   r   expand_groupsexpand_resourcesexpand_rolessaved_analysis_queryr   IsKnownAndSpecifiedr   output_resource_edgesshow_responsegcloud_exceptionsInvalidArgumentExceptionoutput_group_edgesstrr   r   rK   r   AnalyzeIamPolicy!CloudassetAnalyzeIamPolicyRequestr   )rR   rS   rU   rT   rA   r   rc   r   r   r   r   r   r   r   r   r   r   r   r   rV   s                       r    MakeAnalyzeIamPolicyHttpRequestsr      s   
 778I8I8<8<E& 372B2B3t..!%  #..z:T]](((1$**r%$($4$4]$C  +(,(:(:$$$-.2.C.CT**&*&7&7""T,6:6O6O22UYX\  YC  YC$*T*T  IM' 
	!	!"@	A ''  	66
#
EG G !66	66
 
EG G 00	)*D223c9+	m,&&t'7'78K%%003>-22:Ch-:0@,8:V1C4I:L4?,1 1 (& 
		-7F F	/r   c                        e Zd ZdZefdZd Zy)AnalyzeIamPolicyClientzClient for IAM policy analysis.c                 h    || _         t        |      | _        | j                  j                  | _        y NrT   r)   clientr   rS   selfrT   s     r   __init__zAnalyzeIamPolicyClient.__init__?  s%    "DK(DK;;>>DLr   c                 p    t        | j                        }t        || j                  || j                        S )z"Calls MakeAnalyzeIamPolicy method.)r%   rT   r   rS   )r   rR   rU   s      r   AnalyzezAnalyzeIamPolicyClient.AnalyzeE  s2    4++,H+D$,,,0,<,<> >r   Nr   r   r   r   rm   r   r   r   r   r   r   r   <  s    '!4 ">r   r   c                       e Zd ZdZddZd Zy)AssetExportClientzClient for export asset.Nc                     || _         t        | _        t        | j                        | _        |r|j
                  | _        y t        | j                        j
                  | _        y r   )rA   rm   rT   r%   message_moduler   r)   rS   )r   rA   r   s      r   r   zAssetExportClient.__init__O  sH    DK*D%d&6&67D &699DLId6F6F,G,J,JDLr   c           
      6   t        |j                        }t        |j                        }t	        | j
                  j                  j                  |      }|j                  s|j                  rL| j
                  j                  | j
                  j                  |j                  |j                              }n|j                  j                  j                         }| j
                  j                  | j
                  j                  d|j                   z   dz   |j"                  z   |j$                  |j&                  | j
                  j                  |      |j(                              }d}|j*                  rt-        j.                  |j*                        }t	        | j
                  j0                  j2                  |      }| j
                  j1                  |j4                  ||||j6                  	      }| j
                  j9                  | j:                  |
      }	 | j<                  j?                  |      }	|	S # t@        jB                  $ r}
tE        jF                  |
d      d}
~
wt@        jH                  $ r}tE        jF                  |d      d}~ww xY w)z+Export assets with the asset export method.)uri	uriPrefixgcsDestination	projects/
/datasets/)partitionKey)datasettableforcepartitionSpecseparateTablesPerAssetTypebigqueryDestinationN)
assetTypesr@   outputConfigreadTimer?   )rA   exportAssetsRequestz{error_info})error_format)%r5   r4   r;   r:   getattrr   PartitionSpecPartitionKeyValueValuesEnumoutput_pathoutput_path_prefixOutputConfigGcsDestinationCONCEPTSbigquery_tableParseBigQueryDestination	projectId	datasetIdtableIdforce_	per_type_snapshot_timer   rK   ExportAssetsRequestrF   asset_typesrP   CloudassetExportAssetsRequestrA   rS   ExportAssetsapitools_exceptionsHttpBadRequestErrorr   HttpExceptionHttpForbiddenError)r   rR   r4   r:   output_config
source_refr   export_assets_requestrequest_message	operationbad_requestpermission_denys               r   ExportzAssetExportClient.ExportU  s`   )$*;*;<L+D,>,>?M))EEM 422))66,,;;""d.E.E < G 7 Hm ==//557j))66"11EE!J$8$88<G""#&&KK //==, > .)- F 9 7 :m M**4+=+=>m//JJL !//CC## "11 D 3 ))GG{{0E H GO8,,++O<i  22 O$$[~NN11 8$$
8 88s$   H< <JI&&J<JJr   )r   r   r   r   r   r   r   r   r   r   r   L  s     K+r   r   c                   J    e Zd ZdZd Zd ZefdZd Zd Z	d Z
d Zd	 Zd
 Zy)AssetSavedQueriesClientzClient for asset saved queries.c                 6    dj                  t        |            S )Nz, )joinlist)r   keyss     r   DictKeysToStringz(AssetSavedQueriesClient.DictKeysToString  s    99T$Z  r   c           
      x   t        j                  |      }	 t        t        |j	                                     }|| j                  j	                         vrBt        dj                  || j                  | j                  j	                                           ||   }	 t        j                  | j                  |   |      }|S #  t        j                  dj                  || j                  | j                  j	                                           xY w#  t        j                  dj                  |            xY w)zQReturns a message populated from the JSON or YAML file on the specified filepath.z`Query file [{0}] is not a properly formatted YAML or JSON query file. Supported query type: {1}.z7query type {0} not supported. supported query type: {1}zEQuery file [{0}] is not a properly formatted YAML or JSON query file.)r   	load_pathnextiterr   r   BadFileExceptionformatr   supported_query_types	Exceptionr   PyValueToMessage)r   	file_pathfile_contentquery_type_strquery_content	query_objs         r   GetQueryContentFromFilez/AssetSavedQueriesClient.GetQueryContentFromFile  s4   >>),LID!2!2!456n T77<<>>
C
J
J##D$>$>$C$C$EFHI I !0M+++

$
$^
4mEi +I..3396##D$>$>$C$C$EF4HI I+..y)+ +s   "C #D AD&D9c                     || _         t        |      | _        t        |      j                  | _        d| j                  j                  i| _        y )NIamPolicyAnalysisQuery)rA   r%   r   r)   savedQueriesrS   r  r  r   rA   rT   s      r   r   z AssetSavedQueriesClient.__init__  sC    DK%k2D[)66DL $"5"5"L"L"Dr   c                    | j                  |j                        }| j                  j                  |      }t	        j
                  || j                  j                  j                        }| j                  j                  ||j                  |      }| j                  j                  | j                  ||j                        }| j                  j                  |      S )zCreate a SavedQuery.iamPolicyAnalysisQuerycontentdescriptionlabels)rA   
savedQuerysavedQueryId)r  query_file_pathr   QueryContentr   ParseCreateArgs
SavedQueryLabelsValuer  #CloudassetSavedQueriesCreateRequestrA   query_idrS   Create)r   rR   r  saved_query_content
arg_labelssaved_queryr   s          r   r"  zAssetSavedQueriesClient.Create  s    ,,I--::( ; *,,d!!,,88:J%%00#$$ 1 K
 ))MM{{{ N PO<<//r   c                     | j                   j                  dj                  | j                  |j                              }| j
                  j                  |      S )zDescribe a saved query.{}/savedQueries/{}name)r    CloudassetSavedQueriesGetRequestr  rA   r!  rS   Getr   rR   r   s      r   Describez AssetSavedQueriesClient.Describe  sL    ))JJ!((dmmD K FO<<O,,r   c                     | j                   j                  dj                  | j                  |j                              }| j
                  j                  |       y)zDelete a saved query.r'  r(  N)r   #CloudassetSavedQueriesDeleteRequestr  rA   r!  rS   Deleter,  s      r   r0  zAssetSavedQueriesClient.Delete  sI    ))MM!((dmmD N FOLL(r   c                     | j                   j                  | j                        }| j                  j	                  |      S )z"List saved queries under a parent.rA   )r   !CloudassetSavedQueriesListRequestrA   rS   Listr   r   s     r   r4  zAssetSavedQueriesClient.List  s:    ))KK{{ L O<<_--r   c                 ~   t         j                  j                  |      }| j                  j                  j                         }|j                         rg| j                  |      }|j                  | j                  j                  j
                  |j                        }|j                  r|j                  }|dfS |dfS )z!Get the updated labels from args.TF)r   DiffFromUpdateArgsr   r  r  MayHaveUpdatesr-  Applyr  needs_update)r   rR   labels_diffr  orig_resourcelabels_updates         r   GetUpdatedLabelsz(AssetSavedQueriesClient.GetUpdatedLabels  s    ""11$7K  ++779F!!#mmD)m!''



(
(
4
4m6J6JLm		#	#%%t|5=r   c                    d}d}|j                   r<| j                  |j                         }|dz  }| j                  j                  |      }d}|j                  r|j                  }|dz  }| j                  |      \  }}|r|dz  }| j                  j                  |||      }| j                  j                  dj                  | j                  |j                        ||	      }	| j                  j                  |	      S )
zUpdate a saved query. Nr  r  z,descriptionz,labelsr  r'  )r)  r  
updateMask)r  r  r   r  r  r?  r  "CloudassetSavedQueriesPatchRequestr  rA   r!  rS   Patch)
r   rR   update_maskr#  r  updated_descriptionupdated_labels
has_updater%  r   s
             r   UpdatezAssetSavedQueriesClient.Update  s   K..


 iYk //<<!* = , ,,^#k!%!6!6t!<NJYk%%00#' 1 K
 ))LL!((dmmD M  O <<o..r   N)r   r   r   r   r   r  rm   r   r"  r-  r0  r4  r?  rI  r   r   r   r   r     s7    '!8 *= 0"-)./r   r   c                   >    e Zd ZdZefdZd Zd Zd Zd Z	d Z
d Zy	)
AssetFeedClientzClient for asset feed.c                 f    || _         t        |      | _        t        |      j                  | _        y r   )rA   r%   r   r)   feedsrS   r  s      r   r   zAssetFeedClient.__init__  s(    DK%k2D[)//DLr   c                    t        |j                        }t        | j                  j                  j
                  |      }| j                  j                  | j                  j                  |j                              }| j                  j                  |j                  |j                  |j                        }| j                  j	                  |j                  |j                  ||||j                        }| j                  j!                  ||j"                        }| j                  j%                  | j&                  |      }| j(                  j+                  |      S )zCreate a feed.topicpubsubDestination
expressionr   r  r>   r   r@   feedOutputConfig	conditionr?   )feedfeedId)rA   createFeedRequest)r5   r4   r   r   FeedrF   FeedOutputConfigPubsubDestinationpubsub_topicExprcondition_expressioncondition_titlecondition_descriptionrO   r   rP   CreateFeedRequestrX  CloudassetFeedsCreateRequestrA   rS   r"  )r   rR   r4   feed_output_configfeed_conditionrX  create_feed_requestr   s           r   r"  zAssetFeedClient.Create  sO   )$*;*;<L4..33NN')L,,==--??## @ % > & ((--,,"".. . 0N ###### + 11 $ 3D --??$)) @ %))FF{{.A G CO<<//r   c                     | j                   j                  dj                  | j                  |j                              }| j
                  j                  |      S )zDescribe a feed.{}/feeds/{}r(  )r   CloudassetFeedsGetRequestr  rA   rX  rS   r+  r,  s      r   r-  zAssetFeedClient.Describe  sK    ))CC!!$++tyy9 D ;O<<O,,r   c                     | j                   j                  dj                  | j                  |j                              }| j
                  j                  |       y)zDelete a feed.ri  r(  N)r   CloudassetFeedsDeleteRequestr  rA   rX  rS   r0  r,  s      r   r0  zAssetFeedClient.Delete#  sH    ))FF!!$++tyy9 G ;OLL(r   c                     | j                   j                  | j                        }| j                  j	                  |      S )zList feeds under a parent.r2  )r   CloudassetFeedsListRequestrA   rS   r4  r5  s     r   r4  zAssetFeedClient.List)  s:    ))DD{{ E O<<_--r   c                    g }t        |j                        }t        | j                  j                  j
                  |      }dj                  | j                  |j                        }|j                  s|j                  r|j                  d       |j                  r|j                  d       |j                  s|j                  r|j                  d       |j                  s|j                  r|j                  d       |j                   s|j"                  r|j                  d       | j%                  |||      \  }}}dj'                  |      }| j                  j)                  | j                  j+                  |j                        	      }	| j                  j-                  |j                  |j                  |j                   
      }
| j                  j	                  ||||	|
|      }| j                  j/                  ||      }| j                  j1                  ||      }| j2                  j5                  |      S )zUpdate a feed.ri  r4   z+feed_output_config.pubsub_destination.topiczcondition.expressionzcondition.titlezcondition.description,rO  rQ  rS  rU  )rX  rB  )r)  updateFeedRequest)r5   r4   r   r   r[  rF   r  rA   rX  clear_content_typerp   r^  r`  clear_condition_expressionra  clear_condition_titlerb  clear_condition_description%UpdateAssetNamesTypesAndRelationshipsr   r\  r]  r_  UpdateFeedRequestCloudassetFeedsPatchRequestrS   rD  )r   rR   update_masksr4   	feed_namerO   r   rP   rE  re  rf  rX  update_feed_requestr   s                 r   rI  zAssetFeedClient.Update/  s   L)$*;*;<L4..33NN')L$$T[[$))<ID33.)GH  D$C$C01t99+,!!T%E%E12373]3]i4'0K0((<(K,,==--??## @ % > & ((--,,"".. . 0N ## + , $ .D --??k @ +))EE*= F ?O<<o..r   c                    | j                   j                  | j                  j                  |            t	        j
                  |dfd      }||j                  d       ng }t	        j
                  |dfd      }||j                  d       ng }t	        j
                  |dfd      }||j                  d       ng }|||fS )z9Get Updated assetNames, assetTypes and relationshipTypes.r(  rO   c                       j                   S r   )r>   rX  s   r   <lambda>zGAssetFeedClient.UpdateAssetNamesTypesAndRelationships.<locals>.<lambda>]  	    door   r   c                       j                   S r   )r   r~  s   r   r  zGAssetFeedClient.UpdateAssetNamesTypesAndRelationships.<locals>.<lambda>c  r  r   rP   c                       j                   S r   )r?   r~  s   r   r  zGAssetFeedClient.UpdateAssetNamesTypesAndRelationships.<locals>.<lambda>i  s    D,B,Br   )rS   r+  r   rj  r   ParsePrimitiveArgsrp   )r   rR   rz  ry  rO   r   rP   rX  s          @r   rv  z5AssetFeedClient.UpdateAssetNamesTypesAndRelationshipsW  s     <<5595EGD--dM.EGK-(k--dM.EGK-(k!44"$BD%./%777r   N)r   r   r   r   rm   r   r"  r-  r0  r4  rI  rv  r   r   r   rK  rK    s,    )< 0
02-).&/P8r   rK  c                   &    e Zd ZdZdZd Zd Zd Zy)AssetSearchClientzClient for search assets.   c                 &   t        |      | _        || _        |t        k(  ryt	        |      j
                  | _        d| _        | j                  j                  | _	        t	        |      j                  | _        d| _        | j                  j                  | _        y t	        |      j                  | _        d| _        | j                  j                   | _	        t	        |      j                  | _        d| _        | j                  j"                  | _        y )N	SearchAllSearchAllResourcesSearchAllIamPolicies)r%   r   rT   V1P1BETA1_API_VERSIONr)   r`   resource_servicesearch_all_resources_method#CloudassetResourcesSearchAllRequestsearch_all_resources_requestiamPoliciespolicy_servicesearch_all_iam_policies_method%CloudassetIamPoliciesSearchAllRequestsearch_all_iam_policies_requestr   #CloudassetSearchAllResourcesRequest%CloudassetSearchAllIamPoliciesRequestr   s     r   r   zAssetSearchClient.__init__v  s    %k2D"D++'4>>d)4d&*.*=*=*a*ad'%k2>>d,7d)-1-@-@-f-fd*'477d)=d&*.*=*=*a*ad'%k255d,Bd)-1-@-@-f-fd*r   c           
      l   | j                   t        k(  ri }nd|j                  i} | j                  dt	        j
                  |      |j                  |j                  |j                  d|}t        j                  | j                  || j                  d|j                  xs | j                  ddd      S )	z Calls SearchAllResources method.readMaskr   queryr   orderByresultspageSize	pageTokennextPageTokenmethodfield
batch_sizebatch_size_attributecurrent_token_attributenext_token_attributer   )rT   r  	read_maskr  rG   GetDefaultScopeIfEmptyr  r   order_byr   YieldFromListr  r  	page_size_DEFAULT_PAGE_SIZE)r   rR   optional_extra_argsrequests       r   r  z$AssetSearchClient.SearchAllResources  s    00'8/d// 006jj##	
 G ##//>><T%<%<' +,. .r   c           
         | j                   t        k(  r1| j                  t        j                  |      |j
                        }nF| j                  t        j                  |      |j
                  |j                  |j                        }t        j                  | j                  || j                  d|j                  xs | j                  ddd      S )z"Calls SearchAllIamPolicies method.)r   r  r  r  r  r  r  r  )rT   r  r  rG   r  r  r   r  r   r  r  r  r  r  r   rR   r  s      r   r  z&AssetSearchClient.SearchAllIamPolicies  s    00442248

 5 Lg 442248

%%--	 5 !g
 ##22>><T%<%<' +,. .r   N)r   r   r   r   r  r   r  r  r   r   r   r  r  q  s    !g$.,.r   r  c                   "    e Zd ZdZefdZddZy)AssetListClientzClient for list assets.c                 f    || _         t        |      | _        t        |      j                  | _        y r   )rA   r%   r   r)   rQ   rS   r  s      r   r   zAssetListClient.__init__  s(    DK%k2D[)00DLr   c                    d}|j                   rt        j                  |j                         }t        |j                        }| j
                  j                  | j                  t        | j
                  j                  j                  |      |j                  ||j                        }t        j                  | j                  |d|j                  |j                   ddd|r|j"                  	      S d	      S )z'List assets with the asset list method.N)rA   r@   r   r   r?   rQ   r  r  r  )r  limitr  r  r  r  	predicate)r   r   rK   r5   r4   r   CloudassetAssetsListRequestrA   r   rF   r   rP   r   r  rS   r  r  filter_func)r   rR   	do_filterr   r4   list_assets_requests         r   r4  zAssetListClient.List  s    M**4+=+=>m)$*;*;<L--II{{;;''7 ##11 J 3 ##jj>>' +,&/$""	; 	; 6:	; 	;r   N)F)r   r   r   r   rm   r   r4  r   r   r   r  r    s    )< 1
;r   r  c                        e Zd ZdZefdZd Zy)AssetOperationClientzClient for operations.c                 l    t        |      j                  | _        t        |      j                  | _        y r   )r)   
operationsrS   r%   CloudassetOperationsGetRequestmessager   s     r   r   zAssetOperationClient.__init__  s&    [)44DL{+JJDLr   c                 \    | j                  |      }| j                  j                  |      S )Nr(  )r  rS   r+  )r   r)  r  s      r   r+  zAssetOperationClient.Get  s'    lll%G<<G$$r   N)r   r   r   r   rm   r   r+  r   r   r   r  r    s    !4 K%r   r  c                        e Zd ZdZefdZd Zy)GetHistoryClientzClient for get history assets.c                 h    || _         t        |      | _        | j                  j                  | _        y r   r   r   s     r   r   zGetHistoryClient.__init__  s%    "DK(DK;;>>DLr   c                 D    t        || j                  | j                        S r   )rX   rS   rT   )r   rR   s     r   
GetHistoryzGetHistoryClient.GetHistory  s     +D$,,,0,<,<> >r   N)r   r   r   r   rm   r   r  r   r   r   r  r    s    &!4 "
>r   r  c                        e Zd ZdZefdZd Zy)"IamPolicyAnalysisLongrunningClientz-Client for analyze IAM policy asynchronously.c                 X    t        |      | _        t        |      j                  | _        y r   r%   r   r)   r   rS   r   s     r   r   z+IamPolicyAnalysisLongrunningClient.__init__  !    %k2D[),,DLr   c                    | j                   j                         }||_        |j                  d      r+| j                   j	                  |j
                        |_        |j                  d      r+| j                   j                  |j                        |_	        |j                  d      s|j                  d      r| j                   j                         |_        |j                  d      r/|j                  j                  j                  |j                         |j                  d      r/|j                  j                  j                  |j                         d}|j                  rA| j                   j!                  | j                   j#                  |j                        	      }n| j                   j!                  | j                   j%                  |j&                  |j(                  
            }|j                  d      rCt+        | j                   j$                  j,                  |j.                        |j0                  _        |j                  d      r|j4                  |j0                  _        | j                   j9                         }|j:                  r|j:                  |_        |j>                  r|j>                  |_         |jB                  r|jB                  |_"        |jF                  r|jF                  |_$        |jJ                  r|jJ                  |_&        |jN                  r|jN                  |_(        |jS                  d      r|jT                  |_+        d}||_,        |jS                  d      r>| j                   j[                  t]        j^                  |j`                              |_1        | j                   je                  ||      }| j                   jg                  ||      }| jh                  jk                  |      }|S )z"Analyze IAM Policy asynchronously.r   )fullResourceNamerc   )rc   r   r   N)r   r   )r   tablePrefixr   bigquery_partition_keybigquery_write_dispositionr   r   )
accessTime)r   r   )r   "analyzeIamPolicyLongrunningRequest)6r   r  r   rM   ResourceSelectorr   resourceSelectorIdentitySelectorrc   identitySelectorAccessSelectoraccessSelectorr   extendr   gcs_output_pathIamPolicyAnalysisOutputConfig GoogleCloudAssetV1GcsDestination%GoogleCloudAssetV1BigQueryDestinationbigquery_datasetbigquery_table_prefixr   r   r  r   r   r  writeDispositionOptionsr   expandGroupsr   expandResourcesr   expandRolesr   outputResourceEdgesr   outputGroupEdgesr   "analyzeServiceAccountImpersonationr   r   includeDenyPolicyAnalysisoptionsConditionContextr   rK   r   conditionContext"AnalyzeIamPolicyLongrunningRequest,CloudassetAnalyzeIamPolicyLongrunningRequestrS   AnalyzeIamPolicyLongrunning)	r   r   rR   analysis_queryr   r  r   r  r   s	            r   r   z*IamPolicyAnalysisLongrunningClient.Analyze  sy   ((??AN N,-(,(;(;(L(L22 )M )4n%
#(,(;(;(L(L== )M )"n% D$4$4]$C&*&9&9&H&H&Jn#			'	"%%++224::>			-	(%%11889I9IJM))GG,,MM&& N ( H )m ))GG"1100++44 1 6 H 7m
 
		2	39@EE(($*E*E:G))6 
		6	7=A=\=\)):!!))+G!//g $ 5 5g --g!!$($>$>g!!%!8!8g11373]3]g0 >?*.*K*Kg'I$N.(,(;(;(L(L))$*:*:; )M )=n%!!DD$= E BG))VV W AO88IIr   Nr   r   r   r   r  r    s    5!4 ->r   r  c                        e Zd ZdZefdZd Zy)AnalyzeMoveClientz#Client for analyzing resource move.c                 f    || _         t        |      | _        t        |      j                  | _        y r   )rT   r%   r   r)   r   rS   r   s     r   r   zAnalyzeMoveClient.__init__:  s)    "D%k2D[),,DLr   c                    d|j                   z   }|j                  d      rd|j                  z   }nd|j                  z   }| j                  j
                  j                  j                  }|j                  r*| j                  j
                  j                  j                  }| j                  j                  |||      }| j                  j                  |      S )zAnalyze resource move.r   destination_folderzfolders/zorganizations/)destinationParentr+   view)rJ   rM   r  destination_organizationr   CloudassetAnalyzeMoveRequestViewValueValuesEnumFULLblockers_onlyBASICrS   AnalyzeMove)r   rR   rJ   destinationr   r   s         r   r  zAnalyzeMoveClient.AnalyzeMove?  s    DLL(G,-!8!88k$t'D'DDk<<PPUUE!!>>RRXXe))FF%e G EO <<##O44r   N)r   r   r   r   rm   r   r  r   r   r   r  r  7  s    +!4 -
5r   r  c                        e Zd ZdZefdZd Zy)AssetQueryClientzClient for QueryAsset API.c                 f    || _         t        |      | _        t        |      j                  | _        y r   )rA   r%   r   r)   r   rS   r  s      r   r   zAssetQueryClient.__init__U  s(    DK%k2D[),,DLr   c                    d}|j                  d      r"t        j                  |j                        dz   }d}|j                  d      r|j                  j
                  j                         }|st        j                  dd      d}|j                  d      r|j                  j                  dd	      }| j                  j                  | j                  j                  d
|j                  z   dz   |j                  z   |j                   |            }n'|j                  d      rt        j                  dd      d}d}|j                  d      rt#        j$                  |j&                        }d}|j                  d      r<t#        j$                  |j(                        }| j                  j+                  ||      }d}	|j                  d      rt#        j$                  |j,                        }	| j                  j/                  | j0                  | j                  j3                  |j4                  |j6                  |j8                  |j:                  ||	||            }
| j<                  j?                  |
      S )zMake QueryAssets request.Ntimeoutr   r   z--bigquery-tablezg--bigquery-table should have the format of `projects/<ProjectId>/datasets/<DatasetId>/tables/<TableId>`write_disposition-_r   r   )r   r   r  r   z--write_dispositionz:Must be set together with --bigquery-table to take effect.r=   rL   )endTime	startTimer   )jobReferencer  r  	statementr  r   readTimeWindowr   )rA   queryAssetsRequest) rM   six	text_typer  r   r   r   r   r   r  replacer   QueryAssetsOutputConfig<GoogleCloudAssetV1QueryAssetsOutputConfigBigQueryDestinationr   r   r   r   rK   r=   rL   
TimeWindowr   CloudassetQueryAssetsRequestrA   QueryAssetsRequestjob_referencer  
page_tokenr
  rS   QueryAssets)r   rR   r  r   r   r  r=   readtime_windowrL   	read_timequery_assets_requests              r   QueryzAssetQueryClient.QueryZ  sQ   G	"dll+c1gM()}}3399;n88u
 	

 			-	. 22::3D))AA"11GG!N$<$<<|K&&'"**0	 H 2 B 3m 
		-	.66

FH H HO
#%%dmm4hJ%''8j++66j 7 2oI(&&t'9'9:i..KK{{..AA++^^oonn*& B ( L 
) <<##$899r   N)r   r   r   r   rm   r   r  r   r   r   r   r   R  s    ")< -
4:r   r   c                   0    e Zd ZdZdZefdZd Zd Zd Z	y)OrgPolicyAnalyzerClientzClient for org policy analysis.d   c                 X    t        |      | _        t        |      j                  | _        y r   r  r   s     r   r   z OrgPolicyAnalyzerClient.__init__  r  r   c           
          | j                   j                  |j                  |j                        }t	        j
                  | j                  |dd|j                  xs | j                  ddd      S )z,Calls AnalyzeOrgPolicyGovernedAssets method.r   
constraintAnalyzeOrgPolicyGovernedAssetsgovernedAssetsr  r  r  r  )	r   /CloudassetAnalyzeOrgPolicyGovernedAssetsRequestr   r"  r   r  rS   r  r  r  s      r   r#  z6OrgPolicyAnalyzerClient.AnalyzeOrgPolicyGovernedAssets  si    !!QQjjT__ R 6G##/>><T%<%<' +,. .r   c           
          | j                   j                  |j                  |j                        }t	        j
                  | j                  |dd|j                  xs | j                  ddd      S )z0Calls AnalyzeOrgPolicyGovernedContainers method.r!  "AnalyzeOrgPolicyGovernedContainersgovernedContainersr  r  r  r  )	r   3CloudassetAnalyzeOrgPolicyGovernedContainersRequestr   r"  r   r  rS   r  r  r  s      r   r'  z:OrgPolicyAnalyzerClient.AnalyzeOrgPolicyGovernedContainers  si    !!UUjjT__ V 6G##3">><T%<%<' +,. .r   c           
          | j                   j                  |j                  |j                        }t	        j
                  | j                  |dd|j                  xs | j                  ddd      S )z Calls AnalyzeOrgPolicies method.r!  AnalyzeOrgPoliciesorgPolicyResultsr  r  r  r  )	r   #CloudassetAnalyzeOrgPoliciesRequestr   r"  r   r  rS   r  r  r  s      r   r+  z*OrgPolicyAnalyzerClient.AnalyzeOrgPolicies  si    !!EEjjT__ F 6G### >><T%<%<' +,. .r   N)
r   r   r   r   r  rm   r   r#  r'  r+  r   r   r   r  r    s"    '!4 -...r   r  c                        e Zd ZdZefdZd Zy)EffectiveIAMPolicyClientz)Client for Effective IAM Policy analysis.c                 X    t        |      | _        t        |      j                  | _        y r   )r%   r   r)   effectiveIamPoliciesrS   r   s     r   r   z!EffectiveIAMPolicyClient.__init__  s!    %k2D[)>>DLr   c                     | j                   j                  |j                  |j                        }| j                  j                  |      S )z*Calls BatchGetEffectiveIAMPolicies method.)namesr   )r   -CloudassetEffectiveIamPoliciesBatchGetRequestr3  r   rS   BatchGetr  s      r   BatchGetEffectiveIAMPoliciesz5EffectiveIAMPolicyClient.BatchGetEffectiveIAMPolicies  s@    !!OOjj

 P ,G<<  ))r   N)r   r   r   r   rm   r   r6  r   r   r   r/  r/    s    1!4 ?*r   r/  )<r   
__future__r   r   r   apitools.base.pyr   r   r   r   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper    googlecloudsdk.command_lib.assetr
   rG   $googlecloudsdk.command_lib.util.apisr   $googlecloudsdk.command_lib.util.argsr   r   googlecloudsdk.corecore_exceptionsr   r   googlecloudsdk.core.utilr   r  r"   rm   r  V1P5BETA1_API_VERSIONV1P7BETA1_API_VERSION_HEADERS_HTTP_ERROR_FORMATErrorr   r%   r)   r5   r;   rX   r~   r   r   objectr   r   r   rK  r  r  r  r  r  r  r   r  r/  r   r   r   <module>rG     s   = &  ' & > ' , 2 C A : < 9 = # $ * 
  # # # 7#- @.. @ , 	3 * 	3$0% 2E> 4G.f 4G'Z 2EPf>V > 4 4nv/f v/rr8f r8j@. @.F;f ;D	%6 	%
>v 
>E EP5 56<:v <:~1.f 1.h*v *r   