~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to nova/endpoint/images.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# Copyright [2010] [Anso Labs, LLC]
 
3
 
4
#    Licensed under the Apache License, Version 2.0 (the "License");
 
5
#    you may not use this file except in compliance with the License.
 
6
#    You may obtain 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,
 
12
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
#    See the License for the specific language governing permissions and
 
14
#    limitations under the License.
 
15
 
 
16
"""
 
17
Proxy AMI-related calls from the cloud controller, to the running 
 
18
objectstore daemon.
 
19
"""
 
20
 
 
21
import json
 
22
import random
 
23
import urllib
 
24
 
 
25
from nova import vendor
 
26
import boto
 
27
import boto.s3
 
28
 
 
29
from nova import flags
 
30
from nova import utils
 
31
 
 
32
FLAGS = flags.FLAGS
 
33
 
 
34
 
 
35
def modify(user, image_id, operation):
 
36
    conn(user).make_request(
 
37
        method='POST',
 
38
        bucket='_images',
 
39
        query_args=qs({'image_id': image_id, 'operation': operation}))
 
40
 
 
41
    return True
 
42
 
 
43
 
 
44
def register(user, image_location):
 
45
    """ rpc call to register a new image based from a manifest """
 
46
 
 
47
    image_id = utils.generate_uid('ami')
 
48
    conn(user).make_request(
 
49
            method='PUT',
 
50
            bucket='_images',
 
51
            query_args=qs({'image_location': image_location,
 
52
                           'image_id': image_id}))
 
53
 
 
54
    return image_id
 
55
 
 
56
 
 
57
def list(user, filter_list=[]):
 
58
    """ return a list of all images that a user can see
 
59
 
 
60
    optionally filtered by a list of image_id """
 
61
 
 
62
    # FIXME: send along the list of only_images to check for
 
63
    response = conn(user).make_request(
 
64
            method='GET',
 
65
            bucket='_images')
 
66
 
 
67
    return json.loads(response.read())
 
68
 
 
69
 
 
70
def deregister(user, image_id):
 
71
    """ unregister an image """
 
72
    conn(user).make_request(
 
73
            method='DELETE',
 
74
            bucket='_images',
 
75
            query_args=qs({'image_id': image_id}))
 
76
 
 
77
 
 
78
def conn(user):
 
79
    return boto.s3.connection.S3Connection (
 
80
        aws_access_key_id=user.access,
 
81
        aws_secret_access_key=user.secret,
 
82
        is_secure=False,
 
83
        calling_format=boto.s3.connection.OrdinaryCallingFormat(),
 
84
        port=FLAGS.s3_port,
 
85
        host=FLAGS.s3_host)
 
86
 
 
87
 
 
88
def qs(params):
 
89
    pairs = []
 
90
    for key in params.keys():
 
91
        pairs.append(key + '=' + urllib.quote(params[key]))
 
92
    return '&'.join(pairs)