
    E                        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ZddlmZ d Ze
j(                   e
j*                  e
j,                  j.                         G d de
j0                                      Z e
j*                  e
j,                  j4                         G d de             Z e
j*                  e
j,                  j8                         G d de             Zy)z#Command for reserving IP addresses.    )absolute_import)division)unicode_literals)base_classes)	constants)name_generator)base)
exceptions)flagsN)zipc                    t        j                  d      | _        | j                  j                  |d       t        j                  |       |j
                  j                  t         j                         t        j                  |d       t        j                  |       t        j                  |       t        j                  ||       t        j                  |       t        j                         | _        | j                  j                  |       t        j                         | _        | j                   j                  |       |r$t        j"                         j                  |       yy)zArgument parsing.F)requiredcreate)operation_typeN)r   AddressArgumentADDRESSES_ARGAddArgumentAddDescriptiondisplay_infoAddCacheUpdaterAddressesCompleterAddAddressesAndIPVersionsAddNetworkTierAddPrefixLength
AddPurposeAddIPv6EndPointTypeSubnetworkArgumentSUBNETWORK_ARGNetworkArgumentNETWORK_ARGIpCollectionArgument)clsparsersupport_psc_google_apisinclude_ip_collections       'lib/surface/compute/addresses/create.py_Argsr'       s	    ++U;#x@v%%e&>&>?!!&59v623F#//1#  ())+#///f%	 ,,V4     c                   R    e Zd ZdZdZdZdZdZdZe	d        Z
d Zd Zd Zd	 Zd
 Zy)Createak  Reserve IP addresses.

  *{command}* is used to reserve one or more IP addresses. Once an IP address
  is reserved, it will be associated with the project until it is released
  using 'gcloud compute addresses delete'. Ephemeral IP addresses that are in
  use by resources in the project can be reserved using the '--addresses' flag.

  ## EXAMPLES
  To reserve three IP addresses in the 'us-central1' region, run:

    $ {command} address-1 address-2 address-3 --region=us-central1

  To reserve ephemeral IP addresses '162.222.181.198' and '23.251.146.189' which
  are being used by virtual machine instances in the 'us-central1' region, run:

    $ {command} --addresses=162.222.181.198,23.251.146.189 --region=us-central1

  In the above invocation, the two addresses will be assigned random names.

  To reserve an IP address named subnet-address-1 from the subnet 'default' in
  the 'us-central1' region, run:

    $ {command} subnet-address-1 \
      --region=us-central1 \
      --subnet=default

  To reserve an IP range '10.110.0.0/16' from the network 'default' for
  'VPC_PEERING', run:

    $ {command} ip-range-1 --global --addresses=10.110.0.0 --prefix-length=16 \
      --purpose=VPC_PEERING --network=default

  To reserve any IP range with prefix length '16' from the network 'default' for
  'VPC_PEERING', run:

    $ {command} ip-range-1 --global --prefix-length=16 --purpose=VPC_PEERING \
      --network=default

  To reserve an address from network 'default' for PRIVATE_SERVICE_CONNECT, run:

    $ {command} psc-address-1 --global --addresses=10.110.0.10 \
      --purpose=PRIVATE_SERVICE_CONNECT --network=default

  NTFc                 J    t        | || j                  | j                         y )N)r$   r%   )r'   _support_psc_google_apis_include_ip_collection)r"   r#   s     r&   ArgszCreate.Argsn   s"    	 # < <!88	:r(   c                 
   |j                   rw|j                   j                         }|t        j                  v r%|j                  j                  |j                         S t        j                  ddj                  |            y )Nz--network-tierzInvalid network tier [{tier}])tier)	network_tierupperr   !NETWORK_TIER_CHOICES_FOR_INSTANCEAddressNetworkTierValueValuesEnumr
   InvalidArgumentExceptionformat)selfmessagesargsr1   s       r&   ConstructNetworkTierzCreate.ConstructNetworkTierv   sx    &&,,.l	DD	D::4;L;LMM11+222EG 	G r(   c           
      ^   t        j                  | j                               }|j                  }| j	                  |      \  }}|j
                  s||_        | j                  j                  ||j                  t        j                  |            }g }t        ||      D ]  \  }}	| j                  |j                  |||	|j                        }
|	j                         dk(  rN|j                  |j                   j"                  d|j                  j%                  |
|	j&                        f       |	j                         dk(  s|j                  |j                   j(                  d|j                  j+                  |
|	j,                  |	j&                        f        |j/                  |      S )z.Issues requests necessary to create Addresses.)scope_listercompute.globalAddressesInsert)addressprojectcompute.addresses)r@   regionrA   )r   ComputeApiHolderReleaseTrackclient_GetNamesAndAddressesnamer   ResolveAsResource	resourcescompute_flagsGetDefaultScopeListerr   
GetAddressr9   
Collectionappendapitools_clientglobalAddresses#ComputeGlobalAddressesInsertRequestrA   	addressesComputeAddressesInsertRequestrC   MakeRequests)r8   r:   holderrF   namesrS   address_refsrequestsr@   address_refaddress_msgs              r&   Runz
Create.Run   s   **4+<+<+>?F]]F11$7E999di%%77"88@ 8 BL
 H #I| <OOFOOT7K$*$4$46k 
			!%>	>//??LL%0+:M:M M OP 	Q !!#':://998FF%0$/$6$6%0%8%8 G :; 	< != x((r(   c                    |j                   s$|j                  st        j                  ddgd      |j                  r|j                  }n+|j                   D cg c]  }t	        j
                          }}|j                   r|j                   }ndgt        |j                        z  }t        |      t        |      k7  rt        j                  dd      ||fS c c}w )z-Returns names and addresses provided in args.NAMEz	--addressz.At least one name or address must be provided.N--addresseszJIf providing both, you must specify the same number of names as addresses.)rS   rH   r
   MinimumArgumentExceptionr   GenerateRandomNamelenBadArgumentException)r8   r:   rW   _rS   s        r&   rG   zCreate._GetNamesAndAddresses   s    >>$))//;

:< < yyiie =ANNKNq~002NeK~~..i &3tyy>)i
9~U#++
 
 ) Ls   Cc                     ||j                   j                  j                  k7  r:||j                   j                  j                  k7  rt	        j
                  dd      y y )N	--purposezPmust be GCE_ENDPOINT or SHARED_LOADBALANCER_VIP for regional internal addresses.)r4   PurposeValueValuesEnumGCE_ENDPOINTSHARED_LOADBALANCER_VIPr
   r6   )r8   r9   purposes      r&   CheckPurposeInSubnetworkzCreate.CheckPurposeInSubnetwork   sa    8##::GGG//GG	H//
 ! !	H 	Hr(   c                    | j                  ||      }|j                  s|=|j                         dk(  r*|j                  j	                  |j                  xs d      }nd}|j
                  r"|j                  rt        j                  dd      d}|j                  r0|j                  s$|j
                  st        j                  ddgd      |j
                  r|j                         dk(  rt        j                  dd      |j                  s|j                  |_        t        j                         j!                  ||      j#                         }	|j$                  s>|j                  j'                  |j                  xs d      }| j)                  ||       nd}	d}
|j                  rL|j                  j'                  |j                  xs d	      }t        j*                         j!                  ||      j#                         }
||j                  j&                  j,                  k7  r|j                         d
k(  rt        j.                  dd      d	|j                  j&                  j0                  i}| j2                  r#|j                  j&                  j4                  |d<   ||j7                         vrBt        j.                  ddj9                  dj;                  |j=                                           d}|j$                  r%|j                  j?                  |j$                        }d}|j$                  r!|j                  j@                  jB                  }n$|	s|
r |j                  j@                  jD                  }|	 tG        jH                  |      }|jL                  r|r#|s!|j                  j@                  jB                  }n}|jN                  dk7  rl||j                  j&                  j0                  k7  rI||j                  j&                  j,                  k7  r&t        j.                  ddj9                  |            |jL                  sr||j                  j&                  j0                  k(  rt        jP                  dd      ||j                  j&                  j,                  k(  rt        jP                  dd      |j                  ||jL                  |jR                  |||jU                         |||	|
|      }| jV                  rC|jX                  r7t        jZ                         j!                  ||      j#                         |_.        |S # tJ        $ r' t        j.                  ddj9                  |            w xY w)a  Get and validate address setting.

    Retrieve address resource from input arguments and validate the address
    configuration for both external/internal IP address reservation/promotion.

    Args:
      messages: The client message proto includes all required GCE API fields.
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.
      address: Address object.
      address_ref: Reference of the address.
      resource_parser: A resource parser used to parse resource name into url.

    Returns:
      An address resource proto message.

    Raises:
      ConflictingArgumentsException: If both network and subnetwork fields are
      set.
      MinimumArgumentException: Missing network or subnetwork with purpose
      field.
      InvalidArgumentException: The input argument is not set correctly or
      unable to be parsed.
      RequiredArgumentException: The required argument is missing from user
      input.
    Nr>   IPV4z	--networkz--subnetz if --purpose is specifiedz5[--subnet] may not be specified for global addresses.rh   VPC_PEERINGrB   z4network may not be specified for regional addresses.PRIVATE_SERVICE_CONNECTrf   z)must be {} for global internal addresses.z or r_   zInvalid IP address {e})e   z--prefix-lengthzqcan only be used with [--purpose VPC_PEERING/IPSEC_INTERCONNECT] or Internal/External IPv6 reservation. Found {e}z<prefix length is needed for reserving VPC peering IP ranges.zSprefix length is needed for reserving IP ranges for HA VPN over Cloud Interconnect.)r@   prefixLengthdescriptionnetworkTier	ipVersionrH   addressTyperj   
subnetworknetworkipv6EndpointType)/r;   
ip_versionrN   r4   IpVersionValueValuesEnumsubnetrx   r
   ConflictingArgumentsExceptionrj   r`   rc   subnet_regionrC   r   r   rI   SelfLinkendpoint_typerg   rk   r   IPSEC_INTERCONNECTr6   rn   r,   ro   valuesr7   joinkeysIpv6EndpointTypeValueValuesEnumAddressTypeValueValuesEnumEXTERNALINTERNALipaddr	IPAddress
ValueErrorprefix_lengthversionRequiredArgumentExceptionrs   Namer-   ip_collectionr!   ipCollection)r8   r9   r:   r@   rZ   resource_parserr1   rz   rj   subnetwork_urlnetwork_urlsupported_purposesipv6_endpoint_typeaddress_type
ip_addressr[   s                   r&   rM   zCreate.GetAddress   s$   6 ,,Xt<L7?{/E/E/G708##<<T__ >D=CEj j{{t||44[*MMG||DLL//j0I0LN N {{				!%>	>--OQ 	Q(////1CC
!!) ""99$,, ;I:HJ%%h8nK||  77 9F8EGg))+==
!!) 	H$$;;NN	N!!#'::33DF F 8++BBNN
 ((55MM 6
7 ,335533 ++16&++$))+3- ,./ /
 #++KK


 L%%@@IIl	;%%@@IIl
%%g.
 	''BBKK?j00A5))@@LLL44GGH 11@@F AG A
 	
 	H$$;;GG	G22JL 	L 
H$$;;NN	N22  34 	4 ""''$$  !+ # -K ""t'9'9!&!;!;!=!O!O
"!!)  e  
113::W:E
 	

s   >U 0U>)__name__
__module____qualname____doc__r   r   r    r,   r-   classmethodr.   r;   r\   rG   rk   rM    r(   r&   r*   r*   7   sR    +Z -.+! : :
)@6!Yr(   r*   c                       e Zd ZdZdZdZy)
CreateBetaa\  Reserve IP addresses.

  *{command}* is used to reserve one or more IP addresses. Once an IP address
  is reserved, it will be associated with the project until it is released
  using 'gcloud compute addresses delete'. Ephemeral IP addresses that are in
  use by resources in the project can be reserved using the '--addresses' flag.

  ## EXAMPLES
  To reserve three IP addresses in the 'us-central1' region, run:

    $ {command} address-1 address-2 address-3 --region=us-central1

  To reserve ephemeral IP addresses '162.222.181.198' and '23.251.146.189' which
  are being used by virtual machine instances in the 'us-central1' region, run:

    $ {command} --addresses=162.222.181.198,23.251.146.189 --region=us-central1

  In the above invocation, the two addresses will be assigned random names.

  To reserve an IP address named subnet-address-1 from the subnet 'default' in
  the 'us-central1' region, run:

    $ {command} subnet-address-1 --region=us-central1 --subnet=default

  To reserve an IP address that can be used by multiple internal load balancers
  from the subnet 'default' in the 'us-central1' region, run:

    $ {command} shared-address-1 --region=us-central1 --subnet=default \
      --purpose=SHARED_LOADBALANCER_VIP

  To reserve an IP range '10.110.0.0/16' from the network 'default' for
  'VPC_PEERING', run:

    $ {command} ip-range-1 --global --addresses=10.110.0.0 --prefix-length=16 \
      --purpose=VPC_PEERING --network=default

  To reserve any IP range with prefix length '16' from the network 'default' for
  'VPC_PEERING', run:

    $ {command} ip-range-1 --global --prefix-length=16 --purpose=VPC_PEERING \
      --network=default

  To reserve an address from network 'default' for PRIVATE_SERVICE_CONNECT, run:

    $ {command} psc-address-1 --global --addresses=10.110.0.10 \
      --purpose=PRIVATE_SERVICE_CONNECT --network=default

  TN)r   r   r   r   r,   r-   r   r(   r&   r   r   c  s    /b "r(   r   c                       e Zd ZdZdZy)CreateAlphaa[  Reserve IP addresses.

  *{command}* is used to reserve one or more IP addresses. Once an IP address
  is reserved, it will be associated with the project until it is released
  using 'gcloud compute addresses delete'. Ephemeral IP addresses that are in
  use by resources in the project can be reserved using the '--addresses' flag.

  ## EXAMPLES
  To reserve three IP addresses in the 'us-central1' region, run:

    $ {command} address-1 address-2 address-3 --region=us-central1

  To reserve ephemeral IP addresses '162.222.181.198' and '23.251.146.189' which
  are being used by virtual machine instances in the 'us-central1' region, run:

    $ {command} --addresses=162.222.181.198,23.251.146.189 --region=us-central1

  In the above invocation, the two addresses will be assigned random names.

  To reserve an IP address named subnet-address-1 from the subnet 'default' in
  the 'us-central1' region, run:

    $ {command} subnet-address-1 --region=us-central1 --subnet=default

  To reserve an IP address that can be used by multiple internal load balancers
  from the subnet 'default' in the 'us-central1' region, run:

    $ {command} shared-address-1 --region=us-central1 --subnet=default \
      --purpose=SHARED_LOADBALANCER_VIP

  To reserve an IP range '10.110.0.0/16' from the network 'default' for
  'VPC_PEERING', run:

    $ {command} ip-range-1 --global --addresses=10.110.0.0 --prefix-length=16 \
      --purpose=VPC_PEERING --network=default

  To reserve any IP range with prefix length '16' from the network 'default' for
  'VPC_PEERING', run:

    $ {command} ip-range-1 --global --prefix-length=16 --purpose=VPC_PEERING \
      --network=default

  To reserve an address from network 'default' for PRIVATE_SERVICE_CONNECT, run:

    $ {command} psc-address-1 --global --addresses=10.110.0.10 \
      --purpose=PRIVATE_SERVICE_CONNECT --network=default
  TN)r   r   r   r   r,   r   r(   r&   r   r     s    .` "r(   r   )r   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   r   googlecloudsdk.callioper	   r
   "googlecloudsdk.command_lib.computer   rK   ,googlecloudsdk.command_lib.compute.addressesr   	six.movesr   r'   UniverseCompatibleReleaseTracksrE   GACreateCommandr*   BETAr   ALPHAr   r   r(   r&   <module>r      s    * &  ' 7 4 9 ( . E >  5. D%%(()gT g * gT	 D%%**+4  4  ,4 n D%%++,2"* 2" -2"r(   