Skip to content

Commit

Permalink
CASMCMS-9205: Add missing fields to CFS config imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mharding-hpe committed Nov 14, 2024
1 parent c851a50 commit f67e246
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
30 changes: 21 additions & 9 deletions scripts/operations/configuration/import_cfs_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ def change_options(option_data: cfs.CfsOptions, option_names_to_change: List[str
print(f"{opt_name} = {option_updates[opt_name]}")
cfs.update_options(option_updates)

def scrub_layer(layer: JsonDict) -> None:
"""
Check if the layer contains both "branch" and "commit" fields. It is not legal to
specify both for a layer when creating a configuration. In these cases, we remove the
"commit" field when recreating the layer, as it will be automatically populated by CFS.
The alternative (omitting the "branch" field) means that information is lost, since the
"branch" field is only present if it is specified when creating the configuration.
"""
if "commit" in layer and "branch" in layer:
del layer["commit"]

def create_configs(configs_map: NameObjectMap, config_names_to_create: List[str]) -> None:
"""
Loop through the specified configuration names one at a time, and create them in CFS with
Expand All @@ -290,15 +301,16 @@ def create_configs(configs_map: NameObjectMap, config_names_to_create: List[str]
print("")
for config_name in config_names_to_create:
print(f"Importing configuration '{config_name}'")
# First, check for any layers which contain both "branch" and "commit" fields. It is not legal to
# specify both for a layer when creating a configuration. In these cases, we omit the
# "commit" field when recreating the layer, as it will be automatically populated by CFS.
# The alternative (omitting the "branch" field) means that information is lost, since the
# "branch" field is only present if it is specified when creating the configuration.
for layer in configs_map[config_name]["layers"]:
if "commit" in layer and "branch" in layer:
del layer["commit"]
cfs.create_configuration(config_name, configs_map[config_name]["layers"])
config_data = configs_map[config_name]
for layer in config_data["layers"]:
scrub_layer(layer)
# If an additional inventory layer is present, the same procedure must be done for it
if "additional_inventory" in config_data:
scrub_layer(config_data["additional_inventory"])
# When creating a CFS configuration, the 'name' and 'last_updated' fields cannot be specified
for field in [ 'name', 'last_updated' ]:
config_data.pop(field, None)
cfs.create_configuration(config_name, **config_data)

def chunk_list(items: list, max_batch_size: int=500) -> Generator[list, None, None]:
"""
Expand Down
5 changes: 3 additions & 2 deletions scripts/operations/configuration/python_lib/cfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,17 @@ def update_components_by_ids(comp_ids: List[str], update_data: JsonObject) -> Js

# CFS configuration functions

def create_configuration(config_name: str, layers: List[Dict[str, str]]) -> JsonObject:
def create_configuration(config_name: str, layers: List[Dict[str, str]], **config_fields) -> JsonObject:
"""
Creates or updates a CFS configuration with the specified name and layers.
The layers should be dictionaries with the following fields set:
clone_url, commit, name, playbook
The CFS configuration is returned if successful. Otherwise an exception is raised.
"""
config_fields["layers"] = layers
request_kwargs = {"url": f"{CFS_V3_CONFIGS_URL}/{config_name}",
"json": {"layers": layers},
"json": config_fields,
"add_api_token": True,
"expected_status_codes": {200}}
return api_requests.put_retry_validate_return_json(**request_kwargs)
Expand Down

0 comments on commit f67e246

Please sign in to comment.