Skip to content

Commit

Permalink
fix: dont hardcode tfc address here
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollorion committed Jan 24, 2025
1 parent dcbcf0c commit 3c67a4b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions spacemk/commands/import_state_files_to_spacelift.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from spacemk.spacelift import Spacelift


def _create_context(spacelift: Spacelift, space_id: str, token: str):
def _create_context(spacelift: Spacelift, space_id: str, token: str, tfc_address: str):
mutation = """
mutation CreateContextV2($input: ContextInput!) {
contextCreateV2(input: $input) {
Expand All @@ -34,7 +34,7 @@ def _create_context(spacelift: Spacelift, space_id: str, token: str):
--location \
--show-error \
--silent \
"https://app.terraform.io/api/v2/workspaces/${TF_WORKSPACE_ID}/current-state-version" \
"${TFC_ADDRESS}/api/v2/workspaces/${TF_WORKSPACE_ID}/current-state-version" \
| jq -r '.data.attributes."hosted-state-download-url"' )
curl --fail \
Expand Down Expand Up @@ -74,6 +74,13 @@ def _create_context(spacelift: Spacelift, space_id: str, token: str):
"value": token,
"writeOnly": True,
},
{
"description": "",
"id": "TF_ADDRESS",
"type": "ENVIRONMENT_VARIABLE",
"value": tfc_address,
"writeOnly": False,
},
{
"description": "",
"id": "import-state-from-tf.sh",
Expand Down Expand Up @@ -167,9 +174,14 @@ def import_state_files_to_spacelift(config):
spacelift = Spacelift(config.get("spacelift"))
space_ids = _get_space_ids(spacelift=spacelift)

api_endpoint = config.exporter.settings.api_endpoint
if api_endpoint is None:
api_endpoint = "https://app.terraform.io"

for space_id in space_ids:
# Create a Context with the TFC/TFE token that auto-attaches to all stacks
_create_context(spacelift=spacelift, space_id=space_id, token=config.exporter.settings.api_token)
_create_context(spacelift=spacelift, space_id=space_id, token=config.exporter.settings.api_token,
tfc_address=api_endpoint)

for stack in data.get("stacks"):
stack_id = stack.slug
Expand Down

0 comments on commit 3c67a4b

Please sign in to comment.