zone select

This commit is contained in:
Sandy Walsh 2011-05-23 12:08:42 -07:00
parent f216dbd2e4
commit 2ec3c3febd
2 changed files with 48 additions and 2 deletions
novaclient

@ -1,4 +1,20 @@
# Copyright 2010 Jacob Kaplan-Moss
# 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.
"""
Base utilities to build API operation managers and objects on top of.
"""
@ -23,10 +39,14 @@ class Manager(object):
def __init__(self, api):
self.api = api
def _list(self, url, response_key):
def _list(self, url, response_key, obj_class=None):
resp, body = self.api.client.get(url)
return [self.resource_class(self, res) for res in body[response_key]]
if obj_class is None:
obj_class = self.resource_class
return [obj_class(self, res)
for res in body[response_key] if res]
def _get(self, url, response_key):
resp, body = self.api.client.get(url)
return self.resource_class(self, body[response_key])

@ -20,6 +20,19 @@ Zone interface.
from novaclient import base
class Weighting(base.Resource):
def __init__(self, manager, info):
self.name = "n/a"
super(Weighting, self).__init__(manager, info)
def __repr__(self):
return "<Weighting: %s>" % self.name
def to_dict(self):
"""Return the original info setting, which is a dict."""
return self._info
class Zone(base.Resource):
def __init__(self, manager, info):
self.name = "n/a"
@ -90,6 +103,19 @@ class ZoneManager(base.ManagerWithFind):
return self._create("/zones", body, "zone")
def select(self, *args, **kwargs):
"""
Given requirements for a new instance, select hosts
in this zone that best match those requirements.
"""
# 'specs' may be passed in as None, so change to an empty dict.
specs = kwargs.get("specs", "")
url = "/zones/select"
if specs:
url = "%s?%s" % (url, specs)
weighting_list = self._list(url, "weights", Weighting)
return [wt.to_dict() for wt in weighting_list]
def delete(self, zone):
"""
Delete a child zone.