Coverage for scripts / add_customer.py: 0%

46 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-25 18:08 -0500

1#!/usr/bin/env python3 

2""" 

3Add customers to a JSM service desk. 

4 

5Usage: 

6 python add_customer.py SD-1 --account-id 5b10ac8d82e05b22cc7d4ef5 

7 python add_customer.py SD-1 --account-id "id1,id2,id3" 

8 python add_customer.py SD-1 --account-id id1 --dry-run 

9""" 

10 

11import sys 

12import os 

13import argparse 

14from pathlib import Path 

15 

16sys.path.insert(0, str(Path(__file__).parent.parent.parent / 'shared' / 'scripts' / 'lib')) 

17 

18from config_manager import get_jira_client 

19from error_handler import print_error, JiraError 

20from formatters import print_success 

21 

22 

23def parse_account_ids(account_id_string: str) -> list: 

24 """ 

25 Parse comma-separated account IDs. 

26 

27 Args: 

28 account_id_string: Comma-separated account IDs 

29 

30 Returns: 

31 List of account IDs 

32 """ 

33 return [id.strip() for id in account_id_string.split(',') if id.strip()] 

34 

35 

36def validate_account_ids(account_ids: list) -> bool: 

37 """ 

38 Validate account IDs list. 

39 

40 Args: 

41 account_ids: List of account IDs 

42 

43 Returns: 

44 True if valid, False otherwise 

45 """ 

46 if not account_ids: 

47 return False 

48 return all(id.strip() for id in account_ids) 

49 

50 

51def add_customer_to_service_desk(service_desk_id: str, account_ids: list, 

52 profile: str = None) -> None: 

53 """ 

54 Add customers to a service desk. 

55 

56 Args: 

57 service_desk_id: Service desk ID or key 

58 account_ids: List of customer account IDs 

59 profile: JIRA profile to use 

60 """ 

61 with get_jira_client(profile) as client: 

62 client.add_customers_to_service_desk(service_desk_id, account_ids) 

63 

64 

65def main(): 

66 """Main entry point.""" 

67 parser = argparse.ArgumentParser( 

68 description='Add customers to a JSM service desk', 

69 formatter_class=argparse.RawDescriptionHelpFormatter, 

70 epilog=""" 

71Examples: 

72 Add single customer: 

73 %(prog)s SD-1 --account-id 5b10ac8d82e05b22cc7d4ef5 

74 

75 Add multiple customers: 

76 %(prog)s SD-1 --account-id "id1,id2,id3" 

77 

78 Dry-run: 

79 %(prog)s SD-1 --account-id id1 --dry-run 

80 """ 

81 ) 

82 

83 parser.add_argument('service_desk_id', 

84 help='Service desk ID or key (e.g., SD-1)') 

85 parser.add_argument('--account-id', required=True, 

86 help='Customer account ID(s) (comma-separated)') 

87 parser.add_argument('--dry-run', action='store_true', 

88 help='Show what would be added without adding') 

89 parser.add_argument('--profile', 

90 help='JIRA profile to use from config') 

91 

92 args = parser.parse_args() 

93 

94 try: 

95 account_ids = parse_account_ids(args.account_id) 

96 

97 if not validate_account_ids(account_ids): 

98 print_error("Invalid or empty account IDs") 

99 return 1 

100 

101 if args.dry_run: 

102 print("DRY RUN MODE - No changes will be made\n") 

103 print(f"Would add {len(account_ids)} customer(s) to service desk {args.service_desk_id}:") 

104 for account_id in account_ids: 

105 print(f" - {account_id}") 

106 return 0 

107 

108 add_customer_to_service_desk( 

109 service_desk_id=args.service_desk_id, 

110 account_ids=account_ids, 

111 profile=args.profile 

112 ) 

113 

114 print_success(f"Successfully added {len(account_ids)} customer(s) to service desk {args.service_desk_id}") 

115 

116 return 0 

117 

118 except JiraError as e: 

119 print_error(f"Failed to add customer(s): {e}") 

120 return 1 

121 except Exception as e: 

122 print_error(f"Unexpected error: {e}") 

123 return 1 

124 

125 

126if __name__ == '__main__': 

127 sys.exit(main())