
    o                        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  e	j6                  e	j8                  j:                        e	j<                   G d de	j>                                      Z  e	j6                  e	j8                  jB                        e	j<                   G d de	j>                                      Z"y)zCreate a new secret.    )absolute_import)division)unicode_literals)api)base)
exceptions)args)log)util)crc32c)labels_util)map_util)
properties)
console_ioc                   n    e 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ZdZdZdZdZdZdZed        Zd Zy)Createa  Create a new secret.

  Create a secret with the given name and creates a secret version with the
  given data, if any. If a secret already exists with the given name, this
  command will return an error.

  ## EXAMPLES

  Create a secret with an automatic replication policy without creating any
  versions:

    $ {command} my-secret

  Create a new secret named 'my-secret' with an automatic replication policy
  and data from a file:

    $ {command} my-secret --data-file=/tmp/secret

  Create a new secret named 'my-secret' in 'us-central1' with data from a file:

    $ {command} my-secret --data-file=/tmp/secret
    --replication-policy=user-managed \
        --locations=us-central1

  Create a new secret named 'my-secret' in 'us-central1' and 'us-east1' with
  the value "s3cr3t":

    $ printf "s3cr3t" | {command} my-secret --data-file=-
    --replication-policy=user-managed --locations=us-central1,us-east1

  Create a new secret named 'my-secret' in 'us-central1' and 'us-east1' with
  the value "s3cr3t" in PowerShell (Note: PowerShell will add a newline to the
  resulting secret):

    $ Write-Output "s3cr3t" | {command} my-secret --data-file=-
    --replication-policy=user-managed --locations=us-central1,us-east1

  Create a secret with an automatic replication policy and a next rotation time:

    $ {command} my-secret --next-rotation-time="2030-01-01T15:30:00-05:00"

  Create a secret with an automatic replication policy and a rotation period:

    $ {command} my-secret --next-rotation-time="2030-01-01T15:30:00-05:00"
    --rotation-period="7200s"

  Create a secret with delayed secret version destroy enabled:

    $ {command} my-secret --version-destroy-ttl="86400s"
  The value provided for --data-file is the empty string. This can happen if you pass or pipe a variable that is undefined. Please verify that the --data-file flag is not the empty string. If you are not providing secret data, omit the --data-file flag.hThe value provided for --replication-policy is invalid. Valid values are "automatic" and "user-managed".Cannot use the secrets/replication-policy property because its value is invalid. Please either set it to a valid value ("automatic" or "user-managed") or override it for this command by using the --replication-policy flag.(  If --replication-policy is user-managed then --locations must also be provided. Please set the desired storage regions in --locations or the secrets/locations property. For an automatic replication policy, please set --replication-policy or the secrets/replication-policy property to "automatic".If --replication-policy is "automatic" then --locations are not allowed. Please remove the --locations flag or set the --replication-policy to "user-managed".The secrets/replication-policy property is "automatic" and not overridden so --locations are not allowed. Please remove the --locations flag or set the replication-policy to "user-managed".Cannot create a secret with an "automatic" replication policy if the secrets/locations property is set. Please either use a "user-managed" replication policy or unset secrets/locations.Locations are only allowed when creating a secret with a "user-managed" replication policy. Please use the --replication-policy flag to set it or remove --locations to use an automatic replication policy.The --kms-key-name flag can only be used when creating a secret with an "automatic" replication policy. To specify encryption keys for secrets with a "user-managed" replication policy, please use --replication-policy-file.NA --replication-policy-file and --replication-policy cannot both be specified.EA --replication-policy-file and --locations cannot both be specified.HA --replication-policy-file and --kms-key-name cannot both be specified.File cannot be empty.The --kms-key-name flag can only be set for automatically replicated secrets. To create a user managed secret with customer managed encryption keys, please use --replication-policy-file.rThis secret and all of its versions will be automatically deleted at the requested expire-time of [{expire_time}].qThis secret and all of its versions will be automatically deleted after the requested ttl of [{ttl}] has elapsed.The --regional-kms-key-name flag can only be used when creating a regional secret with "--location" and should not be used with "--replication-policy-file" or "--kms-key-name"Regional secret created using "--location" should not have flags like "--replication-policy-file" or "--kms-key-name" or "--locations" or --replication-policyc                 d   t        j                  | ddd       t        j                  | dd       t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                         j                  |        | j                  dd      }t        j                   |d	dt"        t"               y
)zArgs is called by calliope to gather arguments for this command.

    Args:
      parser: An argparse parser that you can use to add arguments that will be
        available to the command.
    	to createTpurpose
positionalrequiredto create secretFr(   hiddenAnnotationsmutexhelpannotationsN)secrets_args	AddSecretAddLocationAddDataFileAddCreateReplicationPolicyGroupr   AddCreateLabelsFlagsAddCreateExpirationGroup	AddTopicsAddCreateRotationGroupAddRegionalKmsKeyNameAddCreateVersionDestroyTTL
GetTagsArgAddToParser	add_groupr   AddMapSetFlagstrparserr2   s     lib/surface/secrets/create.pyArgszCreate.Args   s     t V-?NV$008$$V,))&16"''/&&v.++F3))&1""M"BK;}c3O    c                    t        j                  | j                               }t        j                  |      }|j                  j
                  j                         }|j                  du}t        j                  |j                        }t        j                  |j                  d      }t        j                  ||j                  j                        }t!        j"                  ||j                  j$                        }	|j&                  }
|j(                  }g }|j                  r+|j&                  rt+        j,                  | j.                        |j                  r+|j(                  rt+        j,                  | j0                        |j                  r+|j2                  rt+        j,                  | j4                        |s+|j6                  rt+        j,                  | j8                        |j6                  r7|j                  s|j2                  rt+        j,                  | j8                        |rE|
s$|j2                  s|j                  s|j(                  rt+        j,                  | j:                        |j2                  r|j=                  |j2                         |j                  r=|s t+        j>                  d| j@                        t        jB                  |      \  }
}}n|
s2tD        jF                  jH                  j&                  jK                         }
|
du }|rd}
|
dvrL|j&                  r t+        j>                  d| jL                        t+        j>                  d| jN                        |
dk(  r"|r t+        j>                  d	| jP                        |sEtD        jF                  jH                  j(                  jK                         }|r|jS                  d
      }|
dk(  r"|s t+        jT                  d| jV                        |
dk(  r|j(                  rn|j&                  r t+        j>                  d| jX                        |r t+        j>                  d| jZ                        t+        j>                  d| j\                        |r t+        j>                  d| j^                        g }|j                  dk(  rt+        j`                  | jb                        |jd                  r>| jf                  ji                  |jd                        }tk        jl                  |dd       |jn                  r>| jp                  ji                  |jn                        }tk        jl                  |dd       g }|js                  d      rQ|jt                  jw                         D cg c],  \  }}|j                  jx                  j{                  ||      . }}}|rd}
|j|                  r|j|                   d}nd}t        j~                  |      j                  |||	||
|jd                  |jn                  ||j                  ||j                  |j                  |j6                  ||j                        }|rt        j                  |      }t        j~                  |      j                  ||t        j                  |      |j                        }|r t!        j                  |j                        }nt!        j                  |j                        }t        j                         j                  |       |S t        j~                         j                  |       |S c c}}w )zRun is called by calliope to create the secret.

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

    Returns:
      The response from the create secret API call.
    versionNF	is_binaryreplication-policy	automatic>   rN   user-managedrO   kms-key-name,	locations expire_timeTthrow_if_unattendedcancel_on_nottlset_annotationskeyvaluesapi_version)labelstagsrR   policyrU   rZ   keystopicsr2   next_rotation_timerotation_periodregional_kms_key_nameversion_destroy_ttlsecret_locationrk   )Nsecrets_apiGetApiFromTrackReleaseTrackGetMessagesCONCEPTSsecretParselocationsecrets_utilReadFileOrStdin	data_filereplication_policy_filer   ParseCreateArgsSecretLabelsValuer3   GetTagsFromArgs	TagsValuereplication_policyrR   r   ConflictingArgumentsExceptionPOLICY_AND_POLICY_FILE_MESSAGE!LOCATIONS_AND_POLICY_FILE_MESSAGEkms_key_nameKMS_KEY_AND_POLICY_FILE_MESSAGEri   REGIONAL_KMS_FLAG_MESSAGEREGIONAL_SECRET_MESSAGEappendInvalidArgumentException%REPLICATION_POLICY_FILE_EMPTY_MESSAGEParseReplicationFileContentsr   VALUESsecretsGetINVALID_POLICY_MESSAGEINVALID_POLICY_PROP_MESSAGE KMS_KEY_AND_USER_MANAGED_MESSAGEsplitRequiredArgumentException MANAGED_BUT_NO_LOCATIONS_MESSAGEAUTOMATIC_AND_LOCATIONS_MESSAGENO_POLICY_AND_LOCATIONS_MESSAGE$AUTOMATIC_PROP_AND_LOCATIONS_MESSAGE$AUTOMATIC_AND_LOCATIONS_PROP_MESSAGEBadFileExceptionEMPTY_DATA_FILE_MESSAGErU   CONFIRM_EXPIRE_TIME_MESSAGEformatr   PromptContinuerZ   CONFIRM_TTL_MESSAGEIsSpecifiedr[   itemsAnnotationsValueAdditionalPropertyrj   Secretsr   rf   rg   rh   r   
get_crc32c
AddVersionget_checksumParseRegionalVersionRefnameParseVersionRefsecrets_logVersionsCreatedselfr	   ra   messages
secret_refis_regionaldatareplication_policy_contentsrb   rc   r~   rR   kms_keysdefault_to_automaticmsgr2   
annotationmetadatarj   responsedata_crc32crJ   version_refs                          rE   Runz
Create.Run   s    --d.?.?.ABK&&{;H%%++-J--t+K''7D".">">$$#7((x/J/JKF''hoo.G.GHD00IH##(?(?44

-
-/ /##44

0
02 2##(9(944

.
.0 045544

(
(  !!$$(9(944

(
(  ''>>44

&
&  ood''(##(11 $"L"L
 	
 
3
34O
P .)X  '..66IIMMO/47	(	#@	@""334H484O4OQ Q11 $"B"BD 	D	~	-(11DAAC 	C %%--77;;=	ooc*)	~	-i22>>@ 	@	{	*>> $$55TAAC C!55TAAC C334DDF F33"D$M$MO O	 ~~''(D(DEE,,33&& 4 (c
4d< xx$$+++9c
4d< K)* )-(<(<(B(B(D )E$z8 //
*
*
=
=H > .(D   #778: ""{;BB!$$HH{{22,,"88/ C H$ %%d+k##<GG




k
*--	 H g 
"::7<<H"227<<@$$[1 O ##J/O_   1]N__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   MANAGED_AND_KMS_FLAG_MESSAGEr   r   r   r   r   r   r   r   r   staticmethodrF   r    rG   rE   r   r   "   s    1j0 , 
#  #0 "? '7 'F "#  !
 N $ Q " ,C'? #6 
8 
9   P P.irG   r   c                   n    e 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ZdZdZdZdZdZdZed        Zd Zy)
CreateBetaa  Create a new secret.

  Create a secret with the given name and creates a secret version with the
  given data, if any. Note, the created secret ends with a newline.
  If a secret already exists with the given name, this command will return
  an error.

  ## EXAMPLES

  Create a secret with an automatic replication policy without creating any
  versions:

    $ {command} my-secret

  Create a new secret named 'my-secret' with an automatic replication policy
  and data from a file:

    $ {command} my-secret --data-file=/tmp/secret

  Create a new secret named 'my-secret' in 'us-central1' with data from a file:

    $ {command} my-secret --data-file=/tmp/secret
    --replication-policy=user-managed \
        --locations=us-central1

  Create a new secret named 'my-secret' in 'us-central1' and 'us-east1' with
  the value "s3cr3t":

    $ printf "s3cr3t" | {command} my-secret --data-file=-
    --replication-policy=user-managed --locations=us-central1,us-east1

  Create a new secret named 'my-secret' in 'us-central1' and 'us-east1' with
  the value "s3cr3t" in PowerShell (Note: PowerShell will add a newline to the
  resulting secret):

    $ Write-Output "s3cr3t" | {command} my-secret --data-file=-
    --replication-policy=user-managed --locations=us-central1,us-east1

  Create an expiring secret with an automatic replication policy using a ttl:

    $ {command} my-secret --ttl="600s"

  Create an expiring secret with an automatic replication policy using an
  expire-time:

    $ {command} my-secret --expire-time="2030-01-01T08:15:30-05:00"

  Create a secret with an automatic replication policy and a next rotation time:

    $ {command} my-secret --next-rotation-time="2030-01-01T15:30:00-05:00"

  Create a secret with an automatic replication policy and a rotation period:

    $ {command} my-secret --next-rotation-time="2030-01-01T15:30:00-05:00"
    --rotation-period="7200s"

  Create a secret with delayed secret version destroy enabled:

    $ {command} my-secret --version-destroy-ttl="86400s"
  r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   c                 d   t        j                  | ddd       t        j                  | dd       t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                         j                  |        | j                  dd      }t        j                   |d	dt"        t"               y )
Nr&   Tr'   r+   Fr,   r.   r/   r2   )r3   r4   r5   r6   r7   r   r8   r9   r;   r:   r<   r=   r>   r?   r@   r   rA   rB   rC   s     rE   rF   zCreateBeta.Args  s    t V-?NV$008$$V,))&1''/6"&&v.++F3))&1""M"BK;}c3OrG   c                    t        j                  | j                               }t        j                  |      }|j                  j
                  j                         }|j                  d u}t        j                  |j                        }t        j                  |j                  d      }t        j                  ||j                  j                        }t!        j"                  ||j                  j$                        }	|j&                  }
|j(                  }g }|j                  r+|j&                  rt+        j,                  | j.                        |j                  r+|j(                  rt+        j,                  | j0                        |j                  r+|j2                  rt+        j,                  | j4                        |s+|j6                  rt+        j,                  | j8                        |j6                  r7|j                  s|j2                  rt+        j,                  | j8                        |rE|
s$|j2                  s|j                  s|j(                  rt+        j,                  | j:                        |j2                  r|j=                  |j2                         |j                  r=|s t+        j>                  d| j@                        t        jB                  |      \  }
}}n|
s2tD        jF                  jH                  j&                  jK                         }
|
d u }|rd}
|
dvrL|j&                  r t+        j>                  d| jL                        t+        j>                  d| jN                        |
dk(  r"|r t+        j>                  d| jP                        |sEtD        jF                  jH                  j(                  jK                         }|r|jS                  d	      }|
dk(  r"|s t+        jT                  d
| jV                        |
dk(  r|j(                  rn|j&                  r t+        j>                  d
| jX                        |r t+        j>                  d
| jZ                        t+        j>                  d
| j\                        |r t+        j>                  d| j^                        g }|j                  dk(  rt+        j`                  | jb                        |jd                  r>| jf                  ji                  |jd                        }tk        jl                  |dd       |jn                  r>| jp                  ji                  |jn                        }tk        jl                  |dd       g }|js                  d      rQ|jt                  jw                         D cg c],  \  }}|j                  jx                  j{                  ||      . }}}|rd }
|j|                  r|j|                   d}nd }t        j~                  |      j                  |||	||
|jd                  |jn                  ||j                  |j                  |j                  ||j6                  ||j                        }|rt        j                  |      }t        j~                  |      j                  ||t        j                  |      |j                        }|r t!        j                  |j                        }nt!        j                  |j                        }t        j                         j                  |       |S t        j~                         j                  |       |S c c}}w )NrI   FrK   rM   rN   >   rN   rO   rO   rP   rQ   rR   rS   rT   TrV   rY   r[   r\   r_   r`   )rb   rc   rR   rd   rU   rZ   re   rg   rh   rf   r2   ri   rj   rk   rl   )Nrm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   r   ry   rz   r{   r3   r|   r}   r~   rR   r   r   r   r   r   r   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   r   r   r   rZ   r   r   r[   r   r   r   rj   r   r   rg   rh   rf   r   r   r   r   r   r   r   r   r   r   r   s                          rE   r   zCreateBeta.Run   s   --d.?.?.ABK&&{;H%%++-J--t+K''7D".">">$$#7((x/J/JKF''hoo.G.GHD00IH##(?(?44

-
-/ /##44

0
02 2##(9(944

.
.0 045544

(
(  !!$$(9(944

(
(  ''>>44

&
&  ood''(##(11 $"L"L
 	
 
3
34O
P .)X  '..66IIMMO/47	(	#@	@""334H484O4OQ Q11 $"B"BD 	D	~	-(11DAAC 	C %%--77;;=	ooc*)	~	-i22>>@ 	@	{	*>> $$55TAAC C!55TAAC C334DDF F33"D$M$MO O	 ~~''(D(DEE,,33&& 4 (c
4d< xx$$+++9c
4d< K)* )-(<(<(B(B(D )E$z8 //
*
*
=
=H > .(D   #778: ""{;BB!$$HH22,,{{"88/ C H$ %%d+k##<GG




k
*--	 H g 
"::7<<H"227<<@$$[1 O ##J/O_r   Nr   r   rG   rE   r   r   u  s    ;~0 , 
#  #0 "? '7 'F "#  !
 N $ Q " ,C'? #6 
8 
9   P P"brG   r   N)#r   
__future__r   r   r   googlecloudsdk.api_lib.secretsr   rm   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.secretsr	   r3   r
   r   r   ru   googlecloudsdk.command_lib.utilr   $googlecloudsdk.command_lib.util.argsr   r   googlecloudsdk.corer   googlecloudsdk.core.consoler   ReleaseTracksro   GADefaultUniverseOnlyCreateCommandr   BETAr   r   rG   rE   <module>r      s     &  ' = ( . C A C 2 < 9 * 2 D%%(()NT N  *Nb
 D%%**+K## K  ,KrG   