@@ -3043,8 +3043,8 @@ def directed_read_options(
30433043def set_custom_timeout_and_retry (instance_id , database_id ):
30443044 """Executes a snapshot read with custom timeout and retry."""
30453045 # [START spanner_set_custom_timeout_and_retry]
3046- from google .api_core import retry
30473046 from google .api_core import exceptions as core_exceptions
3047+ from google .api_core import retry
30483048
30493049 # instance_id = "your-spanner-instance"
30503050 # database_id = "your-spanner-db-id"
@@ -3085,6 +3085,65 @@ def set_custom_timeout_and_retry(instance_id, database_id):
30853085 # [END spanner_set_custom_timeout_and_retry]
30863086
30873087
3088+ # [START spanner_create_instance_with_autoscaling_config]
3089+ def create_instance_with_autoscaling_config (instance_id ):
3090+ """Creates a Cloud Spanner instance with an autoscaling configuration."""
3091+ from google .cloud .spanner_admin_instance_v1 .types import \
3092+ spanner_instance_admin
3093+
3094+ spanner_client = spanner .Client ()
3095+
3096+ config_name = "{}/instanceConfigs/regional-us-central1" .format (
3097+ spanner_client .project_name
3098+ )
3099+
3100+ autoscaling_config = spanner_instance_admin .AutoscalingConfig (
3101+ # Only one of minNodes/maxNodes or minProcessingUnits/maxProcessingUnits can be set.
3102+ autoscaling_limits = spanner_instance_admin .AutoscalingConfig .AutoscalingLimits (
3103+ min_nodes = 1 ,
3104+ max_nodes = 2 ,
3105+ ),
3106+ # highPriorityCpuUtilizationPercent and storageUtilizationPercent are both
3107+ # percentages and must lie between 0 and 100.
3108+ autoscaling_targets = spanner_instance_admin .AutoscalingConfig .AutoscalingTargets (
3109+ high_priority_cpu_utilization_percent = 65 ,
3110+ storage_utilization_percent = 95 ,
3111+ ),
3112+ )
3113+
3114+ # Creates a new instance with autoscaling configuration
3115+ # When autoscalingConfig is enabled, nodeCount and processingUnits fields
3116+ # need not be specified.
3117+ request = spanner_instance_admin .CreateInstanceRequest (
3118+ parent = spanner_client .project_name ,
3119+ instance_id = instance_id ,
3120+ instance = spanner_instance_admin .Instance (
3121+ config = config_name ,
3122+ display_name = "This is a display name." ,
3123+ autoscaling_config = autoscaling_config ,
3124+ labels = {
3125+ "cloud_spanner_samples" : "true" ,
3126+ "sample_name" : "snippets-create_instance_with_autoscaling_config" ,
3127+ "created" : str (int (time .time ())),
3128+ },
3129+ ),
3130+ )
3131+
3132+ operation = spanner_client .instance_admin_api .create_instance (request = request )
3133+
3134+ print ("Waiting for operation to complete..." )
3135+ instance = operation .result (OPERATION_TIMEOUT_SECONDS )
3136+
3137+ print (
3138+ "Created instance {} with {} autoscaling config" .format (
3139+ instance_id , instance .autoscaling_config
3140+ )
3141+ )
3142+
3143+
3144+ # [END spanner_create_instance_with_autoscaling_config]
3145+
3146+
30883147if __name__ == "__main__" : # noqa: C901
30893148 parser = argparse .ArgumentParser (
30903149 description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter
@@ -3366,3 +3425,5 @@ def set_custom_timeout_and_retry(instance_id, database_id):
33663425 directed_read_options (args .instance_id , args .database_id )
33673426 elif args .command == "set_custom_timeout_and_retry" :
33683427 set_custom_timeout_and_retry (args .instance_id , args .database_id )
3428+ elif args .command == "create_instance_with_autoscaling_config" :
3429+ create_instance_with_autoscaling_config (args .instance_id )
0 commit comments