
    s_                     <   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       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 ddZ	 	 	 	 	 	 	 ddZ	 ddZ	 	 	 	 	 ddZ	 	 	 	 	 ddZd Z y)z:Convenience functions for dealing with instance templates.    )absolute_import)division)unicode_literals)alias_ip_range_utils)	constants)image_utils)instance_utils)	kms_utils)utils)scope)flags)labels_util)
propertiesNc                 0   |j                         }|Yt        j                         j                  |gt        j
                  j                  || |      d   }|j                         |_        |X| j                  |dt        j                  j                  j                  j                  id      }|j                         |_        ni|g|e| j                  t         j"                  dt        j                  j                  j                  j                  id      }|j                         |_        |||_        |
 |j                   j'                  |
      |_        |r~|
dk7  ry|j+                  t         j,                  |j*                  j.                  j0                        }|t2        k7  r||_        |	 |j*                  j7                  |	      |_        |g|_        d}||G|j+                  t         j<                  |j*                  j.                  j>                        }|g|_         | |j*                  j7                  |      |_        |||_!        ||}||}|P|G|j+                  t         j<                  |j*                  j.                  j>                        }|g|_         ||_"        |P|G|j+                  t         j<                  |j*                  j.                  j>                        }|g|_         ||_#        |||_$        |||_%        |rtM        jN                  |d	|      |_(        | |j                   jS                  |      |_*        |||_+        |||_,        |||_-        | |j                   j]                  |      |_/        |r||_0        |S )
a
  Creates and returns a new NetworkInterface message.

  Args:
    resources: generates resource references,
    scope_lister: function, provides scopes for prompting subnet region,
    messages: GCE API messages,
    network: network,
    private_ip: IPv4 internal IP address to assign to the instance.
    subnet_region: region for subnetwork,
    subnet: regional subnetwork,
    address: specify static address for instance template
               * None - no address,
               * EPHEMERAL_ADDRESS - ephemeral address,
               * string - address name to be fetched from GCE API.
    alias_ip_ranges_string: command line string specifying a list of alias
        IP ranges.
    network_tier: specify network tier for instance template
               * None - no network tier
               * PREMIUM - network tier being PREMIUM
               * SELECT - network tier being SELECT
               * STANDARD - network tier being STANDARD
    stack_type: identify whether IPv6 features are enabled
               * IPV4_ONLY - can only have IPv4 address
               * IPV4_IPV6 - can have both IPv4 and IPv6 address
               * IPV6_ONLY - can only have IPv6 address
    ipv6_network_tier: specify network tier for IPv6 access config
               * PREMIUM - network tier being PREMIUM
               * STANDARD - network tier being STANDARD
    nic_type: specify the type of NetworkInterface Controller
               * GVNIC
               * VIRTIO_NET
    ipv6_public_ptr_domain: a string represents the custom PTR domain assigned
      to the interface.
    ipv6_address: a string represents the external IPv6 address reserved to the
      interface.
    ipv6_prefix_length: a string represents the external IPv6 address
      prefix length reserved to the interface.
    external_ipv6_address: a string represents the external IPv6 address
      reserved to the interface.
    external_ipv6_prefix_length: a string represents the external IPv6 address
      prefix length reserved to the interface.
    internal_ipv6_address: a string represents the internal IPv6 address
      reserved to the interface.
    internal_ipv6_prefix_length:  the internal IPv6 address prefix
      length of the internal IPv6 address reserved to the interface.
    network_attachment: URL of a network attachment to connect the interface to.
    network_queue_count: the number of queues assigned to the network interface.
    vlan: the VLAN tag of the network interface.
    igmp_query: the IGMP query mode of the network interface.
    enable_vpc_scoped_dns: If True, indicates that this network interface can be
      used to send DNS queries to the VPC network's scope.

  Returns:
    network_interface: a NetworkInterface message object
  N)scope_listerr   projectzcompute.networks)params
collection	IPV6_ONLY)nametypeF)1NetworkInterfacesubnet_flagsSubnetworkResolverResolveResourcescompute_scope	ScopeEnumREGIONSelfLink
subnetworkParser   VALUEScorer   	GetOrFailnetworkr   DEFAULT_NETWORK	networkIPStackTypeValueValuesEnum	stackTypeAccessConfigDEFAULT_ACCESS_CONFIG_NAMETypeValueValuesEnumONE_TO_ONE_NATEPHEMERAL_ADDRESSnatIPNetworkTierValueValuesEnumnetworkTieraccessConfigsDEFAULT_IPV6_ACCESS_CONFIG_NAMEDIRECT_IPV6ipv6AccessConfigspublicPtrDomainNameexternalIpv6externalIpv6PrefixLengthipv6AddressinternalIpv6PrefixLengthr   $CreateAliasIpRangeMessagesFromStringaliasIpRangesNicTypeValueValuesEnumnicTypenetworkAttachment
queueCountvlanIgmpQueryValueValuesEnum	igmpQueryenableVpcScopedDns)	resourcesr   messagesr%   
private_ipsubnet_regionsubnetaddressalias_ip_ranges_stringnetwork_tier
stack_typeipv6_network_tiernic_typeipv6_public_ptr_domainipv6_addressipv6_prefix_lengthexternal_ipv6_addressexternal_ipv6_prefix_lengthinternal_ipv6_addressinternal_ipv6_prefix_lengthnetwork_attachmentnetwork_queue_countrA   
igmp_queryenable_vpc_scoped_dnsnetwork_interface
subnet_refnetwork_refaccess_configipv6_access_configs                                 =lib/googlecloudsdk/api_lib/compute/instance_template_utils.pyCreateNetworkInterfaceMessagera   $   s   h //1002CC	&&! D #
 $%&J $.#6#6#8 //:,,1199CCD% " 'K !, 4 4 6 ~,4//!!:,,1199CCD% " 'K !, 4 4 6",!!:::F  {*))11""66EE * GM ###m



:
:<
H  (5o#"&<&H!..66""66BB / D ,>*>'"889JK " '-C* "( !("4&!#0088$$88DD 1 F .@,@)&;# ,!#0088$$88DD 1 F .@,@)2M/&$9! ,1L.AAe3	5 # !!88B  #*<'$#6 	!!!:::F  +@(	    c                 n   g }|r|D ]  }|j                  dd      }d|v xs |j                  dd      }	|dk(  s||	st        }|j                  dd      }
|j                  dd      }d}|rd|v }|j                  t        | |||j                  d	d      |j                  d
d      ||j                  dd      ||j                  dd      |
f
||j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  d      |j                  dd      |j                  dd      |j                  dd      |d        |S )a  Create network interface messages.

  Args:
    resources: generates resource references,
    scope_lister: function, provides scopes for prompting subnet region,
    messages: creates resources.
    network_interface_arg: CLI argument specifying network interfaces.
    subnet_region: region of the subnetwork.
    support_enable_vpc_scoped_dns: Indicates whether setting enable vpc scoped
      dns on network interfaces is supported.

  Returns:
    list, items are NetworkInterfaceMessages.
  rJ   Nz
no-addressznetwork-attachment znetwork-tierznic-typezenable-vpc-scoped-dnsr%   zprivate-network-iprI   aliasesz
stack-typezipv6-network-tierzipv6-public-ptr-domainzipv6-addresszipv6-prefix-lengthzexternal-ipv6-addresszexternal-ipv6-prefix-lengthzinternal-ipv6-addresszinternal-ipv6-prefix-lengthzqueue-countrA   z
igmp-query)rO   rM   rN   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rA   rY   rZ   )getr.   appendra   )rE   r   rF   network_interface_argrH   support_enable_vpc_scoped_dnsresult	interfacerJ   has_no_addressrL   rO   rZ   s                r`   CreateNetworkInterfaceMessagesrm      s   , &*	i.g $y0 &IMM
5&n 
B7?N#]]>48lz40h"	& 79 Dmm
'mmIt,mm0$7mmHd+mmIt,$  "|T: ).A4 H%.]]*D& %==>!*/CT!J$-MM)4% +4--/+ %.MM)4% +4--/+ "+/C!D"+--t"D==."|T:$9G$&! +n 
-rb   c                    |	r| j                   ng }t        |j                  | j                  xs g |      }t	        |||t        | dg       |||
|      }|r^t        |j                  | j                  | j                  | j                  ||| || j                  | j                  | j                        g}nJ|r"|d   j                  r|j                  d      g}n&|r"|d   j                  r|j                  d      g}ng }t        j                   | ||j                        }t        j"                  | ||j                        }||z   |z   |z   |z   S )z4Create disk messages for a single instance template.)container_mount_diskcreate_disk)support_kmssupport_multi_writersupport_replica_zonessupport_disk_labels)rF   	disk_typedisk_device_namedisk_auto_deletedisk_size_gb	image_urikms_argsrq   disk_provisioned_iopsdisk_provisioned_throughputdisk_interfacer   )ro   $CreatePersistentAttachedDiskMessagesrF   disk"CreatePersistentCreateDiskMessagesgetattr$CreateDefaultBootAttachedDiskMessageboot_disk_typeboot_disk_device_nameboot_disk_auto_deleteboot_disk_provisioned_iops boot_disk_provisioned_throughputboot_disk_interfacebootpopcreate_utilsCreateLocalNvdimmMessagesCreateLocalSsdMessages)argsclientrE   r   ry   boot_disk_size_gbcreate_boot_diskrq   rr   match_container_mount_disksrs   rt   ro   persistent_diskspersistent_create_disksboot_disk_listlocal_nvdimms
local_ssdss                     r`   CreateDiskMessagesr   D  s~     $?dB  +
//
))/r35  ?dM2&/1-	 ,__))!77!77*#"&"A"A(,(M(M33	
N #:1#=#B#B-11!45N,Q/44&**1-.NN88
oo- 22
oo*   	 		rb   c           	         g }|D ]X  }|d   }|j                  dd      }|dk(  r!| j                  j                  j                  }n | j                  j                  j                  }|j                  dd      }|j                  dd      }	t        j                  |||      }
| j                  |	||
||| j                  j                  j                        }|j                  d      r\|j                  d      d	k(  r!| j                  j                  j                  }n | j                  j                  j                  }||_        |r|g|z   }H|j                  |       [ |S )
a;  Returns a list of AttachedDisk messages and the boot disk's reference.

  Args:
    messages: GCE API messages,
    disks: disk objects - contains following properties
             * name - the name of disk,
             * mode - 'rw' (R/W), 'ro' (R/O) access mode,
             * boot - whether it is a boot disk,
             * auto-delete - whether disks is deleted when VM is deleted,
             * device-name - device name on VM.
    container_mount_disk: list of disks to be mounted to container, if any.

  Returns:
    list of API messages for attached disks
  r   moderwr   Fauto-delete)
autoDeleter   
deviceNamer   sourcer   rk   SCSI)rf   AttachedDiskModeValueValuesEnum
READ_WRITE	READ_ONLYr	   GetDiskDeviceNamer,   
PERSISTENTInterfaceValueValuesEnumr   NVMErk   rg   )rF   disksro   disks_messagesr   r   
mode_valuer   r   auto_deletedevice_nameattached_diskrk   s                r`   r~   r~     sT   & .d<D&$'JT""66AAd""66@@d88FE"D((=%0K 2243GIK ))""66AA * CM xx	+	&	())BBGG	))BBGG	 )m %7nM*C F 
rb   c	                 <   g }	| j                   }
|xs g D ]  }|j                  d      }|j                  dd      }|dk(  r+| j                   j                  j                  j                  }n*| j                   j                  j                  j
                  }|j                  dd      }|j                  dd      }t        j                  |j                  d            }|j                  d      }|j                  d	      }|j                  d
      }d}|s|r/t        j                  | |      }|j                  ||||d      \  }}d}|r!t        j                  || j                   |      }t        j                  |||      }| j                   j                  ||j                  d      |||j                  d      |j                  d            }|j                  d      }|rG|rEg }|D ]7  }|j!                  |dd|i      }|j#                  |j%                                9 ||_        |j                  d      }|r||_        |j                  d      } |r	| rd|_        |j                  d      }!|!r |
j                  j-                  |!      |_        |rt1        j2                  |j                  d            }"|"rZ|
j                  j5                         }#|"j7                         D $%cg c]  \  }$}%|#j9                  |$|%       c}%}$|#_        |#|_        |j                  d      |_        |j                  d      |_         | j                   j                  |||||| j                   j                  jB                  jD                  |      }&|j                  d      r\|j                  d      dk(  r!|
j                  jF                  jH                  }'n |
j                  jF                  jJ                  }'|'|&_&        |r|&g|	z   }	|	j#                  |&         |	S c c}%}$w ) a  Returns a list of AttachedDisk messages.

  Args:
    client: Compute client adapter
    resources: Compute resources registry
    user_project: name of user project
    create_disks: disk objects - contains following properties * name - the name
      of disk, * description - an optional description for the disk, * mode -
      'rw' (R/W), 'ro' (R/O) access mode, * size - the size of the disk, *
      provisioned-iops - Indicates how many IOPS must be provisioned for the
      disk. * provisioned-throughput - Indicates how much throughput is
      provisioned for the disks. * type - the type of the disk (HDD or SSD), *
      image - the name of the image to initialize from, * image-family - the
      image family name, * image-project - the project name that has the image,
      * auto-delete - whether disks is deleted when VM is deleted ('yes' if
      True), * device-name - device name on VM, * disk-resource-policy -
      resource policies applied to disk. * storage-pool: the storage pool in
      which the new disk is created.
    support_kms: if KMS is supported
    container_mount_disk: list of disks to be mounted to container, if any.
    support_multi_writer: if multi writer disks are supported.
    support_replica_zones: True if we allow creation of regional disks.
    support_disk_labels: True if we allow adding disk labels.

  Returns:
    list of API messages for attached disks
  r   r   r   r   Fr   sizeimagezimage-familyzimage-projectN)user_projectr   image_familyimage_projectreturn_image_resourcedescriptionr   zprovisioned-iops)diskNamer   sourceImage
diskSizeGbdiskTypeprovisionedIopszreplica-zoneszcompute.zonesr   )r   r   zdisk-resource-policyzmulti-writerTarchitecturelabels)keyvaluezprovisioned-throughputzstorage-poolr   r   r   initializeParamsr   r   diskEncryptionKeyrk   r   )'rF   rf   r   r   r   r   r   	BytesToGbr   ImageExpanderExpandImageFlagr
   MaybeGetKmsKeyFromDictr	   r   AttachedDiskInitializeParamsr!   rg   r   replicaZonesresourcePoliciesmultiWriterArchitectureValueValuesEnumr   r   ValidateAndParseLabelsLabelsValueitemsAdditionalPropertyadditionalPropertiesr   provisionedThroughputstoragePoolr,   r   r   r   r   rk   )(r   rE   r   create_disksrq   ro   rr   rs   rt   r   rF   r   r   r   r   r   r   rx   img
img_familyimg_projectry   image_expander_disk_keyr   init_paramsreplica_zonesnormalized_zoneszonezone_refpoliciesmulti_writerdisk_architecturedict_labelslabels_valuer   r   rp   rk   s(                                           r`   r   r     s   N .__( b d88FD&$'JT__))==HHd__))==GGd((=%0K88FE"D??488F#34L
((7
C.)J((?+KI
j"00Cn#33#!# % 4 'li H11$2:<h !2243GIK //>>HH]+&!!34 ? 6K HH_-M$??_i5N # P 1 1 34   "2kxx./H%-k"88N+L $k0

/
/
K
K  66txx7IJk	<<HHJ *//1-
1
U ++5+A1-
)
 *(,1I(JK%"hh~6K//..$__))==HH" / K xx	+	&	())BBGG	))BBGG	'k #}~5nK(M !P 
C-
s   (Pc           	         d}|rt        j                  || |d      }| j                  |||      }|||_        |	|	|_        | j                  |d||| j
                  j                  j                  | j
                  j                  j                  |      }|
rM|
dk(  r!| j
                  j                  j                  }n | j
                  j                  j                  }||_        |S )z=Returns an AttachedDisk message for creating a new boot disk.NT)boot_disk_prefix)r   r   r   r   r   )r
   MaybeGetKmsKeyr   r   r   r   r   r   r,   r   r   r   r   rk   )rF   ru   rv   rw   rx   ry   rz   rq   r{   r|   r}   r   initialize_paramsboot_attached_diskrk   s                  r`   r   r   a  s     (''(Ht=H ;;y < J &(=% ,.I+,,!!(  44??  44??  -  ''@@EEi''@@EEi#, 	rb   c                 v    |g S |d   }t        |j                  dd            }| j                  ||      }|gS )a  Returns a list of accelerator config messages for Instance Templates.

  Args:
    messages: tracked GCE API messages.
    accelerator: accelerator object with the following properties:
        * type: the accelerator's type.
        * count: the number of accelerators to attach. Optional, defaults to 1.

  Returns:
    a list of accelerator config messages that specify the type and number of
    accelerators to attach to an instance.
  r   count   )acceleratorTypeacceleratorCount)intrf   AcceleratorConfig)rF   acceleratoraccelerator_typeaccelerator_countaccelerator_configs        r`   CreateAcceleratorConfigMessagesr     sV     I (+//'15611&9J 2 L
	rb   )NNNNNNNNNNNNNNNNN)F)NFFFFTF)N)FNFTF)NFNNN)!__doc__
__future__r   r   r   googlecloudsdk.api_lib.computer   r   r   r	   r
   r   /googlecloudsdk.api_lib.compute.instances.creater   "googlecloudsdk.command_lib.computer   r   3googlecloudsdk.command_lib.compute.networks.subnetsr   r   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   objectr.   ra   rm   r   r~   r   r   r    rb   r`   <module>r      s    A &  ' ? 4 6 9 4 0 Q E U < *H    $ $	3Kh #(Op  %L` +/7~ Qv  $+\rb   