1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 OpenStack, LLC
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
Simple client class to speak with any RESTful service that implements
20
the Glance Registry API
30
from glance.common import exception
31
from glance.client import BaseClient
34
class RegistryClient(BaseClient):
36
"""A client for the Registry image metadata service"""
40
def __init__(self, host, port=None, use_ssl=False):
42
Creates a new client to a Glance Registry service.
44
:param host: The host where Glance resides
45
:param port: The port where Glance resides (defaults to 9191)
46
:param use_ssl: Should we use HTTPS? (defaults to False)
49
port = port or self.DEFAULT_PORT
50
super(RegistryClient, self).__init__(host, port, use_ssl)
54
Returns a list of image id/name mappings from Registry
56
res = self.do_request("GET", "/images")
57
data = json.loads(res.read())['images']
60
def get_images_detailed(self):
62
Returns a list of detailed image data mappings from Registry
64
res = self.do_request("GET", "/images/detail")
65
data = json.loads(res.read())['images']
68
def get_image(self, image_id):
70
Returns a mapping of image metadata from Registry
72
:raises exception.NotFound if image is not in registry
74
res = self.do_request("GET", "/images/%s" % image_id)
75
data = json.loads(res.read())['image']
78
def add_image(self, image_metadata):
80
Tells registry about an image's metadata
82
if 'image' not in image_metadata.keys():
83
image_metadata = dict(image=image_metadata)
84
body = json.dumps(image_metadata)
85
res = self.do_request("POST", "/images", body)
86
# Registry returns a JSONified dict(image=image_info)
87
data = json.loads(res.read())
90
def update_image(self, image_id, image_metadata):
92
Updates Registry's information about an image
94
if 'image' not in image_metadata.keys():
95
image_metadata = dict(image=image_metadata)
97
body = json.dumps(image_metadata)
99
res = self.do_request("PUT", "/images/%s" % image_id, body)
100
data = json.loads(res.read())
101
image = data['image']
104
def delete_image(self, image_id):
106
Deletes Registry's information about an image
108
self.do_request("DELETE", "/images/%s" % image_id)