diff --git a/mogan_ui/api/__init__.py b/mogan_ui/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mogan_ui/api/client.py b/mogan_ui/api/client.py new file mode 100644 index 0000000..e9d48cb --- /dev/null +++ b/mogan_ui/api/client.py @@ -0,0 +1,45 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from django.conf import settings + +from horizon.utils.memoized import memoized +from moganclient.v1 import client as mogan_client +from openstack_dashboard.api import base + + +@memoized +def moganclient(request): + """Returns a client connected to the Mogan backend. + + :param request: HTTP request. + :return: Mogan client. + """ + insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) + cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) + mogan_url = base.url_for(request, 'baremetal_compute') + + return mogan_client.Client(mogan_url, + project_id=request.user.project_id, + token=request.user.token.id, + insecure=insecure, + cacert=cacert) + + +def server_list(request): + """Retrieve a list of servers. + + :param request: HTTP request. + :return: A list of servers. + """ + server_manager = moganclient(request).server + return server_manager.list(detailed=True, all_projects=False) diff --git a/mogan_ui/api/rest_api.py b/mogan_ui/api/rest_api.py new file mode 100644 index 0000000..b28a1ff --- /dev/null +++ b/mogan_ui/api/rest_api.py @@ -0,0 +1,34 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from django.views import generic + +from mogan_ui.api import client + +from openstack_dashboard.api.rest import urls +from openstack_dashboard.api.rest import utils as rest_utils + + +@urls.register +class Servers(generic.View): + """API for Mogan Servers""" + url_regex = r'mogan/servers/$' + + @rest_utils.ajax() + def get(self, request): + """Get a list of the servers. + + :param request: HTTP request. + :return: servers. + """ + servers = client.server_list(request) + return {'servers': [s.to_dict() for s in servers]} diff --git a/test-requirements.txt b/test-requirements.txt index c73097c..dafc211 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,6 +4,7 @@ # Require Horizon -e git://github.com/openstack/horizon.git#egg=horizon +-e git://github.com/openstack/python-moganclient.git#egg=python-moganclient hacking>=0.12.0,!=0.13.0,<0.14 # Apache-2.0