def create_folder(folderDetails):
    # builds and destroys a folder
    # The configuration YAML file read by read_config_map() determines where this new infrastructure should sit
    # within a GCP project, as well as setting other properties like billing.
    # Accepts JSON content-type input.
    # returns return code and response from terraform
    tf_data = dict()
    ec_config = config.read_config_map()
    logger.debug("folder is %s", folderDetails)
    folder = folderDetails.get('folder')

    folder_name = folder['folderName']
    tf_data['folder_name'] = folder_name
    tf_data['parent_folder_id'] = folder['parentFolderId']

    region = ec_config['region']
    tf_data['region'] = region
    # TODO pass region_zone in - from UI?
    tf_data['region_zone'] = region + "-b"
    tf_data['billing_account'] = ec_config['billing_account']
    tb_discriminator = ec_config['tb_discriminator']
    tf_data['tb_discriminator'] = tb_discriminator

    # TODO this file currently empty - remove need for it?
    env_data = '/app/terraform/input.tfvars'

    backend_prefix = get_folder_backend_prefix(folder_name, tb_discriminator)
    terraform_state_bucket = ec_config['terraform_state_bucket']
    terraform_source_path = '/app/terraform/folder_creation'

    tf = Terraform(working_dir=terraform_source_path, variables=tf_data)

    terraform_init(backend_prefix, terraform_state_bucket, tf)

    return terraform_apply(env_data, tf)
def delete_folder(folder):
    tf_data = dict()
    folder_name = folder.get("id")

    # variables not used on delete
    tf_data['parent_folder_id'] = NOT_USED_ON_DESTROY
    tf_data['random_element'] = NOT_USED_ON_DESTROY
    tf_data['region'] = NOT_USED_ON_DESTROY
    tf_data['region_zone'] = NOT_USED_ON_DESTROY
    tf_data['tb_discriminator'] = NOT_USED_ON_DESTROY
    tf_data['folder_name'] = NOT_USED_ON_DESTROY

    ec_config = config.read_config_map()
    tf_data['billing_account'] = ec_config['billing_account']
    tb_discriminator = ec_config['tb_discriminator']
    tf_data['tb_discriminator'] = tb_discriminator

    env_data = '/app/terraform/input.tfvars'

    backend_prefix = get_folder_backend_prefix(folder_name, tb_discriminator)
    terraform_state_bucket = ec_config['terraform_state_bucket']
    terraform_source_path = '/app/terraform/folder_creation'

    tf = Terraform(working_dir=terraform_source_path, variables=tf_data)
    terraform_init(backend_prefix, terraform_state_bucket, tf)

    return terraform_destroy(env_data, tf)
def create_solution(solutiondata):
    tf_data = dict()
    ec_config = config.read_config_map()

    solution_id = solutiondata.get("id")
    logger.debug("solution_id is %s", solution_id)
    tf_data['cost_centre'] = labellize(solutiondata.get("costCentre"))
    tf_data['business_unit'] = labellize(solutiondata.get("businessUnit"))
    tf_data['deployment_folder_id'] = solutiondata.get("deploymentFolderId")
    tf_data['environments'] = [sanitize(x) for x in (solutiondata.get("environments", list()))]
    solution_name = solutiondata.get("name")
    tf_data['solution_name'] = solution_name

    region = ec_config['region']
    tf_data['region'] = region
    tf_data['billing_account'] = ec_config['billing_account']
    tf_data['shared_vpc_host_project'] = ec_config['shared_vpc_host_project']
    tf_data['shared_network_name'] = ec_config['shared_network_name']
    tf_data['shared_networking_id'] = ec_config['shared_networking_id']
    tb_discriminator = ec_config['tb_discriminator']
    tf_data['tb_discriminator'] = tb_discriminator
    # added to ensure all resources can be deleted and recreated
    tf_data['random_element'] = random_element(num_chars=6)

    backend_prefix = get_solution_backend_prefix(solution_id, tb_discriminator)

    # TODO remove dependency on this
    env_data = '/app/terraform/input.tfvars'
    # TODO pass region_zone in - comes from UI?
    tf_data['region_zone'] = region + "-b"

    terraform_source_path = '/app/terraform/solution_creation'

    tf = Terraform(working_dir=terraform_source_path, variables=tf_data)
    terraform_state_bucket = ec_config['terraform_state_bucket']

    terraform_init(backend_prefix, terraform_state_bucket, tf)

    response = terraform_apply(env_data, tf)
    logger.debug("response {}".format(response))

    repo_name = "{}_workspace".format(solution_name)
    workspace_project_id = response["tf_outputs"]["workspace_project"]["value"]["project_id"]
    eagle_project_id = ec_config['ec_project_name']
    create_repo(repo_name, workspace_project_id, eagle_project_id)

    return response
Exemple #4
0
def delete_solution(solutiondata):
    tf_data = dict()
    solution_id = solutiondata.get("id")
    logger.debug("solution_id is %s", solution_id)

    tf_data['cost_centre'] = NOT_USED_ON_DESTROY
    tf_data['business_unit'] = NOT_USED_ON_DESTROY
    tf_data['deployment_folder_id'] = NOT_USED_ON_DESTROY
    tf_data['environments'] = list()
    tf_data['solution_name'] = NOT_USED_ON_DESTROY
    tf_data['random_element'] = NOT_USED_ON_DESTROY
    tf_data['region'] = NOT_USED_ON_DESTROY
    tf_data['region_zone'] = NOT_USED_ON_DESTROY
    tf_data['tb_discriminator'] = NOT_USED_ON_DESTROY
    tf_data['region_zone'] = NOT_USED_ON_DESTROY

    ec_config = config.read_config_map()

    tf_data['billing_account'] = ec_config['billing_account']
    tb_discriminator = ec_config['tb_discriminator']
    tf_data['tb_discriminator'] = tb_discriminator

    env_data = None

    terraform_state_bucket = ec_config['terraform_state_bucket']
    # location of this solution's state with terraform bucket
    backend_prefix = get_solution_backend_prefix(solution_id, tb_discriminator)
    # source of the terraform used for this deployment
    terraform_source_path = '/app/terraform/solution_creation'

    tf = Terraform(working_dir=terraform_source_path, variables=tf_data)

    terraform_init(backend_prefix, terraform_state_bucket, tf)

    delete_workspace_repo(ec_config, tf)

    return terraform_destroy(env_data, tf)