Merge branch 'master' of http://github.com/rackspace/python-novaclient into lazyload-limit

Conflicts:
	novaclient/base.py
This commit is contained in:
Brian Waldon 2011-08-31 09:30:09 -04:00
commit db38b4c830
12 changed files with 439 additions and 27 deletions

@ -157,6 +157,14 @@ Quick-start using keystone::
[...]
>>> nt.keypairs.list()
[...]
# if you want to use the keystone api to modify users/tenants:
>>> from novaclient import client
>>> conn = client.HTTPClient(USER, PASS, TENANT, KEYSTONE_URL)
>>> from novaclient import keystone
>>> kc = keystone.Client(conn.client)
>>> kc.tenants.list()
[...]
What's new?
-----------

@ -68,9 +68,12 @@ class Manager(object):
if obj_class is None:
obj_class = self.resource_class
objects = [obj_class(self, res, loaded=True) \
for res in body[response_key] if res]
return objects
data = body[response_key]
# NOTE(ja): keystone returns values as list as {'values': [ ... ]}
# unlike other services which just return the list...
if type(data) is dict:
data = data['values']
return [obj_class(self, res, loaded=True) for res in data if res]
def _get(self, url, response_key):
resp, body = self.api.client.get(url)
@ -234,7 +237,11 @@ class Resource(object):
return "<%s %s>" % (self.__class__.__name__, info)
def get(self):
if not hasattr(self.manager, 'get'):
return
self.set_loaded(True)
new = self.manager.get(self.id)
if new:
self._add_details(new._info)

@ -11,6 +11,8 @@ import urllib
import httplib2
import logging
from novaclient import service_catalog
try:
import json
except ImportError:
@ -181,14 +183,14 @@ class HTTPClient(httplib2.Http):
if resp.status == 200: # content must always present
try:
self.management_url = body["auth"]["serviceCatalog"] \
["nova"][0]["publicURL"]
self.auth_token = body["auth"]["token"]["id"]
self.auth_url = url
self.service_catalog = \
service_catalog.ServiceCatalog(body)
self.auth_token = self.service_catalog.token.id
self.management_url = self.service_catalog.url_for('nova',
'public')
except KeyError:
raise exceptions.AuthorizationFailure()
#TODO(chris): Implement service_catalog
self.service_catalog = None
elif resp.status == 305:
return resp['location']
else:

@ -0,0 +1,2 @@
from novaclient.keystone.client import Client

@ -0,0 +1,65 @@
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import copy
from novaclient.keystone import tenants
from novaclient.keystone import users
class Client(object):
"""
Top-level object to access the OpenStack Keystone API.
Create an instance with your creds::
>>> from novaclient import client
>>> conn = client.HTTPClient(USER, PASS, TENANT, KEYSTONE_URL)
>>> from novaclient import keystone
>>> kc = keystone.Client(conn)
Then call methods on its managers::
>>> kc.tenants.list()
...
>>> kc.users.list()
...
"""
def __init__(self, client):
# FIXME(ja): managers work by making calls against self.client
# which assumes management_url is set for the service.
# with keystone you get a token/endpoints for multiple
# services - so we have to clone and override the endpoint
# NOTE(ja): need endpoint from service catalog... no lazy auth
client.authenticate()
self.client = copy.copy(client)
endpoint = client.service_catalog.url_for('identity', 'admin')
self.client.management_url = endpoint
self.tenants = tenants.TenantManager(self)
self.users = users.UserManager(self)
def authenticate(self):
"""
Authenticate against the server.
Normally this is called automatically when you first access the API,
but you can call this method to force authentication right now.
Returns on success; raises :exc:`exceptions.Unauthorized` if the
credentials are wrong.
"""
self.client.authenticate()

@ -0,0 +1,93 @@
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from novaclient import base
class RoleRefs(base.Resource):
def __repr__(self):
return "<Roleref %s>" % self._info
class Tenant(base.Resource):
def __repr__(self):
return "<Tenant %s>" % self._info
def delete(self):
self.manager.delete(self)
def update(self, description=None, enabled=None):
# FIXME(ja): set the attributes in this object if successful
self.manager.update(self.id, description, enabled)
def add_user(self, user):
self.manager.add_user_to_tenant(self.id, base.getid(user))
class TenantManager(base.ManagerWithFind):
resource_class = Tenant
def get(self, tenant_id):
return self._get("/tenants/%s" % tenant_id, "tenant")
# FIXME(ja): finialize roles once finalized in keystone
# right now the only way to add/remove a tenant is to
# give them a role within a project
def get_user_role_refs(self, user_id):
return self._get("/users/%s/roleRefs" % user_id, "roleRefs")
def add_user_to_tenant(self, tenant_id, user_id):
params = {"roleRef": {"tenantId": tenant_id, "roleId": "Member"}}
return self._create("/users/%s/roleRefs" % user_id, params, "roleRef")
def remove_user_from_tenant(self, tenant_id, user_id):
params = {"roleRef": {"tenantId": tenant_id, "roleId": "Member"}}
# FIXME(ja): we have to get the roleref? what is 5?
return self._delete("/users/%s/roleRefs/5" % user_id)
def create(self, tenant_id, description=None, enabled=True):
"""
Create a new tenant.
"""
params = {"tenant": {"id": tenant_id,
"description": description,
"enabled": enabled}}
return self._create('/tenants', params, "tenant")
def list(self):
"""
Get a list of tenants.
:rtype: list of :class:`Tenant`
"""
return self._list("/tenants", "tenants")
def update(self, tenant_id, description=None, enabled=None):
"""
update a tenant with a new name and description
"""
body = {"tenant": {'id': tenant_id }}
if enabled is not None:
body['tenant']['enabled'] = enabled
if description:
body['tenant']['description'] = description
self._update("/tenants/%s" % tenant_id, body)
def delete(self, tenant):
"""
Delete a tenant.
"""
self._delete("/tenants/%s" % (base.getid(tenant)))

@ -0,0 +1,105 @@
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from novaclient import base
class User(base.Resource):
def __repr__(self):
return "<User %s>" % self._info
def delete(self):
self.manager.delete(self)
class UserManager(base.ManagerWithFind):
resource_class = User
def get(self, user):
return self._get("/users/%s" % base.getid(user), "user")
def update_email(self, user, email):
"""
Update email
"""
# FIXME(ja): why do we have to send id in params and url?
params = {"user": {"id": base.getid(user),
"email": email }}
self._update("/users/%s" % base.getid(user), params)
def update_enabled(self, user, enabled):
"""
Update enabled-ness
"""
params = {"user": {"id": base.getid(user),
"enabled": enabled }}
self._update("/users/%s/enabled" % base.getid(user), params)
def update_password(self, user, password):
"""
Update password
"""
params = {"user": {"id": base.getid(user),
"password": password }}
self._update("/users/%s/password" % base.getid(user), params)
def update_tenant(self, user, tenant):
"""
Update default tenant.
"""
params = {"user": {"id": base.getid(user),
"tenantId": base.getid(tenant) }}
# FIXME(ja): seems like a bad url - default tenant is an attribute
# not a subresource!???
self._update("/users/%s/tenant" % base.getid(user), params)
def create(self, user_id, password, email, tenant_id=None, enabled=True):
"""
Create a user.
"""
# FIXME(ja): email should be optional but keystone currently requires it
params = {"user": {"id": user_id,
"password": password,
"tenantId": tenant_id,
"email": email,
"enabled": enabled}}
return self._create('/users', params, "user")
def _create(self, url, body, response_key):
# NOTE(ja): since we post the id, we have to use a PUT instead of POST
resp, body = self.api.client.put(url, body=body)
return self.resource_class(self, body[response_key])
def delete(self, user):
"""
Delete a user.
"""
self._delete("/users/%s" % base.getid(user))
def list(self, tenant_id=None):
"""
Get a list of users (optionally limited to a tenant)
:rtype: list of :class:`User`
"""
if not tenant_id:
return self._list("/users", "users")
else:
return self._list("/tenants/%s/users" % tenant_id, "users")

@ -0,0 +1,97 @@
# Copyright 2011, Piston Cloud Computing, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
def get_resource(key):
resource_classes = {
"glance": GlanceCatalog,
"identity": IdentityCatalog,
"keystone": KeystoneCatalog,
"nova": NovaCatalog,
"nova_compat": NovaCompatCatalog,
"swift": SwiftCatalog,
"serviceCatalog": ServiceCatalog,
}
return resource_classes.get(key)
def snake_case(string):
return re.sub(r'([a-z])([A-Z])', r'\1_\2', string).lower()
class CatalogResource(object):
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.public_url)
def __init__(self, resource_dict):
for key, value in resource_dict.items():
if self.__catalog_key__ in value:
for res_key, res_value in value.items():
for attr, val in res_value.items():
res = get_resource(attr)
if res:
attribute = [res(x) for x in val]
setattr(self, snake_case(attr), attribute)
else:
for key, attr in resource_dict.items():
setattr(self, snake_case(key), attr)
class TokenCatalog(CatalogResource):
__catalog_key__ = "token"
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.id)
class NovaCatalog(CatalogResource):
__catalog_key__ = "nova"
class KeystoneCatalog(CatalogResource):
__catalog_key__ = 'keystone'
class GlanceCatalog(CatalogResource):
__catalog_key__ = 'glance'
class SwiftCatalog(CatalogResource):
__catalog_key__ = 'swift'
class IdentityCatalog(CatalogResource):
__catalog_key__ = 'identity'
class NovaCompatCatalog(CatalogResource):
__catalog_key__ = "nova_compat"
class ServiceCatalog(CatalogResource):
__catalog_key__ = "serviceCatalog"
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.token.id)
def __init__(self, resource):
super(ServiceCatalog, self).__init__(resource)
self.token = TokenCatalog(resource["auth"]["token"])
def url_for(self, catalog_class, url):
catalog = getattr(self, catalog_class)
if catalog:
return getattr(catalog[0], url + "_url")

@ -28,9 +28,15 @@ class Keypair(base.Resource):
def __repr__(self):
return "<Keypair: %s>" % self.uuid
def _add_details(self, info):
dico = 'keypair' in info and \
info['keypair'] or info
for (k, v) in dico.items():
setattr(self, k, v)
@property
def uuid(self):
return self._info['keypair']['name']
return self.name
def delete(self):
self.manager.delete(self)

@ -569,18 +569,23 @@ def do_zone_info(cs, args):
@utils.arg('api_url', metavar='<api_url>', help="URL for the Zone's API")
@utils.arg('zone_username', metavar='<zone_username>',
help='Authentication username.')
@utils.arg('password', metavar='<password>', help='Authentication password.')
@utils.arg('weight_offset', metavar='<weight_offset>',
help='Child Zone weight offset (typically 0.0).')
@utils.arg('weight_scale', metavar='<weight_scale>',
help='Child Zone weight scale (typically 1.0).')
@utils.arg('--zone_username', metavar='<zone_username>',
help='Optional Authentication username. (Default=None)',
default=None)
@utils.arg('--password', metavar='<password>',
help='Authentication password. (Default=None)',
default=None)
@utils.arg('--weight_offset', metavar='<weight_offset>',
help='Child Zone weight offset (Default=0.0))',
default=0.0)
@utils.arg('--weight_scale', metavar='<weight_scale>',
help='Child Zone weight scale (Default=1.0).',
default=1.0)
def do_zone_add(cs, args):
"""Add a new child zone."""
zone = cs.zones.create(args.api_url, args.zone_username,
args.password, args.weight_offset,
args.weight_scale)
args.password, args.weight_offset,
args.weight_scale)
utils.print_dict(zone._info)

@ -95,7 +95,7 @@ class ZoneManager(local_base.BootingManagerWithFind):
detail = "/detail"
return self._list("/zones%s" % detail, "zones")
def create(self, api_url, username, password,
def create(self, api_url, username=None, password=None,
weight_offset=0.0, weight_scale=1.0):
"""
Create a new child zone.
@ -106,14 +106,12 @@ class ZoneManager(local_base.BootingManagerWithFind):
:param weight_offset: The child zone's weight offset.
:param weight_scale: The child zone's weight scale.
"""
body = {"zone": {
"api_url": api_url,
"username": username,
"password": password,
"weight_offset": weight_offset,
"weight_scale": weight_scale
}}
body = {"zone": {"api_url": api_url,
"weight_offset": weight_offset,
"weight_scale": weight_scale,
"username": username,
"password": password}}
return self._create("/zones", body, "zone")
def boot(self, name, image, flavor, meta=None, files=None,

@ -0,0 +1,24 @@
from novaclient import service_catalog
from tests import utils
SERVICE_CATALOG = {'auth': {'token': {'id': "FAKE_ID", },
'serviceCatalog': {
"nova": [{"publicURL": "http://fakeurl"}],
"glance": [{"publicURL": "http://fakeurl"}],
"swift": [{"publicURL": "http://fakeurl"}],
"identity": [{"publicURL": "http://fakeurl"}],
}}}
class ServiceCatalogTest(utils.TestCase):
def test_building_a_service_catalog(self):
sc = service_catalog.ServiceCatalog(SERVICE_CATALOG)
self.assertEqual(sc.__repr__(), "<ServiceCatalog: FAKE_ID>")
self.assertEqual(sc.token.__repr__(), "<TokenCatalog: FAKE_ID>")
self.assertEqual(sc.nova.__repr__(), "[<NovaCatalog: http://fakeurl>]")
self.assertEqual(sc.token.id, "FAKE_ID")
self.assertEqual(sc.url_for('nova', 'public'),
SERVICE_CATALOG['auth']['serviceCatalog']['nova'][0]['publicURL'])