Created
September 5, 2018 18:51
-
-
Save djaboxx/03dc97a486e9d55eb9cd91df12533f78 to your computer and use it in GitHub Desktop.
Simple Python Script to find TFE Oauth Tokens from Terraform Enterprise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import requests | |
import os | |
import json | |
import sys | |
import hcl | |
def hcl_config(api, config): | |
with open(os.path.abspath(config), 'r') as fp: | |
obj = hcl.load(fp) | |
return obj.get('credentials').get(api).get('token') | |
def main(opt, args): | |
# stdin_json = json.loads(sys.stdin.read()) | |
try: | |
org = args.pop() | |
except: | |
print "Not enough arguments. Please provide TFE OrgName" | |
atlas_token = hcl_config(opt.api, opt.config) | |
headers = {"Authorization": "Bearer {0}".format(atlas_token)} | |
resp = requests.get("https://{0}/api/v2/organizations/{1}/oauth-tokens".format(opt.api, org), headers=headers) | |
data = resp.json() | |
try: | |
print "Token: {0}".format(data.get("data")[0].get("id")) | |
except: | |
print "Could not find Token. Have you connected your VCS?" | |
print "Please go to: https://app.terraform.io/app/{0}/settings/version-control".format(org) | |
if "y" in raw_input("Open Browser? "): | |
os.system("open https://app.terraform.io/app/{0}/settings/version-control".format(org)) | |
if __name__ == '__main__': | |
from optparse import OptionParser | |
parser = OptionParser() | |
parser.add_option('--config', default=os.path.expanduser("~/.terraformrc")) | |
parser.add_option('--api', default="app.terraform.io") | |
opt, args = parser.parse_args() | |
main(opt, args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment