Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import os
- from oslo_config import cfg
- from oslo_serialization import jsonutils
- from six.moves.urllib import parse as urlparse
- from tacker.vnfm.monitor_drivers.token import Token
- from tacker import wsgi
- # check alarm url with db --> move to plugin
- LOG = logging.getLogger(__name__)
- username = os.getenv('OS_USERNAME')
- password = os.getenv('OS_PASSWORD')
- project_name = os.getenv('OS_PROJECT_NAME')
- auth_url = os.getenv('OS_AUTH_URL')
- class AlarmReceiver(wsgi.Middleware):
- def process_request(self, req):
- if req.method != 'POST':
- return
- url = req.url
- if not self.handle_url(url):
- return
- prefix, info, params = self.handle_url(req.url)
- #username = cfg.CONF.ks_authtoken.username
- #password = cfg.CONF.ks_authtoken.password
- #auth_url = cfg.CONF.ks_authtoken.auth_uri + "/v3"
- #project_name = cfg.CONF.ks_authtoken.project_name
- token = Token(username, password,
- auth_url, project_name)
- token_identity = token.create_token()
- req.headers['X_AUTH_TOKEN'] = token_identity
- # Change the body request
- if 'alarm_id' in req.body:
- body_dict = dict()
- body_dict['trigger'] = {}
- body_dict['trigger'].setdefault('params', {})
- # Update params in the body request
- alarm_dict = jsonutils.loads(req.body)
- body_dict['trigger']['params']['alarm'] = alarm_dict
- body_dict['trigger']['params']['credential'] = info[6]
- # Update policy and action
- body_dict['trigger']['policy_name'] = info[4]
- body_dict['trigger']['action_name'] = info[5]
- req.body = jsonutils.dumps(body_dict)
- LOG.debug('Body alarm: %s', req.body)
- # Need to change url because of mandatory
- req.environ['PATH_INFO'] = prefix + 'actions'
- req.environ['QUERY_STRING'] = ''
- LOG.debug('alarm url in receiver: %s', req.url)
- def handle_url(self, url):
- parts = urlparse.urlparse(url)
- p = parts.path.split('/')
- if len(p) != 7:
- return None
- if any((p[0] != '', p[2] != 'vnfs')):
- return None
- if any((p[0] != '', p[2] != 'vnfs')):
- return None
- qs = urlparse.parse_qs(parts.query)
- params = dict((k, v[0]) for k, v in qs.items())
- prefix_url = '/%(collec)s/%(vnf_uuid)s/' % {'collec': p[2],
- 'vnf_uuid': p[3]}
- return prefix_url, p, params
Add Comment
Please, Sign In to add comment