def write_keys_to_file(created_keys, session):
    for region in created_keys:
        ssh_key_dir = os.path.join(downloads_dir(), module_info['name'], region)
        if (not os.path.exists(ssh_key_dir)):
            os.makedirs(ssh_key_dir)
        for credential in created_keys[region]:
            if (credential['protocol'] == 'rdp'):
                windows_file_dir = os.path.join(ssh_key_dir, credential['instanceName'])
                try:
                    with open(windows_file_dir, 'w') as windows_file:
                        windows_file.write('instanceName,ipAddress,username,password\n')
                        windows_file.write((credential['instanceName'] + ','))
                        windows_file.write((credential['ipAddress'] + ','))
                        windows_file.write((credential['username'] + ','))
                        windows_file.write((credential['password'] + '\n'))
                except IOError:
                    print('Error writing credential file for {}.'.format(credential['instanceName']))
                    continue
            else:
                private_key_file_dir = os.path.join(ssh_key_dir, credential['instanceName'])
                cert_key_file_dir = os.path.join(ssh_key_dir, (credential['instanceName'] + '-cert.pub'))
                try:
                    with open(private_key_file_dir, 'w') as private_key_file:
                        private_key_file.write(credential['privateKey'])
                    with open(cert_key_file_dir, 'w') as cert_key_file:
                        cert_key_file.write(credential['certKey'])
                except IOError:
                    print('Error writing credential file for {}.'.format(credential['instanceName']))
                    continue
def write_keys_to_file(created_keys, session):
    for region in created_keys:
        ssh_key_dir = os.path.join(downloads_dir(), module_info['name'], region)
        if (not os.path.exists(ssh_key_dir)):
            os.makedirs(ssh_key_dir)
        for credential in created_keys[region]:
            if (credential['protocol'] == 'rdp'):
                windows_file_dir = os.path.join(ssh_key_dir, credential['instanceName'])
                try:
                    with open(windows_file_dir, 'w') as windows_file:
                        windows_file.write('instanceName,ipAddress,username,password\n')
                        windows_file.write((credential['instanceName'] + ','))
                        windows_file.write((credential['ipAddress'] + ','))
                        windows_file.write((credential['username'] + ','))
                        windows_file.write((credential['password'] + '\n'))
                except IOError:
                    print('Error writing credential file for {}.'.format(credential['instanceName']))
                    continue
            else:
                private_key_file_dir = os.path.join(ssh_key_dir, credential['instanceName'])
                cert_key_file_dir = os.path.join(ssh_key_dir, (credential['instanceName'] + '-cert.pub'))
                try:
                    with open(private_key_file_dir, 'w') as private_key_file:
                        private_key_file.write(credential['privateKey'])
                    with open(cert_key_file_dir, 'w') as cert_key_file:
                        cert_key_file.write(credential['certKey'])
                except IOError:
                    print('Error writing credential file for {}.'.format(credential['instanceName']))
                    continue