def create_cred_env_vars(region, workspace, org, temp_creds):
    """Create TFE Credential Environment Variables."""
    # pull the configuration
    configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini",
                                       region)
    conf = configFromS3.config
    # get properly formatted json objs for variable creation
    access_key_id = create_access_key_id_var(
      glom(temp_creds, 'aws_access_key_id', default=False),
      workspace, org)
    secret_access_key = create_secret_access_key_id_var(
      glom(temp_creds, 'aws_secret_access_key', default=False),
      workspace, org)
    # check to see if the temp creds exist
    if access_key_id and secret_access_key:
        api_endpoint = "/vars"
        response = {}
        record = (
          tfe_handler.TFERequest(api_endpoint, access_key_id, conf))
        response['access_key_id'] = record.make_request()
        record = (
          tfe_handler.TFERequest(api_endpoint, secret_access_key, conf))
        response['secret_access_key'] = record.make_request()
        log.debug('Create tfe_var response: {}'.format(response))
        return response
    else:
        log.error("Access Key Id or Secret Access Key not found.")
        return "ERROR: Access Key Id or Secret Access Key not found"
Esempio n. 2
0
def get_workspace(region, org_name, workspace_name):
    """Get the target workspace information."""
    configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini", region)
    conf = configFromS3.config
    api_endpoint = "/organizations/{}/workspaces/{}".format(
        org_name, workspace_name)
    response = tfe_handler.TFERequest(api_endpoint, None, conf)
    log.debug('Get TFE workspace reponse: {}'.format(response))
    return response_handler(response)
Esempio n. 3
0
def delete_workspace(region, org_name, workspace_name):
    """Delete TFE workspace."""
    configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini", region)
    conf = configFromS3.config
    api_endpoint = "/organizations/{}/workspaces/{}".format(
        org_name, workspace_name)
    record = tfe_handler.TFERequest(api_endpoint, None, conf)
    response = record.delete()
    log.debug('Delete TFE workspace response: {}'.format(response))
    return response
Esempio n. 4
0
def create_workspace(region, org_name, workspace_name, repo_id, repo_version):
    """Create TFE workspace."""
    configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini", region)
    conf = configFromS3.config
    w = TFEWorkspace(name=workspace_name,
                     vcs_repo_id=repo_id,
                     vcs_repo_branch=repo_version,
                     oauth_token_id=(tfe_handler.get_vcs_oauth(conf)))
    workspace = w.initialize_workspace()
    api_endpoint = "/organizations/{}/workspaces".format(org_name)
    record = tfe_handler.TFERequest(api_endpoint, workspace, conf)
    return response_handler(record)
Esempio n. 5
0
def create_config_version(region, workspace_id):
    """Create workspace configuration version."""
    # https://www.terraform.io/docs/enterprise/api/configuration-versions.html#create-a-configuration-version
    if workspace_id:
        configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini", region)
        conf = configFromS3.config
        api_endpoint = (
            '/workspaces/{}/configuration-versions'.format(workspace_id))
        data = config_version_data()
        record = tfe_handler.TFERequest(api_endpoint, data, conf)
        log.info('Sending create configuraiton request.')
        return response_handler(record)
    else:
        log.error('Workspace id not provided.')
def create_tfe_tf_vars(region, json_obj, org, workspace):
    """Populate TFE vars with raw var data."""
    configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini",
                                       region)
    conf = configFromS3.config
    raw_vars = parse_vars(json_obj)
    response = {}
    if raw_vars:
        for var in raw_vars:
            new_var = tfe_var_handler.TFEVars(var, raw_vars[var], org=org,
                                              workspace=workspace).get_var()
            print("var payload: {}".format(new_var))
            api_endpoint = "/vars"
            record = tfe_handler.TFERequest(api_endpoint, new_var, conf)
            var_response = record.make_request()
            log.debug('Create tfe_var response: {}'.format(response))
            response[var] = var_response
        return response
    else:
        log.error("Input json object was empty.")
        return "ERROR: vars json object cannot be empty"
def create_tfe_tf_vars(region, json_obj, org, workspace_id):
    """Populate TFE vars with raw var data."""
    configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini", region)
    conf = configFromS3.config
    raw_vars = parse_vars(json_obj)
    response = {}
    if raw_vars:
        for var in raw_vars:
            var_value = raw_vars[var]
            # fix map values
            if "[{" in var_value:
                var_value = re.sub('\[|\]', '', var_value)
                new_var = tfe_var_handler.TFEVars(var,
                                                  var_value,
                                                  org=org,
                                                  workspace_id=workspace_id,
                                                  is_hcl="true").get_var()
            # check if the variable is a map or list
            elif "{" in var_value or "[" in var_value:
                new_var = tfe_var_handler.TFEVars(var,
                                                  var_value,
                                                  org=org,
                                                  workspace_id=workspace_id,
                                                  is_hcl="true").get_var()
            else:
                new_var = tfe_var_handler.TFEVars(
                    var, var_value, org=org,
                    workspace_id=workspace_id).get_var()
            print("var payload: {}".format(new_var))
            api_endpoint = "/vars"
            record = tfe_handler.TFERequest(api_endpoint, new_var, conf)
            var_response = record.make_request()
            log.debug('Create tfe_var response: {}'.format(response))
            response[var] = var_response
        return response
    else:
        log.error("Input json object was empty.")
        return "ERROR: vars json object cannot be empty"