1212TIMEOUT_SECONDS = 1200 # 20 minutes
1313
1414# snippet-start:[neptune.python.delete.cluster.main]
15- from botocore .exceptions import ClientError
16-
1715def delete_db_cluster (neptune_client , cluster_id : str ):
1816 """
1917 Deletes a Neptune DB cluster and throws exceptions to the caller.
@@ -34,7 +32,7 @@ def delete_db_cluster(neptune_client, cluster_id: str):
3432 print (f"Deleting DB Cluster: { cluster_id } " )
3533 neptune_client .delete_db_cluster (** request )
3634
37- except ClientError as err :
35+ except botocore . ClientError as err :
3836 code = err .response ["Error" ]["Code" ]
3937 message = err .response ["Error" ]["Message" ]
4038
@@ -78,7 +76,7 @@ def delete_db_instance(neptune_client, instance_id: str):
7876
7977 print (f"DB Instance '{ instance_id } ' successfully deleted." )
8078
81- except ClientError as err :
79+ except botocore . ClientError as err :
8280 code = err .response ["Error" ]["Code" ]
8381 message = err .response ["Error" ]["Message" ]
8482
@@ -111,7 +109,7 @@ def delete_db_subnet_group(neptune_client, subnet_group_name):
111109 neptune_client .delete_db_subnet_group (** delete_group_request )
112110 print (f"🗑️ Deleting Subnet Group: { subnet_group_name } " )
113111
114- except ClientError as err :
112+ except botocore . ClientError as err :
115113 code = err .response ["Error" ]["Code" ]
116114 message = err .response ["Error" ]["Message" ]
117115
@@ -194,7 +192,7 @@ def start_db_cluster(neptune_client, cluster_identifier: str):
194192 # Initial wait in case the cluster was just stopped
195193 time .sleep (30 )
196194 neptune_client .start_db_cluster (DBClusterIdentifier = cluster_identifier )
197- except ClientError as err :
195+ except botocore . ClientError as err :
198196 code = err .response ["Error" ]["Code" ]
199197 message = err .response ["Error" ]["Message" ]
200198
@@ -213,7 +211,7 @@ def start_db_cluster(neptune_client, cluster_identifier: str):
213211 clusters = []
214212 for page in pages :
215213 clusters .extend (page .get ('DBClusters' , []))
216- except ClientError as err :
214+ except botocore . ClientError as err :
217215 code = err .response ["Error" ]["Code" ]
218216 message = err .response ["Error" ]["Message" ]
219217
@@ -240,8 +238,6 @@ def start_db_cluster(neptune_client, cluster_identifier: str):
240238# snippet-end:[neptune.python.start.cluster.main]
241239
242240# snippet-start:[neptune.python.stop.cluster.main]
243- from botocore .exceptions import ClientError
244-
245241def stop_db_cluster (neptune_client , cluster_identifier : str ):
246242 """
247243 Stops an Amazon Neptune DB cluster and waits until it's fully stopped.
@@ -256,7 +252,7 @@ def stop_db_cluster(neptune_client, cluster_identifier: str):
256252 """
257253 try :
258254 neptune_client .stop_db_cluster (DBClusterIdentifier = cluster_identifier )
259- except ClientError as err :
255+ except botocore . ClientError as err :
260256 code = err .response ["Error" ]["Code" ]
261257 message = err .response ["Error" ]["Message" ]
262258
@@ -275,7 +271,7 @@ def stop_db_cluster(neptune_client, cluster_identifier: str):
275271 clusters = []
276272 for page in pages :
277273 clusters .extend (page .get ('DBClusters' , []))
278- except ClientError as err :
274+ except botocore . ClientError as err :
279275 code = err .response ["Error" ]["Code" ]
280276 message = err .response ["Error" ]["Message" ]
281277
@@ -343,12 +339,12 @@ def describe_db_clusters(neptune_client, cluster_id: str):
343339
344340 if not found :
345341 # Treat empty response as cluster not found
346- raise ClientError (
342+ raise botocore . ClientError (
347343 {"Error" : {"Code" : "DBClusterNotFound" , "Message" : f"No cluster found with ID '{ cluster_id } '" }},
348344 "DescribeDBClusters"
349345 )
350346
351- except ClientError as err :
347+ except botocore . ClientError as err :
352348 code = err .response ["Error" ]["Code" ]
353349 message = err .response ["Error" ]["Message" ]
354350
@@ -381,7 +377,7 @@ def check_instance_status(neptune_client, instance_id: str, desired_status: str)
381377 for page in pages :
382378 instances .extend (page .get ('DBInstances' , []))
383379
384- except ClientError as err :
380+ except botocore . ClientError as err :
385381 code = err .response ["Error" ]["Code" ]
386382 message = err .response ["Error" ]["Message" ]
387383
@@ -434,7 +430,7 @@ def create_db_instance(neptune_client, db_instance_id: str, db_cluster_id: str)
434430 print (f"DB Instance '{ db_instance_id } ' is now available." )
435431 return instance ['DBInstanceIdentifier' ]
436432
437- except ClientError as err :
433+ except botocore . ClientError as err :
438434 code = err .response ["Error" ]["Code" ]
439435 message = err .response ["Error" ]["Message" ]
440436
@@ -483,7 +479,7 @@ def create_db_cluster(neptune_client, db_name: str) -> str:
483479 print (f"DB Cluster created: { cluster_id } " )
484480 return cluster_id
485481
486- except ClientError as e :
482+ except botocore . ClientError as e :
487483 code = e .response ["Error" ]["Code" ]
488484 message = e .response ["Error" ]["Message" ]
489485
@@ -526,8 +522,6 @@ def get_default_vpc_id() -> str:
526522
527523
528524# snippet-start:[neptune.python.create.subnet.main]
529- from botocore .exceptions import ClientError
530-
531525def create_subnet_group (neptune_client , group_name : str ):
532526 """
533527 Creates a Neptune DB subnet group and returns its name and ARN.
@@ -565,22 +559,20 @@ def create_subnet_group(neptune_client, group_name: str):
565559 print (f"ARN: { arn } " )
566560 return name , arn
567561
568- except Exception as e :
569- if isinstance (e , ClientError ):
570- code = e .response ["Error" ]["Code" ]
571- msg = e .response ["Error" ]["Message" ]
562+ except botocore .ClientError as e :
563+ code = e .response ["Error" ]["Code" ]
564+ msg = e .response ["Error" ]["Message" ]
572565
573- if code == "ServiceQuotaExceededException" :
574- print ("Subnet group quota exceeded." )
575- raise RuntimeError ("Subnet group quota exceeded." ) from e
576- else :
577- print (f"AWS error [{ code } ]: { msg } " )
578- raise RuntimeError (f"AWS error [{ code } ]: { msg } " ) from e
566+ if code == "ServiceQuotaExceededException" :
567+ print ("Subnet group quota exceeded." )
568+ raise RuntimeError ("Subnet group quota exceeded." ) from e
579569 else :
580- print (f"Unexpected error creating subnet group '{ group_name } ': { e } " )
581- raise RuntimeError (f"Unexpected error creating subnet group '{ group_name } ': { e } " ) from e
582-
570+ print (f"AWS error [{ code } ]: { msg } " )
571+ raise RuntimeError (f"AWS error [{ code } ]: { msg } " ) from e
583572
573+ except Exception as e :
574+ print (f"Unexpected error creating subnet group '{ group_name } ': { e } " )
575+ raise RuntimeError (f"Unexpected error creating subnet group '{ group_name } ': { e } " ) from e
584576# snippet-end:[neptune.python.create.subnet.main]
585577
586578def wait_for_input_to_continue ():
@@ -666,7 +658,7 @@ def run_scenario(neptune_client, subnet_group_name: str, db_instance_id: str, cl
666658
667659 print ("Neptune resources deleted successfully" )
668660
669- except ClientError as ce :
661+ except botocore . ClientError as ce :
670662 code = ce .response ["Error" ]["Code" ]
671663
672664 if code in ("DBInstanceNotFound" , "DBInstanceNotFoundFault" , "ResourceNotFound" ):
@@ -707,7 +699,6 @@ def main():
707699 https://docs.aws.amazon.com/code-library/latest/ug/what-is-code-library.html
708700 """ )
709701
710-
711702if __name__ == "__main__" :
712703 main ()
713704# snippet-end:[neptune.python.scenario.main]
0 commit comments