Skip to content

Instantly share code, notes, and snippets.

@laidbackware
Last active December 13, 2019 11:07
Show Gist options
  • Save laidbackware/c8bb55f1cea6ccf58b18c55112963d03 to your computer and use it in GitHub Desktop.
Save laidbackware/c8bb55f1cea6ccf58b18c55112963d03 to your computer and use it in GitHub Desktop.
How to update a protected NSX-T object
import requests, json
from pprint import pprint
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
# Setup session object
s = requests.Session()
s.verify = False
# The X-Allow-Overwrite parameter will allow the admin user to update protected objects
s.headers = {'Content-type': 'application/json', 'Accept': 'application/json', 'X-Allow-Overwrite': 'True'}
# Update these 3 variables for your env
nsx_manager = '192.168.0.183'
cluster_router_name = "lb-pks-ea069ad0-223b-4ce9-96e8-3f4efff6e236-cluster-router"
s.auth = ('admin','VMware1!VMware1!')
# Import all logical routers
r = s.get('https://192.168.0.183/api/v1/logical-routers')
routers = r.json()['results']
# Extract cluster router to be changed
for router in routers:
if router['display_name'] == cluster_router_name:
cluster_router = router
break
# Note that the cluster_router variable includes some params nsxt sees as 'read-only'
# These params which begin with '_' and will be ignored when we put back to he API
# Add description field for testing
cluster_router['description'] = 'This is a test!'
pprint(cluster_router)
# Post updated json back to the API
r = s.put('https://%s/api/v1/logical-routers/%s' % (nsx_manager, cluster_router['id']), data=json.dumps(cluster_router))
# If JSON outputs with the revion incrementing then the change has applied
pprint(r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment