OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glance.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2012 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. #
  6. # Copyright 2012 Nebula, Inc.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. # not use this file except in compliance with the License. You may obtain
  10. # a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. # License for the specific language governing permissions and limitations
  18. # under the License.
  19. from __future__ import absolute_import
  20. import logging
  21. import urlparse
  22. from glance import client as glance_client
  23. from horizon.api.base import APIDictWrapper, url_for
  24. LOG = logging.getLogger(__name__)
  25. class Image(APIDictWrapper):
  26. """
  27. Wrapper around glance image dictionary to make it object-like and provide
  28. access to image properties.
  29. """
  30. _attrs = ['checksum', 'container_format', 'created_at', 'deleted',
  31. 'deleted_at', 'disk_format', 'id', 'is_public', 'location',
  32. 'name', 'properties', 'size', 'status', 'updated_at', 'owner']
  33. def __getattr__(self, attrname):
  34. if attrname == "properties":
  35. if not hasattr(self, "_properties"):
  36. properties_dict = super(Image, self).__getattr__(attrname)
  37. self._properties = ImageProperties(properties_dict)
  38. return self._properties
  39. else:
  40. return super(Image, self).__getattr__(attrname)
  41. class ImageProperties(APIDictWrapper):
  42. """
  43. Wrapper around glance image properties dictionary to make it object-like.
  44. """
  45. _attrs = ['architecture', 'image_location', 'image_state', 'kernel_id',
  46. 'project_id', 'ramdisk_id', 'image_type']
  47. def glanceclient(request):
  48. o = urlparse.urlparse(url_for(request, 'image'))
  49. LOG.debug('glanceclient connection created for host "%s:%d"' %
  50. (o.hostname, o.port))
  51. return glance_client.Client(o.hostname,
  52. o.port,
  53. auth_tok=request.user.token)
  54. def image_create(request, image_meta, image_file):
  55. return Image(glanceclient(request).add_image(image_meta, image_file))
  56. def image_delete(request, image_id):
  57. return glanceclient(request).delete_image(image_id)
  58. def image_get(request, image_id):
  59. """
  60. Returns the actual image file from Glance for image with
  61. supplied identifier
  62. """
  63. return glanceclient(request).get_image(image_id)[1]
  64. def image_get_meta(request, image_id):
  65. """
  66. Returns an Image object populated with metadata for image
  67. with supplied identifier.
  68. """
  69. return Image(glanceclient(request).get_image_meta(image_id))
  70. def image_list_detailed(request):
  71. return [Image(i) for i in glanceclient(request).get_images_detailed()]
  72. def image_update(request, image_id, image_meta=None):
  73. image_meta = image_meta and image_meta or {}
  74. return Image(glanceclient(request).update_image(image_id,
  75. image_meta=image_meta))
  76. def snapshot_list_detailed(request):
  77. filters = {}
  78. filters['property-image_type'] = 'snapshot'
  79. filters['is_public'] = 'none'
  80. return [Image(i) for i in glanceclient(request)
  81. .get_images_detailed(filters=filters)]