Repository navigation
Expand file tree
/
Copy pathTFCAPIsPython.py
More file actions
155 lines (142 loc) · 4.48 KB
/
Copy pathTFCAPIsPython.py
File metadata and controls
155 lines (142 loc) · 4.48 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import requests
import json
TOKEN = '<API token>'
#http://man.hubwiz.com/docset/Terraform.docset/Contents/Resources/Documents/docs/enterprise/api/workspaces.html
'''
## CREATE
print("Creating the TFC workspace dynamically...")
url = 'https://app.terraform.io/api/v2/organizations/<your org>/workspaces'
TOKEN = '<API token>'
headers = {'Authorization': 'Bearer '+TOKEN,
'Content-Type': 'application/vnd.api+json'
}
payload={
'data': {
'attributes': {
'name': 'demoworkspace'
},
'type': 'workspaces'
}
}
TFCCreateresponse = requests.request("POST", url, headers=headers, data=json.dumps(payload))
if TFCCreateresponse.status_code == 201:
print("retrieved the response..")
print(TFCCreateresponse.content)
else:
print(TFCCreateresponse.content)
print("Did not get the required response")
'''
## UPDATE
'''
print("Updating the TFC workspace dynamically...")
#TOKEN = ''
Work_SpaceName = 'demoworkspace'
url = 'https://app.terraform.io/api/v2/organizations/<your org>/workspaces/'+Work_SpaceName
headers = {'Authorization': 'Bearer '+TOKEN,
'Content-Type': 'application/vnd.api+json'
}
payload={
'data': {
'attributes': {
'name': 'demoworkspace1'
},
'type': 'workspaces'
}
}
TFCUpdateresponse = requests.request("PATCH", url, headers=headers, data=json.dumps(payload))
if TFCUpdateresponse.status_code == 200:
print("Updated successfully..")
print(TFCUpdateresponse)
else:
print(TFCUpdateresponse)
print("Did not get the required response")
'''
'''
## DELETE
print("Deleting the TFC workspace dynamically...")
#TOKEN = ''
Work_SpaceName = 'demoworkspace1'
url = 'https://app.terraform.io/api/v2/organizations/<your org>/workspaces/'+Work_SpaceName
headers = {'Authorization': 'Bearer '+TOKEN,
'Content-Type': 'application/vnd.api+json'
}
TFCDeleteresponse = requests.request("DELETE", url, headers=headers)
if TFCDeleteresponse.status_code == 204:
print("Deleted successfully..")
print(TFCDeleteresponse)
else:
print(TFCDeleteresponse)
print("Did not get the required response")
'''
'''
## READ
print("Read the TFC workspace dynamically...")
#TOKEN = ''
Work_SpaceName = 'AWSBackup'
url = 'https://app.terraform.io/api/v2/organizations/<your org>/workspaces/'+Work_SpaceName
headers = {'Authorization': 'Bearer '+TOKEN,
'Content-Type': 'application/vnd.api+json'
}
TFCReadresponse = requests.request("GET", url, headers=headers)
if TFCReadresponse.status_code == 200:
print("Got read response successfully..")
print(TFCReadresponse.content)
else:
print(TFCReadresponse.content)
print("Did not get the required response")
'''
'''
## LIST ALL
print("List all the TFC workspace dynamically...")
#TOKEN = ''
url = 'https://app.terraform.io/api/v2/organizations/<your org>/workspaces'
headers = {'Authorization': 'Bearer '+TOKEN,
'Content-Type': 'application/vnd.api+json'
}
TFCListresponse = requests.request("GET", url, headers=headers)
if TFCListresponse.status_code == 200:
print("Got read response successfully..")
print(TFCListresponse.content)
else:
print(TFCListresponse.content)
print("Did not get the required response")
'''
'''
## Lock Workspace ( Needs user access token)
print("Lock the TFC workspace dynamically...")
#TOKEN = ''
Work_Id = '<workspace id>'
url = 'https://app.terraform.io/api/v2/workspaces/'+Work_Id+'/actions/lock'
headers = {'Authorization': 'Bearer '+TOKEN,
'Content-Type': 'application/vnd.api+json'
}
payload = {
'reason': 'Locking workspace-1'
}
TFCLockResponse = requests.request("POST", url, headers=headers,data=json.dumps(payload))
print(TFCLockResponse)
if TFCLockResponse.status_code == 200:
print("Locked WS response successfully..")
print(TFCLockResponse.content)
else:
print(TFCLockResponse.content)
print("Did not get the required response")
'''
'''
##UNLOCK Workspaces
print("Lock the TFC workspace dynamically...")
#TOKEN = ''
Work_Id = '<workspace id>'
url = 'https://app.terraform.io/api/v2/workspaces/'+Work_Id+'/actions/unlock'
headers = {'Authorization': 'Bearer '+TOKEN,
'Content-Type': 'application/vnd.api+json'
}
TFCUNLockResponse = requests.request("POST", url, headers=headers)
print(TFCUNLockResponse)
if TFCUNLockResponse.status_code == 200:
print("UnLocked WS, response successfully..")
print(TFCUNLockResponse.content)
else:
print(TFCUNLockResponse.content)
print("Did not get the required response")
'''