Skip to content

Commit

Permalink
skip backend secrets creation
Browse files Browse the repository at this point in the history
  • Loading branch information
emocharnik committed Jul 14, 2022
1 parent 1065d36 commit 7c38ac3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
1 change: 1 addition & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ resource "null_resource" "migrate" {
--scalr-token=${var.scalr_token} \
--scalr-environment=${var.scalr_environment} \
--skip-workspace-creation=${var.skip_workspace_creation} \
--skip-backend-secrets=${var.skip_backend_secrets} \
-a ${var.scalr_account_id} \
-v ${var.scalr_vcs_provider_id} \
-w "${local.workspaces}" \
Expand Down
57 changes: 35 additions & 22 deletions migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ def validate_vcs_id_set(ctx, param, value):
"--skip-workspace-creation",
default=False,
multiple=False,
help="Whether to create new workspaces in Scalr. Set to True if workspace is already created in Scalr.",
help="Whether to create new workspaces in Scalr. Set to True if the workspace is already created in Scalr.",
)
@click.option(
"--skip-backend-secrets",
default=False,
multiple=False,
help="Whether to create shell variables (`SCALR_` and `TFC_`) in Scalr.",
)
@click.option(
"-l",
Expand All @@ -106,6 +112,7 @@ def migrate(
vcs_id,
workspaces,
skip_workspace_creation,
skip_backend_secrets,
lock
):
def encode_filters(filters):
Expand Down Expand Up @@ -391,30 +398,36 @@ def migrate_variables():
if not next_page:
break

account_relationships = {
"account": {
"data": {
"type": "accounts",
"id": account_id
def init_backend_secrets():
if skip_backend_secrets:
return

account_relationships = {
"account": {
"data": {
"type": "accounts",
"id": account_id
}
}
}
}

vars_to_create = {
"SCALR_HOSTNAME": scalr_hostname,
"SCALR_TOKEN": scalr_token,
"TFE_HOSTNAME": tf_hostname,
"TFE_TOKEN": tf_token,
}

print("Initializing backend secrets...")
for key in vars_to_create:
if fetch_scalr("vars", {"filter[account]": account_id, "filter[key]": key})["data"]:
continue
print(f"Missing shell variable `{key}`. Creating...")
create_variable(key, vars_to_create[key], "shell", False, "Created by migrator", account_relationships)
print("Initializing backend secrets... Done")

vars_to_create = {
"SCALR_HOSTNAME": scalr_hostname,
"SCALR_TOKEN": scalr_token,
"TFE_HOSTNAME": tf_hostname,
"TFE_TOKEN": tf_token,
}

print("Initializing backend secrets...")
for key in vars_to_create:
vars_filters = {"filter[account]": account_id, "filter[key]": key, "filter[environment]": None}
if fetch_scalr("vars", vars_filters)["data"]:
continue
print(f"Missing shell variable `{key}`. Creating...")
create_variable(key, vars_to_create[key], "shell", False, "Created by migrator", account_relationships)
print("Initializing backend secrets... Done")

init_backend_secrets()
organization = fetch_tfc(f"organizations/{tf_organization}")["data"]
if not scalr_environment:
scalr_environment = tf_organization
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ variable "skip_workspace_creation" {
default = false
description = "Whether to create new workspaces in Scalr. Set to True if workspace is already created in Scalr."
}

variable "skip_backend_secrets" {
type = bool
default = false
description = "Whether to create shell variables (`SCALR_` and `TFC_`) in Scalr."
}

0 comments on commit 7c38ac3

Please sign in to comment.