Skip to content

Commit

Permalink
Feat: use elif
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollorion committed Aug 6, 2024
1 parent 6df72d9 commit 254cf54
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions spacemk/exporters/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,62 +1342,58 @@ def _map_contexts_data(self, src_data: dict) -> dict:
"name": variable_set.get("attributes.name")
}
)
else:
# Terraform variable sets can be attached to multiple projects
# while Spacelift contexts are attached to a single space.
# To work around this quirk, we duplicate the context for each space.
if (
"relationships.projects.data" in variable_set
and variable_set.get("relationships.projects.data") is not None
and len(variable_set.get("relationships.projects.data")) > 0
):
for project in variable_set.get("relationships.projects.data"):
logging.info(f"Append context copy '{project.get('id')}' / '{variable_set.get('id')}'")
data.append(
{
"_migration_id": self._generate_migration_id(variable_set.get("id")),
"_relationships": {
"space": {
"_migration_id": self._generate_migration_id(project.get("id"))
},
"stacks": [], # The list is empty because it will be auto-attached to all stacks
},
"_source_id": f"{project.get('id')}_{variable_set.get('id')}",
"description": variable_set.get("attributes.description"),
"labels": ["autoattach:*"],
"name": variable_set.get("attributes.name"),
}
)

# If the variable set is attached to the project, we dont need to also attach it to the workspace
# as it will already be attached to the workspace via the project relationship.
elif (
"relationships.workspaces.data" in variable_set
and variable_set.get("relationships.workspaces.data") is not None
and len(variable_set.get("relationships.workspaces.data")) > 0
):
stacks = []
for workspace in variable_set.get("relationships.workspaces.data"):
stacks.append({"_migration_id": self._generate_migration_id(workspace.get("id"))})

elif (
"relationships.projects.data" in variable_set
and variable_set.get("relationships.projects.data") is not None
and len(variable_set.get("relationships.projects.data")) > 0
):
for project in variable_set.get("relationships.projects.data"):
logging.info(f"Append context copy '{project.get('id')}' / '{variable_set.get('id')}'")
data.append(
{
"_migration_id": self._generate_migration_id(variable_set.get("id")),
"_relationships": {
"space": {
"_migration_id": self._generate_migration_id(
variable_set.get("relationships.organization.data.id")
)
"_migration_id": self._generate_migration_id(project.get("id"))
},
"stacks": stacks,
"stacks": [], # The list is empty because it will be auto-attached to all stacks
},
"_source_id": variable_set.get("id"),
"_source_id": f"{project.get('id')}_{variable_set.get('id')}",
"description": variable_set.get("attributes.description"),
"labels": [],
"labels": ["autoattach:*"],
"name": variable_set.get("attributes.name"),
}
)

# If the variable set is attached to the project, we dont need to also attach it to the workspace
# as it will already be attached to the workspace via the project relationship.
elif (
"relationships.workspaces.data" in variable_set
and variable_set.get("relationships.workspaces.data") is not None
and len(variable_set.get("relationships.workspaces.data")) > 0
):
stacks = []
for workspace in variable_set.get("relationships.workspaces.data"):
stacks.append({"_migration_id": self._generate_migration_id(workspace.get("id"))})

data.append(
{
"_migration_id": self._generate_migration_id(variable_set.get("id")),
"_relationships": {
"space": {
"_migration_id": self._generate_migration_id(
variable_set.get("relationships.organization.data.id")
)
},
"stacks": stacks,
},
"_source_id": variable_set.get("id"),
"description": variable_set.get("attributes.description"),
"labels": [],
"name": variable_set.get("attributes.name"),
}
)

logging.info("Stop mapping contexts data")

return data
Expand Down

0 comments on commit 254cf54

Please sign in to comment.