1
# Copyright [2010] [Anso Labs, LLC]
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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.
16
Nova User API client library.
20
from boto.ec2.regioninfo import RegionInfo
23
class UserInfo(object):
24
""" Information about a Nova user
30
and an optional field containing a zip with X509 cert & rc
34
def __init__(self, connection=None, username=None, endpoint=None):
35
self.connection = connection
36
self.username = username
37
self.endpoint = endpoint
40
return 'UserInfo:%s' % self.username
42
def startElement(self, name, attrs, connection):
45
def endElement(self, name, value, connection):
46
if name == 'username':
47
self.username = str(value)
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)
56
class NovaAdminClient(object):
57
def __init__(self, clc_ip='127.0.0.1', region='nova', access_key='admin',
58
secret_key='admin', **kwargs):
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,
66
region=RegionInfo(None, region, clc_ip),
68
path='/services/Admin',
70
self.apiconn.APIVersion = 'nova'
72
def connection_for(self, username, **kwargs):
74
Returns a boto ec2 connection for the given username.
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,
81
region=RegionInfo(None, self.region, self.clc_ip),
83
path='/services/Cloud',
88
""" grabs the list of all users """
89
return self.apiconn.get_list('DescribeUsers', {}, (['item', UserInfo]))
91
def get_user(self, name):
92
""" grab a single user by name """
93
user = self.apiconn.get_object('DescribeUser', {'Name': name}, UserInfo)
95
if user.username != None:
98
def has_user(self, username):
99
""" determine if user exists """
100
return self.get_user(username) != None
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)
106
def delete_user(self, username):
107
""" deletes a user """
108
return self.apiconn.get_object('DeregisterUser', {'Name': username}, UserInfo)
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