Coverage for scripts / link_asset.py: 28%

32 statements  

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

1#!/usr/bin/env python3 

2""" 

3Link an asset to a service request. 

4 

5Creates a link between an asset/CMDB object and a service request. 

6Requires JSM Premium license or Assets license. 

7 

8Usage: 

9 link_asset.py --request REQ-123 --asset-id 10001 

10 link_asset.py --request REQ-123 --asset-id 10001 --comment "Primary server affected" 

11""" 

12 

13import argparse 

14import sys 

15from pathlib import Path 

16 

17# Add shared lib to path 

18shared_lib_path = str(Path(__file__).parent.parent.parent.parent / 'shared' / 'scripts' / 'lib') 

19if shared_lib_path not in sys.path: 

20 sys.path.insert(0, shared_lib_path) 

21 

22from config_manager import get_jira_client 

23 

24 

25def link_asset(asset_id: int, issue_key: str, comment: str = None): 

26 """ 

27 Link an asset to a service request. 

28 

29 Args: 

30 asset_id: Asset object ID 

31 issue_key: Request issue key 

32 comment: Optional comment about the link 

33 

34 Returns: 

35 None 

36 """ 

37 with get_jira_client() as client: 

38 # Check license first 

39 if not client.has_assets_license(): 

40 print("ERROR: Assets/Insight not available. Requires JSM Premium license.", file=sys.stderr) 

41 sys.exit(1) 

42 

43 # Link asset to request (adds internal comment) 

44 client.link_asset_to_request(asset_id, issue_key) 

45 

46 # Add additional comment if provided 

47 if comment: 

48 client.add_request_comment(issue_key, comment, public=False) 

49 

50 

51def main(): 

52 parser = argparse.ArgumentParser( 

53 description="Link asset to service request", 

54 formatter_class=argparse.RawDescriptionHelpFormatter, 

55 epilog=__doc__ 

56 ) 

57 parser.add_argument('--request', required=True, 

58 help='Request issue key (e.g., REQ-123)') 

59 parser.add_argument('--asset-id', type=int, required=True, 

60 help='Asset object ID') 

61 parser.add_argument('--comment', help='Optional comment about the link') 

62 parser.add_argument('--profile', help='JIRA profile to use') 

63 

64 args = parser.parse_args() 

65 

66 try: 

67 link_asset(args.asset_id, args.request, args.comment) 

68 

69 print(f"✓ Asset {args.asset_id} linked to {args.request} successfully!") 

70 

71 except SystemExit: 

72 raise 

73 except Exception as e: 

74 print(f"Error linking asset: {str(e)}", file=sys.stderr) 

75 sys.exit(1) 

76 

77 

78if __name__ == "__main__": 

79 main()