def list_all_policy(**policy_parameter): #/v3/policies{?type,page,per_page} 1125 method_name = 'listAllPolicy' request_parameter = [] if policy_parameter is not None and len(policy_parameter) > 0 and isinstance(policy_parameter, dict): item = 0 for (key, value) in policy_parameter.items(): if item == 0: request_parameter.append('?'+key) else: request_parameter.append('&'+key) item += 1 request_parameter.append('='+str(value)) if len(request_parameter) > 0: logger.debug(method_name+'parameter:' + ''.join(request_parameter)) req = Util.createRequest(KEY_STORE_URL+'/v3/policies%s' % ''.join(request_parameter), 'GET') else: req = Util.createRequest(KEY_STORE_URL+'/v3/policies', 'GET') try: response = urllib2.urlopen(req, 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('Reason: not found the endPoint')
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 create_user(userObject,**user_entity):#{'user',user} method_name = 'create_user' # if Util.validate_parameter(*parameter, **user): print 'user_entityxxxxxx',user_entity req = Util.createRequest(KEY_STORE_URL+"/v3/users", 'POST',userObject) if user_entity.get('user').get('role') is not None: role_id = user_entity.get('user')['role'] del user_entity.get('user')['role'] try: response = urllib2.urlopen(req, json.dumps(user_entity), 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)) if code == 201: user = content.get('user') role_assign = {'project_id':user['default_project_id'],'user_id':user["id"], 'role_id':role_id} code = assign_role_to_user(userObject,**role_assign) return code, content else: 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('create tenant failed', 'User Duplicated') else: raise APIError('HttpError,please contact the admin')
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_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 delete_project(user, project_ids):#1046 method_name = 'delete_project' req = Util.createRequest(KEY_STORE_URL+"v3/projects/" + project_ids, 'DELETE', user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) code = response.code logger.debug(method_name+" code:" + str(code)) return code except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) raise APIError('HttpError,please contact the admin')
def delete_user(user,user_id):#1201 Normal response codes: 204 {'tenant_id':tenantId} method_name = 'delete_user' req = Util.createRequest(KEY_STORE_URL+"/v3/users/%s" % user_id, 'DELETE',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) code = response.code 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 User')
def list_project_quota(user, admin_project_id , project_id):#10460d40d7dffdc84e97856358ec02f3917d method_name = 'list_project_quota' req = Util.createRequest(CINDER_URL+"v2/%s/os-quota-sets/%s" %(admin_project_id,project_id),'GET',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(response.code)+" content:" + str(content)) return code, content except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) raise APIError('HttpError,please contact the admin')
def list_project(user):#1046 method_name = 'list_project' req = Util.createRequest(KEY_STORE_URL+"/v3/projects", 'GET',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(response.code)+" content:" + str(content)) return code, content except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) raise APIError('HttpError,please contact the admin')
def update_project_quota(user,admin_project_id,project_id,**quota):#1046 method_name = 'update_project_quota' req = Util.createRequest(NOVA_URL+"v2/%s/os-quota-sets/%s" %(admin_project_id,project_id),'PUT',user) ## cinder /network/nova all need to update the Quota separatelly try: response = urllib2.urlopen(req,json.dumps(quota), timeout=TIME_OUT) obj = response.read() if obj and len(obj): content = json.loads(obj) code = response.code logger.debug(method_name+" code:" + str(response.code)+" content:" + str(content)) return code, content except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) raise APIError('HttpError,please contact the admin')
def assign_role_to_user(user,**kargs):#{'project_id':project_id,'user_id':user_id, 'role_id':role_id} method_name = 'assign_role_to_user' url = KEY_STORE_URL+"v3/projects/%s/users/%s/roles/%s" % (kargs.get('project_id'), kargs.get('user_id'), kargs.get('role_id')) req = Util.createRequest(url, 'PUT',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) obj = response.read() if obj is not None: code = response.code logger.debug(method_name+" code:" + str(code)) return code except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) raise APIError('HttpError,please contact the admin')
def get_service_list(user):#1201 Normal response codes: 204 {'tenant_id':tenantId} method_name = 'get_service_list' req = Util.createRequest(KEY_STORE_URL+"/v3/services", 'GET', user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) obj = response.read() if obj and len(obj): code = response.code content = json.loads(obj) logger.debug(method_name+" code:" + str(code)) return code,content 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 Endpoint')
def get_end_point(user):#Lists endpoints for a tenant 1217 {'tenant_id':tenant_id,'endpoint_id':endpoint_id} method_name = 'get_end_point' req = Util.createRequest(KEY_STORE_URL+"/v3/endpoints", 'GET',user) try: response = urllib2.urlopen(req, 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('Reason: not found the endPoint')
def add_user_to_project(user,project_id,user_id,role_id): #1045 {'project':project} method_name = 'add_user_to_project' req = Util.createRequest(KEY_STORE_URL+"/v3/projects/%s/users/%s/roles/%s" %(project_id,user_id,role_id), 'PUT',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) obj = response.read() if obj and len(obj): code = response.code logger.debug(method_name+" code:" + str(code)) return 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')
def upload_image(user,**image):#1269 {'image':image} method_name = 'upload_image' req = Util.createRequest(GLANCE_URL+'v2/images', 'POST',user) fileName = None image_content = image.get('image') if image_content['fileName'] is not None: fileName = image_content['fileName'] del image_content['fileName'] try: 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 logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) image_id = content['id'] response_code = put_image_data(image_id,fileName,user) #add image to cache if response_code == 204: try: # image_content ={"protected": False, "name": "portal002", "container_format": "bare", "disk_format": "qcow2", "uri": "1", "visibility": "public", "description": "desc", # "fileName":"E:\portal_training\cirros-0.3.4-x86_64-disk.img"} image_obj = {} image_obj['name'] = image_content.get('name') image_obj['description'] = image_content.get('description') image_obj['container_format'] = image_content.get('container_format') image_obj['protected'] = image_content.get('protected') image_obj['disk_format'] = image_content.get('disk_format') image_obj['visibility'] = image_content.get('visibility') image_obj['owner'] = user.get('projectid') image_obj['size'] = os.path.getsize(fileName) now_time_eightHoursAgo = (datetime.datetime.now() - datetime.timedelta(hours = 8)) now_time = now_time_eightHoursAgo.strftime('%Y-%m-%d %H:%M:%S') image_obj['created_at'] = str(now_time) image_obj['id'] = image_id update_cache(user,image_id, 'add',image_obj) except Exception, e: print e.code delete_cache(image_id, user) else: delete_cache(image_id, user) return response_code, content else:
def list_role(user,filter=None): method_name = 'list_role' url = KEY_STORE_URL+"/v3/roles" if filter is not None: url = KEY_STORE_URL+"/v3/roles?"+str(filter) req = Util.createRequest(url, 'GET',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) obj = response.read() if obj is not None 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('HttpError,please contact the admin')
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 show_image_detail_by_id(user,image_id): method_name = 'show_image_detail_by_id' req = Util.createRequest(GLANCE_URL+'v2/images/%s' % image_id, 'GET',user) code = 500 try: response = urllib2.urlopen(req, 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 else: logger.error(method_name+' Error code: '+response.code) return code, {"error":"error"} except HTTPError, e: logger.error(method_name+' Error code: '+str(e.code)) if 404 == e.code: logger.error(method_name+'show image failed.Reason: not found the image') return e.code, {"error":"error"}
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 list_image(user,parameter='/v2/images'):#1258 opcache = [] opcache = Opcache.objects.filter(user=user.get('username')).filter(params=parameter).filter(category='list_image') if opcache is not None and len(opcache) >0: obj = opcache[0].content if obj and len(obj): content = json.loads(obj) for image in content['images']: image_type = image.get('image_type') if image_type is None or image_type!='snapshot': del image_type image['image_type']='image' # logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return 200, content method_name = 'list_image' req = Util.createRequest(GLANCE_URL+parameter, 'GET',user) try: response = urllib2.urlopen(req, timeout=TIME_OUT) obj = response.read() o = Opcache() o.user = user.get('username') o.category = 'list_image' o.params = parameter o.content = obj o.save() if obj and len(obj): content = json.loads(obj) code = response.code for image in content['images']: image_type = image.get('image_type') if image_type is None or image_type!='snapshot': del image_type image['image_type']='image' # logger.debug(method_name+" code:" + str(code)+" content:" + str(content)) return code, content 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 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 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)
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)