22
33from CommonServerPython import *
44
5- from packaging .version import parse , Version , LegacyVersion
5+ from packaging .version import parse , Version
66
77SCRIPT_NAME = 'MarketplacePackInstaller'
88
@@ -12,24 +12,37 @@ class ContentPackInstaller:
1212 """
1313 PACK_ID_VERSION_FORMAT = '{}::{}'
1414
15- def __init__ (self ):
16- self .installed_packs : Dict [str , Union [Version , LegacyVersion ]] = dict ()
17- self .newly_installed_packs : Dict [str , Version ] = dict ()
18- self .already_on_machine_packs : Dict [str , Union [Version , LegacyVersion ]] = dict ()
19- self .packs_data : Dict [str , Dict [str , str ]] = dict ()
20- self .packs_dependencies : Dict [str , Dict [str , Dict [str , str ]]] = dict ()
15+ def __init__ (self , instance_name : str = None ):
16+ self .installed_packs : Dict [str , Version ] = {}
17+ self .newly_installed_packs : Dict [str , Version ] = {}
18+ self .already_on_machine_packs : Dict [str , Version ] = {}
19+ self .packs_data : Dict [str , Dict [str , str ]] = {}
20+ self .packs_dependencies : Dict [str , Dict [str , Dict [str , str ]]] = {}
21+ self .packs_failed : Dict [str , str ] = {}
22+ self .instance_name : Optional [str ] = instance_name
2123
2224 self .get_installed_packs ()
2325
24- def get_installed_packs (self ) -> None :
25- """Gets the current installed packs on the machine.
26- """
26+ def _call_execute_command (self , command , args ):
27+ if self .instance_name :
28+ args ["using" ] = self .instance_name
29+
2730 status , res = execute_command (
28- 'demisto-api-get' ,
29- { 'uri' : '/contentpacks/metadata/installed' } ,
31+ command ,
32+ args ,
3033 fail_on_error = False ,
3134 )
3235
36+ if type (res ) is list :
37+ res = res [0 ]
38+ return status , res
39+
40+ def get_installed_packs (self ) -> None :
41+ """Gets the current installed packs on the machine."""
42+
43+ args = {"uri" : "/contentpacks/metadata/installed" }
44+
45+ status , res = self ._call_execute_command ("core-api-get" , args )
3346 if not status :
3447 return
3548
@@ -50,17 +63,15 @@ def get_pack_data_from_marketplace(self, pack_id: str) -> Dict[str, str]:
5063 if pack_id in self .packs_data :
5164 return self .packs_data [pack_id ]
5265
53- status , res = execute_command (
54- 'demisto-api-get' ,
55- {'uri' : f'/contentpacks/marketplace/{ pack_id } ' },
56- fail_on_error = False ,
57- )
66+ args = {"uri" : f"/contentpacks/marketplace/{ pack_id } " }
67+
68+ _ , res = self ._call_execute_command ("core-api-get" , args )
5869
5970 self .packs_data [pack_id ] = res
6071
6172 return res
6273
63- def get_pack_dependencies_from_marketplace (self , pack_data : Dict [str , str ]) -> Dict [str , Dict [str , str ]]:
74+ def get_pack_dependencies_from_marketplace (self , pack_data : Dict [str , str ]) -> Dict [str , Dict [str , str ]]: # pragma: no cover
6475 """Returns the dependencies of the pack from marketplace's data.
6576
6677 Args:
@@ -74,14 +85,9 @@ def get_pack_dependencies_from_marketplace(self, pack_data: Dict[str, str]) -> D
7485 if pack_key in self .packs_dependencies :
7586 return self .packs_dependencies [pack_key ]
7687
77- status , res = execute_command (
78- 'demisto-api-post' ,
79- {
80- 'uri' : '/contentpacks/marketplace/search/dependencies' ,
81- 'body' : [pack_data ]
82- },
83- fail_on_error = False ,
84- )
88+ args = {'uri' : '/contentpacks/marketplace/search/dependencies' , 'body' : [pack_data ]}
89+
90+ _ , res = self ._call_execute_command ("core-api-post" , args )
8591
8692 try :
8793 self .packs_dependencies [pack_key ] = res .get ('response' , {}).get ('packs' , [])[0 ] \
@@ -100,8 +106,15 @@ def get_latest_version_for_pack(self, pack_id: str) -> str:
100106 Returns:
101107 str. The latest version of the pack.
102108 """
103- res = self .get_pack_data_from_marketplace (pack_id )
104- return res .get ('response' , {}).get ('currentVersion' ) # type: ignore[call-overload, union-attr]
109+ try :
110+ res = self .get_pack_data_from_marketplace (pack_id )
111+ return res .get ('response' , {}).get ('currentVersion' ) # type: ignore[call-overload, union-attr]
112+ except AttributeError as e :
113+ raise ValueError (
114+ f"Error while fetching { pack_id } from the marketplace. "
115+ f"Make sure the Core REST API integration is properly configured and { pack_id } exists. "
116+ f"Raw Response:\n { res } "
117+ )
105118
106119 def get_packs_data_for_installation (self , packs_to_install : List [Dict [str , str ]]) -> List [Dict [str , str ]]:
107120 """Creates a list of packs' data for the installation request.
0 commit comments