
    %3              	          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mZ ddlmZ ddlZdZej>                   ej@                  ejB                  jD                         e
jF                  e
jH                  jJ                         G d dejL                  e
jN                                             Z(ej>                   ej@                  ejB                  jR                         e
jF                  e
jH                  jR                         G d de(                           Z*ej>                   ej@                  ejB                  jV                         e
jF                  e
jH                  jX                         G d de(                           Z-y)zdeployments create command.    )absolute_import)division)unicode_literals)
exceptions)dm_api_util)dm_base)apis)base)alpha_flags)dm_util)dm_write)flags)importer)	arg_utils)labels_util)log)
propertiesNi  c                       e Zd ZdZddiZ ej                  d ej                  dd      j                  j                  dd	      Zeej                  j                  fd
       Zd Zd Z	 ddZd Zd Zd Zy)CreatemCreate a deployment.

  This command inserts (creates) a new deployment based on a provided config
  file.
  EXAMPLESa  
To create a new deployment from a top-level YAML file, run:

  $ {command} my-deployment --config=config.yaml --description="My deployment"

To create a new deployment from a top-level template file, run:

  $ gcloud deployment-manager deployments create my-deployment   --template=template.{jinja|py}   --properties="string-key:'string-value',integer-key:12345"

To create a new deployment directly from a composite type, run:

  $ gcloud deployment-manager deployments create my-deployment   --composite-type=<project-id>/composite:<type-name>   --properties="string-key:'string-value',integer-key:12345"

To preview a deployment without actually creating resources, run:

  $ {command} my-new-deployment --config=config.yaml --preview

To instantiate a deployment that has been previewed, issue an update command for that deployment without specifying a config file.

More information is available at https://cloud.google.com/deployment-manager/docs/configuration/.
z--create-policydeploymentmanagerv2betaz;Create policy for resources that have changed in the updatezcreate-or-acquire)help_strdefaultc                    | j                         }| j                  d      }t        j                  |       t        j                  |       t        j                  |        t        j
                  |        t        j                  |        |j                  ddddd       | j                  d	d
d       | j                  ddddd       | j                  j                  t        j                         y)a\  Args is called by calliope to gather arguments for this command.

    Args:
      parser: An argparse parser that you can use to add arguments that go
          on the command line after this command. Positional arguments are
          allowed.
      version: The version this tool is running as. base.ReleaseTrack.GA
          is the default.
    T)requiredz--automatic-rollback-on-errorzIf the create request results in a deployment with resource errors, delete that deployment immediately after creation. (default=False)automatic_rollbackF
store_true)helpdestr   actionz--descriptionz1Optional description of the deployment to insert.description)r    r!   z	--previewzePreview the requested create without actually instantiating the underlying resources. (default=False)previewN)add_mutually_exclusive_groupr   AddConfigFlagsAddAsyncFlagAddDeploymentNameFlagAddPropertiesFlagr   AddCreateLabelsFlagsadd_argumentdisplay_info	AddFormatRESOURCES_AND_OUTPUTS_FORMAT)parserversiongroupconfig_groups       4lib/surface/deployment_manager/deployments/create.pyArgszCreate.ArgsZ   s     //1E666EL	&	u	'	F#$$V,	' "   @  
 0   !!%"D"DE    c                 H    |s t         j                  j                  d       yy)zCalled after resources are displayed if the default format was used.

    Args:
      resources_were_displayed: True if resources were displayed.
    z1No resources or outputs found in your deployment.N)r   statusPrint)selfresources_were_displayeds     r3   EpilogzCreate.Epilog   s     $	jjJK $r5   c           
         | j                   j                  |j                  dt        j                  j
                  j                  j                  id      }|j                  d      s!|j                  rt        j                  |_        | j                  j                  |j                  t!        j"                  | j                  |j$                  |j&                  |j(                  |j                              }| j+                  ||       	 | j,                  j.                  j1                  | j3                  |t5        j6                         |            }t9        j:                  | j,                  | j                  t5        j6                         |j                        }t=        j>                  |       |j                  r|S |jJ                  }	 tM        jN                  | j,                  | j                  |d	t5        j6                         tP        
      }t=        jR                  |d       t9        j\                  | j,                  | j                  t5        j6                         |j                        S # t@        jB                  $ r)}tE        jF                  |t8        jH                        d}~ww xY w# t@        jB                  $ r)}tE        jF                  |t8        jH                        d}~wtT        jV                  $ rU}| jY                  |||t5        j6                         |      }t[        |dd      rt        j                  |_        |cY d}~S d}~ww xY w)a   Run 'deployments create'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns a struct containing the list of resources and list of
        outputs in the deployment.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ConfigError: Config file could not be read or parsed, or the
          deployment creation operation encountered an error.
    projectzdeploymentmanager.deployments)params
collectionformat)configtemplatecomposite_typer   )nametarget)argsr=   
deploymentNcreate)operation_descriptionr=   timeoutr   r   F)/	resourcesParsedeployment_namer   VALUEScorer=   	GetOrFailIsSpecifiedasync_r   OPERATION_FORMATr@   messages
DeploymentrG   r   BuildTargetConfigrA   rB   rC   _SetMetadataclientdeploymentsInsert_BuildRequestr   
GetProjectr   FetchDeploymentFingerprintr   PrintFingerprintapitools_exceptions	HttpErrorr   HttpExceptionHTTP_ERROR_FORMATrD   r   WaitForOperationOPERATION_TIMEOUTLogOperationStatusdm_exceptionsOperationError_HandleOperationErrorgetattrFetchResourcesAndOutputs)	r9   rF   deployment_refrG   	operationfingerprinterrorop_nameresponses	            r3   Runz
Create.Run   s   $ ^^)):,,1199CCD2 * 4N X&T[[**dk))&&))$--1537==9=9L9L59__	F * J 	dJ'K++))00


!3!3!5*  NOi
  ::
++
--





#
#	%k
 {+ {{g--KKMM"*&&(%'	 	""9h7 11$++t}}292D2D2F2@2K2KM M9 (( K$$UK,I,IJJK !** M&&uk.K.KLL)) --e.2.7.5.@.@.B.<	>
 4-u5..$+sE   B&I1 AJ0 1J-$J((J-0M$K''M=A
MMMc                 ,   | j                   j                  |||j                        }|ri|j                  r]t        j
                  j                  |j                        j                  }| j                   j                  j                  |      |_	        |S )N)r=   rG   r$   )
rT   )DeploymentmanagerDeploymentsInsertRequestr$   create_policyr   _create_policy_flag_mapGetEnumForChoicerD   CreatePolicyValueValuesEnumcreatePolicy)r9   rF   r=   rG   supports_create_policyrequestparsed_create_flags          r3   r[   zCreate._BuildRequest   s    
 mmEEJ F FG$"4"4!99JJ


"d  --
A
A
%
%&8
9  Nr5   c                     |j                   r_| j                  |j                  t        j                  |            }t        j                  | j                  | j                  ||      }||gS |N)	r   _PerformRollbackrG   six	text_typer   GetOperationrX   rT   )r9   rn   rF   rl   r=   rk   delete_operationcreate_operations           r3   rh   zCreate._HandleOperationError   sg    ..~/H/H/2}}U/CE$11$++t}}2;WF  011
Kr5   c                    |j                   r|j                   |_         t        j                  |      }|rRt        t	        j
                  |            D cg c]"  \  }}| j                  j                  ||      $ c}}|_        y y c c}}w )N)keyvalue)	r#   r   GetUpdateLabelsDictFromArgssortedr   	iteritemsrT   DeploymentLabelEntrylabels)r9   rF   rG   
label_dictkvs         r3   rW   zCreate._SetMetadata   s~    #//j88>J S]]:677da --
,
,!
,
<7j s   'Bc                    t        j                  d|z   dz   |z          t         j                  j                  d       	 | j                  j
                  j                  | j                  j                  t        j                         |            }t#        j$                  | j                  | j                  |j&                  dt        j                         t(               t        j*                  | j                  | j                  |t        j                               }|S # t        j                  $ r)}t        j                  |t        j                         d }~ww xY w)NzThere was an error deploying z:
zP`--automatic-rollback-on-error` flag was supplied; deleting failed deployment...)r=   rG   delete)rJ   )r   warningr7   r8   rX   rY   DeleterT   )DeploymentmanagerDeploymentsDeleteRequestr   r\   r_   r`   r   ra   r   rb   r   rc   rD   rd   r   )r9   rM   error_messager   rn   completed_operations         r3   r~   zCreate._PerformRollback  s-   KK/!"$)*,9: ; JJ 5 6K0077
--
A
A((*( B  dkk"mm.33&%002&79 &224;;37==3C3:3E3E3GI  (( K$$UK,I,IJJKs   AD   E3$EEN)F)__name__
__module____qualname____doc__detailed_helpr   ChoiceEnumMapperr	   GetMessagesModule)DeploymentmanagerDeploymentsUpdateRequestrw   ru   staticmethodr
   ReleaseTrackGAr4   r;   rq   r[   rh   rW   r~    r5   r3   r   r   -   s      -8 7I66t18<112M2ML!# ,,// *F *FXLPMl ,1	
r5   r   c                   <     e Zd ZdZed        Z fdZ fdZ xZS )CreateAlphar   c                 0   t         j                  | t        j                  j                         t        j                  |        | j                  j                  t
        j                         t         j                  j                  j                  |        y N)r0   )r   r4   r
   r   ALPHAr   AddCredentialFlagr,   r-   r.   ru   
choice_argAddToParserr/   s    r3   r4   zCreateAlpha.Args3  s`    
KK 1 1 7 7K8!!&)
!!+"J"JK
""--99&Ar5   c                     |j                   r/t        j                  | j                  |j                         |_         t        t
        |   ||       y r}   )
credentialr   CredentialFromrT   superr   rW   )r9   rF   rG   	__class__s      r3   rW   zCreateAlpha._SetMetadata:  s<    %44T]]59__Fj	+t)$
;r5   c                 2    t         t        |   |||d      S NT)rF   r=   rG   ry   )r   r   r[   r9   rF   r=   rG   r   s       r3   r[   zCreateAlpha._BuildRequest@  s(    d1#	 2 % %r5   )	r   r   r   r   r   r4   rW   r[   __classcell__r   s   @r3   r   r   )  s-     B B<% %r5   r   c                   2     e Zd ZdZed        Z fdZ xZS )
CreateBetar   c                     t         j                  | t        j                  j                         t         j
                  j                  j                  |        y r   )r   r4   r
   r   BETAru   r   r   r   s    r3   r4   zCreateBeta.ArgsR  s8    
KK 1 1 6 6K7
""--99&Ar5   c                 2    t         t        |   |||d      S r   )r   r   r[   r   s       r3   r[   zCreateBeta._BuildRequestW  s(    T0#	 1 % %r5   )r   r   r   r   r   r4   r[   r   r   s   @r3   r   r   H  s(     B B% %r5   r   ).r   
__future__r   r   r   apitools.base.pyr   r_   )googlecloudsdk.api_lib.deployment_managerr   r   rf   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   -googlecloudsdk.command_lib.deployment_managerr   r   r   r   r   $googlecloudsdk.command_lib.util.apisr   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   r   r   rd   UnicodeIsSupportedReleaseTracksr   r   UseDmApiDmApiVersionV2CreateCommand	DmCommandr   r   r   r   V2BETAr   r   r5   r3   <module>r      s    " &  ' > A = Q , ( . E A B ? B : < # * 
   D%%(()'&&))*vT!2!2 v + * vr D%%++,'&&,,-%& % . - %8 D%%**+'&&--.% % / , %r5   