@@ -3043,8 +3043,8 @@ def directed_read_options(
3043
3043
def set_custom_timeout_and_retry (instance_id , database_id ):
3044
3044
"""Executes a snapshot read with custom timeout and retry."""
3045
3045
# [START spanner_set_custom_timeout_and_retry]
3046
- from google .api_core import retry
3047
3046
from google .api_core import exceptions as core_exceptions
3047
+ from google .api_core import retry
3048
3048
3049
3049
# instance_id = "your-spanner-instance"
3050
3050
# database_id = "your-spanner-db-id"
@@ -3085,6 +3085,65 @@ def set_custom_timeout_and_retry(instance_id, database_id):
3085
3085
# [END spanner_set_custom_timeout_and_retry]
3086
3086
3087
3087
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
+
3088
3147
if __name__ == "__main__" : # noqa: C901
3089
3148
parser = argparse .ArgumentParser (
3090
3149
description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter
@@ -3366,3 +3425,5 @@ def set_custom_timeout_and_retry(instance_id, database_id):
3366
3425
directed_read_options (args .instance_id , args .database_id )
3367
3426
elif args .command == "set_custom_timeout_and_retry" :
3368
3427
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