
    5                     H   S 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	r " S
 S\R                  5      r " S S\R                  5      r " S S\R                  5      rS rS rS rS rS rS rS rS rS rS rS rS rS rS"S jrS r S  r!S! r"g)#z,Utilities for Data Catalog crawler commands.    )absolute_import)division)unicode_literals)crawlers)	arg_utils)repeated)
exceptionsv1alpha3c                       \ rS rSrSrSrg)InvalidCrawlScopeError   z"Error if a crawl scope is invalid. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       <lib/googlecloudsdk/command_lib/data_catalog/crawlers/util.pyr   r      s    *r   r   c                       \ rS rSrSrSrg)InvalidRunOptionError!   z!Error if a run option is invalid.r   Nr   r   r   r   r   r   !   s    )r   r   c                       \ rS rSrSrSrg)NoFieldsSpecifiedError%   z5Error if no fields are specified for a patch request.r   Nr   r   r   r   r   r   %   s    =r   r   c                     [         R                  " 5       n[        R                  R	                  UR
                  U 5      n[        XX$5      n[        XX$5      n[        XX$5      n[        XU5      n[        XX$5      nU$ )a  Python hook that validates and parses crawler config flags.

Normally all the functions called here would be provided directly as
modify_request_hooks in the update command YAML file. However, this would
require setting read_modify_update: True to obtain the existing crawler
beforehand, incurring an extra GET API call that may be unnecessary depending
on what fields need to be updated.

Args:
  ref: The crawler resource reference.
  args: The parsed args namespace.
  request: The update crawler request.
Returns:
  Request with scope and scheduling flags set appropriately.
Raises:
  InvalidCrawlScopeError: If the crawl scope configuration is not valid.
  InvalidRunOptionError: If the scheduling configuration is not valid.
)r   CrawlersClientr   CachedResultFromFuncGetValidateScopeFlagsForUpdate ValidateSchedulingFlagsForUpdateParseScopeFlagsForUpdateParseSchedulingFlagsForUpdateParseBundleSpecsFlagsForUpdate)refargsrequestclientcrawlers        r   ValidateAndParseFlagsForUpdater-   )   sq    & ""$& !!**6::s;''7D',SI'$SA')#W='*3gG'	.r   c                    A [        U5      (       d  U$ UR                  5       R                  R                  =(       d    / nUR	                  S5      (       a  / nUR	                  S5      (       a.  [        UR                  5      nU Vs/ s H  ofU;  d  M
  UPM     nnUR	                  S5      (       a  XAR                  -  n[        R                  " USU5        U$ s  snf )a,  Python hook that parses the bundle spec args into the update request.

Args:
  ref: The crawler resource reference.
  args: The parsed args namespace.
  request: The update crawler request.
  crawler: CachedResult, The cached crawler result.
Returns:
  Request with bundling specs set appropriately.
clear_bundle_specsremove_bundle_specsadd_bundle_specsz8googleCloudDatacatalogV1alpha3Crawler.config.bundleSpecs)
_IsChangeBundleSpecsSpecifiedr"   configbundleSpecsIsSpecifiedsetr0   r1   r   SetFieldInMessage)r(   r)   r*   r,   bundle_specs	to_removebs          r   r'   r'   I   s     
	&t	,	,N%%117R,	*++L	+,,D,,-I+B|!	/AA|LB	()))))L@ 
. Cs   	CCc                     A UR                  S5      (       a  UR                  S:w  a  [        S5      eUR                  S5      (       d  UR                  S:X  a  [        S5      eU$ )a&  Validates scope flags for create.

Args:
  ref: The crawler resource reference.
  args: The parsed args namespace.
  request: The create request.
Returns:
  The request, if the crawl scope configuration is valid.
Raises:
  InvalidCrawlScopeError: If the crawl scope configuration is not valid.
bucketsbucketz}Argument `--buckets` is only valid for bucket-scoped crawlers. Use `--crawl-scope=bucket` to specify a bucket-scoped crawler.zLArgument `--buckets` must be provided when creating a bucket-scoped crawler.)r5   crawl_scoper   r(   r)   r*   s      r   ValidateScopeFlagsForCreater@   h   ss     
	i  T%5%5%A
 	IJ J 
		)	$	$)9)9X)E
 	  
.r   c                    A [        U5      nU(       aV  UR                  S:w  aF  UR                  S5      (       d%  UR                  5       R                  R
                  c  [        S5      eU(       d  UR                  S:X  a  [        S5      eU$ )aZ  Validates scope flags for update.

Args:
  ref: The crawler resource reference.
  args: The parsed args namespace.
  request: The update request.
  crawler: CachedResult, The cached crawler result.
Returns:
  The request, if the crawl scope configuration is valid.
Raises:
  InvalidCrawlScopeError: If the crawl scope configuration is not valid.
r=   r>   zArguments `--add-buckets`, `--remove-buckets`, and `--clear-buckets` are only valid for bucket-scoped crawlers. Use `--crawl-scope=bucket` to specify a bucket-scoped crawler.z_Must provide buckets to add or remove when updating the crawl scope of a bucket-scoped crawler.)_IsChangeBucketsSpecifiedr>   r5   r"   r3   bucketScoper   )r(   r)   r*   r,   change_bucketss        r   r#   r#      s     
,T2.))X5&&{{}''/
 	./ / 
D,,8
 	&' ' 
.r   c                    A / nUR                  S5      (       a  UR                  S5        UR                  S5      (       a  UR                  S5        [        U5      (       a  UR                  S5        UR                  S:X  a  UR                  S5        OCUR                  S:X  a  UR                  S5        O![	        U5      (       a  UR                  S	5        UR
                  S
:X  a  UR                  S5        O7UR
                  S:X  d  UR                  S5      (       a  UR                  S5        U(       d  [        S5      eSR                  U5      Ul        U$ )a9  Python hook that computes the update mask for a patch request.

Args:
  ref: The crawler resource reference.
  args: The parsed args namespace.
  request: The update crawler request.
Returns:
  Request with update mask set appropriately.
Raises:
  NoFieldsSpecifiedError: If no fields were provided for updating.
descriptiondisplay_namedisplayNamezconfig.bundleSpecsprojectzconfig.projectScopeorganizationzconfig.organizationScopezconfig.bucketScopemanualzconfig.adHocRun	scheduledrun_schedulezconfig.scheduledRunz.Must specify at least one parameter to update.,)	r5   appendr2   r>   rB   
run_optionr   join
updateMask)r(   r)   r*   update_masks       r   SetUpdateMaskrT      s)    
+	m$$}%	n%%}%"4((+,	",->)12 &&+,	__ ()+%)9)9.)I)I,-	
 8: : xx,'	.r   c                    A [         R                  " 5       nUR                  nUR                  S5      (       a*  UR                   Vs/ s H  nUR                  US9PM     nnOSn[        UR                  XbU5      $ s  snf )zPython hook that parses the crawl scope args into the create request.

Args:
  ref: The crawler resource reference.
  args: The parsed args namespace.
  request: The create crawler request.
Returns:
  Request with crawl scope set appropriately.
r<   r=   N)r   r   messagesr5   r<   (GoogleCloudDatacatalogV1alpha3BucketSpec_SetScopeInRequestr>   )r(   r)   r*   r+   rW   r:   r<   s          r   ParseScopeFlagsForCreaterZ      s     
""$&__(	i  &$1 @@@J$  &G G	D,,g	II	&s   A<c                     A [         R                  " 5       nUR                  n[        U5      (       a  [	        XU5      nSnOSnUR
                  n[        XvX%5      $ )a)  Python hook that parses the crawl scope args into the update request.

Args:
  ref: The crawler resource reference.
  args: The parsed args namespace.
  request: The update crawler request.
  crawler: CachedResult, The cached crawler result.
Returns:
  Request with crawl scope set appropriately.
r=   N)r   r   rW   rB   _GetBucketsPatchr>   rY   )r(   r)   r*   r,   r+   rW   r<   r>   s           r   r%   r%      sY     
""$&__(t$$th7G KG""K	K'	DDr   c                     U R                  S5      =(       d)    U R                  S5      =(       d    U R                  S5      $ )Nadd_bucketsremove_bucketsclear_bucketsr5   r)   s    r   rB   rB      s<    


=
) ,


+
,,


?
+-r   c                     U R                  S5      =(       d)    U R                  S5      =(       d    U R                  S5      $ )Nr1   r0   r/   ra   rb   s    r   r2   r2      s>    


-
. 1


0
11


/
02r   c                    U S:X  a,  U(       d  [        S5      e[        R                  " USU5        U$ U S:X  a(  [        R                  " USUR                  5       5        U$ U S:X  a&  [        R                  " USUR	                  5       5        U$ )z)Returns request with the crawl scope set.r=   zSAt least one bucket must be included in the crawl scope of a bucket-scoped crawler.z@googleCloudDatacatalogV1alpha3Crawler.config.bucketScope.bucketsrI   z9googleCloudDatacatalogV1alpha3Crawler.config.projectScoperJ   z>googleCloudDatacatalogV1alpha3Crawler.config.organizationScope)r   r   r7   0GoogleCloudDatacatalogV1alpha3ParentProjectScope5GoogleCloudDatacatalogV1alpha3ParentOrganizationScope)r>   r<   r*   rW   s       r   rY   rY     s    H"#$ $ J 
. iCAACE 
. n$HFFHJ
 
.r   c                    UR                  5       R                  R                  nU(       a  UR                  O/ nU R	                  S5      (       a  / nU R	                  S5      (       a8  [        U R                  5      nU Vs/ s H  ofR                  U;  d  M  UPM     nnU R	                  S5      (       a-  UU R                   Vs/ s H  nUR                  US9PM     sn-  nU$ s  snf s  snf )zReturns list of buckets for a patch request based on the args provided.

Args:
  args: The parsed args namespace.
  crawler: CachedResult, The cached crawler result.
  messages: The messages module.
Returns:
  Desired list of buckets.
r`   r_   r^   rV   )
r"   r3   rC   r<   r5   r6   r_   r=   r^   rX   )r)   r,   rW   bucket_scoper<   r9   r:   s          r   r\   r\     s     %%11,$0L  b'	o&&G	&''D''(I!?'QXXY%>q'G?	m$$))+)A AAAK)+ +G	.	 @+s   C%C%C*c                     A [        X5      $ N_ValidateSchedulingFlagsr?   s      r    ValidateSchedulingFlagsForCreaterm   7  s    		!$	00r   c                     A [        XUSS9$ )NT)
for_updaterk   )r(   r)   r*   r,   s       r   r$   r$   <  s    		!$T	JJr   Nc                 N   U R                   S:X  a!  U R                  S5      (       d  [        S5      eU R                   S:w  ac  U R                  S5      (       aM  U(       a;  U R                  S5      (       d%  UR                  5       R                  R
                  c  [        S5      eU$ )ar  Validates scheduling flags.

Args:
  args: The parsed args namespace.
  request: The create or update request.
  crawler: CachedResult, The cached crawler result.
  for_update: If the request is for update instead of create.
Returns:
  The request, if the scheduling configuration is valid.
Raises:
  InvalidRunOptionError: If the scheduling configuration is not valid.
rL   rM   zUArgument `--run-schedule` must be provided if `--run-option=scheduled` was specified.rP   zArgument `--run-schedule` can only be provided for scheduled crawlers. Use `--run-option=scheduled` to specify a scheduled crawler.)rP   r5   r   r"   r3   scheduledRun)r)   r*   r,   ro   s       r   rl   rl   A  s     
__#D,<,<^,L,L
	  
__#(8(8(H(H &&))1! 
 
.r   c                     A [         R                  " 5       nUR                  n[        UR                  UR
                  X$5      $ rj   )r   r   rW   _SetRunOptionInRequestrP   rM   )r(   r)   r*   r+   rW   s        r   ParseSchedulingFlagsForCreatert   b  s:    	""$&__(	
oot(('
= =r   c                     A [         R                  " 5       nUR                  nUR                  S5      (       a  SOUR                  n[        XQR                  X$5      $ )NrM   rL   )r   r   rW   r5   rP   rs   rM   )r(   r)   r*   r+   rW   rP   s         r   r&   r&   j  sU    	""$&__(  $//??__ 	##W
8 8r   c                     U S:X  a(  [         R                  " USUR                  5       5        U$ U S:X  aC  [         R                  " UUR                  R
                  5      n[         R                  " USU5        U$ )z(Returns request with the run option set.rK   z5googleCloudDatacatalogV1alpha3Crawler.config.adHocRunrL   zLgoogleCloudDatacatalogV1alpha3Crawler.config.scheduledRun.scheduledRunOption)r   r7   &GoogleCloudDatacatalogV1alpha3AdhocRunChoiceToEnum*GoogleCloudDatacatalogV1alpha3ScheduledRun!ScheduledRunOptionValueValuesEnum)rP   rM   r*   rW   scheduled_run_options        r   rs   rs   w  s~    8?779; 
. [ $11		<	<
+
+. V 
.r   )NF)#r   
__future__r   r   r   #googlecloudsdk.api_lib.data_catalogr   $googlecloudsdk.command_lib.util.apisr   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer	   DATACATALOG_CRAWLER_API_VERSIONErrorr   r   r   r-   r'   r@   r#   rT   rZ   r%   rB   r2   rY   r\   rm   r$   rl   rt   r&   rs   r   r   r   <module>r      s    3 &  ' 8 : 9 *", +Z-- +*J,, *>Z-- >@>0<(VJ*E8-2201
K
B=
8r   