84 lines
3.0 KiB
Python
Raw Normal View History

# Copyright 2015 IBM Corp.
# Copyright 2015, Hewlett-Packard Development Company, L.P.
#
# 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 datetime import datetime
from django.conf import settings
from django.http import JsonResponse
from django.views import generic
import pytz
[NG] Support local file upload in Create Image workflow First, now there are 2 '/api/glance/images/ API wrapper endpoints for creating a new image - POST and PUT. The POST endpoint which existed before now resides at PUT. This was done to support legacy (i.e. proxied by web-server) file uploads in Angular Create Image, because Django (which we need to use in that case) doesn't correctly process PUT request. So, if local file binary payload is added to the form contents, we send it using POST request. Second, speaking of '/api/glance/images' PUT request previously known as POST... Where before the POST call to Horizon REST API wrappers returned the final image object that Glance just created, now there are two possibilities for what happens after PUT is sent. * When Create Image form Source Type is set to URL, then everything is going as before. * When Source Type is set to File, then just a file name instead of an actual Blob is sent to '/api/glance/images', then the Glance Image is queued for creation and Horizon web-server responds with an Image object which dict() representation has 2 additional keys: 'upload_url' and 'token_id'. The 'upload_url' tells the location for the subsequent CORS request, while 'token_id' is passed as a header in that request, so Keystone would let it in. CORS upload is started immediately as Image is queued for creation (first promise is resolved) and returns the second promise, which is resolved once the upload finishes. The modal form hangs until second promise resolves to indicate that upload is in progress. Upload progress notification is added in a follow-up patch. DEPLOY NOTES The client-side code relies on CORS being enabled for Glance service (otherwise browser would forbid the PUT request to a location different from the one form content came from). In a Devstack setup you'll need to edit [cors] section of glance-api.conf file, setting `allowed_origin` setting to the full hostname of the web server (say, http://<HOST_IP>/dashboard). Related-Bug: #1467890 Implements blueprint: horizon-glance-large-image-upload Change-Id: I5d842d614c16d3250380ea1dc1c6e0289d206fb5
2016-05-24 14:54:54 +03:00
from openstack_dashboard import api
from openstack_dashboard.api.rest import urls
from openstack_dashboard.api.rest import utils as rest_utils
# settings that we allow to be retrieved via REST API
# these settings are available to the client and are not secured.
# *** THEY SHOULD BE TREATED WITH EXTREME CAUTION ***
settings_required = settings.REST_API_REQUIRED_SETTINGS
settings_additional = settings.REST_API_ADDITIONAL_SETTINGS
settings_allowed = settings_required + settings_additional
@urls.register
class Settings(generic.View):
"""API for retrieving settings.
This API returns read-only settings values.
This configuration object can be fetched as needed.
Examples of settings: OPENSTACK_HYPERVISOR_FEATURES
"""
url_regex = r'settings/$'
[NG] Support local file upload in Create Image workflow First, now there are 2 '/api/glance/images/ API wrapper endpoints for creating a new image - POST and PUT. The POST endpoint which existed before now resides at PUT. This was done to support legacy (i.e. proxied by web-server) file uploads in Angular Create Image, because Django (which we need to use in that case) doesn't correctly process PUT request. So, if local file binary payload is added to the form contents, we send it using POST request. Second, speaking of '/api/glance/images' PUT request previously known as POST... Where before the POST call to Horizon REST API wrappers returned the final image object that Glance just created, now there are two possibilities for what happens after PUT is sent. * When Create Image form Source Type is set to URL, then everything is going as before. * When Source Type is set to File, then just a file name instead of an actual Blob is sent to '/api/glance/images', then the Glance Image is queued for creation and Horizon web-server responds with an Image object which dict() representation has 2 additional keys: 'upload_url' and 'token_id'. The 'upload_url' tells the location for the subsequent CORS request, while 'token_id' is passed as a header in that request, so Keystone would let it in. CORS upload is started immediately as Image is queued for creation (first promise is resolved) and returns the second promise, which is resolved once the upload finishes. The modal form hangs until second promise resolves to indicate that upload is in progress. Upload progress notification is added in a follow-up patch. DEPLOY NOTES The client-side code relies on CORS being enabled for Glance service (otherwise browser would forbid the PUT request to a location different from the one form content came from). In a Devstack setup you'll need to edit [cors] section of glance-api.conf file, setting `allowed_origin` setting to the full hostname of the web server (say, http://<HOST_IP>/dashboard). Related-Bug: #1467890 Implements blueprint: horizon-glance-large-image-upload Change-Id: I5d842d614c16d3250380ea1dc1c6e0289d206fb5
2016-05-24 14:54:54 +03:00
SPECIALS = {
'HORIZON_IMAGES_UPLOAD_MODE': api.glance.get_image_upload_mode(),
'HORIZON_ACTIVE_IMAGE_VERSION': str(api.glance.VERSIONS.active),
'IMAGES_ALLOW_LOCATION': settings.IMAGES_ALLOW_LOCATION,
'AJAX_POLL_INTERVAL': settings.HORIZON_CONFIG.get(
'ajax_poll_interval', 2500)
[NG] Support local file upload in Create Image workflow First, now there are 2 '/api/glance/images/ API wrapper endpoints for creating a new image - POST and PUT. The POST endpoint which existed before now resides at PUT. This was done to support legacy (i.e. proxied by web-server) file uploads in Angular Create Image, because Django (which we need to use in that case) doesn't correctly process PUT request. So, if local file binary payload is added to the form contents, we send it using POST request. Second, speaking of '/api/glance/images' PUT request previously known as POST... Where before the POST call to Horizon REST API wrappers returned the final image object that Glance just created, now there are two possibilities for what happens after PUT is sent. * When Create Image form Source Type is set to URL, then everything is going as before. * When Source Type is set to File, then just a file name instead of an actual Blob is sent to '/api/glance/images', then the Glance Image is queued for creation and Horizon web-server responds with an Image object which dict() representation has 2 additional keys: 'upload_url' and 'token_id'. The 'upload_url' tells the location for the subsequent CORS request, while 'token_id' is passed as a header in that request, so Keystone would let it in. CORS upload is started immediately as Image is queued for creation (first promise is resolved) and returns the second promise, which is resolved once the upload finishes. The modal form hangs until second promise resolves to indicate that upload is in progress. Upload progress notification is added in a follow-up patch. DEPLOY NOTES The client-side code relies on CORS being enabled for Glance service (otherwise browser would forbid the PUT request to a location different from the one form content came from). In a Devstack setup you'll need to edit [cors] section of glance-api.conf file, setting `allowed_origin` setting to the full hostname of the web server (say, http://<HOST_IP>/dashboard). Related-Bug: #1467890 Implements blueprint: horizon-glance-large-image-upload Change-Id: I5d842d614c16d3250380ea1dc1c6e0289d206fb5
2016-05-24 14:54:54 +03:00
}
@rest_utils.ajax()
def get(self, request):
# TODO(amotoki): Drop the default value of getattr.
# It will be unnecessary once all default settings are defined.
[NG] Support local file upload in Create Image workflow First, now there are 2 '/api/glance/images/ API wrapper endpoints for creating a new image - POST and PUT. The POST endpoint which existed before now resides at PUT. This was done to support legacy (i.e. proxied by web-server) file uploads in Angular Create Image, because Django (which we need to use in that case) doesn't correctly process PUT request. So, if local file binary payload is added to the form contents, we send it using POST request. Second, speaking of '/api/glance/images' PUT request previously known as POST... Where before the POST call to Horizon REST API wrappers returned the final image object that Glance just created, now there are two possibilities for what happens after PUT is sent. * When Create Image form Source Type is set to URL, then everything is going as before. * When Source Type is set to File, then just a file name instead of an actual Blob is sent to '/api/glance/images', then the Glance Image is queued for creation and Horizon web-server responds with an Image object which dict() representation has 2 additional keys: 'upload_url' and 'token_id'. The 'upload_url' tells the location for the subsequent CORS request, while 'token_id' is passed as a header in that request, so Keystone would let it in. CORS upload is started immediately as Image is queued for creation (first promise is resolved) and returns the second promise, which is resolved once the upload finishes. The modal form hangs until second promise resolves to indicate that upload is in progress. Upload progress notification is added in a follow-up patch. DEPLOY NOTES The client-side code relies on CORS being enabled for Glance service (otherwise browser would forbid the PUT request to a location different from the one form content came from). In a Devstack setup you'll need to edit [cors] section of glance-api.conf file, setting `allowed_origin` setting to the full hostname of the web server (say, http://<HOST_IP>/dashboard). Related-Bug: #1467890 Implements blueprint: horizon-glance-large-image-upload Change-Id: I5d842d614c16d3250380ea1dc1c6e0289d206fb5
2016-05-24 14:54:54 +03:00
plain_settings = {k: getattr(settings, k, None) for k
in settings_allowed if k not in self.SPECIALS}
plain_settings.update(self.SPECIALS)
plain_settings.update(self.disk_formats(request))
[NG] Support local file upload in Create Image workflow First, now there are 2 '/api/glance/images/ API wrapper endpoints for creating a new image - POST and PUT. The POST endpoint which existed before now resides at PUT. This was done to support legacy (i.e. proxied by web-server) file uploads in Angular Create Image, because Django (which we need to use in that case) doesn't correctly process PUT request. So, if local file binary payload is added to the form contents, we send it using POST request. Second, speaking of '/api/glance/images' PUT request previously known as POST... Where before the POST call to Horizon REST API wrappers returned the final image object that Glance just created, now there are two possibilities for what happens after PUT is sent. * When Create Image form Source Type is set to URL, then everything is going as before. * When Source Type is set to File, then just a file name instead of an actual Blob is sent to '/api/glance/images', then the Glance Image is queued for creation and Horizon web-server responds with an Image object which dict() representation has 2 additional keys: 'upload_url' and 'token_id'. The 'upload_url' tells the location for the subsequent CORS request, while 'token_id' is passed as a header in that request, so Keystone would let it in. CORS upload is started immediately as Image is queued for creation (first promise is resolved) and returns the second promise, which is resolved once the upload finishes. The modal form hangs until second promise resolves to indicate that upload is in progress. Upload progress notification is added in a follow-up patch. DEPLOY NOTES The client-side code relies on CORS being enabled for Glance service (otherwise browser would forbid the PUT request to a location different from the one form content came from). In a Devstack setup you'll need to edit [cors] section of glance-api.conf file, setting `allowed_origin` setting to the full hostname of the web server (say, http://<HOST_IP>/dashboard). Related-Bug: #1467890 Implements blueprint: horizon-glance-large-image-upload Change-Id: I5d842d614c16d3250380ea1dc1c6e0289d206fb5
2016-05-24 14:54:54 +03:00
return plain_settings
def disk_formats(self, request):
# The purpose of OPENSTACK_IMAGE_FORMATS is to provide a simple object
# that does not contain the lazy-loaded translations, so the list can
# be sent as JSON to the client-side (Angular).
return {'OPENSTACK_IMAGE_FORMATS': [
value
for (value, name) in api.glance.get_image_formats(request)
]}
@urls.register
class Timezones(generic.View):
"""API for timezone service."""
url_regex = r'timezones/$'
@rest_utils.ajax()
def get(self, request):
zones = {tz: datetime.now(pytz.timezone(tz)).strftime('%z')
for tz in pytz.common_timezones}
return JsonResponse({'timezone_dict': zones})