~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to nova/adminclient.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
# Copyright [2010] [Anso Labs, LLC]
 
2
#
 
3
#    Licensed under the Apache License, Version 2.0 (the "License");
 
4
#    you may not use this file except in compliance with the License.
 
5
#    You may obtain a copy of the License at
 
6
#
 
7
#        http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
#    Unless required by applicable law or agreed to in writing, software
 
10
#    distributed under the License is distributed on an "AS IS" BASIS,
 
11
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
#    See the License for the specific language governing permissions and
 
13
#    limitations under the License.
 
14
 
 
15
"""
 
16
Nova User API client library.
 
17
"""
 
18
 
 
19
import boto
 
20
from boto.ec2.regioninfo import RegionInfo
 
21
import base64
 
22
 
 
23
class UserInfo(object):
 
24
    """ Information about a Nova user
 
25
    fields include:
 
26
        username
 
27
        accesskey
 
28
        secretkey
 
29
 
 
30
    and an optional field containing a zip with X509 cert & rc
 
31
        file
 
32
    """
 
33
 
 
34
    def __init__(self, connection=None, username=None, endpoint=None):
 
35
        self.connection = connection
 
36
        self.username = username
 
37
        self.endpoint = endpoint
 
38
 
 
39
    def __repr__(self):
 
40
        return 'UserInfo:%s' % self.username
 
41
 
 
42
    def startElement(self, name, attrs, connection):
 
43
        return None
 
44
 
 
45
    def endElement(self, name, value, connection):
 
46
        if name == 'username':
 
47
            self.username = str(value)
 
48
        elif name == 'file':
 
49
            self.file = base64.b64decode(str(value))
 
50
        elif name == 'accesskey':
 
51
            self.accesskey = str(value)
 
52
        elif name == 'secretkey':
 
53
            self.secretkey = str(value)
 
54
 
 
55
 
 
56
class NovaAdminClient(object):
 
57
    def __init__(self, clc_ip='127.0.0.1', region='nova', access_key='admin',
 
58
                 secret_key='admin', **kwargs):
 
59
        self.clc_ip = clc_ip
 
60
        self.region = region
 
61
        self.access = access_key
 
62
        self.secret = secret_key
 
63
        self.apiconn = boto.connect_ec2(aws_access_key_id=access_key,
 
64
                                        aws_secret_access_key=secret_key,
 
65
                                        is_secure=False,
 
66
                                        region=RegionInfo(None, region, clc_ip),
 
67
                                        port=8773,
 
68
                                        path='/services/Admin',
 
69
                                        **kwargs)
 
70
        self.apiconn.APIVersion = 'nova'
 
71
 
 
72
    def connection_for(self, username, **kwargs):
 
73
        """
 
74
        Returns a boto ec2 connection for the given username.
 
75
        """
 
76
        user = self.get_user(username)
 
77
        return boto.connect_ec2(
 
78
            aws_access_key_id=user.accesskey,
 
79
            aws_secret_access_key=user.secretkey,
 
80
            is_secure=False,
 
81
            region=RegionInfo(None, self.region, self.clc_ip),
 
82
            port=8773,
 
83
            path='/services/Cloud',
 
84
            **kwargs
 
85
        )
 
86
 
 
87
    def get_users(self):
 
88
        """ grabs the list of all users """
 
89
        return self.apiconn.get_list('DescribeUsers', {}, (['item', UserInfo]))
 
90
 
 
91
    def get_user(self, name):
 
92
        """ grab a single user by name """
 
93
        user = self.apiconn.get_object('DescribeUser', {'Name': name}, UserInfo)
 
94
 
 
95
        if user.username != None:
 
96
            return user
 
97
 
 
98
    def has_user(self, username):
 
99
        """ determine if user exists """
 
100
        return self.get_user(username) != None
 
101
 
 
102
    def create_user(self, username):
 
103
        """ creates a new user, returning the userinfo object with access/secret """
 
104
        return self.apiconn.get_object('RegisterUser', {'Name': username}, UserInfo)
 
105
 
 
106
    def delete_user(self, username):
 
107
        """ deletes a user """
 
108
        return self.apiconn.get_object('DeregisterUser', {'Name': username}, UserInfo)
 
109
 
 
110
    def get_zip(self, username):
 
111
        """ returns the content of a zip file containing novarc and access credentials. """
 
112
        return self.apiconn.get_object('GenerateX509ForUser', {'Name': username}, UserInfo).file
 
113