forked from Kiran-r/openstack-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_delete.py
83 lines (79 loc) · 2.81 KB
/
port_delete.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
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
import requests
import json
import exceptions
import time
SUCCESS = 1
FAILURE = 0
CREATED = 201
ACCEPTED = 202
UN_AUTH = 401
DELETED = 204
CONFLICT = 409
userName = 'admin'
password = 'f6b53b58af604644'
tenantName = 'admin'
authPort ='5000'
protocol = 'http'
hostIp = '10.160.1.3'
postCred = '{"auth": {"tenantName": "%s", "passwordCredentials": {"username": "%s", "password": "%s"}}}' % (tenantName, userName, password)
url = "%s://%s:%s/v2.0/tokens" % (protocol, hostIp, authPort)
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-keystoneclient'}
session = requests.Session()
try:
resp = session.post(url, data=postCred, headers=headers, timeout=10.0)
if resp.status_code == requests.codes.ok:
token = json.loads(resp.text)["access"]["token"]["id"]
#print token
if resp.status_code == UN_AUTH:
#print "Unauthorized Access"
exit()
except exceptions.Exception as e:
#print e
#print "Unable To Reach Host %s" % hostIp
exit()
#print "Token value : ", token
#headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent':'python-keystoneclient', 'X-Auth-Token':'%s' % token}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent':'python-neutronclient', 'X-Auth-Token':'%s' % token}
#postCred = {"tenant": {"enabled": true, "name": "test", "description": null}}
#postCred = '{"tenant": {"enabled": true, "name": "te6s121t1", "description": null}}'
authPort = '9696'
url = "%s://%s:%s/v2.0/ports.json" % (protocol, hostIp, authPort)
#print 'url '+url
author = (userName,password)
i=0
port_list = []
try:
resp = session.get(url, headers=headers, timeout=10.0)
#print "Response ", resp.text
if resp.status_code == requests.codes.ok:
ports = json.loads(resp.text)
while i!=len(ports["ports"]):
port_list.append(str(ports["ports"][i]["id"]))
i += 1
print "LEN",len(port_list)
elif resp.status_code == UN_AUTH:
print "Unauthorized Access"
exit()
except exceptions.Exception as e:
print e
print "Unable To Reach Host %s" % hostIp
exit()
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent':'python-neutronclient', 'X-Auth-Token':'%s' % token}
url = 'http://10.160.1.3:9696/v2.0/ports/'
try:
for port in port_list:
durl = url+port+".json"
print url
resp = session.delete(durl, headers=headers, timeout=10.0)
#print "Response Neutron ", resp.text
if resp.status_code == requests.codes.ok:
#LOG.info('TENANT RESPONSE: %s',json.loads)
#print json.loads(resp.text)
pass
if resp.status_code == UN_AUTH:
print "Unauthorized Access"
exit()
except exceptions.Exception as e:
print e
print "Unable To Reach Host %s" % hostIp
exit()