Skip to content

Commit

Permalink
sc-12506 standardize config data output to always have projects (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Warren <[email protected]>
  • Loading branch information
mattwwarren and Matthew Warren committed May 6, 2024
1 parent c84a58d commit c87b013
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
23 changes: 11 additions & 12 deletions src/dynamic_importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def process_configs(

click.echo(f"Writing config data to: {config_out_file}")
with open(config_out_file, "w+") as fp:
json.dump(config_data, fp, indent=4)
json.dump({project: config_data}, fp, indent=4)


@import_config.command()
Expand Down Expand Up @@ -160,13 +160,14 @@ def regenerate_template(default_values, env_values, file_type, data_file):
input_files[env] = file_path
click.echo(f"Processing {file_type} files from: {', '.join(input_files)}")

config_data = {}
project_config_data = {}
with open(data_file, "r") as fp:
config_data = json.load(fp)
project_config_data = json.load(fp)

processing_class = get_processor_class(file_type)
processor: BaseProcessor = processing_class(input_files)
template, _ = processor.process(hints=config_data)
for _, config_data in project_config_data.items():
processing_class = get_processor_class(file_type)
processor: BaseProcessor = processing_class(input_files)
template, _ = processor.process(hints=config_data)

input_filename = ".".join(
list(input_files.values())[0].split("/")[-1].split(".")[:-1]
Expand All @@ -191,17 +192,15 @@ def regenerate_template(default_values, env_values, file_type, data_file):
help="Full path to template file generated from process_configs command",
required=True,
)
@click.option(
"-p", "--project", help="CloudTruth project to import data into", required=True
)
@click.option("-k", help="Ignore SSL certificate verification", is_flag=True)
@click.option("-c", help="Create missing projects and enviroments", is_flag=True)
@click.option("-u", help="Upsert values", is_flag=True)
def create_data(data_file, template_file, project, k, c, u):
def create_data(data_file, template_file, k, c, u):
with open(data_file, "r") as dfp, open(template_file, "r") as tfp:
config_data = json.load(dfp)
project_config_data = json.load(dfp)
template_data = tfp.read()
_create_data(config_data, str(template_file), template_data, project, k, c, u)
for project, config_data in project_config_data.items():
_create_data(config_data, str(template_file), template_data, project, k, c, u)

click.echo("Data upload to CloudTruth complete!")

Expand Down
8 changes: 2 additions & 6 deletions src/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_create_data_no_api_key(tmp_path):
NamedTemporaryFile(dir=td, delete=False) as tmp_data,
NamedTemporaryFile(dir=td, delete=False) as tmp_template,
):
tmp_data.write(b"{}")
tmp_data.write(b'{"nullproj": {}}')
tmp_data.close()
tmp_template.write(b"{}")
tmp_template.close()
Expand All @@ -133,12 +133,10 @@ def test_create_data_no_api_key(tmp_path):
f"{tmp_data.name}",
"-m",
f"{tmp_template.name}",
"-p",
"testproj",
],
catch_exceptions=False,
)
assert result.exit_code == 2
assert result.exit_code == 2, result.output
assert (
"Error: CLOUDTRUTH_API_KEY environment variable is required."
in result.output
Expand Down Expand Up @@ -315,8 +313,6 @@ def test_cli_import_data_json(mock_get, mock_post, tmp_path):
f"{td}/testproj-json.ctconfig",
"-m",
f"{td}/testproj-json.cttemplate",
"-p",
"testproj",
],
catch_exceptions=False,
)
Expand Down

0 comments on commit c87b013

Please sign in to comment.