diff --git a/devstack/tools/nsxv3_cleanup.py b/devstack/tools/nsxv3_cleanup.py index 961c3a58a8..42d55cda5e 100755 --- a/devstack/tools/nsxv3_cleanup.py +++ b/devstack/tools/nsxv3_cleanup.py @@ -13,16 +13,16 @@ # License for the specific language governing permissions and limitations # under the License. -import sys -import json import base64 +import jsonutils +import optparse import requests requests.packages.urllib3.disable_warnings() class NSXClient(object): - """ Base NSX REST client """ + """Base NSX REST client""" API_VERSION = "v1" def __init__(self, host, username, password, *args, **kwargs): @@ -103,7 +103,7 @@ class NSXClient(object): """ self.__set_url(endpoint=endpoint) response = requests.put(self.url, headers=self.headers, - verify=self.verify, data=json.dumps(body)) + verify=self.verify, data=jsonutils.dumps(body)) return response def delete(self, endpoint=None, params=None): @@ -121,7 +121,8 @@ class NSXClient(object): """ self.__set_url(endpoint=endpoint) response = requests.post(self.url, headers=self.headers, - verify=self.verify, data=json.dumps(body)) + verify=self.verify, + data=jsonutils.dumps(body)) return response def get_transport_zones(self): @@ -412,9 +413,8 @@ class NSXClient(object): if __name__ == "__main__": - from optparse import OptionParser - parser = OptionParser() + parser = optparse.OptionParser() parser.add_option("--mgr-ip", dest="mgr_ip", help="NSX Manager IP address") parser.add_option("-u", "--username", default="admin", dest="username", help="NSX Manager username") diff --git a/devstack/tools/nsxv_cleanup.py b/devstack/tools/nsxv_cleanup.py index 22a40eb57b..6942cef50c 100755 --- a/devstack/tools/nsxv_cleanup.py +++ b/devstack/tools/nsxv_cleanup.py @@ -1,4 +1,18 @@ #!/usr/bin/env python +# Copyright 2015 VMware Inc +# All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. """ Purpose: Sometimes NSXv backend are out of sync with OpenStack and all @@ -35,16 +49,17 @@ If you have any comment or find a bug, please contact Tong Liu """ -import sys -import json import base64 +import jsonutils +import optparse import requests +import sys requests.packages.urllib3.disable_warnings() class VSMClient(object): - """ Base VSM REST client """ + """Base VSM REST client """ API_VERSION = "2.0" def __init__(self, host, username, password, *args, **kwargs): @@ -136,16 +151,17 @@ class VSMClient(object): self.__set_url(endpoint=endpoint) self.__set_headers() response = requests.post(self.url, headers=self.headers, - verify=self.verify, data=json.dumps(body)) + verify=self.verify, + data=jsonutils.dumps(body)) return response def get_vdn_scope_id(self): """ Retrieve existing network scope id """ - self.__set_api_version('2.0'); + self.__set_api_version('2.0') self.__set_endpoint("/vdn/scopes") - response = self.get(); + response = self.get() if len(response.json()['allScopes']) == 0: return else: @@ -154,7 +170,7 @@ class VSMClient(object): def query_all_logical_switches(self): lswitches = [] self.__set_api_version('2.0') - vdn_scope_id = self.get_vdn_scope_id(); + vdn_scope_id = self.get_vdn_scope_id() if not vdn_scope_id: return lswitches endpoint = "/vdn/scopes/%s/virtualwires" % (vdn_scope_id) @@ -164,10 +180,10 @@ class VSMClient(object): paging_info = response.json()['dataPage']['pagingInfo'] page_size = int(paging_info['pageSize']) total_count = int(paging_info['totalCount']) - print "There are total %s logical switches and page size is %s" % ( - total_count, page_size) + print ("There are total %s logical switches and page size is %s" % ( + total_count, page_size)) pages = ceil(total_count, page_size) - print "Total pages: %s" % pages + print ("Total pages: %s" % pages) for i in range(0, pages): start_index = page_size * i params = {'startindex': start_index} @@ -178,16 +194,16 @@ class VSMClient(object): return lswitches def cleanup_logical_switch(self): - print "Cleaning up logical switches on NSX manager" + print ("Cleaning up logical switches on NSX manager") lswitches = self.query_all_logical_switches() - print "There are total %s logical switches" % len(lswitches) + print ("There are total %s logical switches" % len(lswitches)) for ls in lswitches: - print "\nDeleting logical switch %s (%s) ..." % (ls['name'], - ls['objectId']) + print ("\nDeleting logical switch %s (%s) ..." % (ls['name'], + ls['objectId'])) endpoint = '/vdn/virtualwires/%s' % ls['objectId'] response = self.delete(endpoint=endpoint) if response.status_code != 200: - print "ERROR: response status code %s" % response.status_code + print ("ERROR: response status code %s" % response.status_code) def query_all_firewall_sections(self): firewall_sections = [] @@ -201,23 +217,23 @@ class VSMClient(object): firewall_sections = [s for s in l3_sections if s['name'] != "Default Section Layer3"] else: - print "ERROR: wrong response status code! Exiting..." + print ("ERROR: wrong response status code! Exiting...") sys.exit() return firewall_sections def cleanup_firewall_section(self): - print "\n\nCleaning up firewall sections on NSX manager" + print ("\n\nCleaning up firewall sections on NSX manager") l3_sections = self.query_all_firewall_sections() - print "There are total %s firewall sections" % len(l3_sections) + print ("There are total %s firewall sections" % len(l3_sections)) for l3sec in l3_sections: - print "\nDeleting firewall section %s (%s) ..." % (l3sec['name'], - l3sec['id']) + print ("\nDeleting firewall section %s (%s) ..." % (l3sec['name'], + l3sec['id'])) endpoint = '/firewall/globalroot-0/config/layer3sections/%s' % \ l3sec['id'] response = self.delete(endpoint=endpoint) if response.status_code != 204: - print "ERROR: response status code %s" % response.status_code + print ("ERROR: response status code %s" % response.status_code) def query_all_security_groups(self): security_groups = [] @@ -228,7 +244,7 @@ class VSMClient(object): if response.status_code is 200: sg_all = response.json() else: - print "ERROR: wrong response status code! Exiting..." + print ("ERROR: wrong response status code! Exiting...") sys.exit() # Remove Activity Monitoring Data Collection, which is not # related to any security group created by OpenStack @@ -237,17 +253,17 @@ class VSMClient(object): return security_groups def cleanup_security_group(self): - print "\n\nCleaning up security groups on NSX manager" + print ("\n\nCleaning up security groups on NSX manager") security_groups = self.query_all_security_groups() - print "There are total %s security groups" % len(security_groups) + print ("There are total %s security groups" % len(security_groups)) for sg in security_groups: - print "\nDeleting security group %s (%s) ..." % (sg['name'], - sg['objectId']) + print ("\nDeleting security group %s (%s) ..." % (sg['name'], + sg['objectId'])) endpoint = '/services/securitygroup/%s' % sg['objectId'] params = {'force': self.force} response = self.delete(endpoint=endpoint, params=params) if response.status_code != 200: - print "ERROR: response status code %s" % response.status_code + print ("ERROR: response status code %s" % response.status_code) def query_all_spoofguard_policies(self): self.__set_api_version('4.0') @@ -255,7 +271,7 @@ class VSMClient(object): # Query all spoofguard policies response = self.get() if response.status_code is not 200: - print "ERROR: Faield to get spoofguard policies" + print ("ERROR: Faield to get spoofguard policies") return sgp_all = response.json() policies = [sgp for sgp in sgp_all['policies'] if @@ -263,15 +279,15 @@ class VSMClient(object): return policies def cleanup_spoofguard_policies(self): - print "\n\nCleaning up spoofguard policies" + print ("\n\nCleaning up spoofguard policies") policies = self.query_all_spoofguard_policies() - print "There are total %s policies" % len(policies) + print ("There are total %s policies" % len(policies)) for spg in policies: - print "\nDeleting spoofguard policy %s (%s) ..." % (spg['name'], - spg['policyId']) + print ("\nDeleting spoofguard policy %s (%s) ..." % + (spg['name'], spg['policyId'])) endpoint = '/services/spoofguard/policies/%s' % spg['policyId'] response = self.delete(endpoint=endpoint) - print "Response code: %s" % response.status_code + print ("Response code: %s" % response.status_code) def query_all_edges(self): edges = [] @@ -282,10 +298,10 @@ class VSMClient(object): paging_info = response.json()['edgePage']['pagingInfo'] page_size = int(paging_info['pageSize']) total_count = int(paging_info['totalCount']) - print "There are total %s edges and page size is %s" % ( - total_count, page_size) + print ("There are total %s edges and page size is %s" % ( + total_count, page_size)) pages = ceil(total_count, page_size) - print "Total pages: %s" % pages + print ("Total pages: %s" % pages) for i in range(0, pages): start_index = page_size * i params = {'startindex': start_index} @@ -296,14 +312,15 @@ class VSMClient(object): return edges def cleanup_edge(self): - print "\n\nCleaning up edges on NSX manager" + print ("\n\nCleaning up edges on NSX manager") edges = self.query_all_edges() for edge in edges: - print "\nDeleting edge %s (%s) ..." % (edge['name'], edge['id']) + print ("\nDeleting edge %s (%s) ..." % (edge['name'], edge['id'])) endpoint = '/edges/%s' % edge['id'] response = self.delete(endpoint=endpoint) if response.status_code != 204: - print "ERROR: response status code %s" % response.status_code + print ("ERROR: response status code %s" % + response.status_code) def cleanup_all(self): self.cleanup_firewall_section() @@ -322,9 +339,8 @@ def ceil(a, b): if __name__ == "__main__": - from optparse import OptionParser - parser = OptionParser() + parser = optparse.OptionParser() parser.add_option("--vsm-ip", dest="vsm_ip", help="NSX Manager IP address") parser.add_option("-u", "--username", default="admin", dest="username", help="NSX Manager username") @@ -333,10 +349,10 @@ if __name__ == "__main__": parser.add_option("-f", "--force", dest="force", action="store_true", help="Force cleanup option") (options, args) = parser.parse_args() - print "vsm-ip: %s" % options.vsm_ip - print "username: %s" % options.username - print "password: %s" % options.password - print "force: %s" % options.force + print ("vsm-ip: %s" % options.vsm_ip) + print ("username: %s" % options.username) + print ("password: %s" % options.password) + print ("force: %s" % options.force) # Get VSM REST client if options.force: diff --git a/tox.ini b/tox.ini index 8c743bcbd1..1d0cbd2b43 100644 --- a/tox.ini +++ b/tox.ini @@ -82,7 +82,7 @@ commands = python setup.py build_sphinx ignore = E125,E126,E128,E129,E265,H305,H307,H402,H404,H405,H904 show-source = true builtins = _ -exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,.ropeproject,devstack +exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,.ropeproject [testenv:pylint] deps =