
    F                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  Sr " S S\5      rS rS rS rS rS rSS jrS rS rS r S r!S r"g)z(Common classes and functions for images.    )absolute_import)division)unicode_literals)	constants)request_helper)utils)arg_parsers)log)
properties)
console_io)universe_descriptor)fileszfamily/c                   H    \ rS rSrSrS rS rS rS r       S
S jr	S	r
g)ImageExpander"   z"Class for expanding image aliases.c                     UR                   U l        UR                  U l        UR                   R                  U l        UR                  U l        X l        g)a  Instantiate ImageExpander and embed all required data into it.

ImageExpander is a class depending on "base_classes"
class layout (properties side-derived from one of base_class class). This
function can be used to avoid unfeasible inheritance and use composition
instead when refactoring away from base_classes into stateless style.

This constructor embeds following properties into ImageExpander instance:
 - compute
 - messages
 - http
 - batch_url
 - resources

Example:
  compute_holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
  client = compute_holder.client
  resources = compute_holder.resources

  image_expander = ImageExpander(client, resources)
    or
  image_expander = ImageExpander(self.compute_client, self.resources)
    to use in a class derived from some of base_classes

  image_expander.ExpandImageFlag(...)

Args:
  compute_client: compute_holder.client
  resources: compute_holder.resources
N)	apitools_client_computemessages	_messageshttp_http	batch_url
_batch_url
_resources)selfcompute_client	resourcess      1lib/googlecloudsdk/api_lib/compute/image_utils.py__init__ImageExpander.__init__%   sC    > #22DM#,,DN//44DJ$..DOO    c           	         U R                   R                  nUSU R                  R                  SR	                  UR
                  5      [        R                  UR                  S94USU R                  R                  SR	                  U5      [        R                  US94/n[        R                  " UU R                  U R                  US9$ )zAYields images from a public image project and the user's project.Listzname eq ^{0}(-.+)*-v.+)filter
maxResultsprojectzname eq ^{0}$requestsr   r   errors)r   imagesr   ComputeImagesListRequestformatname_prefixr   MAX_RESULTS_PER_PAGEr'   r   MakeRequestsr   r   )r   user_projectimagealiasr*   servicer)   s          r   GetMatchingImagesImageExpander.GetMatchingImagesJ   s    mm""G				0	0,33E4E4EF!66]] 
1 
$	% 
			0	0#**51!66! 
1 
#	$H &&ZZ//	 r"   c           
      p   / n/ nUR                  5       nUR                  [        5      (       a[  UR                  U R                  R
                  SU R                  R                  U[        [        5      S UR                  S945        OJUR                  U R                  R
                  SU R                  R                  UUR                  S945        [        [        R                  " UU R                  U R                  US95      nU(       a$  [         R"                  " U[         R$                  SS9  US	   $ )
z@Returns the image resource corresponding to the given reference.GetFromFamilyNfamilyr'   Get)r2   r'   r(   zCould not fetch image resource:)error_messager   )Name
startswithFAMILY_PREFIXappendr   r+   r   !ComputeImagesGetFromFamilyRequestlenr'   ComputeImagesGetRequestlistr   r0   r   r   r   RaiseExceptionImageNotFoundError)r   	image_refr*   r)   nameress         r   GetImageImageExpander.GetImageb   s   FH>>D}%%oot}}++&~~GG"&s='9':";#,#4#4 H 67 8 oot}}++~~==!%#,#4#4 > 67 8 ~**ZZ//	 C
 


"
"9; q6Mr"   c                     [         R                  " 5       (       a  U$ [        R                  " 5       R	                  [         R
                  " 5       5      R                  nUS-   U-   $ )N:)r   IsDefaultUniverser   UniverseDescriptorr;   GetUniverseDomainproject_prefix)r   project_nameprefixs      r   _AddUniversePrefix ImageExpander._AddUniversePrefix   sS    ##%% 
0
0
23z++-.> 
 c\L((r"   Nc	                 	   U(       a*  U R                   R                  USS9n	U	R                  5       nU=(       a    U[        R                  ;   n
SnSnU=(       d.    [
        R                  R                  R                  R                  nSU0nU(       a  U R                   R                  UUUS9nGOU(       aM  U=(       d2    [
        R                  R                  R                  R                  5       nU(       d  U
(       a  SOSnU(       a  US:X  a  SUS	'   S
nOcUb(  [        R                  U   nU R                  S5      US'   O8[        R                  nU R                  S5      US'   U(       a  US:w  a  SUS	'   S
nU R                   R                  UUUS9n[!        US5      (       a  UR"                  R%                  [&        5      (       df  UR)                  5       nUS[+        UR"                  5      *  [&        -   UR"                  -   nU R                   R-                  XR/                  5       5      nU(       a+  UR1                  5       U(       a  U R3                  U5      4$ S4$ [        R4                  R7                  UR                  5       5      nU(       d-  [        R8                  R7                  UR                  5       5      nU(       d+  UR1                  5       U(       a  U R3                  U5      4$ S4$ [;        U5        / nU R=                  XR                  5       UU5      nSn/ nU H`  nUR>                  (       a  M  U R                   R                  UR@                  SSS9nUR                  U:X  a  UnMO  URC                  U5        Mb     U(       d  U(       d?  [D        RF                  " USRI                  UR                  5       UR                  5      5        S n[K        UUS9nU(       aW  UU/n[L        RN                  " U Vs/ s H  o"R@                  PM     snSSRI                  UR                  5       5      S9nUU   nOUn[P        RR                  " SUR@                  5        UR@                  U(       a  U4$ S4$ s  snf )aE  Resolves the image or image-family value.

If the value of image is one of the aliases defined in the
constants module, both the user's project and the public image
project for the alias are queried. Otherwise, only the user's
project is queried. If image is an alias and image-project is
provided, only the given project is queried.

Args:
  user_project: The user's project.
  image: The name of the image.
  image_family: The family of the image. Is ignored if image name is
    specified.
  image_project: The project of the image.
  return_image_resource: If True, always makes an API call to also
    fetch the image resource.
  confidential_vm_type: If not None, use default guest image based on
    confidential-VM encryption type.
  image_family_scope: Override for selection of global or zonal image
    views.
  support_image_family_scope: If True, add support for the
    --image-family-scope flag.

Returns:
  A tuple where the first element is the self link of the image. If
    return_image_resource is False, the second element is None, otherwise
    it is the image resource.
zcompute.projects
collectionNzcompute.imagesr'   )paramsrX   zonal-zonezcompute.imageFamilyViewszubuntu-os-cloudzdebian-cloudglobalr2   T)rX   enforce_collectionzCFailed to find image for alias [{0}] in public image project [{1}].c                     U R                   R                  SS5      n[        U5      S:w  a"  [        R                  " SU R                   5        gUS   $ )zCExtracts the "20140718" from an image name like "debian-v20140718".v      z(Skipping image with malformed name [%s]. )rH   rsplitrB   r
   debug)r2   partss     r   
GetVersion1ImageExpander.ExpandImageFlag.<locals>.GetVersion  sB    jjQ'e	Uq		<ejjI1Xor"   )keyr   z5Found two possible choices for [--image] value [{0}].)optionsdefaultmessagezImage resolved to [%s].)*r   Parser=   r   PUBLIC_IMAGE_PROJECTSr   VALUEScorer'   	GetOrFailcomputeimage_family_scoper;   )DEFAULT_IMAGE_FAMILY_FOR_CONFIDENTIAL_VMSrT   DEFAULT_IMAGE_FAMILYhasattrr2   r>   r?   RelativeNamerB   ParseRelativeName
CollectionSelfLinkrJ   IMAGE_ALIASESgetHIDDEN_IMAGE_ALIASES	WarnAliasr5   
deprecatedselfLinkr@   r   RaiseToolExceptionr-   maxr   PromptChoicer
   re   )r   r1   r2   image_familyimage_projectreturn_image_resourceconfidential_vm_typers   support_image_family_scopeimage_project_refpublic_image_projectrG   rX   r'   rY   relative_namer3   r*   r+   
user_imagepublic_images
image_ref2rg   public_candidaterj   idxrI   s                              r   ExpandImageFlagImageExpander.ExpandImageFlag   s3   P ////
$6 0 8',,.m) ?m(>>/? I!JGz0055==GGG!F//''
 ( !i 
$ B  ((;;??A 	 "*>wD
	(&.1*+ JJ 
 !334EFy 55 33NCy%*<*H&.1*//''
 ( !i
 )W
%
%//,,];;!..0&'=Y__)=(=>&')29OO55//13	   "*?dmmI&K KEIK K ##''	(89E ,,001ABe
   "*?dmmI&K KEIK K eF##L..2BE$*,F JM			??((
..%5$ ) Pj			|	+
U#  ] 

O6).."EMM24
 =j9-.g##/67we>>w7JF9>>+,	/c CLc cII'6LL!6#AADAA 8s   	R)r   r   r   r   r   )NNNFNNF)__name__
__module____qualname____firstlineno____doc__r    r5   rJ   rT   r   __static_attributes__ r"   r   r   r   "   s8    *# J0<	) !#'$(,1+/)-16lBr"   r   c                     U R                    H3  nUR                  USS9nUR                  [        R                  ;   d  M3    g   g)z>Returns True if the given image or disk has a Windows license.zcompute.licensesrW   TF)licensesrm   r'   r   WINDOWS_IMAGE_PROJECTS)resourceresource_parserlicense_urilicense_refs       r   HasWindowsLicenser   :  sI    &&k!'' 2 ( 4Ki>>>	 '
 
r"   c                 $    U R                  SSS9  g)z*Adds the --image flag to the given parser.z--image-projecta        The Google Cloud project against which all image and
      image family references will be resolved. It is best practice to define
      image-project. A full list of available projects can be generated by
      running `gcloud projects list`.
          * If specifying one of our public images, image-project must be
            provided.
          * If there are several of the same image-family value in multiple
            projects, image-project must be specified to clarify the image to be
            used.
          * If not specified and either image or image-family is provided, the
            current default project is used.
        )helpNadd_argumentparsers    r   AddImageProjectFlagr   D  s      r"   c                     SnU R                   b(  USR                  U R                   U R                  S9-  nOUS-  n[        R                  " U5        g)zEWarnAlias outputs a warning telling users to not use the given alias.zFImage aliases are deprecated and will be removed in a future version. NzIPlease use --image-family={family} and --image-project={project} instead.r9   z6Please use --image-family and --image-project instead.)r:   r-   r'   r
   warning)r3   msgs     r   r~   r~   W  sR    
#
\\ ellEMMJKC CCC++cr"   c                     UR                   R                  nUR                  R                  /n[	        UR                  5        Vs/ s H  oDU;  d  M
  UPM     sn5      nU R                  SUSS9  gs  snf )zAdd the image architecture arg.z--architecturezSpecifies the architecture or processor type that this image can support. For available processor types on Compute Engine, see https://cloud.google.com/compute/docs/cpu-platforms.)choicesr   N)ImageArchitectureValueValuesEnumARCHITECTURE_UNSPECIFIEDrH   sortednamesr   )r   r   architecture_enum_typeexcluded_enumsearchitecture_choicess         r   AddArchitectureArgr   d  sy    #>>EE*CCHHI.(..0L0Q^4Kq0LN" @	  	 Ms   	A1A1c                 l    [        U UUR                  R                  R                  R                  /S9  g)z2Add the guest-os-features arg for import commands.)supported_featuresN)AddGuestOsFeaturesArgGuestOsFeatureTypeValueValuesEnumUEFI_COMPATIBLErH   )r   r   s     r   AddGuestOsFeaturesArgForImportr   r  s0    

!
!
5
5
E
E
J
J	r"   Nc           
      *   UR                   R                  nSS/n[        UR                  5       5      nUR	                  U5        U(       a  UR                  U5        U(       d  gU R                  SS[        R                  " S [        U5      S9SS	9  g)
zAdd the guest-os-features arg.FEATURE_TYPE_UNSPECIFIEDSECURE_BOOTNz--guest-os-featuresGUEST_OS_FEATUREc                 "    U R                  5       $ N)upper)xs    r   <lambda>'AddGuestOsFeaturesArg.<locals>.<lambda>  s
    r"   )element_typer   z      Enables one or more features for VM instances that use the
      image for their boot disks. See the descriptions of supported features at:
      https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images#guest-os-features.)metavartyper   )
r   r   setr   difference_updateintersection_updater   r	   ArgListr   )r   r   r   features_enum_typer   guest_os_featuress         r   r   r   |  s    ..BB .
 ,2245%%n5))*<=	
 *F;L4MOp  qr"   c                 ,    U R                  SSSS/SS9  g)z Add the image-family-scope flag.z--image-family-scopeIMAGE_FAMILY_SCOPErZ   r]   a        Sets the scope for the `--image-family` flag. By default, when
      specifying an image family in a public image project, the zonal image
      family scope is used. All other projects default to the global
      image. Use this flag to override this behavior.)r   r   r   Nr   r   s    r   AddImageFamilyScopeFlagr     s(    "!9	  :r"   c                     [         R                  " U 5      nSnU R                  S5      (       a  SnX4$ [        U5      (       d  [        R
                  " S5      eSnX4$ )z;Helper function used for read file and determine file type.rc   z.binBINz$File is not in X509 binary DER form.X509)
file_utilsReadBinaryFileContentsendswith	IsDERFormr   IncorrectX509FormError)	file_pathfile_content	file_types      r   GetFileContentAndFileTyper     se    229=,)I
 
	   \""(()OPPI		  r"   c                 <    [        U 5      S:  =(       a    U SS S:H  $ )zFHelper function that returns true if the file is X509 binary DER form.rb   r   s   0)rB   )r   s    r   r   r     s%     
\	a		DL1$5$DDr"   c                     U R                  5       n[        U5      u  p4X2l        U R                   R                  U5      Ul        U$ )z:Helper function to read file and return FileContentBuffer.)FileContentBufferr   contentFileTypeValueValuesEnumfileType)r   r   file_content_bufferr   r   s        r   CreateFileContentBufferr     sF     2240;' '  88C 	r"   c                 (   UR                  5       nSnU R                  (       a  [        UU R                  5      nXBl        Sn[	        U S/ 5      nU(       a1  U H+  n[        X5      nUR
                  R                  U5        SnM-     [	        U S/ 5      nU(       a1  U H+  n[        X5      nUR                  R                  U5        SnM-     [	        U S/ 5      nU(       a1  U H+  n[        X5      nUR                  R                  U5        SnM-     X#4$ )z5Helper function used for creating InitialStateConfig.FTkey_exchange_key_filesignature_database_fileforbidden_database_file)	InitialStateConfigplatform_key_filer   pkgetattrkeksr@   dbsdbxs)	argsr   initial_state_confighas_setr   key_exchange_key_file_pathsr   signature_database_file_paths'forbidden_signature_database_file_pathss	            r   CreateInitialStateConfigr     s   !446'	1(262H2HJ1G '.Er J 0	3HH&&':;g 1 #*$0I2"N"2	3HH%%&9:g 3 -4D4M46-8) -<	3HH&&':;g = 
	&&r"   r   )#r   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   r   googlecloudsdk.callioper	   googlecloudsdk.corer
   r   googlecloudsdk.core.consoler   'googlecloudsdk.core.universe_descriptorr   googlecloudsdk.core.utilr   r   r?   objectr   r   r   r~   r   r   r   r   r   r   r   r   r   r"   r   <module>r     s}    / &  ' 4 9 0 / # * 2 G 8UBF UBp&
		q4
:
!E'r"   