
    3'                        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 Z G d de      Ze
j.                   e
j0                  e
j2                  j4                  e
j2                  j6                         G d de
j8                                      Z e
j0                  e
j2                  j<                         G d de             Zy)z)Command for updating service attachments.    )absolute_import)division)unicode_literals)encoding)base_classes)base)flags)scope)service_attachments_utilsc                      ddddS )N2Update a Google Compute Engine service attachment.a        *{command}* is used to update service attachments. A service producer
      creates service attachments to make a service available to consumers.
      Service consumers use Private Service Connect endpoints to privately
      forward traffic to the service attachment.
      a        To update the connection policy of a service attachment to be ACCEPT_MANUAL, run:

        $ {command} SERVICE_ATTACHMENT_NAME --region=us-central1 --connection-preference=ACCEPT_MANUAL

      To update all supported fields of a service attachment, run:

        $ {command} SERVICE_ATTACHMENT_NAME --region=us-central1 --connection-preference=ACCEPT_AUTOMATIC --nat-subnets=MY_SUBNET1,MY_SUBNET2 --enable-proxy-protocol --consumer-reject-list=PROJECT_ID1,PROJECT_ID2 --consumer-accept-list=PROJECT_ID3=10,PROJECT_ID4=20

      )briefDESCRIPTIONEXAMPLES r       1lib/surface/compute/service_attachments/update.py_DetailedHelpr      s     ?
	

 r   c                   R    e Zd ZdZdZdZd Zed        Zd Z	d Z
d Zd Zd	 Zd
 Zy)UpdateHelperr   Nc                      || _         || _        y N)_holder_support_target_service_arg)selfholdersupport_target_service_args      r   __init__zUpdateHelper.__init__>   s    DL'AD$r   c                 4   t        j                         | _        | j                  j                  |d       t	        j
                  d      | _        | j                  j                  |       t        j                  |       |rt        j                  |       t        j                  |d       t        j                  |       t        j                  |       t        j                  |       t        j                  |       t        j                  |       y)a  Create a Google Compute Engine service attachment.

    Args:
      parser: the parser that parses the input from the user.
      support_target_service_arg: Whether to add arguments for producer
        forwarding rule.

    cls: Hold onto the definition of a complex argument so it can be used later
      to process the user's input.
    flags: Define and register command-line arguments with the argument parser.
    update)operation_typeF)requiredT)	is_updateN)r	   ServiceAttachmentArgumentSERVICE_ATTACHMENT_ARGAddArgumentsubnetwork_flags&SubnetworkArgumentForServiceAttachmentNAT_SUBNETWORK_ARGAddDescriptionAddTargetServiceArgsForUpdateAddConnectionPreferenceAddEnableProxyProtocolForUpdate AddReconcileConnectionsForUpdateAddConsumerRejectListAddConsumerAcceptListAddPropagatedConnectionLimit)clsparserr   s      r   ArgszUpdateHelper.ArgsB   s     "'!@!@!BC**6(*K-TTC&&v.	 !))&1	!!&D9	))&1	**62	'	'	&&v.r   c                 z    |j                   |j                   |j                  fS |j                  |j                  fS r   )projectIdOrNumconnectionLimit
networkUrl)r   consumer_limits     r   _GetProjectOrNetworkz!UpdateHelper._GetProjectOrNetwork_   s<    $$0++^-K-KLL%%~'E'EFFr   c                      |j                   j                  di |j                         }|j                  j                  }|j                  |d|fg      d   S )z0Returns the existing ServiceAttachment resource.Getr   r   )messages#ComputeServiceAttachmentsGetRequestAsDictapitools_clientserviceAttachmentsMakeRequests)r   clientservice_attachment_refrequest
collections        r   _GetOldResourcezUpdateHelper._GetOldResourced   sZ    AfooAA +
 
'
'
)+G''::JUG <=>qAAr   c                     |j                   j                  d|j                  j                  |j                  |j
                  |j                         |      fS )z3Returns a request to update the service attachment.Patch)projectregionserviceAttachmentserviceAttachmentResource)r@   rA   r=   %ComputeServiceAttachmentsPatchRequestrJ   rK   Name)r   rC   rD   replacements       r   _GetPatchRequestzUpdateHelper._GetPatchRequestk   sV    ""55wOOAA.66-44"8"="="?*5	 B 78 8r   c                    | j                   j                  ||j                  t        j                  j
                  t        j                  |j                              }|D cg c]  }|j                          }}|S c c}w )z.Returns nat subnetwork urls from the argument.)default_scopescope_lister)
r)   ResolveAsResource	resourcescompute_scope	ScopeEnumREGIONcompute_flagsGetDefaultScopeListerrC   SelfLink)r   r   argsnat_subnetwork_refsnat_subnetwork_refnat_subnetworkss         r   _GetNatSubnetszUpdateHelper._GetNatSubnetst   s    11CC#--44"88G	 D I #6"5 	##%"5   	s   #A>c                    t        j                  |      }d}| j                  r$|j                  d      rd}|j                  |_        |j                  d      r,|j                  |j                  k7  r|j                  |_        d}|j                  d      rBt        j                  ||j                  j                        }||j                  k7  r	||_        d}|j                  d      r,|j                  |j                  k7  r|j                  |_        d}|j                  d      rHt        | j                  ||            }|j                   |t        |j                         k7  r	||_        d}|j                  d	      rUt        |j"                        }	|j$                  |	t        |j$                        k7  r|	|_        d}|	s|j'                  d
       |j                  d      rt        j(                  ||j                  j                        }
t        |
| j*                        }|j,                  $|t        |j,                  | j*                        k7  r||_        d}|s|j'                  d       |j                  d      r,|j.                  |j0                  k7  r|j.                  |_        d}|j                  d      r,|j2                  |j4                  k7  r|j2                  |_        d}|r|S y)z'Returns the updated service attachment.Ftarget_serviceTdescriptionconnection_preferenceenable_proxy_protocolnat_subnetsNconsumer_reject_listconsumerRejectListsconsumer_accept_list)keyconsumerAcceptListsreconcile_connectionspropagated_connection_limit)r   CopyProtoMessager   IsSpecifiedrc   targetServicerd   r   GetConnectionPreferencerC   r=   connectionPreferencerf   enableProxyProtocolsortedra   
natSubnetsrh   ri   appendGetConsumerAcceptListr:   rl   rm   reconcileConnectionsrn   propagatedConnectionLimit)r   r   r]   old_resourcecleared_fieldsrP   
is_updatednew_connection_preferencenew_nat_subnetsnew_reject_listrj   new_accept_lists               r   _ModifyzUpdateHelper._Modify   s   ++L9KJ''D,<,<=M,Nj"&"5"5k&			\55	5"&"2"2
/0
#
;
;FMM**  
 
#l&G&G	G+D(
/0		#	#|'G'G	G*.*D*D'
&t2264@Ao		 	 	(Ov

!
!@# -#!0
./t889o		)	)	1_

*
*I, 6,*9'



 5
6./6LL
&&(
D$=$=o 
	)	)	1_

*
*0I0II 6 +:'



 5
6/0		#	#|'H'H	H+/+E+E(
56

*
*334 150P0P-
r   c                    | j                   j                  }| j                  j                  || j                   j                  t
        j                  j                        }| j                  ||      }g }| j                  | j                   |||      }||S |j                  j                  |      5  |j                  | j                  |||      g      cddd       S # 1 sw Y   yxY w))Issue a service attachment PATCH request.)rS   N)r   rC   r%   rU   rV   rW   rX   rY   rG   r   r@   IncludeFieldsrB   rQ   )r   r]   rC   rD   r{   r|   rP   s          r   RunzUpdateHelper.Run   s    \\  F!88JJ#--44 K 
 ''0FGLN,,t||T<PK				-	-n	=    )?M
NP 
>	=	=s   2#CC()__name__
__module____qualname____doc__r%   r)   r   classmethodr4   r:   rG   rQ   ra   r   r   r   r   r   r   r   8   sN    :B / /8G
B8L\Pr   r   c                   8    e Zd ZdZdZ e       Zed        Zd Z	y)Updater   Fc                 D    t         j                  || j                         y r   )r   r4   r   )r2   r3   s     r   r4   zUpdate.Args   s    ''r   c                     t        j                  | j                               }t        || j                        j                  |      S )r   )r   ComputeApiHolderReleaseTrackr   r   r   )r   r]   r   s      r   r   z
Update.Run   s?    **4+<+<+>?F(( 
c$ir   N)
r   r   r   r   r   r   detailed_helpr   r4   r   r   r   r   r   r      s+     ; %/- r   r   c                   "    e Zd ZdZdZ e       Zy)UpdateAlphar   TN)r   r   r   r   r   r   r   r   r   r   r   r      s    : $/-r   r   N) r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   rZ   r
   rW   3googlecloudsdk.command_lib.compute.networks.subnetsr'   6googlecloudsdk.command_lib.compute.service_attachmentsr   r   objectr   UniverseCompatibleReleaseTracksr   GABETAUpdateCommandr   ALPHAr   r   r   r   <module>r      s    0 &  ' % 7 ( E E Y H \2gP6 gPT D%%(($*;*;*@*@AT  B * D%%++,"& " -"r   