
                            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dZ ej*                  ej,                  j.                         G d dej0                               Z ej*                  ej,                  j4                         G d de             Zd Zd Zd Zd Zee_         y)z+Bare Metal Solution network update command.    )absolute_import)division)unicode_literals)	BmsClient)IpRangeReservation)waiter)base)
exceptions)flags)labels_util)log)	resourcesa	  
          Update a Bare Metal Solution network.

          This call returns immediately, but the update operation may take
          several minutes to complete. To check if the operation is complete,
          use the `describe` command for the network.
        aH  
          To update an network called ``my-network'' in region ``us-central1'' with
          a new label ``key1=value1'', run:

          $ {command} my-network  --region=us-central1 --update-labels=key1=value1

          To clear all labels, run:

          $ {command} my-network --region=us-central1 --clear-labels
        )DESCRIPTIONEXAMPLESc                   &    e Zd ZdZed        Zd Zy)Update%Update a Bare Metal Solution network.c                     t        j                  | d       t        j                  |        t        j
                  j                  |        t        j                  | d       y)z Register flags for this command.T)
positionalF)hiddenN)r   AddNetworkArgToParserr   AddUpdateLabelsFlagsr	   
ASYNC_FLAGAddToParserAddNetworkIpReservationToParserparsers    "lib/surface/bms/networks/update.pyArgszUpdate.Args:   sD     
48$$V,OO'	))&?    c                    t               }|j                  j                  j                         }d }t        j
                  j                  |      }|j                  |      }|j                  |j                  j                  j                  |j                        j                         }t        ||      }|j                  |||      }|j                   r't#        j$                  |j'                         d       |S |j(                  rIt"        j*                  j-                  dj/                  |j'                         |j0                               |S t2        j4                  j7                  |j0                  dd      }	t9        j:                  |j<                        }
t9        j>                  |
|	dj/                  |j0                              }t#        j$                  |j'                         d       |S )	N)network_resourcelabelsip_reservationsnetwork)kindz@Update request issued for: [{}]
Check operation [{}] for status.z/baremetalsolution.projects.locations.operationsv2)
collectionapi_versionz&Waiting for operation [{}] to complete) r   CONCEPTSr%   Parser   DiffFromUpdateArgs
GetNetworkApplymessagesNetworkLabelsValuer#   	GetOrNone_ApplyIpReservationsUpdatesUpdateNetworkdoner   UpdatedResourceNameasync_statusPrintformatnamer   REGISTRYParseRelativeNamer   CloudOperationPollerNoResourcesoperation_serviceWaitFor)selfargsclientr%   labels_updatelabels_difforig_resourcer$   op_refop_resourcepollerress               r   Runz
Update.RunB   s   [Fmm##))+GM""11$7K%%g.M%%++'ik  2$FO!! ' " )F {{	',,.y9m{{	jj **0&*MOm$$66D 7 K 33  "F
..AHH#[[*+C Y7Jr    N)__name__
__module____qualname____doc__staticmethodr   rM    r    r   r   r   6   s    -@ @#r    r   c                        e Zd ZdZed        Zy)UpdateAlphar   c                 .    t         j                  |        y N)r   r   r   s    r   r   zUpdateAlpha.Argsl   s     KKr    N)rN   rO   rP   rQ   rR   r   rS   r    r   rU   rU   h   s    - r    rU   c                    t        | d      rg S |j                  D cg c]-  }t        |j                  |j                  |j
                        / }}t        | d      rA| j                  }t        |t        j                  d       |t        |d   |d   |d         gz   S t        | d      rt        || j                        S yc c}w )	aG  Applies the changes in args to the reservations in existing_network.

  Returns None if no changes were to be applied.

  Args:
    args: The arguments passed to the command.
    existing_network: The existing network.

  Returns:
    List of IP range reservations after applying updates or None if there are
    no changes.
  clear_ip_range_reservationsadd_ip_range_reservationzadd-ip-range-reservationstart-addressend-addressnoteremove_ip_range_reservationN)_IsSpecifiedreservationsr   startAddress
endAddressr]   rZ   _ValidateAgainstSpecr   IP_RESERVATION_SPEC_RemoveReservationr^   )rD   existing_networkrL   existing_reservationsres_dicts        r   r4   r4   r   s     $56I "...# ))3>>388D.  
 $23,,H5#<#<35 8O4h}6M#F+	-$  
 $563">>@ @ 7s   2Cc                    t        |t        j                  d       |d   }|d   }t        |       D ]4  \  }}|j                  |k(  s|j
                  |k(  s&| d | | |dz   d  z   c S  t        dj                  ||            )Nzremove-ip-range-reservationr[   r\      zPCannot find an IP range reservation with start-address [{}] and end-address [{}])rc   r   IP_RESERVATION_KEY_SPEC	enumeratestart_addressend_addressLookupErrorr<   )r`   remove_key_dictrm   rn   irL   s         r   re   re      s    (E(E46!/2-.+,'fa
M)coo.L"1QUV 444 ( 	 1178C2E	F Fr    c                 b    |j                         D ]  }|| vst        j                  ||       y rW   )keysr
   MissingPropertyError)dict_to_validatespec	flag_nameprops       r   rc   rc      s.    iikd##++It<< r    c                 $    | j                  |      S )zAReturns true if an arg is defined and specified, false otherwise.)IsKnownAndSpecified)rD   r=   s     r   r_   r_      s    		!	!$	''r    N)!rQ   
__future__r   r   r   %googlecloudsdk.api_lib.bms.bms_clientr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper	   googlecloudsdk.command_lib.bmsr
   r   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   r   DETAILED_HELPReleaseTracksReleaseTrackGAUpdateCommandr   ALPHArU   r4   re   rc   r_   detailed_helprS   r    r   <module>r      s    2 &  ' ; D . ( 5 0 < # )			. D%%(().T . *.b D%%++,&  -!@H
F=( % r    