Browse Source

Add servers list API

Change-Id: Ice7a8ea206e7676a5f33dbbfec5a9dfcb2b17954
master
Zhenguo Niu 2 years ago
parent
commit
08bf0b6d85
4 changed files with 80 additions and 0 deletions
  1. 0
    0
      mogan_ui/api/__init__.py
  2. 45
    0
      mogan_ui/api/client.py
  3. 34
    0
      mogan_ui/api/rest_api.py
  4. 1
    0
      test-requirements.txt

+ 0
- 0
mogan_ui/api/__init__.py View File


+ 45
- 0
mogan_ui/api/client.py View File

@@ -0,0 +1,45 @@
1
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
2
+#    not use this file except in compliance with the License. You may obtain
3
+#    a copy of the License at
4
+#
5
+#         http://www.apache.org/licenses/LICENSE-2.0
6
+#
7
+#    Unless required by applicable law or agreed to in writing, software
8
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
+#    License for the specific language governing permissions and limitations
11
+#    under the License.
12
+
13
+from django.conf import settings
14
+
15
+from horizon.utils.memoized import memoized
16
+from moganclient.v1 import client as mogan_client
17
+from openstack_dashboard.api import base
18
+
19
+
20
+@memoized
21
+def moganclient(request):
22
+    """Returns a client connected to the Mogan backend.
23
+
24
+    :param request: HTTP request.
25
+    :return: Mogan client.
26
+    """
27
+    insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
28
+    cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
29
+    mogan_url = base.url_for(request, 'baremetal_compute')
30
+
31
+    return mogan_client.Client(mogan_url,
32
+                               project_id=request.user.project_id,
33
+                               token=request.user.token.id,
34
+                               insecure=insecure,
35
+                               cacert=cacert)
36
+
37
+
38
+def server_list(request):
39
+    """Retrieve a list of servers.
40
+
41
+    :param request: HTTP request.
42
+    :return: A list of servers.
43
+    """
44
+    server_manager = moganclient(request).server
45
+    return server_manager.list(detailed=True, all_projects=False)

+ 34
- 0
mogan_ui/api/rest_api.py View File

@@ -0,0 +1,34 @@
1
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
2
+#    not use this file except in compliance with the License. You may obtain
3
+#    a copy of the License at
4
+#
5
+#         http://www.apache.org/licenses/LICENSE-2.0
6
+#
7
+#    Unless required by applicable law or agreed to in writing, software
8
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
+#    License for the specific language governing permissions and limitations
11
+#    under the License.
12
+
13
+from django.views import generic
14
+
15
+from mogan_ui.api import client
16
+
17
+from openstack_dashboard.api.rest import urls
18
+from openstack_dashboard.api.rest import utils as rest_utils
19
+
20
+
21
+@urls.register
22
+class Servers(generic.View):
23
+    """API for Mogan Servers"""
24
+    url_regex = r'mogan/servers/$'
25
+
26
+    @rest_utils.ajax()
27
+    def get(self, request):
28
+        """Get a list of the servers.
29
+
30
+        :param request: HTTP request.
31
+        :return: servers.
32
+        """
33
+        servers = client.server_list(request)
34
+        return {'servers': [s.to_dict() for s in servers]}

+ 1
- 0
test-requirements.txt View File

@@ -4,6 +4,7 @@
4 4
 
5 5
 # Require Horizon
6 6
 -e git://github.com/openstack/horizon.git#egg=horizon
7
+-e git://github.com/openstack/python-moganclient.git#egg=python-moganclient
7 8
 
8 9
 hacking>=0.12.0,!=0.13.0,<0.14  # Apache-2.0
9 10
 

Loading…
Cancel
Save