
    !                     >   d 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	m
Z
  ej                  d       ej                  d      d	Zej                   ej                  ej                  j                          G d
 dej"                                      Zee_        y)z6Command to update a cluster-director cluster resource.    N)utils)base)flags)loga|  
        *{command}* facilitates the updation of a cluster resource.

        There are following ways to update a cluster:
        - [Preferred] Use granular flags to update cluster specs, based on read-modify-update pattern.
          - Read the existing cluster specs through `get` cluster request.
          - Modify the cluster specs through granular flags.
          - Update the cluster specs through `patch` cluster request.
        - Use --config with cluster specs and --update-mask flags, both in JSON format.
          - Map and repeated fields update requires existing and new values.
          - For e.g. if we want to update a cluster to add a new nodeset, then we will use the update_mask "orchestrator.slurm.node_sets", and the patch cluster must include all the existing nodesets as well as the new one.

        Please refer to the examples below for more details.
        aU  
        To update a cluster `my-cluster` in location `us-central1-a` with granular flags, run the following example:

        Add labels, compute instances, slurm node sets, slurm partitions and update description and default partition:

        $ {command} my-cluster --location us-central1-a         --description "My updated cluster description"         --add-labels env=prod,client=gcloud-cli         --add-on-demand-instances id=compute1,zone=us-central1-a,machineType=n2-standard-2         --add-reserved-instances id=compute2,reservation=zones/us-central1-a/reservations/{reservation},machineType={machineType}         --add-spot-instances id=compute3,zone=us-central1-a,machineType=n2-standard-2         --add-dws-flex-instances id=compute4,zone=us-central1-a,machineType=a4-highgpu-8g,maxDuration=10000s         --add-slurm-node-sets id=nodeset1,computeId=compute1         --add-slurm-node-sets id=nodeset2,computeId=compute2         --add-slurm-node-sets id=nodeset3,computeId=compute3         --add-slurm-node-sets id=nodeset4,computeId=compute4         --add-slurm-partitions id=partition1,nodesetIds=[nodeset1]         --add-slurm-partitions id=partition2,nodesetIds=[nodeset2,nodeset3,nodeset4]         --slurm-default-partition partition1

        Update slurm node sets and slurm partitions:

        $ {command} my-cluster --location us-central1-a         --update-slurm-node-sets id=nodeset1,staticNodeCount=2,maxDynamicNodeCount=10         --update-slurm-partitions id=partition1,nodesetIds=[nodeset0],exclusive=true

        Remove slurm node sets, slurm partitions and compute instances and update default partition:

        $ {command} my-cluster --location us-central1-a         --slurm-default-partition partition0         --remove-labels env,client         --remove-slurm-partitions partition1         --remove-slurm-partitions partition2         --remove-slurm-node-sets nodeset1         --remove-slurm-node-sets nodeset2         --remove-slurm-node-sets nodeset3         --remove-slurm-node-sets nodeset4         --remove-on-demand-instances compute1         --remove-reserved-instances compute2         --remove-spot-instances compute3         --remove-dws-flex-instances compute4

        Or cluster `my-cluster` in location `us-central1-a` with config JSON run the following JSON example:

        $ {command} my-cluster --location=us-central1-a --update-mask=labels --config='{"key": "value"}'

        Or create a JSON file `my-cluster-config.json` with the cluster specs and run the following file example:

        $ {command} my-cluster --location=us-central1-a --update-mask=labels --config-from-file=my-cluster-config.json

        Or create a JSON file with the update mask and run the following file example:

        $ {command} my-cluster --location=us-central1-a --update-mask-from-file=my-update-mask.json --config-from-file=my-cluster-config.json
        )DESCRIPTIONEXAMPLESc                   &    e Zd ZdZed        Zd Zy)Updatez$Updates a Cluster Director resource.c                 2   t         j                  j                  |       t        j                  | j                               }t        j                  ||       |j                  ddd      }|j                  d      }t        j                  ||d       t        j                  ||d       |j                  d      }t        j                  ||       t        j                  ||d	       t        j                  ||d	       t        j                  ||d	       t        j                   ||d	       t        j"                  ||d	       t        j$                  ||d	       t        j&                  ||d	       t        j(                  ||       y
)znSpecifies command flags.

    Args:
      parser: argparse.Parser: Parser object for command line inputs.
    )parserapi_versionz4Cluster configuration for provisioning with updates.T)helpmutexrequiredz"Cluster configuration for updates.)r   )r   r   r   z.Flag Configurations to define cluster updates.)r   r   include_update_flagsN)r   
ASYNC_FLAGAddToParser	api_utilsGetApiVersionReleaseTrackr   AddClusterNameArgToParser	add_groupr   	AddConfigAddUpdateMaskAddDescription	AddLabelsAddOnDemandInstancesAddSpotInstancesAddReservedInstancesAddDwsFlexInstancesAddSlurmNodeSetsAddSlurmPartitionsAddSlurmDefaultPartition)clsr   r   groupconfig_group
flag_groups         /lib/surface/cluster_director/clusters/update.pyArgszUpdate.Argsh   su    	OO'))#*:*:*<=K	##6{KC  E
 ??1 # L 
OO<[4P	t = ! J 

D	OO{ 
{ 
{ 
{ 
{ 
{ 
{ 
""*+N    c                 l   | j                         }t        j                  |      }t        j                  |      }d}|j	                  d      st        j
                  |||      }t        j                  |||      }|j                  }|j	                  d      r|j                         \  }}	n|j                         \  }}	|j                  j                  |j                  |j                         ||	            }
t        j                   j#                  dj%                  |j'                                      |j(                  r|
S t        j*                  ||
dj%                  |
j,                        d      }t        j                   j#                  d	j%                  |j'                                      |S )
zConstructs and sends request.

    Args:
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.

    Returns:
      ProcessHttpResponse of the request made.
    Nconfig)argsmessage_moduleexisting_cluster)namecluster
updateMaskz Update request issued for: [{0}]z'Waiting for operation [{0}] to completei  )client	operationmessagemax_wait_seczUpdated cluster [{0}].)r   r   GetClientInstanceGetMessagesModuleIsSpecified
GetClusterr   ClusterUtilcluster_refMakeClusterPatchFromConfigMakeClusterPatchprojects_locations_clustersPatch8HypercomputeclusterProjectsLocationsClustersPatchRequestRelativeNamer   statusPrintformatNameasync_WaitForOperationr0   )selfr-   release_trackr3   messagesr/   cluster_utilr<   cluster_patchupdate_maskr4   responses               r(   Runz
Update.Run   s    %%'M((7F**=9HH%"--fdHE$$(=ML **K!#/#J#J#L m[#/#@#@#B m[2288II))+!" 	J 	
I JJ*11+2B2B2DE {{))9@@NN
 H JJ-44[5E5E5GHIOr*   N)__name__
__module____qualname____doc__classmethodr)   rP    r*   r(   r
   r
   c   s!     -.O .O`0r*   r
   )rT   textwrap*googlecloudsdk.api_lib.hypercomputeclusterr   r   googlecloudsdk.callioper   4googlecloudsdk.command_lib.cluster_director.clustersr   googlecloudsdk.corer   dedentDETAILED_HELPDefaultUniverseOnlyReleaseTracksr   ALPHAUpdateCommandr
   detailed_helprV   r*   r(   <module>rc      s     =  I ( F F # #8?? $    5! 5EP D%%++,dT d - dN % r*   