def perform_sync()

in python/syncinstances/sync_instances.py [0:0]


def perform_sync(src, tgt, src_role=None, tgt_role=None, dry_run=False):
    src_arn = parse_arn(src)
    tgt_arn = parse_arn(tgt)

    if src_role:
        src_session = assumed_role_session(src_role).client("connect")
        src_connect = src_session.client("connect", region_name=src_arn['region'])
    else:
        src_connect = boto3.client("connect", region_name=src_arn['region'])
    
    if tgt_role:
        tgt_session = assumed_role_session(tgt_role).client("connect")
        tgt_connect = tgt_session.client("connect", region_name=tgt_arn['region'])
    else:
        tgt_connect = boto3.client("connect", region_name=tgt_arn['region'])        

    src_instance_id = src_arn['resource']
    tgt_instance_id = tgt_arn['resource']

    # Get Shallow Users
    src_users = get_users_by_username(src_connect, src_instance_id)
    tgt_users = get_users_by_username(tgt_connect, tgt_instance_id)

    # Calculate difference between two sets:
    diff_users = src_users.keys() - tgt_users.keys()

    if len(diff_users) == 0:
        print("Nothing to update, exiting...")
        sys.exit(0)

    print(f"There are {len(diff_users)} different users to update.")
    deep_users = [
        get_deep_user(src_connect, src_instance_id, src_users[user]["Id"])
        for user in diff_users
    ]

    # Get Mappings of Security and Routing Profiles
    # Please note this only works if you have the same names
    # for your security profiles between regions
    # and those names are not in UUIDv4 format.
    src_routing = get_routing_profiles_index(src_connect, src_instance_id)
    tgt_routing = get_routing_profiles_index(tgt_connect, tgt_instance_id)

    src_security = get_security_profiles_index(src_connect, src_instance_id)
    tgt_security = get_security_profiles_index(tgt_connect, tgt_instance_id)

    for user in deep_users:
        security_profiles = []
        for profile in user["SecurityProfileIds"]:
            security_profiles.append(tgt_security[src_security[profile]["Name"]]["Id"])
        routing_profiles = tgt_routing[src_routing[user["RoutingProfileId"]]["Name"]]["Id"]
        if not dry_run:
            resp = tgt_connect.create_user(
                InstanceId=tgt_instance_id,
                Username=user["Username"],
                IdentityInfo=user["IdentityInfo"],
                PhoneConfig=user["PhoneConfig"],
                SecurityProfileIds=security_profiles,
                RoutingProfileId=routing_profiles,
                Tags=user["Tags"],
                # Hopefully password not required, it depends on how your authentication was set up
                # Password="D3l3tM3L8ter",
            )
        print(f"Added to {tgt_instance_id}: {user}")