
    -                         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  G d dej$                        Zy)z+Export Cloud Build trigger to file command.    )absolute_import)division)unicode_literals)
exceptions)cloudbuild_util)base)flags)
properties)	resourcesc                   B    e Zd ZdZdddZed        Zd Zd Zd Z	d	 Z
y
)ImportzImport a build trigger.z        To import a trigger from a file:
        $ cat > trigger.yaml <<EOF
        name: my-trigger
        github:
          owner: GoogleCloudPlatform
          name: cloud-builders
          push:
            branch: .*
        EOF
          z|
        To import a build trigger from a file called trigger.yaml, run:

          $ {command} --source=trigger.yaml
      )DESCRIPTIONEXAMPLESc                 X    t        j                  |        | j                  dddd       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.
    z--sourcePATHTz9File path where trigger should be imported from.
        )metavarrequiredhelpN)build_flagsAddRegionFlagadd_argument)parsers    %lib/surface/builds/triggers/import.pyArgszImport.Args4   s3     f%
	      c                    |j                   }|s|j                  }t        j                  j	                  |j                   |||dd      j                         }||_        |j                  j                  |j                  |||            S )N)
projectsIdlocationsId
triggersIdz&cloudbuild.projects.locations.triggers)params
collection)resourceName	triggerIdbuildTrigger)
idnamer   REGISTRYParseRelativeNamer"   projects_locations_triggersPatch/CloudbuildProjectsLocationsTriggersPatchRequest)selfclientmessages
project_idlocation_idtrigger
trigger_idr&   s           r   _UpdateTriggerzImport._UpdateTriggerF   s    J<<j##

$&$

 < $ = >J\^ 	  G--33@@' 	A 	KL Lr   c                     t         j                  j                  d||      j                         }|j                  j                  |j                  ||            S )Nzcloudbuild.projects.locations)r!   r   r   )parentr$   )r   r'   Creater)   r*   0CloudbuildProjectsLocationsTriggersCreateRequest)r-   r.   r/   projectlocationr2   r6   s          r   _CreateTriggerzImport._CreateTriggerX   sf    &&2 '  +ln 
 --44AA 	B 	12 2r   c                    |j                   r| j                  |||||      S |j                  r	 | j                  |||||      S | j                  |||||      S # t        j                  $ r | j                  |||||      cY S w xY w)N)r%   r4   r&   apitools_exceptionsHttpNotFoundErrorr;   )r-   r.   r/   r9   r:   r2   s         r   _CreateOrUpdateTriggerzImport._CreateOrUpdateTriggerb   s    zz  7HgNN	Q""68WhPP   7HgNN !22 Q""68WhPPQs   A (BBc           
         t        j                         }t        j                         }t        j                  j
                  j                  j                  d      }t        j                  j                  j                  j                         }|j                  xs |xs t         j                  }t        j                  |j                  |j                  ddg      }|D cg c]  }| j                  |||||       c}S c c}w )zImports a build trigger.

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

    Returns:
      Some value that we want to have printed later.
    T)r   BuildTriggersubstitutions)skip_camel_case)r   GetClientInstanceGetMessagesModuler
   VALUEScorer9   GetbuildsregionDEFAULT_REGIONLoadMessagesFromPathsourcerA   r?   )	r-   argsr.   r/   r9   
regionpropr:   triggersr2   s	            r   Runz
Import.Runt   s     ..0F002H$$,,00$0?G""))00446J{{JjJO,J,JH33()	+H =E<D 	##FHgx$+	-<D  s   #DN)__name__
__module____qualname____doc__detailed_helpstaticmethodr   r4   r;   r?   rQ    r   r   r   r      s@    

-(  "L$2O$r   r   N)rU   
__future__r   r   r   apitools.base.pyr   r=   !googlecloudsdk.api_lib.cloudbuildr   googlecloudsdk.callioper   !googlecloudsdk.command_lib.buildsr	   r   googlecloudsdk.corer
   r   Commandr   rX   r   r   <module>r`      s7    2 &  ' > = ( B * )pT\\ pr   