def update_image(user,**image):#Attribute disk_format can be only replaced for a queued image method_name = 'update_image' parameter = ['image_id', 'image_content'] if Util.validate_parameter(*parameter, **image): image_id = image.get('image_id') req = Util.createRequest(GLANCE_URL+'v2/images/%s' % image_id, 'PATCH', user, 'patch') try: image_content = image.get('image_content') response = urllib2.urlopen(req, json.dumps(image_content), timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code if code == 200 or code == 204: try: update_cache(user,image_id,'update',image_content) except Exception, e: print e.code delete_cache(image_id, user) logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return code, content logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) if 404 == e.code: raise APIError(method_name+' failed.Reason: not found the image')
def create_project(user,**project): #1045 {'project':project} method_name = 'create_project' parameter = ['project'] import pdb pdb.set_trace() if Util.validate_parameter(*parameter, **project): req = Util.createRequest(KEY_STORE_URL+"/v3/projects", 'POST',user) try: response = urllib2.urlopen(req, json.dumps(project), timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) project_id = content['project'].get('id') code = response.code logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) user_id=content['project'].get('project_user_id') # if code ==201 and user_id is not None: # role_id="31fd1d2bb7e741218637e230c13df800"#Need to change roleId # add_user_to_project(user,project_id,user_id,role_id) return code,content logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+":"+str(e.message)) if 409 == e.code: raise APIError('create project failed', 'Project Duplicated') else: raise APIError('HttpError,please contact the admin') except URLError, e: logger.error(method_name+"URLError:"+e.message) raise APIError('URLError',e.reason)
def update_tenant(**tenant):#1200 {'tenant_id':tenant_id,'teant':teant} method_name = 'update_tenant' parameter = ('tenant_id', 'teant') if Util.validate_parameter(*parameter, **tenant): # teant = { # "tenant": { # "id": tenant.get('tenant_id'), # "name": "ACMExxx corp", # "description": "A description ...", # "enabled": True # } # } teant = tenant.get('teant') params = json.dumps(teant) req = Util.createRequest(KEY_STORE_URL+"v2.0/tenants/%s" % tenant.get('tenant_id'), 'POST') try: response = urllib2.urlopen(req, params, timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return content logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+' Error code: '+str(e.message)) if 409 == e.code: raise APIError(method_name+' Duplicated,please update the Tenant to create again') except URLError, e: logger.error(method_name+' URLError: '+str(e.reason)) raise APIError(method_name+' URLError: ', e.reason)
def share_image(user,**image):#1273 {'image_id':image_id,'members':members} method_name = 'share_image' parameter = ('image_id', 'members') if Util.validate_parameter(*parameter, **image): req = Util.createRequest(GLANCE_URL+"/v2/images/%s/members" % image.get('image_id'), 'POST') try: response = urllib2.urlopen(req, json.dumps(image.get('members')), timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return code, content logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) raise APIError(method_name+' Error code: '+str(e.code)) except URLError, e: logger.error(method_name+' URLError: '+str(e.code)) raise APIError(method_name+' URLError: '+str(e.reason))
def delete_tenant(**tenant):#1201 Normal response codes: 204 {'tenant_id':tenantId} method_name = 'delete_tenant' parameter = ['tenant_id'] if Util.validate_parameter(*parameter, **tenant): req = Util.createRequest(KEY_STORE_URL+"v2.0/tenants/%s" % tenant.get('tenant_id'), 'DELETE') try: response = urllib2.urlopen(req, json.dumps({}), timeout=TIME_OUT) obj = response.read() if obj and len(obj): code = response.code logger.debug(method_name+" code:" + str(code)) return code logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) if 404 == e.code: raise APIError(method_name+' failed.Reason: not found the Tenant') except URLError, e: logger.error(method_name+' URLError: '+str(e.reason)) raise APIError(' URLError: ', e.reason)
def get_policy_by_id(**policy):#{'policy_id':policy_id} method_name = 'get_policy_by_id' parameter = ('policy_id') if Util.validate_parameter(*parameter, **policy): req = Util.createRequest(KEY_STORE_URL+'v3/policies/%s'+'/topologies/%d/node/%s' % policy.get('policy_id'), 'GET') try: response = urllib2.urlopen(req, json.dumps({}), timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return code, content logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) if 404 == e.code: raise APIError(' failed.Reason: not found the policy') except URLError, e: logger.error(method_name,' URLError: '+str(e.reason)) raise APIError(' URLError: ', e.reason)
def download_image(image_dict):#1270 {'image_id':image_id, 'file_name':file_name} method_name = 'download_image' parameter = ['image_id', 'file_name'] if Util.validate_parameter(*parameter, **image): req = Util.createRequest(GLANCE_URL+'v2/images/%s/file' % image.get('image_id'), 'GET', 'stream') try: response = urllib2.urlopen(req, timeout=TIME_OUT) with open(image.get('file_name'), 'wb') as f: while True: tmp = response.read(1024) if not tmp: break f.write(tmp) code = response.getcode() logger.debug(method_name+" code:" + str(code)) return code except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) if 404 == e.code: raise APIError(method_name+' failed.Reason: not found the image') except URLError, e: logger.error(method_name+' URLError: '+str(e.code)) raise APIError(method_name+' URLError: '+str(e.reason))
def delete_image(user,**image):#1265 {'image_id':imageId} method_name = 'delete_image' #You can delete an image in all status except deleted. # You must first set the 'protected' attribute to false (boolean) and then perform the delete. parameter = ['image_id'] if Util.validate_parameter(*parameter, **image): image_id = image.get('image_id') req = Util.createRequest(GLANCE_URL+'v2/images/%s' % image_id, 'DELETE',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) code = response.code logger.debug(method_name+" code:" + str(code)) # update cache failed, delete the whole cache table data try: update_cache(user,image_id, 'delete') except Exception, e: print e.code delete_cache(image_id, user) return code except HTTPError, e: logging.error(method_name+' Error code: '+str(e.code)) if 404 == e.code: raise APIError('not found the image')
def update_user(**user):#1200 {'tenant_id':tenant_id,'teant':teant} method_name = 'update_user' parameter = ('user_id', 'user') if Util.validate_parameter(*parameter, **user): user = user.get('user') params = json.dumps(teant) req = Util.createRequest(KEY_STORE_URL+"v2.0/users/%s" % user.get('tenant_id'), 'POST') try: response = urllib2.urlopen(req, params, timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return content logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+' Error code: '+str(e.message)) if 409 == e.code: raise APIError(method_name+' Duplicated,please update the Tenant to create again') except URLError, e: logger.error(method_name+' URLError: '+str(e.reason)) raise APIError(method_name+' URLError: ', e.reason)
def create_tenant(**teant): #1199{'teant':teant} method_name = 'create_tenant' parameter = ('teant') if Util.validate_parameter(*parameter, **teant): req = Util.createRequest(KEY_STORE_URL+"v2.0/tenants/", 'POST') try: response = urllib2.urlopen(req, json.dumps(teant.get('teant')), timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return content logger.error(method_name+' Error code: '+response.code) except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) if 409 == e.code: raise APIError('create tenant failed', 'Tenant Duplicated,please update the Tenant to create again') else: raise APIError('HttpError,please contact the admin') except URLError, e: logger.error(method_name+' URLError: ', e.reason) raise APIError('URLError:'+e.reason)