
    .                         d 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dlm	Z	 ddl
mZ dd	lmZ ddlZ G d
 de      Zy)z*Distributed Cloud Edge Network router API.    )absolute_import)division)unicode_literalsN)encoding)utils)parser_errors)
exceptionsc                   l    e Zd ZdZdZdZdZd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y)RoutersClientz2Client for private connections service in the API.	interfacebgp_peerroute_advertisementsNc                     |xs t        j                  |      | _        |xs t        j                  |      | _        | j                  j
                  | _        t        j                  |      | _        y N)	r   GetClientInstance_clientGetMessagesModule	_messages projects_locations_zones_routers_serviceGetResourceParser_resource_parser)selfrelease_trackclientmessagess       Clib/googlecloudsdk/api_lib/edge_cloud/networking/routers/routers.py__init__zRoutersClient.__init__&   sT    CU44]CDLG!8!8!GDNLLAADM!33MBD    c                 X    t        j                  | j                  || j                        S )z=Waits for the given google.longrunning.Operation to complete.)r   WaitForOperationr   r   )r   	operations     r   r!   zRoutersClient.WaitForOperation,   s    !!$,,	4==IIr   c                    t        j                  |      }| j                  j                  |j                        }|j
                  )| j                  j                  d|j
                  |j                  |j                  |j                        }|j                  |j                  t        j                  d      	 t        j                  |j                        }|j                  |j$                  kD  r"t        j                  d|j$                  z        dj'                  |j                  |j                        }	|j(                  dk(  r|	|_        n|	|_        |j/                         |_        |j2                  ]| j                  j                  d	|j2                  |j                  |j                  |j                  
      }
|
j/                         |_        |j4                  |j4                  |_        |j8                  j;                  |       |S # t         $ r#}t        j                  t#        |            d}~ww xY w)z&Mutate the router to add an interface.nameNz<edgenetwork.projects.locations.zones.interconnectAttachments)interconnectAttachmentsId
projectsIdlocationsIdzonesIdz---ip-address and --ip-mask-length must be setz'--ip-mask-length should be less than %sz{0}/{1}   z,edgenetwork.projects.locations.zones.subnets)	subnetsIdr'   r(   r)   )r   CopyProtoMessager   	Interfaceinterface_nameinterconnect_attachmentr   Creater'   r(   r)   ip_mask_length
ip_addressr   ArgumentException	ipaddress
ValueErrorstrmax_prefixlenformatversionipv4Cidripv6CidrRelativeNamelinkedInterconnectAttachment
subnetworkloopback_ip_addressesloopbackIpAddressesr   append)r   
router_refargsexistingreplacementnew_interfaceattachment_refr2   errcidr
subnet_refs              r   ModifyToAddInterfacez"RoutersClient.ModifyToAddInterface0   s   ++H5KNN,,$2E2E,FM##/,,33
H$($@$@** ,,$$ 4 &n 
			$(?--;
 	
8))$//:
 
		z77	7--5
8P8PP
 	
 doot/B/BCd			q	 !%!%3A3N3N3Pm0"((//
8OO** ,,$$ 0 &j ",!8!8!:m!!-*.*D*Dm'  /;  8--c#h778s   ?H 	IH<<Ic                 (   |j                   r|j                   ng }||j                  r|j                  gng z   }g }t        j                  |      }t        j                  |      }|j                  D ]G  }|j
                  |v s|j                  j                  |       |j                  |j
                         I t        t        |      t        |      z
        }|r5dj                  dj                  |            }	t        j                  |	      |S )z1Mutate the router to delete a list of interfaces.zinterface [{}] not found, )interface_namesr.   r   r,   r   r%   removerA   sortedsetr8   joincore_exceptionsError)
r   rC   rD   input_remove_listactual_remove_listrE   existing_routerifacenot_found_interface	error_msgs
             r   ModifyToRemoveInterfacez%RoutersClient.ModifyToRemoveInterfacec   s    150D0D,,")040C0C /3.A.A-BIKM ++H5K//9O **	(	($$U+!!%**- + !%7!88:,33
))'
(*i!!),,r   c                 Z   t        j                  |      }|j                  |j                  |j                  d}|j
                  |j
                  |d<   d|v r|j                  |j                  |d<    | j                  j                  di |}|j                  j                  |       |S )z$Mutate the router to add a BGP peer.)r%   r   peerAsnpeerIpv4Cidrpeer_ipv6_rangepeerIpv6Cidr )r   r,   	peer_namer   peer_asnpeer_ipv4_ranger_   r   BgpPeerbgpPeerrA   )r   rC   rD   rE   bgp_peer_argsnew_bgp_peers         r   ModifyToAddBgpPeerz RoutersClient.ModifyToAddBgpPeer}   s     ++H5K^^==M '&*&:&:mN# D T%9%9%E&*&:&:mN#)4>>)):M:L|,r   c                 (   |j                   r|j                   ng }||j                  r|j                  gng z   }g }t        j                  |      }t        j                  |      }|j                  D ]G  }|j
                  |v s|j                  j                  |       |j                  |j
                         I t        t        |      t        |      z
        }|r5dj                  dj                  |            }	t        j                  |	      |S )z&Mutate the router to delete BGP peers.zpeer [{}] not foundrM   )
peer_namesrb   r   r,   rf   r%   rO   rA   rP   rQ   r8   rR   rS   rT   )
r   rC   rD   rU   rV   rE   rW   peernot_found_peerrZ   s
             r   ModifyToRemoveBgpPeerz#RoutersClient.ModifyToRemoveBgpPeer   s    +/??)04 /3nn-=DFH++H5K//9O''	'	'""4(!!$)), ( C 12S9K5LLMN'..tyy/HIi!!),,r   c                 T   | j                   j                  |j                               }| j                  j	                  |      }| j                  |||      }| j                   j                  |j                         || j                        }| j                  j                  |      S )z Create an interface on a router.r$   r%   router
updateMask)	r   2EdgenetworkProjectsLocationsZonesRoutersGetRequestr<   r   GetrK   4EdgenetworkProjectsLocationsZonesRoutersPatchRequestFIELD_PATH_INTERFACEPatchr   rB   rC   get_router_reqrouter_objectnew_router_objectupdate_router_reqs          r   AddInterfacezRoutersClient.AddInterface   s     ^^VV$$& W (NMM%%n5M 11*d2?A [[$$& ,, \ . ==011r   c                 R   | j                   j                  |j                               }| j                  j	                  |      }| j                  ||      }| j                   j                  |j                         || j                        }| j                  j                  |      S )z(Remove a list of interfaces on a router.r$   rp   )	r   rs   r<   r   rt   r[   ru   rv   rw   rx   s          r   RemoveInterfacezRoutersClient.RemoveInterface   s     ^^VV$$& W (NMM%%n5M44T=I[[$$& ,, \ .
 ==011r   c                    d }d } ||j                         }t        j                  |      }|j                  r[t	        |j                        } |||z        }|r3t        j                  dj                  dj                  |                  ||z  }n|j                  rX ||j                        }	 ||	|z
        }
|
r3t        j                  dj                  dj                  |
                  ||	z  }n4|j                  r ||j                        }nt        j                  d      t        t        t        t!        |                  |_         |S )z;Create a router based on `existing` with the routes change.c                 &    t        d | D              S )Nc              3   F   K   | ]  }t        j                  |        y wr   )r4   
ip_network).0cidrstrs     r   	<genexpr>zSRoutersClient.ModifyToApplyAdvertisementChanges.<locals>.cidrset.<locals>.<genexpr>   s     Hi7%%g.is   !)rQ   )	cidr_strss    r   cidrsetz@RoutersClient.ModifyToApplyAdvertisementChanges.<locals>.cidrset   s    HiHHHr   c                 d    t        |       D cg c]  }t        j                  |       c}S c c}w r   )rP   six	text_type)cidrsrI   s     r   sorted_stringszGRoutersClient.ModifyToApplyAdvertisementChanges.<locals>.sorted_strings   s'    .4Um<mdcmmD!m<<<s   -z5attempting to add routes that are already present: {}rM   z4attempting to remove routes that are not present: {}z`Missing --add-advertisement-ranges, --remove-advertisement-ranges, or --set-advertisement-ranges)routeAdvertisementsr   r,   add_advertisement_rangesrQ   rS   rT   r8   rR   remove_advertisement_rangesset_advertisement_rangesr   r3   listmapr6   rP   )r   rC   rD   r   r   advertisementsrE   to_addalready_presentto_rmalready_missings              r   !ModifyToApplyAdvertisementChangesz/RoutersClient.ModifyToApplyAdvertisementChanges   sL   I= X99:N++H5K$$4001f&~'>?o	##CJJ		/*,- 	- n		)	)d667e&u~'=>o	##BII		/*,- 	- n		&	&t<<=n++IJ J '+3sF>4J+K&LK#r   c                 R   | j                   j                  |j                               }| j                  j	                  |      }| j                  ||      }| j                   j                  |j                         || j                        }| j                  j                  |      S )zNCreate a patch request that updates the Route advertisements of a router.
    r$   rp   )	r   rs   r<   r   rt   r   ru   FIELD_PATH_ROUTE_ADVERTISEMENTSrw   )r   rB   rC   ry   rz   r{   update_router_requests          r   ChangeAdvertisementsz"RoutersClient.ChangeAdvertisements   s     ^^VV$$& W (NMM%%n5M>>m 	KK((*$;; 	L 	=  ==455r   c                 R   | j                   j                  |j                               }| j                  j	                  |      }| j                  ||      }| j                   j                  |j                         || j                        }| j                  j                  |      S )z'Mutate the router so to add a BGP peer.r$   rp   )	r   rs   r<   r   rt   ri   ru   FIELD_PATH_BGP_PEERrw   rx   s          r   
AddBgpPeerzRoutersClient.AddBgpPeer  s     ^^VV$$& W (NMM%%n5M //mD[[$$& ++ \ - ==011r   c                 R   | j                   j                  |j                               }| j                  j	                  |      }| j                  ||      }| j                   j                  |j                         || j                        }| j                  j                  |      S )z*Mutate the router so to remove a BGP peer.r$   rp   )	r   rs   r<   r   rt   rn   ru   r   rw   rx   s          r   RemoveBgpPeerzRoutersClient.RemoveBgpPeer  s     ^^VV$$& W (NMM%%n5M 224G[[$$& ++ \ - ==011r   c                     | j                   j                  |j                               }| j                  j	                  |      S )z%Get the status of a specified router.r$   )r   7EdgenetworkProjectsLocationsZonesRoutersDiagnoseRequestr<   r   Diagnose)r   rB   get_router_status_reqs      r   	GetStatuszRoutersClient.GetStatus!  s?     NNbb$$& c (==!!"788r   )NN)__name__
__module____qualname____doc__rv   r   r   r   r!   rK   r[   ri   rn   r}   r   r   r   r   r   r   ra   r   r   r   r      s\    :$"$:!CJ1f4*,2"2 $L6$2 2 9r   r   )r   
__future__r   r   r   r4   apitools.base.pyr   ,googlecloudsdk.api_lib.edge_cloud.networkingr   googlecloudsdk.callioper   googlecloudsdk.corer	   rS   r   objectr   ra   r   r   <module>r      s3    1 &  '  % > 1 = 
F9F F9r   