def process_response(response): """Main.""" log.debug('recieved response: {}'.format(response)) configFromS3 = config.ConfigFromS3("tfsh-config", "config.ini", "us-east-1") conf = configFromS3.config repo_namespace = get_gitlab_namespace(response) cat_item_sys_id = get_sn_tf_cat_item(repo_namespace, conf) gitlab_template_version = get_gitlab_template_version(response) project_name = get_gitlab_project_name(response) repo_url = get_repo_ssh_url(response) sn_template_version = get_sn_template_version(cat_item_sys_id, conf) if cat_item_sys_id: if compare_versions(sn_template_version, gitlab_template_version): log.info("ServiceNow terraform module update triggered") update_sn_template(repo_url, project_name, repo_namespace, gitlab_template_version, cat_item_sys_id, conf) log.info("ServiceNow terraform module update complete.") else: log.info("No change in module detected.") else: log.info('Catalog item not found, creating new ServiceNow Terraform' + ' module.') module_version = get_gitlab_template_version(response) cat_item_description = "module: {} version: {}".format( repo_namespace, module_version) create_sn_template(repo_url, project_name, repo_namespace, module_version, project_name, cat_item_description, conf) log.info('New ServiceNow Terraform module created.')
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"
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)
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
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)
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"