From 3e83c1553c435486eebcc28de6efbae285e36948 Mon Sep 17 00:00:00 2001 From: Adam Gandelman Date: Mon, 16 Nov 2015 15:39:48 -0800 Subject: [PATCH] Resolve astara admin. API via keystone This fetches the astara administrative API endpoint via keystone instead of creating one from a pre-configured networking prefix. Partial-bug: #1516787 Partially-implements: blueprint astara-horizon-mitaka Depends-on: I2b96137c05b832a68ad01a11ec0cfb2371111c3c Change-Id: I5879a2fb9e93c981196f55d1f298c137f315590d --- .../astara_openstack_dashboard/api/astara.py | 69 ++++++++----------- .../dashboards/admin/astararouters/forms.py | 6 +- .../dashboards/admin/astararouters/tables.py | 6 +- .../dashboards/admin/astaratenants/tables.py | 10 +-- 4 files changed, 38 insertions(+), 53 deletions(-) diff --git a/astara_horizon/astara_openstack_dashboard/api/astara.py b/astara_horizon/astara_openstack_dashboard/api/astara.py index 406eaa0..1b2303e 100644 --- a/astara_horizon/astara_openstack_dashboard/api/astara.py +++ b/astara_horizon/astara_openstack_dashboard/api/astara.py @@ -15,30 +15,13 @@ from datetime import datetime from django.conf import settings from horizon.utils import functions as utils -import netaddr import requests as r +from openstack_dashboard.api import base from openstack_dashboard.api.nova import novaclient from openstack_dashboard.api.neutron import neutronclient -def _mgt_url(host, port, path): - if ':' in host: - host = '[%s]' % host - return 'http://%s:%s%s' % (host, port, path) - - -def _make_request(url): - try: - return r.put(url).ok - except r.RequestException: - return False - - -def _get_local_service_ip(management_prefix): - mgt_net = netaddr.IPNetwork(management_prefix) - rug_ip = '%s/%s' % (netaddr.IPAddress(mgt_net.first + 1), - mgt_net.prefixlen) - return rug_ip +KEYSTONE_SERVICE_NAME = 'astara' class Router(object): @@ -57,39 +40,41 @@ class Router(object): class AstaraClient(object): def __init__(self): - self.host = ( - _get_local_service_ip(settings.RUG_MANAGEMENT_PREFIX) - .split('/')[0] - ) - self.port = settings.RUG_API_PORT self.image_uuid = settings.ROUTER_IMAGE_UUID self.api_limit = getattr(settings, 'API_RESULT_LIMIT', 1000) - def poll(self): + def _make_request(self, request, path): + url = base.url_for(request, KEYSTONE_SERVICE_NAME) + path + try: + return r.put(url).ok + except r.RequestException: + return False + + def poll(self, request): path = '/poll' - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def config_reload(self): + def config_reload(self, request): path = '/config/reload' - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def workers_debug(self): + def workers_debug(self, request): path = '/workers/debug' - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def router_debug(self, router_id): + def router_debug(self, request, router_id): path = '/router/debug/{router_id}'.format(router_id=router_id) - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def router_manage(self, router_id): + def router_manage(self, request, router_id): path = '/router/manage/{router_id}'.format(router_id=router_id) - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def router_update(self, router_id): + def router_update(self, request, router_id): path = '/router/update/{router_id}'.format(router_id=router_id) - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def router_rebuild(self, router_id, router_image_uuid=None): + def router_rebuild(self, request, router_id, router_image_uuid=None): if router_image_uuid: path = ('/router/rebuild/{router_id}/--router_image_uuid/' + '{router_image_uuid}').format( @@ -98,15 +83,15 @@ class AstaraClient(object): ) else: path = '/router/rebuild/{router_id}/'.format(router_id=router_id) - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def tenant_debug(self, tenant_id): + def tenant_debug(self, request, tenant_id): path = '/tenant/debug/{tenant_id}'.format(tenant_id=tenant_id) - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) - def tenant_manage(self, tenant_id): + def tenant_manage(self, request, tenant_id): path = '/tenant/manage/{tenant_id}'.format(tenant_id=tenant_id) - return _make_request(_mgt_url(self.host, self.port, path)) + return self._make_request(request, path) def get_routers(self, request, **search_opts): page_size = utils.get_page_size(request) diff --git a/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/forms.py b/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/forms.py index 539195a..7821187 100644 --- a/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/forms.py +++ b/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/forms.py @@ -35,7 +35,7 @@ def _image_choice_title(img): class PollForm(forms.SelfHandlingForm): def handle(self, request, data): try: - rc.poll() + rc.poll(request) messages.success(request, _('Routers were polled')) except Exception: exceptions.handle(request, _('Unable to poll routers.')) @@ -69,9 +69,9 @@ class RebuildForm(forms.SelfHandlingForm): def handle(self, request, data): try: if data['image']: - rc.router_rebuild(data['router_id'], data['image']) + rc.router_rebuild(request, data['router_id'], data['image']) else: - rc.router_rebuild(data['router_id']) + rc.router_rebuild(request, data['router_id']) messages.success(request, _('Rebuilt Router: %s.') % data['router_name']) except Exception: diff --git a/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/tables.py b/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/tables.py index 4b659e6..2352d36 100644 --- a/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/tables.py +++ b/astara_horizon/astara_openstack_dashboard/dashboards/admin/astararouters/tables.py @@ -50,7 +50,7 @@ class ManageAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.router_manage(obj_id) + rc.router_manage(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg) @@ -82,7 +82,7 @@ class DebugAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.router_debug(obj_id) + rc.router_debug(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg) @@ -114,7 +114,7 @@ class UpdateAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.router_update(obj_id) + rc.router_update(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg) diff --git a/astara_horizon/astara_openstack_dashboard/dashboards/admin/astaratenants/tables.py b/astara_horizon/astara_openstack_dashboard/dashboards/admin/astaratenants/tables.py index fc2abd4..f8d766f 100644 --- a/astara_horizon/astara_openstack_dashboard/dashboards/admin/astaratenants/tables.py +++ b/astara_horizon/astara_openstack_dashboard/dashboards/admin/astaratenants/tables.py @@ -63,7 +63,7 @@ class TenantManageAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.tenant_manage(obj_id) + rc.tenant_manage(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg) @@ -95,7 +95,7 @@ class TenantDebugAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.tenant_debug(obj_id) + rc.tenant_debug(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg) @@ -143,7 +143,7 @@ class RouterManageAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.router_manage(obj_id) + rc.router_manage(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg) @@ -175,7 +175,7 @@ class RouterDebugAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.router_debug(obj_id) + rc.router_debug(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg) @@ -207,7 +207,7 @@ class RouterUpdateAction(tables.BatchAction): def action(self, request, obj_id): try: - rc.router_update(obj_id) + rc.router_update(request, obj_id) except Exception: msg = _('Failed to manage route %s') % obj_id exceptions.handle(request, msg)