
                             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  G d de
j*                        Zy)zUpdate Pub/Sub trigger command.    )absolute_import)division)unicode_literals)cloudbuild_util)trigger_config)base)
exceptions)resource_args)concept_parsers)log)
properties)	resourcesc                   4    e Zd ZdZddiZed        Zd Zd Zy)UpdatePubsubz-Update a Pub/Sub trigger used by Cloud Build.EXAMPLESa              To update the branch from which the trigger clones:

              $ {command} my-trigger --source-to-build-branch=my-branch

            To update the topic:

              $ {command} my-trigger --topic=projects/my-project/topics/my-topic

            To update the substitutions of the trigger:
              $ {command} my-trigger --update-substitutions=_REPO_NAME=my-repo,_BRANCH_NAME=master
          c                 d   t         j                  j                  dt        j                         dd      j                  |        t        j                  | dd      }|j                  dd	       t        j                  |d
       t        j                  |       t        j                  |       y)zRegister flags for this command.

    Args:
      parser: An argparse.ArgumentParser-like object. It is mocked out in order
        to capture some information, but behaves like an ArgumentParser.
    TRIGGERzBuild Trigger.TrequiredF)add_region_flagadd_namez--topicz1The topic to which this trigger should subscribe.)help)has_file_sourceN)r   ConceptParserForResourcer
   GetTriggerResourceSpecAddToParsertrigger_utilsAddTriggerArgsadd_argumentAddBuildConfigArgsForUpdateAddRepoSourceForUpdateAddFilterArgForUpdate)parserflag_configs     ,lib/surface/builds/triggers/update/pubsub.pyArgszUpdatePubsub.Args3   s     !!--,,.4 . ) *5V)<..7K@  B --T+((5''4    c           
      .   t        j                         }t        j                  ||      \  }}|r|S |j                  r!|j                  |j                        |_        t        j                  j                  j                  j                  d      }d|d|j                  d}t        j                  ||||||dd       |j                  |_        |j                  "|j                  st!        j"                  dd	      |j$                  rd
|_        |S )zParses arguments into a build trigger.

    Args:
      args: An argparse arguments object.
      old_trigger: The existing trigger to be updated.
      update_mask: The fields to be updated.

    Returns:
      A build trigger object.
    )topicTr   zgcr.io/z/gcb-z:$COMMIT_SHA)has_repo_sourcer   z--subscription-filterzucannot update subscription filter to be empty; please use `--clear-subscription-filter` to clear subscription filter. )r   GetMessagesModuler   ParseTriggerArgsForUpdater*   PubsubConfigpubsubConfigr   VALUEScoreprojectGetr   ParseBuildConfigArgsForUpdatesubscription_filterfilterc_exceptionsInvalidArgumentExceptionclear_subscription_filter)	selfargsold_triggerupdate_maskmessagestriggerdoner3   default_images	            r&   ParseTriggerFromFlagsz"UpdatePubsub.ParseTriggerFromFlagsJ   s     002H!;;D(KMGTnzz%222Dg$$,,00$0?G6=t||LM//	 --GN+D4L4L11
!I 
 
	'	'gnNr(   c                    t        j                         }t        j                         }t        j                  j
                  j                  j                  d      }t        j                  j                  j                  j                         }|j                  xs |xs t         j                  }|j                  }t        j                  j                  ||||dd      j                         }|j                   j                  |j"                  j%                  ||            }	g }
| j'                  ||	|
      }d|
j)                  t        j*                  |             t-        t/        t1        fd|
                  }
|j3                  |||d	j5                  |
      
      }|j                   j7                  |      }t9        j:                  |d       |S )zThis is what handles the command execution.

    Args:
      args: An argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The updated webhook trigger.
    Tr   )
projectsIdlocationsId
triggersIdz&cloudbuild.projects.locations.triggers)params
collection)name	triggerIdsubstitutionsc                 .    | j                        rS | S )N)
startswith)msubs    r&   <lambda>z"UpdatePubsub.Run.<locals>.<lambda>   s    c!2#99r(   ,)resourceNamerK   buildTrigger
updateMaskbuild_trigger)kind)r   GetClientInstancer-   r   r1   r2   r3   r4   buildsregionDEFAULT_REGIONr   r   REGISTRYParseRelativeNameprojects_locations_triggersMESSAGES_MODULE-CloudbuildProjectsLocationsTriggersGetRequestrC   extendMessageToFieldPathssortedsetmap/CloudbuildProjectsLocationsTriggersPatchRequestjoinPatchr   UpdatedResource)r;   r<   clientr?   r3   
regionproplocation	triggeridrJ   r=   r>   r@   requpdated_triggerrP   s                 @r&   RunzUpdatePubsub.Runv   s    ..0F002H$$,,00$0?G""))00446J{{JjJO,J,JHI##!##

 < $ = >J\^ 	 4488LL 	M 	,-K K(({KHG C::7CDC9;GHK 
B
B88K(	 C *C 88>>sCO	8r(   N)	__name__
__module____qualname____doc__detailed_helpstaticmethodr'   rC   rq    r(   r&   r   r       s5    5 -  5 5,*X2r(   r   N)ru   
__future__r   r   r   !googlecloudsdk.api_lib.cloudbuildr   r   r   googlecloudsdk.callioper   r	   r8   %googlecloudsdk.command_lib.cloudbuildr
   (googlecloudsdk.command_lib.util.conceptsr   googlecloudsdk.corer   r   r   UpdateCommandr   rx   r(   r&   <module>r      sB    & &  ' = M ( > ? D # * )H4%% Hr(   