Openstack API Cinder

 

http://developer.openstack.org/api-ref-blockstorage-v2.html

 

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import base64
import urllib, urllib2
from urllib2 import URLError 
import requests
import httplib
import os
from urlparse import urlparse
try:
    import json
except Exception:
    import simplejson as json
from auth.Token import AuthToken

tokenID = AuthToken().auth()

#print tokenID


class Cinder():
    
    def __init__(self):
        #self.url = "172.16.200.105:8776"
        self.url = "http://172.16.200.105:8776"
        self.header = {"X-Auth-Token": tokenID, "Content-Type": 'application/json'} 
        #self.header = {"X-Auth-Token": tokenID}
        
    def getAllVolumes(self, tenantID):
        #payload = {'key1': 'value1', 'key2': 'value2'}
        req = requests.get(self.url+"/v2/%s/volumes" % tenantID, headers=self.header)
        #print req.headers
        #return req.content
        return req.status_code
        '''
        params = urllib.urlencode({})
        print params
        conn = httplib.HTTPConnection(self.url)
        #conn.request("GET", '/v2/​%s/volumes' % tenantId, params, self.header)
        conn.request("GET", '/v2/​462dc687079d46bb9fb1f14e61b84002/volumes', params, self.header)
        response = conn.getresponse()
        data = response.read()
        res = json.loads(data)
        conn.close()
        return res
        '''
        
    
    def getAllVolumesDetail(self, tenantID):
        req = requests.get(self.url+"/v2/%s/volumes/detail" % tenantID, headers=self.header)
        #print req.url
        #print req.headers
        return req.content
        '''
        params = urllib.urlencode({})
        conn = httplib.HTTPConnection(self.url)
        conn.request("GET", '/v2/​%s​/volumes/detail' % volumesId, params, self.header)
        response = conn.getresponse()
        statusCode = response.status
        data = response.read()
        res = json.loads(data)
        conn.close()
        return statusCode, res
        '''
   
    def showVolumeDetails(self, tenantID, volumeID):
        req = requests.get(self.url+"/v2/%s/volumes/%s" % (tenantID, volumeID), headers=self.header)
        #print req.url
        #print req.headers
        return req.content



def main():
    CinderStorage = Cinder()
    tenantID = "462dc687079d46bb9fb1f14e61b84002"
    #print json.dumps(Cinder.getAllVolumes(tenantID))
    #volumeID = "300f6442-163a-41e1-97f6-df2f836c33ef"
    #print json.dumps(Cinder.getAllVolumesDetail(tenantID))
    #print json.dumps(Cinder.showVolumeDetails(tenantID, volumeID))
    code = 0
    if CinderStorage.getAllVolumes(tenantID) != 200:
        code = 1
    print code
        
    #print json.dumps(GlanceImage.getAllImages())
    #print json.dumps(Cinder.getAllVolumesDetail(tenantID))
    #print json.dumps(Cinder.showVolumeDetails(tenantID, volumeID))

if __name__ == "__main__":
    main()

  

posted @ 2016-08-04 15:34  schangech  阅读(477)  评论(0编辑  收藏  举报