Coverage for scripts / get_asset.py: 24%

49 statements  

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

1#!/usr/bin/env python3 

2""" 

3Get asset/CMDB object details by ID. 

4 

5Retrieves full asset details including all attributes. 

6Requires JSM Premium license or Assets license. 

7 

8Usage: 

9 get_asset.py --id 10001 

10 get_asset.py --id 10001 --output json 

11""" 

12 

13import argparse 

14import sys 

15import json 

16from pathlib import Path 

17 

18# Add shared lib to path 

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

20if shared_lib_path not in sys.path: 

21 sys.path.insert(0, shared_lib_path) 

22 

23from config_manager import get_jira_client 

24 

25 

26def get_asset(asset_id: int): 

27 """ 

28 Get asset details by ID. 

29 

30 Args: 

31 asset_id: Asset object ID 

32 

33 Returns: 

34 Asset object with all attributes 

35 """ 

36 with get_jira_client() as client: 

37 # Check license first 

38 if not client.has_assets_license(): 

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

40 sys.exit(1) 

41 

42 return client.get_asset(asset_id) 

43 

44 

45def format_text(asset: dict) -> str: 

46 """Format asset as human-readable text.""" 

47 output = [f"Asset: {asset.get('objectKey', 'N/A')} ({asset.get('label', 'N/A')})\n"] 

48 

49 if 'objectType' in asset: 

50 output.append(f"Object Type: {asset['objectType'].get('name', 'N/A')}") 

51 

52 if 'attributes' in asset: 

53 output.append("\nAttributes:") 

54 for attr in asset['attributes']: 

55 attr_name = attr.get('objectTypeAttribute', {}).get('name', 'Unknown') 

56 values = attr.get('objectAttributeValues', []) 

57 if values: 

58 attr_value = values[0].get('value', 'N/A') 

59 output.append(f" {attr_name}: {attr_value}") 

60 

61 if '_links' in asset and 'self' in asset['_links']: 

62 output.append(f"\nURL: {asset['_links']['self']}") 

63 

64 return "\n".join(output) 

65 

66 

67def format_json(asset: dict) -> str: 

68 """Format asset as JSON.""" 

69 return json.dumps(asset, indent=2) 

70 

71 

72def main(): 

73 parser = argparse.ArgumentParser( 

74 description="Get asset/CMDB object details", 

75 formatter_class=argparse.RawDescriptionHelpFormatter, 

76 epilog=__doc__ 

77 ) 

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

79 help='Asset object ID') 

80 parser.add_argument('--output', choices=['text', 'json'], default='text', 

81 help='Output format (default: text)') 

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

83 

84 args = parser.parse_args() 

85 

86 try: 

87 asset = get_asset(args.id) 

88 

89 if args.output == 'json': 

90 print(format_json(asset)) 

91 else: 

92 print(format_text(asset)) 

93 

94 except SystemExit: 

95 raise 

96 except Exception as e: 

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

98 sys.exit(1) 

99 

100 

101if __name__ == "__main__": 

102 main()