def modify_fact(object_id): """ Express a fact modification as a list of constraints on the linked node.facts, and run the solved result """ data = flask.request.json api = api_from_models() model_object = api._model_get_by_id('facts', object_id) if not model_object: return generic.http_notfound() node_id = model_object['node_id'] # FIXME: TYPECASTING WHEN WE HAVE FACT TYPES constraints = ['facts.%s = "%s"' % (model_object['key'], data['value'])] return generic.http_solver_request( node_id, constraints, api=api, result={'fact': {'id': model_object['id'], 'node_id': node_id, 'key': model_object['key'], 'value': data['value']}})
def run_plan(): # this comes in just like an optioned plan. We'll stuff any returned # args and call it a plan. <rimshot> # api = api_from_models() data = flask.request.json if not 'node' in data: return generic.http_badrequest(msg='no node specified') if not 'plan' in data: return generic.http_badrequest(msg='no plan specified') plan = data['plan'] # this is more than a bit awkward for step in plan: if 'args' in step: for arg in step['args']: if 'value' in step['args'][arg]: step['ns'][arg] = step['args'][arg]['value'] step.pop('args') # now our plan is a standard plan. Let's run it return generic.http_solver_request(data['node'], [], api=api, plan=plan)
def create(): old_fact = None # if we are creating with the same host_id and key, then we'll just update # fields = api._model_get_columns(object_type) api = api_from_models() data = flask.request.json try: node_id = data['node_id'] key = data['key'] except TypeError: return generic.http_badrequest('node_id and key are required.') except KeyError: pass else: query = 'node_id=%d and key="%s"' % (int(node_id), key) old_fact = api._model_get_first_by_query(object_type, query) if old_fact: return modify_fact(old_fact['id']) # here, if the fact is a fact on a container, # we need to solve for fact application on all # child nodes. <eek> # # FIXME(rp): so we'll punt for now, and just refuse fact # creates on containers. children = api._model_query('nodes', 'facts.parent_id = %s' % data['node_id']) if len(children) > 0: return generic.http_response(403, msg='cannot set fact on containers', friendly='oopsie') constraints = ['facts.%s = "%s"' % (data['key'], data['value'])] return generic.http_solver_request( data['node_id'], constraints, api=api, result={'fact': {'id': -1, 'node_id': data['node_id'], 'key': data['key'], 'value': data['value']}})
def execute_adventure(adventure_id): data = flask.request.json if not 'node' in data: return generic.http_badrequest(msg='node not specified') api = api_from_models() try: adventure = api._model_get_by_id('adventures', int(adventure_id)) except exceptions.IdNotFound: message = 'Not Found: Adventure %s' % adventure_id return generic.http_notfound(msg=message) try: return generic.http_solver_request(data['node'], [], api=api, plan=adventure['dsl']) except exceptions.IdNotFound: #Can IdNotFound be raised for any other reason? return generic.http_notfound(msg='Not Found: Node %s' % data['node'])
def run_plan(): # this comes in just like an optioned plan. We'll stuff any returned # args and call it a plan. <rimshot> # api = api_from_models() data = flask.request.json if not 'node' in data: return generic.http_badrequest(msg='no node specified') if not 'plan' in data: return generic.http_badrequest(msg='no plan specified') plan = data['plan'] # this is more than a bit awkward for step in plan: if 'args' in step: for arg in step['args']: if 'value' in step['args'][arg]: step['ns'][arg] = step['args'][arg]['value'] #add_isolation################### #for add NovaCluster if arg == "tenant_name": uid=request.headers.get('uid') tenant=request.headers.get('tenant') log("log uid:%s tenant:%s " %(uid,tenant)) step['ns'][arg] = tenant #for add Backup and Restore if arg == "auth": cookie_data=request.headers.get('Cookie') log("log cookie:%s " %(cookie_data)) step['ns'][arg] = cookie_data #add_isolation################### step.pop('args') # now our plan is a standard plan. Let's run it return generic.http_solver_request(data['node'], [], api=api, plan=plan)
def create(): old_fact = None plan = None # if we are creating with the same host_id and key, then we'll just update # fields = api._model_get_columns(object_type) api = api_from_models() data = flask.request.json #logging.debug('facts_please.py:create() data = %s' % data) try: node_id = data['node_id'] key = data['key'] except TypeError: return generic.http_badrequest('node_id and key are required.') except KeyError: pass else: query = 'node_id=%d and key="%s"' % (int(node_id), key) old_fact = api._model_get_first_by_query(object_type, query) # try: if (key == 'backends' and 'sdn' not in data['value']): raise ORIGINAL if (key == 'parent_id'): node = api._model_get_by_id('nodes', data['node_id']) parent = api._model_get_by_id('nodes', data['value']) if ('sdn' not in node['facts'].get('backends', [])): if ('sdn' not in parent['facts'].get('backends', {})): raise ORIGINAL else: return generic.http_badrequest('bad parent container') else: if ('sdn' not in parent['facts'].get('backends', {})): return generic.http_badrequest('bad parent container') # New # sdn_s cookie_data = flask.request.headers.get('Cookie') tokenId = '' cookie_split=cookie_data.split(';') for cookie in cookie_split: index = cookie.find(TOKENKEYWORD) if index != -1: tokenId = cookie[index + len(TOKENKEYWORD):] break if 0 == len(tokenId): logging.debug('facts_please.py:create() not find tokenID') return generic.http_badrequest(msg='not find tokenID') # logging.debug('TokenID=%s' % tokenId) # logging.debug('User-Agent=%s' % flask.request.headers.get('User-Agent')) # logging.debug('tenant=%s' % flask.request.headers.get('tenant')) # sdn_e plan = [{'primitive': 'node.set_parent', 'ns': {'parent': data['value']}, 'timeout': 30}, {'primitive': 'node.add_backend', 'ns': {'backend': 'nova'}}, # sdn_s {'primitive': 'nova.set_sdnbox', 'ns': {'backend': 'sdn','tokenid': tokenId}}] # sdn_e if old_fact: model_object = api._model_get_by_id('facts', old_fact['id']) if not model_object: return generic.http_notfound() node_id = model_object['node_id'] # FIXME: TYPECASTING WHEN WE HAVE FACT TYPES constraints = ['facts.%s = "%s"' % (model_object['key'], data['value'])] return generic.http_solver_request( node_id, constraints, api=api, result={'fact': {'id': model_object['id'], 'node_id': node_id, 'key': model_object['key'], 'value': data['value']}}, plan=plan) # New if old_fact: model_object = api._model_update_by_id( object_type, old_fact['id'], data) # send update notification generic._notify(model_object, object_type, old_fact['id']) #shellscript = node['attrs'].get('opencenter_sdn_sh', []) #subprocess.call(shellscript, shell=True) else: try: model_object = api._model_create(object_type, data) except KeyError as e: # missing required field return generic.http_badrequest(msg=str(e)) generic._notify(model_object, object_type, model_object['id']) href = flask.request.base_url + str(model_object['id']) return generic.http_response(201, '%s Created' % singular_object_type.capitalize(), ref=href, **{singular_object_type: model_object}) except ORIGINAL: pass # logging.debug('facts_please.py:create() in ORIGINAL') # if old_fact: logging.debug('facts_please.py:create() goto modify_fact') return modify_fact(old_fact['id']) # here, if the fact is a fact on a container, # we need to solve for fact application on all # child nodes. <eek> # # FIXME(rp): so we'll punt for now, and just refuse fact # creates on containers. children = api._model_query('nodes', 'facts.parent_id = %s' % data['node_id']) if len(children) > 0: return generic.http_response(403, msg='cannot set fact on containers', friendly='oopsie') constraints = ['facts.%s = "%s"' % (data['key'], data['value'])] return generic.http_solver_request( data['node_id'], constraints, api=api, result={'fact': {'id': -1, 'node_id': data['node_id'], 'key': data['key'], 'value': data['value']}}, plan=plan)