diff --git a/commodore/cluster.py b/commodore/cluster.py index c757dc37d..a2569da1c 100644 --- a/commodore/cluster.py +++ b/commodore/cluster.py @@ -20,20 +20,26 @@ def fetch_cluster(cfg, clusterid): def reconstruct_api_response(target_yml): - target_data = yaml_load(target_yml)['parameters'] + target_data = yaml_load(target_yml) + target_parameters = target_data['parameters'] + target_classes = target_data['classes'] api_resp = { - 'id': target_data['cluster']['name'], + 'id': target_parameters['cluster']['name'], 'facts': { - 'cloud': target_data['cloud']['provider'], - 'distribution': target_data['cluster']['dist'], + 'cloud': target_parameters['cloud']['provider'], + 'distribution': target_parameters['cluster']['dist'], }, 'gitRepo': { - 'url': target_data['cluster']['catalog_url'], + 'url': target_parameters['cluster']['catalog_url'], }, - 'tenant': target_data['customer']['name'], + 'tenant': target_parameters['customer']['name'], } - if 'region' in target_data['cloud']: - api_resp['facts']['region'] = target_data['cloud']['region'] + if 'region' in target_parameters['cloud']: + api_resp['facts']['region'] = target_parameters['cloud']['region'] + for cl in target_classes: + if cl.startswith('global.lieutenant-instance.'): + api_resp['facts']['lieutenant-instance'] = cl.split('.')[2] + break return api_resp @@ -54,6 +60,9 @@ def _full_target(cluster, components, catalog): f"global.cloud.{cloud_provider}"] if 'region' in cluster_facts: global_defaults.append(f"global.cloud.{cloud_provider}.{cluster_facts['region']}") + if 'lieutenant-instance' in cluster_facts: + global_defaults.append( + f"global.lieutenant-instance.{cluster_facts['lieutenant-instance']}") global_defaults.append(f"{customer}.{cluster_id}") target = { 'classes': component_defaults + global_defaults, diff --git a/tests/test_target.py b/tests/test_target.py index 4e2860612..5fe88b443 100644 --- a/tests/test_target.py +++ b/tests/test_target.py @@ -7,26 +7,35 @@ from commodore import cluster -cluster_obj = { - 'id': 'mycluster', - 'tenant': 'mytenant', - 'facts': { - 'distribution': 'rancher', - 'cloud': 'cloudscale', +@pytest.fixture +def data(): + """ + Setup test data + """ + + data = { + 'cluster': { + 'id': 'mycluster', + 'tenant': 'mytenant', + 'facts': { + 'distribution': 'rancher', + 'cloud': 'cloudscale', + }, + }, + 'components': [], + 'catalog': 'ssh://git@git.example.com/cluster-catalogs/mycluster', } -} -components = ['test-component'] -catalog = 'ssh://git@git.example.com/cluster-catalogs/mycluster' + return data -def test_render_target(): - target = cluster._full_target(cluster_obj, components, catalog) - facts = cluster_obj['facts'] - all_classes = [f"defaults.{cn}" for cn in components] + [ +def test_render_target(data): + target = cluster._full_target(data['cluster'], data['components'], data['catalog']) + facts = data['cluster']['facts'] + all_classes = [f"defaults.{cn}" for cn in data['components']] + [ 'global.common', f"global.distribution.{facts['distribution']}", f"global.cloud.{facts['cloud']}", - f"{cluster_obj['tenant']}.{cluster_obj['id']}"] + f"{data['cluster']['tenant']}.{data['cluster']['id']}"] assert target != "" assert len(target['classes']) == len(all_classes), \ "rendered target includes different amount of classes" @@ -35,23 +44,24 @@ def test_render_target(): assert target['parameters']['target_name'] == 'cluster' -def test_optional_facts(): - cluster_obj['facts']['region'] = 'rma1' - target = cluster._full_target(cluster_obj, components, catalog) - facts = cluster_obj['facts'] +def test_optional_fact_region(data): + data['cluster']['facts']['region'] = 'rma1' + target = cluster._full_target(data['cluster'], data['components'], data['catalog']) + facts = data['cluster']['facts'] assert f"global.cloud.{facts['cloud']}.{facts['region']}" in target['classes'] -def test_missing_facts(): - cl = { - 'id': 'mycluster', - 'tenant': 'mytenant', - 'facts': { - 'distribution': 'rancher', - } - } +def test_optional_fact_lieutenant_instance(data): + data['cluster']['facts']['lieutenant-instance'] = 'lieutenant-dev' + target = cluster._full_target(data['cluster'], data['components'], data['catalog']) + facts = data['cluster']['facts'] + assert f"global.lieutenant-instance.{facts['lieutenant-instance']}" in target['classes'] + + +def test_missing_facts(data): + data['cluster']['facts'].pop('cloud') with pytest.raises(click.ClickException): - cluster._full_target(cl, components, catalog) + cluster._full_target(data['cluster'], data['components'], data['catalog']) def test_reconstruct_api_response(tmp_path): @@ -85,7 +95,7 @@ def test_reconstruct_api_response(tmp_path): def test_reconstruct_api_response_no_region(tmp_path): targetyml = tmp_path / 'cluster.yml' with open(targetyml, 'w') as file: - file.write('''classes: + file.write('''classes: [] parameters: cloud: provider: localdev @@ -101,10 +111,30 @@ def test_reconstruct_api_response_no_region(tmp_path): assert 'region' not in api_response['facts'] -def test_reconstruct_api_response_missing_fact(tmp_path): +def test_reconstruct_api_response_with_lieutenant_fact(tmp_path): targetyml = tmp_path / 'cluster.yml' with open(targetyml, 'w') as file: file.write('''classes: +- global.lieutenant-instance.lieutenant-dev +parameters: + cloud: + provider: localdev + cluster: + catalog_url: ssh://git@git.vshn.net/syn-dev/cluster-catalogs/srueg-k3d-int.git + dist: k3d + name: c-twilight-water-9032 + customer: + name: t-delicate-pine-3938 + target_name: cluster ''') + + api_response = cluster.reconstruct_api_response(targetyml) + assert api_response['facts']['lieutenant-instance'] == "lieutenant-dev" + + +def test_reconstruct_api_response_missing_fact(tmp_path): + targetyml = tmp_path / 'cluster.yml' + with open(targetyml, 'w') as file: + file.write('''classes: [] parameters: cloud: region: north