Guest User

Untitled

a guest
Sep 9th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import logging
  2. import os
  3. from oslo_config import cfg
  4. from oslo_serialization import jsonutils
  5. from six.moves.urllib import parse as urlparse
  6. from tacker.vnfm.monitor_drivers.token import Token
  7. from tacker import wsgi
  8. # check alarm url with db --> move to plugin
  9.  
  10. LOG = logging.getLogger(__name__)
  11. username = os.getenv('OS_USERNAME')
  12. password = os.getenv('OS_PASSWORD')
  13. project_name = os.getenv('OS_PROJECT_NAME')
  14. auth_url = os.getenv('OS_AUTH_URL')
  15.  
  16. class AlarmReceiver(wsgi.Middleware):
  17. def process_request(self, req):
  18. if req.method != 'POST':
  19. return
  20. url = req.url
  21. if not self.handle_url(url):
  22. return
  23. prefix, info, params = self.handle_url(req.url)
  24. #username = cfg.CONF.ks_authtoken.username
  25. #password = cfg.CONF.ks_authtoken.password
  26. #auth_url = cfg.CONF.ks_authtoken.auth_uri + "/v3"
  27. #project_name = cfg.CONF.ks_authtoken.project_name
  28. token = Token(username, password,
  29. auth_url, project_name)
  30. token_identity = token.create_token()
  31. req.headers['X_AUTH_TOKEN'] = token_identity
  32. # Change the body request
  33. if 'alarm_id' in req.body:
  34. body_dict = dict()
  35. body_dict['trigger'] = {}
  36. body_dict['trigger'].setdefault('params', {})
  37. # Update params in the body request
  38. alarm_dict = jsonutils.loads(req.body)
  39. body_dict['trigger']['params']['alarm'] = alarm_dict
  40. body_dict['trigger']['params']['credential'] = info[6]
  41. # Update policy and action
  42. body_dict['trigger']['policy_name'] = info[4]
  43. body_dict['trigger']['action_name'] = info[5]
  44. req.body = jsonutils.dumps(body_dict)
  45. LOG.debug('Body alarm: %s', req.body)
  46. # Need to change url because of mandatory
  47. req.environ['PATH_INFO'] = prefix + 'actions'
  48. req.environ['QUERY_STRING'] = ''
  49. LOG.debug('alarm url in receiver: %s', req.url)
  50.  
  51. def handle_url(self, url):
  52. parts = urlparse.urlparse(url)
  53. p = parts.path.split('/')
  54. if len(p) != 7:
  55. return None
  56.  
  57. if any((p[0] != '', p[2] != 'vnfs')):
  58. return None
  59.  
  60. if any((p[0] != '', p[2] != 'vnfs')):
  61. return None
  62. qs = urlparse.parse_qs(parts.query)
  63. params = dict((k, v[0]) for k, v in qs.items())
  64. prefix_url = '/%(collec)s/%(vnf_uuid)s/' % {'collec': p[2],
  65. 'vnf_uuid': p[3]}
  66. return prefix_url, p, params
Add Comment
Please, Sign In to add comment