Skip to content

Instantly share code, notes, and snippets.

@upa
Created June 24, 2014 07:12
Show Gist options
  • Save upa/1adc4cb99e2b7fae9107 to your computer and use it in GitHub Desktop.
Save upa/1adc4cb99e2b7fae9107 to your computer and use it in GitHub Desktop.
Change port stauts via neutron api using neutron-python binding
#!/usr/bin/env python
import sys
import json
from neutronclient.v2_0 import client as neutronclient
authurl = 'http://10.0.2.15:5000/v2.0'
username = 'demo'
password = 'pass'
tenantname = 'demo'
def main () :
try :
(mac, ip, state) = sys.argv[1:4]
except :
print "%s [MACDDR] [IPADDR] [STATE]" % sys.argv[0]
sys.exit (0)
portid = search_port_id (mac, ip)
if not portid :
print "Can not find interface having %s and %s" % (mac, ip)
sys.exit (0)
update_port_state (portid, state)
def update_port_state (portid, boolen) :
nc = neutronclient.Client (username = username, password = password,
tenant_name = tenantname, auth_url = authurl)
ctx = { 'port' : { 'admin_state_up' : boolen } }
nc.update_port (portid, ctx)
return
def search_port_id (mac, ip) :
nc = neutronclient.Client (username = username, password = password,
tenant_name = tenantname, auth_url = authurl)
portjson = nc.list_ports ()
for port in portjson["ports"] :
if port["mac_address"] == mac :
for addresses in port["fixed_ips"] :
if addresses["ip_address"] == ip :
return port["id"]
return None
main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment