
                         j   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Zdd
lZ ej$                  ej&                  j(                  ej&                  j*                  ej&                  j,                         G d dej.                               Zdddde_        y
)z/Command for expanding IP range of a subnetwork.    )absolute_import)division)unicode_literals)base_classes)base)
exceptions)flags)
console_ioNc                   N    e Zd ZdZdZed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Zy)ExpandIpRangez Expand IP range of a subnetwork.Nc                     t        j                         | _        | j                  j                  |       |j	                  dt
        dd       y )N--prefix-lengthzThe new prefix length of the subnet. It must be smaller than the original and in the private address space 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16 defined in RFC 1918.T)typehelprequired)r	   SubnetworkArgumentSUBNETWORK_ARGAddArgumentadd_argumentint)clsparsers     7lib/surface/compute/networks/subnets/expand_ip_range.pyArgszExpandIpRange.Args&   sK    113C""6*
C       c                    t        j                  | j                               }|j                  }| j	                  |j
                        }| j                  j                  ||j                        }| j                  ||      }| j                  |j                         ||      }| j                  |j                         ||       | j                  |||      }|j                  |j                  j                   d|fg      S )z,Issues requests for expanding IP CIDR range.ExpandIpCidrRange)r   ComputeApiHolderReleaseTrackclient_ValidatePrefixLengthprefix_lengthr   ResolveAsResource	resources_GetOriginalIpCidrRange_InferNewIpCidrRangeName_PromptToConfirm_CreateExpandIpCidrRangeRequestMakeRequestsapitools_clientsubnetworks)	selfargsholderr    new_prefix_lengthsubnetwork_reforiginal_ip_cidr_rangenew_ip_cidr_rangerequests	            r   RunzExpandIpRange.Run4   s   **4+<+<+>?F]]F2243E3EF((::f N!99 1157HJ57HJ226>3DFG!7!7!C!C!4g!?  @ A Ar   c                 L    d|cxk  rdk  sn t        j                  dd      |S )Nr      r   z+Prefix length must be in the range [0, 29].)r   InvalidArgumentException)r-   r0   s     r   r!   z#ExpandIpRange._ValidatePrefixLengthG   s1    !'R'//

79 9 r   c                     | j                  ||      }|s>t        j                  dj                  |j	                         |j
                              |j                  S )Nz3Subnet [{subnet}] was not found in region {region}.)subnetregion)_GetSubnetworkcompute_exceptionsArgumentErrorformatr'   r;   ipCidrRange)r-   r    r1   
subnetworks       r   r%   z%ExpandIpRange._GetOriginalIpCidrRangeN   sc    $$V^<J,,
?
F
F#((*>3H3H G JK K !!!r   c                     dj                  |j                  d      d   |      }t        j                  t	        j
                  |      d      }t	        j
                  |      S )Nz{0}/{1}/r   F)strict)r?   split	ipaddressIPv4Networksix	text_type)r-   subnet_namer2   r0   unmasked_new_ip_rangenetworks         r   r&   z"ExpandIpRange._InferNewIpCidrRangeW   sY    %,,$$S)!, ##CMM2G$H+02G==!!r   c                     d}|j                  |||      }t        j                  |d      st        j                  d      y )NzThe IP range of subnetwork [{0}] will be expanded from {1} to {2}. This operation may take several minutes to complete and cannot be undone.T)messagedefaultzOperation aborted by user.)r?   r
   PromptContinuer=   AbortedError)r-   subnetwork_namer2   r3   prompt_message_templateprompt_messages         r   r(   zExpandIpRange._PromptToConfirma   sP    	   -33/1BDN$$^TJ++,HII Kr   c                     |j                   j                  |      }|j                   j                  |j                         ||j                  |j
                        S )N)r@   )rA   #subnetworksExpandIpCidrRangeRequestprojectr;   )messages#SubnetworksExpandIpCidrRangeRequest*ComputeSubnetworksExpandIpCidrRangeRequestr'   rW   r;   )r-   r    r1   r3   request_bodys        r   r)   z-ExpandIpRange._CreateExpandIpCidrRangeRequestl   s]    ??FF% G 'L??EE!&&(,8&&$$	 F & &r   c                     |j                   j                  d|j                  j                  |j                  |j
                  |j                               f}|j                  |g      }|r|d   S d S )NGet)rW   r;   rA   r   )r+   r,   rX   ComputeSubnetworksGetRequestrW   r;   r'   r*   )r-   r    r1   get_requestobjectss        r   r<   zExpandIpRange._GetSubnetworkv   su    **44"**!((%**, 	5 	./K !!;-0G 71:*d*r   )__name__
__module____qualname____doc__r   classmethodr   r5   r!   r%   r&   r(   r)   r<    r   r   r   r      sC     ). A&""	J&+r   r   z2Expand the IP range of a Compute Engine subnetworka  
*{command}* expands the IP range of a VPC subnetwork.

For more information about expanding a subnet, see [Expanding a primary IP
range](https://cloud.google.com/vpc/docs/using-vpc#expand-subnet).

This command doesn't work for secondary subnets or for subnets that are used
exclusively for load balancer proxies. For more information, see [Proxy-only
subnets for load balancers](https://cloud.google.com/load-balancing/docs/l7-internal/proxy-only-subnets).
zq
To expand the IP range of ``SUBNET'' to /16, run:

  $ {command} SUBNET --region=us-central1 --prefix-length=16
)briefDESCRIPTIONEXAMPLES)rd   
__future__r   r   r   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.computer=   3googlecloudsdk.command_lib.compute.networks.subnetsr	   googlecloudsdk.core.consoler
   rF   rH   ReleaseTracksr   GABETAALPHASilentCommandr   detailed_helprf   r   r   <module>rv      s    6 &  ' 7 ( < O E 2  
 D%%(($*;*;*@*@%%++-`+D&& `+-`+H B	 r   