1
- from dataclasses import dataclass
2
- from contentctl .input .director import DirectorInputDto
3
- from contentctl .output .conf_output import ConfOutput
4
-
5
-
6
- from typing import Union
7
-
8
- @dataclass (frozen = True )
9
- class ACSDeployInputDto :
10
- director_input_dto : DirectorInputDto
11
- splunk_api_username : str
12
- splunk_api_password : str
13
- splunk_cloud_jwt_token : str
14
- splunk_cloud_stack : str
15
- stack_type : str
1
+ from contentctl .objects .config import deploy_acs , StackType
2
+ from requests import post
3
+ import pprint
16
4
17
5
18
6
class Deploy :
19
- def execute (self , input_dto : ACSDeployInputDto ) -> None :
7
+ def execute (self , config : deploy_acs , appinspect_token : str ) -> None :
20
8
21
- conf_output = ConfOutput (input_dto .director_input_dto .input_path , input_dto .director_input_dto .config )
9
+ #The following common headers are used by both Clasic and Victoria
10
+ headers = {
11
+ 'Authorization' : f'Bearer { config .splunk_cloud_jwt_token } ' ,
12
+ 'ACS-Legal-Ack' : 'Y'
13
+ }
14
+ try :
15
+
16
+ with open (config .getPackageFilePath (include_version = False ),'rb' ) as app_data :
17
+ #request_data = app_data.read()
18
+ if config .stack_type == StackType .classic :
19
+ # Classic instead uses a form to store token and package
20
+ # https://docs.splunk.com/Documentation/SplunkCloud/9.1.2308/Config/ManageApps#Manage_private_apps_using_the_ACS_API_on_Classic_Experience
21
+ address = f"https://admin.splunk.com/{ config .splunk_cloud_stack } /adminconfig/v2/apps"
22
+
23
+ form_data = {
24
+ 'token' : (None , appinspect_token ),
25
+ 'package' : app_data
26
+ }
27
+ res = post (address , headers = headers , files = form_data )
28
+ elif config .stack_type == StackType .victoria :
29
+ # Victoria uses the X-Splunk-Authorization Header
30
+ # It also uses --data-binary for the app content
31
+ # https://docs.splunk.com/Documentation/SplunkCloud/9.1.2308/Config/ManageApps#Manage_private_apps_using_the_ACS_API_on_Victoria_Experience
32
+ headers .update ({'X-Splunk-Authorization' : appinspect_token })
33
+ address = f"https://admin.splunk.com/{ config .splunk_cloud_stack } /adminconfig/v2/apps/victoria"
34
+ res = post (address , headers = headers , data = app_data .read ())
35
+ else :
36
+ raise Exception (f"Unsupported stack type: '{ config .stack_type } '" )
37
+ except Exception as e :
38
+ raise Exception (f"Error installing to stack '{ config .splunk_cloud_stack } ' (stack_type='{ config .stack_type } ') via ACS:\n { str (e )} " )
22
39
23
- appinspect_token = conf_output .inspectAppAPI (input_dto .splunk_api_username , input_dto .splunk_api_password , input_dto .stack_type )
24
-
25
-
26
- if input_dto .splunk_cloud_jwt_token is None or input_dto .splunk_cloud_stack is None :
27
- if input_dto .splunk_cloud_jwt_token is None :
28
- raise Exception ("Cannot deploy app via ACS, --splunk_cloud_jwt_token was not defined on command line." )
29
- else :
30
- raise Exception ("Cannot deploy app via ACS, --splunk_cloud_stack was not defined on command line." )
40
+ try :
41
+ # Request went through and completed, but may have returned a non-successful error code.
42
+ # This likely includes a more verbose response describing the error
43
+ res .raise_for_status ()
44
+ except Exception as e :
45
+ try :
46
+ error_text = res .json ()
47
+ except Exception as e :
48
+ error_text = "No error text - request failed"
49
+ formatted_error_text = pprint .pformat (error_text )
50
+ raise Exception (f"Error installing to stack '{ config .splunk_cloud_stack } ' (stack_type='{ config .stack_type } ') via ACS:\n { formatted_error_text } " )
31
51
32
- conf_output .deploy_via_acs (input_dto .splunk_cloud_jwt_token ,
33
- input_dto .splunk_cloud_stack ,
34
- appinspect_token ,
35
- input_dto .stack_type )
52
+ print (f"'{ config .getPackageFilePath (include_version = False )} ' successfully installed to stack '{ config .splunk_cloud_stack } ' (stack_type='{ config .stack_type } ') via ACS!" )
36
53
37
54
38
55
0 commit comments