Skip to content

Commit bf63cdd

Browse files
committed
Implement suggestions from
casey on PR. Improve naming, address other PEP8 concerns, and move some logic from contentctl.main into contentctl.test_common_func
1 parent 97f0fa9 commit bf63cdd

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

contentctl/api.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from pathlib import Path
22
from typing import Any, Union, Type
3-
#from contentctl import contentctl
43
from contentctl.input.yml_reader import YmlReader
5-
from contentctl.objects.config import test_common, test_servers, test
4+
from contentctl.objects.config import test_common, test, test_servers
65
from contentctl.objects.security_content_object import SecurityContentObject
6+
from contentctl.input.director import DirectorOutputDto
77

8-
9-
def configFromFile(path:Path=Path("contentctl.yml"), config: dict[str,Any]={},
10-
configType:Type[Union[test,test_servers]]=test)->Union[test,test_servers]:
8+
def config_from_file(path:Path=Path("contentctl.yml"), config: dict[str,Any]={},
9+
configType:Type[Union[test,test_servers]]=test)->test_common:
1110

1211
"""
1312
Fetch a configuration object that can be used for a number of different contentctl
@@ -36,23 +35,23 @@ def configFromFile(path:Path=Path("contentctl.yml"), config: dict[str,Any]={},
3635
except Exception as e:
3736
raise Exception(f"Failed to load contentctl configuration from file '{path}': {str(e)}")
3837

39-
#Apply settings that have been overridden from the ones in the file
38+
# Apply settings that have been overridden from the ones in the file
4039
try:
4140
yml_dict.update(config)
4241
except Exception as e:
4342
raise Exception(f"Failed updating dictionary of values read from file '{path}'"
4443
f" with the dictionary of arguments passed: {str(e)}")
4544

4645
# The function below will throw its own descriptive exception if it fails
47-
configObject = configFromDict(yml_dict, configType=configType)
46+
configObject = config_from_dict(yml_dict, configType=configType)
4847

4948
return configObject
5049

5150

5251

5352

54-
def configFromDict(config: dict[str,Any]={},
55-
configType:Type[Union[test,test_servers]]=test)->Union[test,test_servers]:
53+
def config_from_dict(config: dict[str,Any]={},
54+
configType:Type[Union[test,test_servers]]=test)->test_common:
5655
"""
5756
Fetch a configuration object that can be used for a number of different contentctl
5857
operations including validate, build, inspect, test, and test_servers. A dict will
@@ -79,7 +78,7 @@ def configFromDict(config: dict[str,Any]={},
7978
return test_object
8079

8180

82-
def updateConfig(config:Union[test,test_servers], **key_value_updates:dict[str,Any])->Union[test,test_servers]:
81+
def update_config(config:Union[test,test_servers], **key_value_updates:dict[str,Any])->test_common:
8382

8483
"""Update any relevant keys in a config file with the specified values.
8584
Full validation will be performed after this update and descriptive errors
@@ -109,8 +108,8 @@ def updateConfig(config:Union[test,test_servers], **key_value_updates:dict[str,A
109108
# Collect any errors that may occur
110109
errors:list[Exception] = []
111110

112-
#We need to do this one by one because the extra:forbid argument does not appear to
113-
#be respected, but validate_assignmen
111+
# We need to do this one by one because the extra:forbid argument does not appear to
112+
# be respected at this time.
114113
for key, value in key_value_updates.items():
115114
try:
116115
setattr(config_copy,key,value)
@@ -123,8 +122,8 @@ def updateConfig(config:Union[test,test_servers], **key_value_updates:dict[str,A
123122
return config_copy
124123

125124

126-
from contentctl.input.director import DirectorOutputDto
127-
def contentToDict(director:DirectorOutputDto)->dict[str,list[dict[str,Any]]]:
125+
126+
def content_to_dict(director:DirectorOutputDto)->dict[str,list[dict[str,Any]]]:
128127
output_dict:dict[str,list[dict[str,Any]]] = {}
129128
for contentType in ['detections','stories','baselines','investigations',
130129
'playbooks','macros','lookups','deployments','ssa_detections']:
@@ -133,7 +132,6 @@ def contentToDict(director:DirectorOutputDto)->dict[str,list[dict[str,Any]]]:
133132
t:list[SecurityContentObject] = getattr(director,contentType)
134133

135134
for item in t:
136-
print(item.model_dump())
137135
output_dict[contentType].append(item.model_dump())
138136
return output_dict
139137

contentctl/contentctl.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ def deploy_rest_func(config:deploy_rest):
102102

103103

104104
def test_common_func(config:test_common):
105+
if type(config) == test:
106+
#construct the container Infrastructure objects
107+
config.getContainerInfrastructureObjects()
108+
#otherwise, they have already been passed as servers
109+
105110
director_output_dto = build_func(config)
106111
gitServer = GitService(director=director_output_dto,config=config)
107112
detections_to_test = gitServer.getContent()
@@ -212,10 +217,6 @@ def main():
212217
elif type(config) == deploy_rest:
213218
deploy_rest_func(config)
214219
elif type(config) == test or type(config) == test_servers:
215-
if type(config) == test:
216-
#construct the container Infrastructure objects
217-
config.getContainerInfrastructureObjects()
218-
#otherwise, they have already been passed as servers
219220
test_common_func(config)
220221
else:
221222
raise Exception(f"Unknown command line type '{type(config).__name__}'")

0 commit comments

Comments
 (0)