Skip to content

Instantly share code, notes, and snippets.

@Kevinstronger
Created February 20, 2014 02:14
Show Gist options
  • Save Kevinstronger/9105790 to your computer and use it in GitHub Desktop.
Save Kevinstronger/9105790 to your computer and use it in GitHub Desktop.
#coding:utf-8
# Author: --<>
# Purpose:
# Created: 2013/7/3
import sys
import httplib
import json
import urllib
nova_url='10.28.174.2:8774'
key_url='10.28.174.2:5000'
usernames='admin'
passwords='password'
token=''
auth_key='{"auth":{"passwordCredentials":{"username":"'+usernames+'","password":"'+passwords+'","tenantName":"admin"}}}'
print auth_key
key_conn=httplib.HTTPConnection(key_url)
header={"Content-Type":"application/json","Accept":"application/json"}
key_conn.request('POST','/v2.0/tokens',headers=header,body=auth_key)
result=key_conn.getresponse()
if result.status==200:
auth_result=result.read()
key_conn.close()
token_json=json.loads(auth_result)
print token_json
token = token_json['access']['token']['id']
params = urllib.urlencode({})
headers = { "Accept":"application/json", "X-Auth-Token":token,"Content-type":"application/json","X-Auth-Project-Id":"51e0e50ecb6f4864833058d867208380" }
service =sys.argv[1]
if len(sys.argv)==2:
if service=='instance':
nova_conn=httplib.HTTPConnection(nova_url)
print headers
nova_conn.request('GET','/v1.1/51e0e50ecb6f4864833058d867208380/servers',headers=headers)
nova_result=nova_conn.getresponse()
print nova_result.read()
for servers in json.loads(nova_result.read()).values():
for server in servers:
print server['name']
sys.exit()
else:
print "login failed! please check!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment