~jaypipes/glance/api-add-image

« back to all changes in this revision

Viewing changes to glance/registry/client.py

  • Committer: Tarmac
  • Author(s): jaypipes at gmail
  • Date: 2010-12-17 16:37:06 UTC
  • mfrom: (24.1.9 refactoring)
  • Revision ID: tarmac-20101217163706-6lvp8gu58tifeksv
Major refactoring...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 OpenStack, LLC
 
4
# All Rights Reserved.
 
5
#
 
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
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
16
#    under the License.
 
17
 
 
18
"""
 
19
Simple client class to speak with any RESTful service that implements
 
20
the Glance Registry API
 
21
"""
 
22
 
 
23
import httplib
 
24
import json
 
25
import logging
 
26
import urlparse
 
27
import socket
 
28
import sys
 
29
 
 
30
from glance.common import exception
 
31
from glance.client import BaseClient
 
32
 
 
33
 
 
34
class RegistryClient(BaseClient):
 
35
 
 
36
    """A client for the Registry image metadata service"""
 
37
 
 
38
    DEFAULT_ADDRESS = 'http://127.0.0.1'
 
39
    DEFAULT_PORT = 9191
 
40
 
 
41
    def __init__(self, **kwargs):
 
42
        """
 
43
        Creates a new client to a Registry service.  All args are keyword
 
44
        arguments.
 
45
 
 
46
        :param address: The address where Registry resides (defaults to
 
47
                        http://127.0.0.1)
 
48
        :param port: The port where Registry resides (defaults to 9191)
 
49
        """
 
50
        super(RegistryClient, self).__init__(**kwargs)
 
51
 
 
52
    def get_images(self):
 
53
        """
 
54
        Returns a list of image id/name mappings from Registry
 
55
        """
 
56
        res = self.do_request("GET", "/images")
 
57
        data = json.loads(res.read())['images']
 
58
        return data
 
59
 
 
60
    def get_images_detailed(self):
 
61
        """
 
62
        Returns a list of detailed image data mappings from Registry
 
63
        """
 
64
        res = self.do_request("GET", "/images/detail")
 
65
        data = json.loads(res.read())['images']
 
66
        return data
 
67
 
 
68
    def get_image(self, image_id):
 
69
        """
 
70
        Returns a mapping of image metadata from Registry
 
71
 
 
72
        :raises exception.NotFound if image is not in registry
 
73
        """
 
74
        res = self.do_request("GET", "/images/%s" % image_id)
 
75
        data = json.loads(res.read())['image']
 
76
        return data
 
77
 
 
78
    def add_image(self, image_metadata):
 
79
        """
 
80
        Tells registry about an image's metadata
 
81
        """
 
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())
 
88
        return data['image']
 
89
 
 
90
    def update_image(self, image_id, image_metadata):
 
91
        """
 
92
        Updates Registry's information about an image
 
93
        """
 
94
        if 'image' not in image_metadata.keys():
 
95
            image_metadata = dict(image=image_metadata)
 
96
        body = json.dumps(image_metadata)
 
97
        self.do_request("PUT", "/images/%s" % image_id, body)
 
98
        return True
 
99
 
 
100
    def delete_image(self, image_id):
 
101
        """
 
102
        Deletes Registry's information about an image
 
103
        """
 
104
        self.do_request("DELETE", "/images/%s" % image_id)
 
105
        return True