"Fossies" - the Fresh Open Source Software Archive 
Member "horizon-16.0.0/openstack_dashboard/api/_nova.py" (16 Oct 2019, 5461 Bytes) of package /linux/misc/openstack/horizon-16.0.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "_nova.py" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
15.1.0_vs_16.0.0.
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 """
14 This module is a special module to define functions or other resources
15 which need to be imported outside of openstack_dashboard.api.nova
16 (like cinder.py) to avoid cyclic imports.
17 """
18
19 from django.conf import settings
20 from glanceclient import exc as glance_exceptions
21 from novaclient import api_versions
22 from novaclient import client as nova_client
23
24 from horizon import exceptions as horizon_exceptions
25 from horizon.utils import memoized
26
27 from openstack_dashboard.api import base
28 from openstack_dashboard.api import glance
29 from openstack_dashboard.api import microversions
30 from openstack_dashboard.contrib.developer.profiler import api as profiler
31
32
33 # Supported compute versions
34 VERSIONS = base.APIVersionManager("compute", preferred_version=2)
35 VERSIONS.load_supported_version(1.1, {"client": nova_client, "version": 1.1})
36 VERSIONS.load_supported_version(2, {"client": nova_client, "version": 2})
37
38 INSECURE = settings.OPENSTACK_SSL_NO_VERIFY
39 CACERT = settings.OPENSTACK_SSL_CACERT
40
41
42 class Server(base.APIResourceWrapper):
43 """Simple wrapper around novaclient.server.Server.
44
45 Preserves the request info so image name can later be retrieved.
46 """
47 _attrs = ['addresses', 'attrs', 'id', 'image', 'links', 'description',
48 'metadata', 'name', 'private_ip', 'public_ip', 'status', 'uuid',
49 'image_name', 'VirtualInterfaces', 'flavor', 'key_name', 'fault',
50 'tenant_id', 'user_id', 'created', 'locked',
51 'OS-EXT-STS:power_state', 'OS-EXT-STS:task_state',
52 'OS-EXT-SRV-ATTR:instance_name', 'OS-EXT-SRV-ATTR:host',
53 'OS-EXT-AZ:availability_zone', 'OS-DCF:diskConfig']
54
55 def __init__(self, apiresource, request):
56 super(Server, self).__init__(apiresource)
57 self.request = request
58
59 # TODO(gabriel): deprecate making a call to Glance as a fallback.
60 @property
61 def image_name(self):
62 if not self.image:
63 return None
64 elif hasattr(self.image, 'name'):
65 return self.image.name
66 elif 'name' in self.image:
67 return self.image['name']
68 else:
69 try:
70 image = glance.image_get(self.request, self.image['id'])
71 self.image['name'] = image.name
72 return image.name
73 except (glance_exceptions.ClientException,
74 horizon_exceptions.ServiceCatalogException):
75 self.image['name'] = None
76 return None
77
78 @property
79 def internal_name(self):
80 return getattr(self, 'OS-EXT-SRV-ATTR:instance_name', "")
81
82 @property
83 def availability_zone(self):
84 return getattr(self, 'OS-EXT-AZ:availability_zone', "")
85
86 @property
87 def host_server(self):
88 return getattr(self, 'OS-EXT-SRV-ATTR:host', '')
89
90
91 @memoized.memoized
92 def get_microversion(request, features):
93 client = novaclient(request)
94 min_ver, max_ver = api_versions._get_server_version_range(client)
95 return (microversions.get_microversion_for_features(
96 'nova', features, api_versions.APIVersion, min_ver, max_ver))
97
98
99 def get_auth_params_from_request(request):
100 """Extracts properties needed by novaclient call from the request object.
101
102 These will be used to memoize the calls to novaclient.
103 """
104 return (
105 request.user.username,
106 request.user.token.id,
107 request.user.tenant_id,
108 request.user.token.project.get('domain_id'),
109 base.url_for(request, 'compute'),
110 base.url_for(request, 'identity')
111 )
112
113
114 @memoized.memoized
115 def cached_novaclient(request, version=None):
116 (
117 username,
118 token_id,
119 project_id,
120 project_domain_id,
121 nova_url,
122 auth_url
123 ) = get_auth_params_from_request(request)
124 if version is None:
125 version = VERSIONS.get_active_version()['version']
126 c = nova_client.Client(version,
127 username,
128 token_id,
129 project_id=project_id,
130 project_domain_id=project_domain_id,
131 auth_url=auth_url,
132 insecure=INSECURE,
133 cacert=CACERT,
134 http_log_debug=settings.DEBUG,
135 auth_token=token_id,
136 endpoint_override=nova_url)
137 return c
138
139
140 def novaclient(request, version=None):
141 if isinstance(version, api_versions.APIVersion):
142 version = version.get_string()
143 return cached_novaclient(request, version)
144
145
146 def get_novaclient_with_instance_desc(request):
147 microversion = get_microversion(request, "instance_description")
148 return novaclient(request, version=microversion)
149
150
151 @profiler.trace
152 def server_get(request, instance_id):
153 return Server(get_novaclient_with_instance_desc(request).servers.get(
154 instance_id), request)