55import argparse
66import boto3
77from botocore .exceptions import ClientError
8+ from boto3 .dynamodb .types import TypeSerializer ,TypeDeserializer
89import re
910import sys
1011import datetime
@@ -198,8 +199,25 @@ def hosted_zone_id(hzonename):
198199 print (e )
199200 raise
200201
201- def make_ddb_entry (cluname ,hzid ,recordname ,region ):
202- #Make cluster, hostedzone entries in the dynamodb table
202+ #serializes python dict obj to dynamodb for insertion
203+ def srl_ddb (python_obj : dict ) -> dict :
204+ serializer = TypeSerializer ()
205+ return {
206+ k : serializer .serialize (v )
207+ for k , v in python_obj .items ()
208+ }
209+
210+ #deserializes to dynamodb map data to python dict obj for use
211+ def dsrl_ddb (dynamo_obj : dict ) -> dict :
212+ deserializer = TypeDeserializer ()
213+ return {
214+ k : deserializer .deserialize (v )
215+ for k , v in dynamo_obj .items ()
216+ }
217+
218+
219+ def make_ddb_entry (cluname ,hzid ,recordname ,region ,allregions ):
220+ #Make cluster, hostedzone and region entries in the dynamodb table
203221 try :
204222 ddbclient = boto3 .client ('dynamodb' ,region_name = region )
205223
@@ -217,6 +235,9 @@ def make_ddb_entry(cluname,hzid,recordname,region):
217235 },
218236 "region" : {
219237 "S" : region
238+ },
239+ "allregions" : {
240+ "M" : allregions
220241 }
221242 }
222243 )
@@ -347,11 +368,26 @@ def main():
347368 gdbclient = boto3 .client ("rds" ,region_name = region )
348369 response = gdbclient .describe_global_clusters (GlobalClusterIdentifier = gdbcluname )
349370
350- # define boto client globally, since they get used in functions.
371+ # define dns client globally, since they get used in functions outside of the main function .
351372
352373 global dnsclient
353374 dnsclient = boto3 .client ("route53" , region_name = region )
354375
376+ #find all regions for this global database, and populate in allregion array
377+
378+ allregions = {}
379+
380+ for i in response ['GlobalClusters' ][0 ]['GlobalClusterMembers' ]:
381+ resourcename = i ['DBClusterArn' ]
382+ resourcename = resourcename .split (':' )
383+ regioname = resourcename [3 ] #region name is in the 3rd postion
384+ cluname = resourcename [6 ] #clustername is in the 6th position
385+ allregions [cluname ]= regioname
386+
387+ #serialize the data
388+ gdbobj = srl_ddb (allregions )
389+
390+
355391 # Loop thorugh each regional cluster member for the provided global cluster
356392 for i in response ['GlobalClusters' ][0 ]['GlobalClusterMembers' ]:
357393 resourcename = i ['DBClusterArn' ] #This is the ARN
@@ -391,7 +427,7 @@ def main():
391427 print ("CNAME" ,recordname ,"doesn't exist. Adding CNAME record.." )
392428 create_hosted_zone_record (hostedzonename ,recordname ,recordvalue ) # create a cname record in the hosted zone for the writer endpoint
393429 hzid = hosted_zone_id (hostedzonename ) #get hosted zone id
394- make_ddb_entry (cluname ,hzid ,recordname ,regioname ) # Add dynamodb entry
430+ make_ddb_entry (cluname ,hzid ,recordname ,regioname , gdbobj ) # Add dynamodb entry
395431 else :
396432 if (skipvpc == False ):
397433 print ("Vpc" ,instancevpc ," doesn't exist. Adding vpc.." )
@@ -407,12 +443,12 @@ def main():
407443 print ("CNAME" ,recordname ,"doesn't exist. Adding CNAME record for Primary region cluster" , cluname )
408444 create_hosted_zone_record (hostedzonename ,recordname ,recordvalue ) # create a cname record in the hosted zone for the writer endpoint
409445 hzid = hosted_zone_id (hostedzonename ) #get hosted zone id
410- make_ddb_entry (cluname ,hzid ,recordname ,regioname ) # Add dynamodb entry
446+ make_ddb_entry (cluname ,hzid ,recordname ,regioname , gdbobj ) # Add dynamodb entry
411447 else :
412448 print ("Hosted Zone doesn't exists. Creating " ,hostedzonename )
413449 hzid = create_hosted_zone (hostedzonename ,regioname ,instancevpc )
414450
415- make_ddb_entry (cluname ,hzid ,recordname ,regioname ) #Make ddb entry. This should only work from the calling region.
451+ make_ddb_entry (cluname ,hzid ,recordname ,regioname , gdbobj ) #Make ddb entry. This should only work from the calling region.
416452
417453 recordvalue = get_writer_endpoint (cluname ) # Get writer endpoint for the cluster
418454 print ("Adding CNAME record " , recordname ,"for for Primary region cluster " ,cluname )
@@ -440,20 +476,20 @@ def main():
440476 print ("VPC " ,instancevpc ,"Already exists for secondary region cluster " , cluname )
441477
442478 hzid = hosted_zone_id (hostedzonename ) #get hosted zone id
443- make_ddb_entry (cluname ,hzid ,recordname ,regioname ) # Add dynamodb entry
479+ make_ddb_entry (cluname ,hzid ,recordname ,regioname , gdbobj ) # Add dynamodb entry
444480
445481 else :
446482 if (skipvpc == False ):
447483 print ("VPC" ,instancevpc ," doesn't exist in the hosted zone. Adding vpc.." )
448484 hzid = update_hosted_zone (hostedzonename ,regioname ,instancevpc )
449- make_ddb_entry (cluname ,hzid ,recordname ,regioname ) #Make ddb entry. This should only work from the calling region.
485+ make_ddb_entry (cluname ,hzid ,recordname ,regioname , gdbobj ) #Make ddb entry. This should only work from the calling region.
450486 else :
451487 print ("Vpc" ,instancevpc ," doesn't exist. But skipping due to skip vpc flag." )
452488
453489 else :
454490 print ("Hosted Zone doesn't exists. Creating " ,hostedzonename )
455491 hzid = create_hosted_zone (hostedzonename ,regioname ,instancevpc )
456- make_ddb_entry (cluname ,hzid ,recordname ,regioname ) #Make ddb entry. This should only work from the calling region.
492+ make_ddb_entry (cluname ,hzid ,recordname ,regioname , gdbobj ) #Make ddb entry. This should only work from the calling region.
457493 else :
458494 print ("No entries made for this cluster. Cluster is not in the current region." )
459495
0 commit comments