OpenStack Image Management (Glance)
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.

client.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright 2012 OpenStack Foundation
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import os
  16. from oslo_serialization import jsonutils as json
  17. from glance.common import client as base_client
  18. from glance.common import exception
  19. from glance.i18n import _
  20. class CacheClient(base_client.BaseClient):
  21. DEFAULT_PORT = 9292
  22. DEFAULT_DOC_ROOT = '/v1'
  23. def delete_cached_image(self, image_id):
  24. """
  25. Delete a specified image from the cache
  26. """
  27. self.do_request("DELETE", "/cached_images/%s" % image_id)
  28. return True
  29. def get_cached_images(self, **kwargs):
  30. """
  31. Returns a list of images stored in the image cache.
  32. """
  33. res = self.do_request("GET", "/cached_images")
  34. data = json.loads(res.read())['cached_images']
  35. return data
  36. def get_queued_images(self, **kwargs):
  37. """
  38. Returns a list of images queued for caching
  39. """
  40. res = self.do_request("GET", "/queued_images")
  41. data = json.loads(res.read())['queued_images']
  42. return data
  43. def delete_all_cached_images(self):
  44. """
  45. Delete all cached images
  46. """
  47. res = self.do_request("DELETE", "/cached_images")
  48. data = json.loads(res.read())
  49. num_deleted = data['num_deleted']
  50. return num_deleted
  51. def queue_image_for_caching(self, image_id):
  52. """
  53. Queue an image for prefetching into cache
  54. """
  55. self.do_request("PUT", "/queued_images/%s" % image_id)
  56. return True
  57. def delete_queued_image(self, image_id):
  58. """
  59. Delete a specified image from the cache queue
  60. """
  61. self.do_request("DELETE", "/queued_images/%s" % image_id)
  62. return True
  63. def delete_all_queued_images(self):
  64. """
  65. Delete all queued images
  66. """
  67. res = self.do_request("DELETE", "/queued_images")
  68. data = json.loads(res.read())
  69. num_deleted = data['num_deleted']
  70. return num_deleted
  71. def get_client(host, port=None, timeout=None, use_ssl=False, username=None,
  72. password=None, tenant=None,
  73. auth_url=None, auth_strategy=None,
  74. auth_token=None, region=None,
  75. is_silent_upload=False, insecure=False):
  76. """
  77. Returns a new client Glance client object based on common kwargs.
  78. If an option isn't specified falls back to common environment variable
  79. defaults.
  80. """
  81. if auth_url or os.getenv('OS_AUTH_URL'):
  82. force_strategy = 'keystone'
  83. else:
  84. force_strategy = None
  85. creds = {
  86. 'username': username or
  87. os.getenv('OS_AUTH_USER', os.getenv('OS_USERNAME')),
  88. 'password': password or
  89. os.getenv('OS_AUTH_KEY', os.getenv('OS_PASSWORD')),
  90. 'tenant': tenant or
  91. os.getenv('OS_AUTH_TENANT', os.getenv('OS_TENANT_NAME')),
  92. 'auth_url': auth_url or
  93. os.getenv('OS_AUTH_URL'),
  94. 'strategy': force_strategy or
  95. auth_strategy or
  96. os.getenv('OS_AUTH_STRATEGY', 'noauth'),
  97. 'region': region or
  98. os.getenv('OS_REGION_NAME'),
  99. }
  100. if creds['strategy'] == 'keystone' and not creds['auth_url']:
  101. msg = _("--os_auth_url option or OS_AUTH_URL environment variable "
  102. "required when keystone authentication strategy is enabled\n")
  103. raise exception.ClientConfigurationError(msg)
  104. return CacheClient(
  105. host=host,
  106. port=port,
  107. timeout=timeout,
  108. use_ssl=use_ssl,
  109. auth_token=auth_token or
  110. os.getenv('OS_TOKEN'),
  111. creds=creds,
  112. insecure=insecure,
  113. configure_via_auth=False)