Add servers list API

Change-Id: Ice7a8ea206e7676a5f33dbbfec5a9dfcb2b17954
This commit is contained in:
Zhenguo Niu 2017-04-18 16:47:20 +08:00
parent de8575d0d7
commit 08bf0b6d85
4 changed files with 80 additions and 0 deletions

0
mogan_ui/api/__init__.py Normal file
View File

45
mogan_ui/api/client.py Normal file
View File

@ -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)

34
mogan_ui/api/rest_api.py Normal file
View File

@ -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]}

View File

@ -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