
    5                     F   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	Z G d
 dej                        Z G d dej                        Z G d dej                        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! Z"y)#z,Utilities for Data Catalog crawler commands.    )absolute_import)division)unicode_literals)crawlers)	arg_utils)repeated)
exceptionsv1alpha3c                       e Zd ZdZy)InvalidCrawlScopeErrorz"Error if a crawl scope is invalid.N__name__
__module____qualname____doc__     <lib/googlecloudsdk/command_lib/data_catalog/crawlers/util.pyr   r      s    *r   r   c                       e Zd ZdZy)InvalidRunOptionErrorz!Error if a run option is invalid.Nr   r   r   r   r   r   !   s    )r   r   c                       e Zd ZdZy)NoFieldsSpecifiedErrorz5Error if no fields are specified for a patch request.Nr   r   r   r   r   r   %   s    =r   r   c                    t        j                         }t        j                  j	                  |j
                  |       }t        | |||      }t        | |||      }t        | |||      }t        | ||      }t        | |||      }|S )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(   )   s    & ""$& !!**6::s;''T7GD',S$I'$S$A')#tW='*3gwG'	.r   c                    ~ t        |      s|S |j                         j                  j                  xs g }|j	                  d      rg }|j	                  d      r)t        |j                        }|D cg c]	  }||vs| }}|j	                  d      r||j                  z  }t        j                  |d|       |S c c}w )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setr+   r,   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|!q	/AA|LB	()D)))L@ 
. Cs   4	B=>B=c                     ~ |j                  d      r|j                  dk7  rt        d      |j                  d      s|j                  dk(  rt        d      |S )a8  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.)r0   crawl_scoper   r#   r$   r%   s      r   ValidateScopeFlagsForCreater;   h   sm     
	i T%5%5%A
 	IJ J 
		)	$)9)9X)E
 	  
.r   c                     ~ t        |      }|rO|j                  dk7  r@|j                  d      s$|j                         j                  j
                  t        d      |s|j                  dk(  rt        d      |S )an  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.
  r8   r9   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.)_IsChangeBucketsSpecifiedr9   r0   r   r.   bucketScoper   )r#   r$   r%   r'   change_bucketss        r   r   r      s     
,T2.))X5&{{}''/
 	./ / 
D,,8
 	&' ' 
.r   c                 r   ~ g }|j                  d      r|j                  d       |j                  d      r|j                  d       t        |      r|j                  d       |j                  dk(  r|j                  d       n=|j                  dk(  r|j                  d       nt	        |      r|j                  d	       |j
                  d
k(  r|j                  d       n1|j
                  dk(  s|j                  d      r|j                  d       |st        d      dj                  |      |_        |S )aK  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.,)	r0   appendr-   r9   r=   
run_optionr   join
updateMask)r#   r$   r%   update_masks       r   SetUpdateMaskrO      s    
+	m$}%	n%}%"4(+,	",->)12 &+,	__ ()+%)9)9.)I,-	
 8: : xx,'	.r   c                     ~ t        j                         }|j                  }|j                  d      r*|j                  D cg c]  }|j                  |       }}nd}t        |j                  |||      S c c}w )a  Python 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.
  r7   r8   N)r   r   messagesr0   r7   (GoogleCloudDatacatalogV1alpha3BucketSpec_SetScopeInRequestr9   )r#   r$   r%   r&   rR   r5   r7   s          r   ParseScopeFlagsForCreaterU      s     
""$&__(	i &$1 @@@J$  & G	D,,gw	II	&s   A7c                     ~ t        j                         }|j                  }t        |      rt	        |||      }d}nd}|j
                  }t        ||||      S )a9  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.
  r8   N)r   r   rR   r=   _GetBucketsPatchr9   rT   )r#   r$   r%   r'   r&   rR   r7   r9   s           r   r    r       s\     
""$&__(t$tWh7G KG""K	K'8	DDr   c                 p    | j                  d      xs$ | j                  d      xs | j                  d      S )Nadd_bucketsremove_bucketsclear_bucketsr0   r$   s    r   r=   r=      s<    


=
) ,


+
,,


?
+-r   c                 p    | j                  d      xs$ | j                  d      xs | j                  d      S )Nr,   r+   r*   r\   r]   s    r   r-   r-      s>    


-
. 1


0
11


/
02r   c                    | dk(  r&|st        d      t        j                  |d|       |S | dk(  r't        j                  |d|j                                |S | dk(  r%t        j                  |d|j	                                |S )z)Returns request with the crawl scope set.r8   zSAt least one bucket must be included in the crawl scope of a bucket-scoped crawler.z@googleCloudDatacatalogV1alpha3Crawler.config.bucketScope.bucketsrD   z9googleCloudDatacatalogV1alpha3Crawler.config.projectScoperE   z>googleCloudDatacatalogV1alpha3Crawler.config.organizationScope)r   r   r2   0GoogleCloudDatacatalogV1alpha3ParentProjectScope5GoogleCloudDatacatalogV1alpha3ParentOrganizationScope)r9   r7   r%   rR   s       r   rT   rT     s    H"#$ $ J 
. iCAACE 
. n$HFFHJ
 
.r   c                    |j                         j                  j                  }|r|j                  ng }| j	                  d      rg }| j	                  d      r3t        | j                        }|D cg c]  }|j                  |vs| }}| j	                  d      r-|| j                  D cg c]  }|j                  |       c}z  }|S c c}w c c}w )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[   rZ   rY   rQ   )
r   r.   r>   r7   r0   r1   rZ   r8   rY   rS   )r$   r'   rR   bucket_scoper7   r4   r5   s          r   rW   rW     s     %%11,$0L  b'	o&G	&'D''(I!?'QQXXY%>q'G?	m$))+)A AAAK)+ +G	.	 @+s   2CC,Cc                     ~ t        ||      S N_ValidateSchedulingFlagsr:   s      r    ValidateSchedulingFlagsForCreaterh   7  s    		!$	00r   c                 "    ~ t        |||d      S )NT)
for_updaterf   )r#   r$   r%   r'   s       r   r   r   <  s    		!$T	JJr   Nc                     | j                   dk(  r| j                  d      st        d      | j                   dk7  rS| j                  d      rB|r5| j                  d      s$|j                         j                  j
                  t        d      |S )a  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.
  rG   rH   zUArgument `--run-schedule` must be provided if `--run-option=scheduled` was specified.rK   zArgument `--run-schedule` can only be provided for scheduled crawlers. Use `--run-option=scheduled` to specify a scheduled crawler.)rK   r0   r   r   r.   scheduledRun)r$   r%   r'   rj   s       r   rg   rg   A  s     
__#D,<,<^,L
	  
__#(8(8(H &))1! 
 
.r   c                     ~ t        j                         }|j                  }t        |j                  |j
                  ||      S re   )r   r   rR   _SetRunOptionInRequestrK   rH   )r#   r$   r%   r&   rR   s        r   ParseSchedulingFlagsForCreatero   b  s<    	""$&__(	
oot(('8
= =r   c                     ~ t        j                         }|j                  }|j                  d      rdn|j                  }t        ||j                  ||      S )NrH   rG   )r   r   rR   r0   rK   rn   rH   )r#   r$   r%   r&   rR   rK   s         r   r!   r!   j  sV    	""$&__(  $//?__ 	$##Wh
8 8r   c                     | dk(  r't        j                  |d|j                                |S | dk(  rAt        j                  ||j                  j
                        }t        j                  |d|       |S )z(Returns request with the run option set.rF   z5googleCloudDatacatalogV1alpha3Crawler.config.adHocRunrG   zLgoogleCloudDatacatalogV1alpha3Crawler.config.scheduledRun.scheduledRunOption)r   r2   &GoogleCloudDatacatalogV1alpha3AdhocRunChoiceToEnum*GoogleCloudDatacatalogV1alpha3ScheduledRun!ScheduledRunOptionValueValuesEnum)rK   rH   r%   rR   scheduled_run_options        r   rn   rn   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   rO   rU   r    r=   r-   rT   rW   rh   r   rg   ro   r!   rn   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   