Ejemplo n.º 1
0
from terrasnek.api import TFC
import os

###
# Documentation of the Py client libry
# https://terrasnek.readthedocs.io/en/latest/
###

TFC_TOKEN = os.getenv("TFC_TOKEN", None)
TFC_URL = os.getenv("TFC_URL", None)  #  https://app.terraform.io
TFC_ORG = os.getenv("TFC_ORG")  # org: paulm

if __name__ == "__main__":
    api = TFC(TFC_TOKEN, url=TFC_URL)
    api.set_org("TFC_ORG")
    ws = api.workspaces.list
    ## Get the WS ID / Help here to get the id
    ws_id = api.workspaces(workspace_name="terraform-paulm-org")  #.id
    ## Get the Run ID
    run_show_list = api.runs.show(ws_id)
    ## Filter the last run
    run_id = api.runs.list_all(ws_id)[0]
    ## Apply the plan
    applied_run = api.runs.apply(run_id)
Ejemplo n.º 2
0
from flask import Flask, send_from_directory, jsonify
from flask_cors import CORS
from terrasnek.api import TFC
from util import api_request_helpers

app = Flask(__name__, static_folder='react_app/build')
CORS(app)

TFC_ORG_NAME = os.getenv("TFC_ORG_NAME", None)
TFC_OAUTH_TOKEN_ID = os.getenv("TFC_OAUTH_TOKEN_ID", None)
TFC_TOKEN = os.getenv("TFC_TOKEN", None)
TFC_SAAS_URL = "https://app.terraform.io"
TFC_URL = os.getenv("TFC_URL", TFC_SAAS_URL)

api = TFC(TFC_TOKEN, url=TFC_URL)
api.set_org(TFC_ORG_NAME)


@app.route('/config_bundles/')
def config_bundles():
    return jsonify(api_request_helpers.get_config_bundles())


@app.route('/workspaces/')
def list_workspaces():
    all_workspaces = api.workspaces.list()["data"]
    return jsonify(all_workspaces)


@app.route('/workspaces/plan/<workspace_id>')
def plan_workspace(workspace_id):
Ejemplo n.º 3
0
    }
    run = api.runs.create(payload)
    if run == None:
        print(
            'Error: Unable to queue destroy plan. The provided token probably does not have "apply" permission.'
        )
        exit(1)
    run_id = run["data"]["id"]
    return run_id


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print('Usage: python3 destroy-plan.py [workspace-name]')
        print('')
        print(
            'Please also ensure that the following environment variables are set to the appropriate values for your TFE install:'
        )
        print(' * TFE_TOKEN')
        print(' * TFE_URL')
        print(' * TFE_ORG')
        exit(1)
    TFE_TOKEN = os.getenv("TFE_TOKEN", None)
    TFE_URL = os.getenv("TFE_URL", None)
    TFE_ORG = os.getenv("TFE_ORG", None)

    api = TFC(TFE_TOKEN, url=TFE_URL)
    api.set_org(TFE_ORG)

    destroy_run_id = queue_destroy_run(api, sys.argv[1])
    print('Successfully queued destroy plan')
Ejemplo n.º 4
0
        default=DEFAULT_SENSITIVE_DATA_FILE, \
            help="Path the the sensitive values file. Defaults to `sensitive_data.txt`.")
    parser.add_argument('--migrate-sensitive-data', dest="migrate_sensitive_data", action="store_true", \
        help="Migrate sensitive data to the target organization.")
    parser.add_argument('--delete-all', dest="delete_all", action="store_true", \
        help="Delete all resources from the target API.")
    parser.add_argument('--no-confirmation', dest="no_confirmation", action="store_true", \
        help="If set, don't ask for confirmation before deleting all target resources.")
    parser.add_argument('--debug', dest="debug", action="store_true", \
        help="If set, run the logger in debug mode.")
    args = parser.parse_args()

    api_source = TFC(TFE_TOKEN_SOURCE,
                     url=TFE_URL_SOURCE,
                     verify=TFE_VERIFY_SOURCE)
    api_source.set_org(TFE_ORG_SOURCE)

    api_target = TFC(TFE_TOKEN_TARGET,
                     url=TFE_URL_TARGET,
                     verify=TFE_VERIFY_TARGET)
    api_target.set_org(TFE_ORG_TARGET)

    if not os.path.exists(args.vcs_file_path):
        open(DEFAULT_VCS_FILE, "w").close()
    else:
        with open(args.vcs_file_path, "r") as f:
            TFE_VCS_CONNECTION_MAP = json.loads(f.read())

    if not os.path.exists(args.sensitive_data_file_path):
        open(DEFAULT_SENSITIVE_DATA_FILE, "w").close()
    else:
Ejemplo n.º 5
0
TFC_URL = os.environ["TFC_URL"]
TFC_TOKEN = os.environ["TFC_TOKEN"]
TFC_ORG = os.environ["TFC_ORG"]
NUM_WORKSPACES = int(os.environ["NUM_WORKSPACES"])


if __name__ == "__main__":
    mapping = []

    # Write the updated version of the migration targets so we can
    # reference what occurred later
    with open("mapping.json", "r") as f:
        mapping = json.loads(f.read())

    api = TFC(TFC_TOKEN, url=TFC_URL)
    api.set_org(TFC_ORG)

    oauth_clients = api.oauth_clients.list()["data"]
    oauth_token_id = None

    for oac in oauth_clients:
        org_name = oac["relationships"]["organization"]["data"]["id"]
        if org_name == TFC_ORG:
            oauth_token_id = oac["relationships"]["oauth-tokens"]["data"][0]["id"]

    """
    for mt in mapping:
        for i in range(NUM_WORKSPACES):
            workspace_name = f"%s-%d" % (mt["workspace-name"], i)
            shown_workspace = api.workspaces.show(workspace_name=workspace_name)["data"]
            if "id" in shown_workspace:
Ejemplo n.º 6
0
create_module_version_payload = {
    "data": {
        "type": "registry-module-versions",
        "attributes": {
            "version": "0.0.1"
        }
    }
}

# TODO: build this into the purge logic.
TFC_TOKEN = os.getenv("TFC_TOKEN", None)
TFC_URL = os.getenv("TFC_URL", None)

if __name__ == "__main__":
    api = TFC(TFC_TOKEN, url=TFC_URL)
    api.set_org("terrasnek-unittest")

    modules = api.registry_modules.list()["modules"]

    for module in modules:
        module_name = module["name"]
        module_provider = module["provider"]

        create_payload = create_module_payload
        create_payload["data"]["attributes"]["name"] = module_name

        created_module = api.registry_modules.create(create_payload)["data"]
        created_version = \
            api.registry_modules.create_version(\
                module_name, module_provider, create_module_version_payload)["data"]
Ejemplo n.º 7
0
import os
from terrasnek.api import TFC
from functions import *

# SOURCE ORG
TFE_TOKEN_ORIGINAL = os.getenv("TFE_TOKEN_ORIGINAL", None)
TFE_URL_ORIGINAL = os.getenv("TFE_URL_ORIGINAL", None)
TFE_ORG_ORIGINAL = os.getenv("TFE_ORG_ORIGINAL", None)

api_original = TFC(TFE_TOKEN_ORIGINAL, url=TFE_URL_ORIGINAL)
api_original.set_org(TFE_ORG_ORIGINAL)

# NEW ORG
TFE_TOKEN_NEW = os.getenv("TFE_TOKEN_NEW", None)
TFE_URL_NEW = os.getenv("TFE_URL_NEW", None)
TFE_ORG_NEW = os.getenv("TFE_ORG_NEW", None)
TFE_OAUTH_NEW = os.getenv("TFE_OAUTH_NEW", None)

api_new = TFC(TFE_TOKEN_NEW, url=TFE_URL_NEW)
api_new.set_org(TFE_ORG_NEW)

if __name__ == "__main__":
    teams_map = migrate_teams(api_original, api_new)
    print('teams successfully migrated')

    # migrate_organization_memberships(api_original, api_new, teams_map)
    # print('organization memberships successfully migrated')

    ssh_keys_map, ssh_key_name_map = migrate_ssh_keys(api_original, api_new)
    print('ssh keys successfully migrated')