def index(self, req): if not req.context.is_admin: raise exception.Forbidden() now = timeutils.utcnow(with_timezone=True) _services = service_obj.Service.get_all(req.context) svcs = [] for svc in _services: updated_at = svc.updated_at delta = now - (svc.updated_at or svc.created_at) delta_sec = delta.total_seconds() alive = abs(delta_sec) <= CONF.service_down_time art = (alive and "up") or "down" active = 'enabled' if svc.disabled: active = 'disabled' if updated_at: updated_at = timeutils.normalize_time(updated_at) ret_fields = { 'id': svc.id, 'host': svc.host, 'binary': svc.binary, 'topic': svc.topic, 'disabled_reason': svc.disabled_reason, 'status': active, 'state': art, 'updated_at': updated_at } svcs.append(ret_fields) return {'services': svcs}
def _get_token(self, **kwargs): """Get a valid token based on the credential provided. :param cred: Rebuilt credential dictionary for authentication. """ try: token = driver_base.SenlinDriver().identity.get_token(**kwargs) except Exception as ex: LOG.exception('Webhook failed authentication: %s.', ex) raise exc.Forbidden() return token
def test_event_index_global_project_true(self, mock_call, mock_parse, mock_enforce): self._mock_enforce_setup(mock_enforce, 'index', True) params = {'global_project': 'True'} req = self._get('/events', params=params) obj = mock.Mock() mock_parse.return_value = obj error = senlin_exc.Forbidden() mock_call.side_effect = shared.to_remote_error(error) resp = shared.request_with_middleware(fault.FaultWrapper, self.controller.index, req) self.assertEqual(403, resp.json['code']) self.assertEqual('Forbidden', resp.json['error']['type']) mock_parse.assert_called_once_with("EventListRequest", mock.ANY, {'project_safe': False}) mock_call.assert_called_once_with(req.context, 'event_list', obj)