Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7612404
  • 博文数量: 1769
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16352
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1769)

文章存档

2024年(15)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: 系统运维

2016-06-03 14:38:48


  1. #!/usr/bin/python
  2. #coding:utf-8

  3. import json
  4. import urllib2
  5. def byteify(input):
  6.     if isinstance(input, dict):
  7.         return {byteify(key): byteify(value) for key, value in input.iteritems()}
  8.     elif isinstance(input, list):
  9.         return [byteify(element) for element in input]
  10.     elif isinstance(input, unicode):
  11.         return input.encode('utf-8')
  12.     else:
  13.         return input

  14. def token_post():
  15.     auth = {"auth":{"identity":{"methods":["password"],"password":{"user":{"domain":{"id":"default"},"name":"admin","password":"admin"}}},"scope":{"project":{"domain":{"id":"default"},"name":"admin"}}}}
  16.     content = json.dumps(auth)
  17.     headers = {"Content-type":"application/json","Accept": "application/json;charset=UTF-8"}
  18.     auth_url = ''
  19.     token_url = auth_url+"auth/tokens"
  20.     req = urllib2.Request(token_url, content, headers)
  21.     response = urllib2.urlopen(req)
  22.     token = response.info().getheader("X-Subject-Token")
  23.     response.encoding='utf-8'
  24.     response_json = json.loads(response.read())
  25.     response_json = byteify(response_json)
  26.     response_json['authtoken'] = token
  27.     return response_json

  28. def service_list(response):
  29.     auth_url = ''
  30.     service_url = auth_url+"services"
  31.     req = urllib2.Request(service_url)
  32.     tokenid = response['authtoken']
  33.     req.add_header('X-Auth-Token', tokenid)
  34.     response = urllib2.urlopen(req)
  35.     response_json = json.loads(response.read())
  36.     return response_json

  37. def get_nova_endpoint(response):
  38.     services = response['token']['catalog']
  39.     for i in range(0, len(services)):
  40.         if services[i]['name'] == 'nova':
  41.             nova_endpoint = services[i]['endpoints'][0]['url']
  42.     return nova_endpoint

  43. def image_list(response):
  44.     novaurl = get_nova_endpoint(response)
  45.     #print "novaurl:",novaurl
  46.     servers_url = novaurl+'/'+'images'
  47.     req = urllib2.Request(servers_url)
  48.     tokenid = response['authtoken']
  49.     req.add_header('X-Auth-Token', tokenid)
  50.     response = urllib2.urlopen(req)
  51.     response_json = json.loads(response.read())
  52.     return response_json

  53. def flavors_list(response):
  54.     novaurl = get_nova_endpoint(response)
  55.     servers_url = novaurl+'/'+'flavors'
  56.     req = urllib2.Request(servers_url)
  57.     tokenid = response['authtoken']
  58.     req.add_header('X-Auth-Token', tokenid)
  59.     response = urllib2.urlopen(req)
  60.     response_json = json.loads(response.read())
  61.     return response_json


  62. def instances_list(response):
  63.     novaurl = get_nova_endpoint(response)
  64.     servers_url = novaurl+'/'+'servers'
  65.     req = urllib2.Request(servers_url)
  66.     tokenid = response['authtoken']
  67.     req.add_header('X-Auth-Token', tokenid)
  68.     response = urllib2.urlopen(req)
  69.     response_json = json.loads(response.read())
  70.     return response_json


  71. def get_images_id(response):
  72.     images = response['images']
  73.     for i in images:
  74.         if i['name'] == "cirros-0.3.4-x86_64-uec":
  75.             images_id = i['id']
  76.     return images_id

  77. def get_flavors_id(response):
  78.     flavors = response['flavors']
  79.     #print flavors
  80.     for i in flavors:
  81.         if i['name'] == "m1.tiny":
  82.             images_id = i['id']
  83.     return images_id

  84. def instances_create(response):
  85.     novaurl = get_nova_endpoint(response)
  86.     name = "url_api"
  87.     images_id = get_images_id(image_list(response))
  88.     flavors_id = get_flavors_id(flavors_list(response))
  89.     server = json.dumps({"server":{"name":name, "imageRef": images_id, "flavorRef":flavors_id}})
  90.     print server
  91.     headers = {"Content-type":"application/json","Accept": "application/json;charset=UTF-8"}
  92.     servers_url = novaurl+'/'+'servers'
  93.     req = urllib2.Request(servers_url, server, headers)
  94.     tokenid = response['authtoken']
  95.     req.add_header('X-Auth-Token', tokenid)
  96.     response = urllib2.urlopen(req)
  97.     response_json = json.loads(response.read())
  98.     return response_json




  99. if __name__ == '__main__':
  100.     response = token_post()
  101.     #print service_list(response)
  102.     #print image_list(response)
  103.     #print flavors_list(response)
  104.     #print instances_list(response)
  105.     print instances_create(response)

阅读(981) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~