
    ;m                     H   d Z ddl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 ddlmZ dd	lmZ dd
lmZ g dZ ej&                  d      ZdZdZdZd Zd Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZd Zd Zd Zd Z 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d dZ!d Z"d Z#d Z$y)!zSpanner instance API helper.    N)
list_pager)timestamp_pb2)response_util)apis)iam_util)
properties)	resources)
console_io)zroles/spanner.adminzroles/spanner.databaseAdminzroles/spanner.databaseReaderzroles/spanner.databaseUserzroles/spanner.viewer   )secondsspannerv1autoscalingConfigc                    d|vrd|vrd|vrd|vrd|vrd|vr	d|vrd|vry	| j                  | j                         
      }d|v r|d   |j                  _        d|v r|d   |j                  _        d|v r|d   |j                  _        d|v r|d   |j                  _        d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_	        d|v r
|d   |_
        |S )zReturns AutoscalingConfigOverrides object if any override is found in the parsed command-line flag key-value pairs, otherwise returns None.	min_nodes	max_nodesmin_processing_unitsmax_processing_unitshigh_priority_cpu_targettotal_cpu_target%disable_high_priority_cpu_autoscalingdisable_total_cpu_autoscalingN)autoscalingLimits)AutoscalingConfigOverridesAutoscalingLimitsr   minNodesmaxNodesminProcessingUnitsmaxProcessingUnits2autoscalingTargetHighPriorityCpuUtilizationPercent+autoscalingTargetTotalCpuUtilizationPercent!disableHighPriorityCpuAutoscalingdisableTotalCpuAutoscaling)msgsasymmetric_autoscaling_optionobjs      /lib/googlecloudsdk/api_lib/spanner/instances.pyMaybeGetAutoscalingOverrider(   .   sc    66
:
:
 (E
E
 (E
E
$,I
I
$A
A
1*+
)1N
N''..0 	( 	# 11%B;%OC"11%B;%OC"<</L0C, <</L0C,  #@@%&@A : 88%&89 3 -0MM%&MN ) %(EE%&EF " 
*    c                    ||y||S ||S |}|j                   |j                   | j                         |_         |j                   j                  %|j                   j                  |j                   _        |j                   j                  %|j                   j                  |j                   _        |j                   j                  %|j                   j                  |j                   _        |j                   j
                  %|j                   j
                  |j                   _        |j                  	 |j                  |_        |j                  |j                  |_        |j                  $|j                  |_        |j                  rd|_        |j                  $|j                  |_	        |j                  rd|_        |S )a:  Merges two AutoscalingConfigOverrides objects.

  Args:
    msgs: The messages module for the Spanner API.
    existing_overrides: The existing AutoscalingConfigOverrides object.
    new_overrides: The new AutoscalingConfigOverrides object to merge.

  Returns:
    The merged AutoscalingConfigOverrides object.
  N)
r   r   r   r   r   r   r    r!   r"   r#   )r$   existing_overridesnew_overridesresults       r'   MergeAutoscalingConfigOverrider.   `   s    M$9 &$$0'!%!7!7!9f&&//;

)
)
2
2 ' &&//;

)
)
2
2 ' &&99E

)
)
<
< 1 &&99E

)
)
<
< 1 FF 	HH = >>JAA 6 44@77 , //BFf?--9(5(P(PF%((;?f8	-r)   c                    i }|j                   x}r*|j                  D ]  }|||j                  j                  <    |D ]P  }|j                  j                  }||v r/||   }t	        | |j
                  |j
                        ||   _        L|||<   R |j                   j                  j                          |j                         D ]'  }	|j                   j                  j                  |	       ) y)a  Patch asymmetric autoscaling options.

  Args:
    msgs: API messages module.
    instance_obj: The instance object to patch.
    current_instance: The current instance object.
    asym_options_patch: A list of AsymmetricAutoscalingOption objects to patch.
  N)	r   asymmetricAutoscalingOptionsreplicaSelectionlocationr.   	overridesclearvaluesappend)
r$   instance_objcurrent_instanceasym_options_patchoption_by_locationconfigexisting_optionpatch_optionr2   opts
             r'   !PatchAsymmetricAutoscalingOptionsr?      s     111V1!>>
 99BBC ?
 )l,,55H%%*84o/M
))<+A+A0",
 &2" )   ==CCE&&(c""??FFsK )r)   c           
      J   t        j                  t        t              }t        j                  t        t              }t
        j                  j                  |dt        j                  j                  j                  j                  id      }t
        j                  j                  dt        j                  j                  j                  j                        }|j                  |j                         |      }|r||_        nU|r||_        nK|s|s
|s|s|	s|
s|r=|j%                  |j'                  ||||      |j)                  |	|
|      	      |_        |||_        ||j/                  |
      |_        |||_        |r9|j5                         r)|j                         dz   |j5                         z   |_        | |j                  j9                  |      |_        | |j                  j=                  |      |_        |p|j                  jA                  tC        |jE                               D cg c],  \  }}|j                  j@                  jG                  ||      . c}}      |_$        |Z|D ]U  }|j*                  jJ                  jM                  |jO                  tQ        ||      |jS                  |d                      W |jU                  |j                         |jW                  | |            }|jX                  j                  |      S c c}}w )a  Create a new instance.

  Args:
    instance: The instance to create.
    config: The instance config to use.
    description: The instance description.
    nodes: The number of nodes to use.
    processing_units: The number of processing units to use.
    autoscaling_min_nodes: The minimum number of nodes to use.
    autoscaling_max_nodes: The maximum number of nodes to use.
    autoscaling_min_processing_units: The minimum number of processing units to
      use.
    autoscaling_max_processing_units: The maximum number of processing units to
      use.
    autoscaling_high_priority_cpu_target: The high priority CPU target to use.
    autoscaling_total_cpu_target: The total CPU target to use.
    autoscaling_storage_target: The storage target to use.
    asymmetric_autoscaling_options: A list of ordered dict of key-value pairs
      representing the asymmetric autoscaling options.
    instance_type: The instance type to use.
    expire_behavior: The expire behavior to use.
    default_storage_type: The default storage type to use.
    ssd_cache: The ssd cache to use.
    edition: The edition to use.
    default_backup_schedule_type: The type of default backup schedule to use.
    tags: The parsed tags value.

  Returns:
    The created instance.
  
projectsId spanner.projects.instanceConfigsparams
collectionspanner.projectsrA   )r;   displayNamer   r   r   r   !highPriorityCpuUtilizationPercenttotalCpuUtilizationPercentstorageUtilizationPercentr   autoscalingTargetsexpireBehavior/ssdCaches/)keyvalue)additionalPropertiesr2   r2   )r3   r1   )
instanceIdinstance)parentcreateInstanceRequest)-r   GetClientInstance_SPANNER_API_NAME_SPANNER_API_VERSIONGetMessagesModuler	   REGISTRYParser   VALUEScoreproject	GetOrFailCreateInstanceRelativeName	nodeCountprocessingUnitsAutoscalingConfigr   AutoscalingTargetsr   instanceTypeFreeInstanceMetadatafreeInstanceMetadatadefaultStorageTypestripssdCacheEditionValueValuesEnumedition(DefaultBackupScheduleTypeValueValuesEnumdefaultBackupScheduleType	TagsValuesorteditemsAdditionalPropertytagsr0   r6   AsymmetricAutoscalingOptionr(   InstanceReplicaSelection%SpannerProjectsInstancesCreateRequestCreateInstanceRequestprojects_instances)rX   r;   descriptionnodesprocessing_unitsautoscaling_min_nodesautoscaling_max_nodes autoscaling_min_processing_units autoscaling_max_processing_units$autoscaling_high_priority_cpu_targetautoscaling_total_cpu_targetautoscaling_storage_targetasymmetric_autoscaling_optionsinstance_typeexpire_behaviordefault_storage_type	ssd_cachers   default_backup_schedule_typerz   clientr$   
config_refproject_refr7   rS   rT   asym_optionreqs                                r'   re   re      s)   h !!"35IJ&			 13G	H$!!''J--22::DDE3 ( *
 ""))Z%6%6%;%;%C%C%M%M * + $$&K  , "L#3L 		)	)	-	%	#%)%;%;00**??	 1 
  22.R'C&@ 3 
 &< &L"  -L (,(A(A& )B )L% %&:L#9??$!M1IOO4EE  ==??HL!->>(	
 *
 
// %TZZ\2
2
U MM##663e6L2
 0 L $/5$$AAHH

*
*3D+F#<<&z2  =   +  6 	22%%' 66 7  	3 	# 
	"	"	)	)#	../
s   71L
c                 >   t        j                  t        t              }t        j                  t        t              }t
        j                  |_        |j                  | j                         |j                  ||            }|j                  j                  |      S )zDSaves the given policy on the instance, overwriting whatever exists.)policy
updateMask)resourcesetIamPolicyRequest)r   r[   r\   r]   r^   r   !MAX_LIBRARY_IAM_SUPPORTED_VERSIONversion+SpannerProjectsInstancesSetIamPolicyRequestrg   SetIamPolicyRequestr   SetIamPolicy)instance_refr   
field_maskr   r$   r   s         r'   	SetPolicyr   Y  s    !!"35IJ&			 13G	H$==&.88((*22&>H 3 J 	9 	K# 
	"	"	/	/	44r)   c                 N   t        j                  t        t              }t        j                  t        t              }|j                  | j                         |j                  |j                  t        j                                    }|j                  j                  |      S )z#Gets the IAM policy on an instance.)requestedPolicyVersion)options)r   getIamPolicyRequest)r   r[   r\   r]   r^   +SpannerProjectsInstancesGetIamPolicyRequestrg   GetIamPolicyRequestGetPolicyOptionsr   r   r   GetIamPolicy)r   r   r$   r   s       r'   r   r   e  s    !!"35IJ&			 13G	H$88((*22''88 ( : 3 ; 	9 	<# 
	"	"	/	/	44r)   c                    t        j                  t        t              }t        j                  t        t              }t
        j                  j                  | dt        j                  j                  j                  j                  id      }|j                  |j                               }|j                  j!                  |      S )zDelete an instance.rA   spanner.projects.instancesrC   name)r   r[   r\   r]   r^   r	   r_   r`   r   ra   rb   rc   rd   %SpannerProjectsInstancesDeleteRequestrg   r   DeleterX   r   r$   refr   s        r'   r   r   r  s    !!"35IJ&			 13G	H$  J--22::DDE- 	! 	/# 	228H8H8J2K#		"	"	)	)#	..r)   c                    t        j                  t        t              }t        j                  t        t              }t
        j                  j                  | dt        j                  j                  j                  j                  id      }|j                  |j                               }|j                  j!                  |      S )zGet an instance by name.rA   r   rC   r   )r   r[   r\   r]   r^   r	   r_   r`   r   ra   rb   rc   rd   "SpannerProjectsInstancesGetRequestrg   r   Getr   s        r'   r   r   ~  s    !!"35IJ&			 13G	H$  J--22::DDE- 	! 	/# 	//S5E5E5G/H#		"	"	&	&s	++r)   c                  f   t        j                  t        t              } t        j                  t        t              }t
        j                  j                  dt        j                  j                  j                  j                        }t        j                         }|j                  t         j                   j#                         t$        z          |j'                  |j)                         |j+                               }t-        j.                  | j0                  |ddt2        j4                        S )zList instances in the project.rF   rG   )rY   instanceDeadline	instancespageSize)fieldbatch_size_attributeget_field_func)r   r[   r\   r]   r^   r	   r_   re   r   ra   rb   rc   rd   r   	TimestampFromDatetimedatetimeutcnowUNREACHABLE_INSTANCE_TIMEOUT#SpannerProjectsInstancesListRequestrg   ToJsonStringr   YieldFromListr   r   GetFieldAndLogUnreachable)r   r$   r   tp_protor   s        r'   Listr     s    !!"35IJ&			 13G	H$""))Z%6%6%;%;%C%C%M%M * O+$$&(
 #??A00%%',,. 	1 	0# 
	!	!	%"<<
> >r)   c           
      0	   g }||j                  d       ||j                  d       ||j                  d       |r|s|r|r|s|	r|
r|j                  t               n|r|j                  d       |r|j                  d       |r|j                  d       |r|j                  d       |r|j                  d       |	r|j                  d	       |
r|j                  d
       t        j                  t        t
              }t        j                  t        t
              }|j                  |      }|r||_        nU|r||_	        nK|s|s
|s|s|s|	s|
r=|j                  |j                  ||||      |j                  ||	|
            |_        |t        |vr|j                  d       t        |       }g }|D ]A  }|j                  |j                  |j!                  |d         t#        ||                   C |j                  |j                         |_        t%        ||||       |t        |vr|j                  d       t        |       }t'        |      }|j                  |j                         |_        g |j                  _        |j                  j(                  D ]@  }|j*                  j,                  |vs|j                  j(                  j                  |       B ||j                  d       ||_        |(|j                  d       |j1                  |      |_        |U|j                  d       d}|j5                         r+t        |       }|j6                  dz   |j5                         z   }||_        |1|j                  d       |j                  j;                  |      |_        |1|j                  d       |j                  j?                  |      |_         tB        jD                  jG                  | dtH        jJ                  jL                  jN                  jP                  id      }|jS                  |jU                         |jW                  djY                  |      |             }|jZ                  j]                  |      S )!zUpdate an instance.rH   znodeCount,autoscalingConfigz!processingUnits,autoscalingConfigz,autoscalingConfig.autoscalingLimits.minNodesz,autoscalingConfig.autoscalingLimits.maxNodesz6autoscalingConfig.autoscalingLimits.minProcessingUnitsz6autoscalingConfig.autoscalingLimits.maxProcessingUnitszFautoscalingConfig.autoscalingTargets.highPriorityCpuUtilizationPercentz?autoscalingConfig.autoscalingTargets.totalCpuUtilizationPercentz>autoscalingConfig.autoscalingTargets.storageUtilizationPercent)rH   rI   rJ   rN   z.autoscalingConfig.asymmetricAutoscalingOptionsr2   rV   )r1   r3   rl   z#freeInstanceMetadata.expireBehaviorrP   rq    rR   rs   ru   rA   r   rC   ,)	fieldMaskrX   )r   updateInstanceRequest)/r6   _FIELD_MASK_AUTOSCALING_CONFIGr   r[   r\   r]   r^   rf   ri   rh   rj   r   rk   r   r   r{   r|   r(   r?   setr0   r1   r2   rl   rm   rn   rp   r;   rq   rr   rs   rt   ru   r	   r_   r`   r   ra   rb   rc   rd   $SpannerProjectsInstancesPatchRequestrg   UpdateInstanceRequestjoinr   Patch)rX   r   r   r   r   r   r   r   r   r   r   r   $clear_asymmetric_autoscaling_optionsr   r   ssd_cache_idrs   r   fieldsr   r$   r7   r8   r9   r   locations_to_remover   instance_resr   r   s                                 r'   r   r     s   * &
MM- 

MM/0!
MM56 !6
*/O
.2N'
MM01mmBCmmBC'mmLM'mmLM+mm
R $mm
K "mm
J !!"35IJ&			 13G	H$;7,#3L "L		)	)	-	%	#%)%;%;00**??	 1 
  22.R'C&@ 3 
 &< &L" $/%V3mmDE8} 6

*
*#<<&z2  =   4D+F	 +  6 %%-'+'='='?l$%l,.@ *5%V3mmDE8}BC%%-'+'='='?l$BDL""? 
	+	+	H	H 			%	%	.	.6I	I&&CCJJ	
 
I 
MM.! -L 
MM78(,(A(A& )B )(L% 
MM*I]l%%58J8J8LLi%L
MM)==??HL!-
MM-.>>(	
 * 	  J--22::DDE- 	! 	/# 	11 66HHV$| 7 = 	2 	># 
	"	"	(	(	--r)   c                    t        j                  t        t              }t        j                  t        t              }t        |       }|j                  |j                        }|j                  j                  |      }|r=g }|j                  D ]*  }|j                  |j                  |j                  d       , |S t               }	|j                  D ]  }|	j                  |j                          |	D cg c]  }d|i }}|S c c}w )z,Get all the replica regions for an instance.r   )r2   typer2   )r   r[   r\   r]   r^   r   (SpannerProjectsInstanceConfigsGetRequestr;   projects_instanceConfigsreplicasr6   r2   r   r   add)
rX   verbose_flagr   r$   r   
config_req
config_rescommand_outputitem
region_sets
             r'   GetLocationsr   @  s    !!"35IJ&			 13G	H$X,<< =  *..22:>*N##		JK $ 
	 J##nnT]]# $5?@ZTz4(ZN@	 As   8Dc                    t        j                  t        t              }t        j                  t        t              }t
        j                  j                  |dt        j                  j                  j                  j                  id      }t
        j                  j                  | dt        j                  j                  j                  j                  id      }t        j                  dj                  | t!        |       |      dd       d	|j#                         i}||g |d<   |D ]r  }|d   j%                  d      }	i }
g |
d<   |	D ]  }|
d   j'                  |         |j(                  di |
}|d   j'                  |j+                  |d   |             t |j-                   |j.                  di ||j#                               }|j0                  j3                  |      j4                  }t7        j8                  d|      j;                  d      }t=        dj                  |j#                         ||              y
)aA  Moves an instance from one instance-config to another.

  Args:
      instance: Instance to move.
      target_instance_config: The target instance configuration to move the
        instance.
      target_database_move_configs: Configurations for databases in the
        destination instance config.

  The configs can be google-managed or user-managed.
  Ex: gcloud spanner instances move instance-to-move
  --target-config=instance-config-to-move-to

  Above example will move the instance(instance-to-move) to the following
  instance config(instance-config-to-move-to).
  rA   rB   rC   r   a:  You are about to move instance {0} from {1} to {2}. This is a long-running operation with potential service implications:


	* Increased latencies: Read and write operations may experience delays.

	* Elevated abort rate: Transactions may have a higher chance of failing.

	* Spiked CPU utilization: System resources will be strained, impacting performance.

	* Additional costs: Instance moves incur extra charges, as described in the documentation.

	* Backups: It is important that you copy your backups before moving your instance. Backups need to be deleted from the Instance before the move. You cannot create a backup while the move is in progress.

Before proceeding, and for detailed information and best practices, refer to the documentation at https://cloud.google.com/spanner/docs/move-instance#move-prerequisites.TzDo you want to proceed)messagecancel_on_noprompt_stringtargetConfigNtargetDatabaseMoveConfigszkms-key-namesr   kmsKeyNameszdatabase-id)
databaseIdencryptionConfig)moveInstanceRequestr   z.*/operations/(.*)   zj
Instance move started for {0}

Track progress with: gcloud spanner operations describe {1} --instance={2} )r   r[   r\   r]   r^   r	   r_   r`   r   ra   rb   rc   rd   r
   PromptContinueformatGetInstanceConfigrg   splitr6   InstanceEncryptionConfigDatabaseMoveConfig#SpannerProjectsInstancesMoveRequestMoveInstanceRequestr   Mover   researchgroupprint)rX   target_instance_configtarget_database_move_configsr   r$   r   r   req_argstarget_database_move_configkms_key_namesencryption_config_argskms_key_nameencryption_configmove_reqmove_operation_idoperation_ids                   r'   r   r   T  sE   " !!"35IJ&			 13G	H$!!''J--22::DDE3 ( *
 ##))J--22::DDE- * ,
 U 6)(35K ,+. j5578(!-,.H()'C#1/BHHMm!.0]+',}-44\B (7$77 " *+22

!
!4]C0 "  (D 552$22>X>$$& 6 ( //44X>CC/1BCII!L,%%+V

#
#
%|X&r)   c                    t        j                  t        t              }t        j                  t        t              }t
        j                  j                  | dt        j                  j                  j                  j                  id      }|j                  |j                         d      }|j                  j!                  |      }t#        j$                  d|j&                        j)                  d      }|S )z/Get the instance config of the passed instance.rA   r   rC   r;   )r   r   z.*/instanceConfigs/(.*)r   )r   r[   r\   r]   r^   r	   r_   r`   r   ra   rb   rc   rd   r   rg   r   r   r   r   r;   r   )rX   r   r$   r   instance_reqinstance_infoinstance_configs          r'   r   r     s    !!"35IJ&			 13G	H$##))J--22::DDE- * ,
 88$$&( 9 , ++//=-II!5!5	E!H  
r)   )NNNNNNNNNNNNNNNN)N)NNNNNNNNNNNNNNNNN)%__doc__r   r   apitools.base.pyr   cloudsdk.google.protobufr   googlecloudsdk.api_lib.spannerr   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.iamr   googlecloudsdk.corer   r	   googlecloudsdk.core.consoler
   KNOWN_ROLES	timedeltar   r\   r]   r   r(   r.   r?   re   r   r   r   r   r   r   r   r   r   r   r)   r'   <module>r     s	   #  	 ' 2 8 , 3 * ) 2  2x11"=   !4 .dET LP %)%))-!%##'!%	)I/X	5
5	/	,>, 
%)%))-!%##')-!%%_.D(Rjr)   