forked from AdamCavaliere/Producer-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-pushCode.py
36 lines (27 loc) · 1.21 KB
/
api-pushCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests
import tarfile
import os
import json
tfeURL = "https://ptfe.this-demo.rocks"
org = "SE_Org"
AtlasToken = os.environ["PTFE_TOKEN"]
workspaceName = "VMWorld-Staging"
headers = {'Authorization': "Bearer " + AtlasToken, 'Content-Type' : 'application/vnd.api+json'}
workspaceID_URL = tfeURL + "/api/v2/organizations/" + org + "/workspaces/" + workspaceName
configVersion = json.dumps({"data":{"type":"configuration-version"}})
# Get Current Working Directory
cwd = os.getcwd()
def make_tarfile(source_dir):
with tarfile.open("./terraConfig.tar.gz", "w:gz") as tar:
tar.add(source_dir, arcname=os.path.sep)
return(source_dir + "/terraConfig.tar.gz")
tfeWorkspaceID = requests.get(workspaceID_URL, headers=headers)
workspaceID = json.loads(tfeWorkspaceID.text).get('data').get('id')
tfeUploadURL = tfeURL + "/api/v2/workspaces/" + workspaceID + "/configuration-versions"
uploadURL_JSON = requests.post(tfeUploadURL,data=configVersion, headers=headers)
uploadURL = json.loads(uploadURL_JSON.text).get('data').get('attributes').get('upload-url')
filename = make_tarfile(cwd)
with open(filename, 'rb') as data:
results = requests.put(uploadURL, data=data)
os.remove(filename)
print(results.status_code)