azure_credentials.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. from azure_storage.methods import create_parent_parser, delete_keyring_credentials, \
  3. set_account_name, set_connection_string, setup_arguments
  4. from argparse import ArgumentParser, RawTextHelpFormatter
  5. import logging
  6. import sys
  7. import os
  8. def store_credentials(args):
  9. """
  10. Run the credentials setting methods
  11. :param args: type ArgumentParser arguments
  12. """
  13. logging.info('Setting Azure storage credentials from system keyring')
  14. # Set the account name in the keyring
  15. set_account_name(passphrase=args.passphrase,
  16. account_name=args.account_name)
  17. # Set the connection string in the keyring
  18. set_connection_string(passphrase=args.passphrase,
  19. account_name=args.account_name)
  20. def delete_credentials(args):
  21. """
  22. Run the credentials deleting methods
  23. :param args: type ArgumentParser arguments
  24. """
  25. logging.info('Deleting Azure storage credentials from system keyring')
  26. # Delete the account name in the keyring
  27. delete_keyring_credentials(passphrase=args.passphrase,
  28. account_name=args.passphrase)
  29. # Delete the connection string in the keyring
  30. delete_keyring_credentials(passphrase=args.passphrase,
  31. account_name=args.account_name)
  32. def cli():
  33. parser = ArgumentParser(description='Set, modify, or delete Azure storage credentials in the system keyring')
  34. # Create the parental parser, and the subparser
  35. subparsers, parent_parser = create_parent_parser(parser=parser,
  36. container=False)
  37. # Credentials storing/modifying subparser
  38. store_subparser = subparsers.add_parser(parents=[parent_parser],
  39. name='store',
  40. description='Store or update Azure storage credentials in the system '
  41. 'keyring',
  42. formatter_class=RawTextHelpFormatter,
  43. help='Store or update Azure storage credentials in the system keyring')
  44. store_subparser.set_defaults(func=store_credentials)
  45. # Credentials deleting subparser
  46. delete_subparser = subparsers.add_parser(parents=[parent_parser],
  47. name='delete',
  48. description='Delete Azure storage credentials in the system keyring',
  49. formatter_class=RawTextHelpFormatter,
  50. help='Delete Azure storage credentials in the system keyring')
  51. delete_subparser.set_defaults(func=delete_credentials)
  52. # Set up the arguments, and run the appropriate subparser
  53. arguments = setup_arguments(parser=parser)
  54. # Prevent the arguments being printed to the console (they are returned in order for the tests to work)
  55. sys.stderr = open(os.devnull, 'w')
  56. return arguments
  57. if __name__ == '__main__':
  58. cli()