Added Hypervisor Client and Model
Created a hypervisor client and model Change-Id: Ibbcdaa97682c27f9b33fac5b731b70f3f792ddb1
This commit is contained in:
69
cloudcafe/compute/hypervisors_api/client.py
Normal file
69
cloudcafe/compute/hypervisors_api/client.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
Copyright 2013 Rackspace
|
||||
|
||||
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 cafe.engine.clients.rest import AutoMarshallingRestClient
|
||||
from cloudcafe.compute.hypervisors_api.model.hypervisors import Hypervisor
|
||||
|
||||
|
||||
class HypervisorsClient(AutoMarshallingRestClient):
|
||||
|
||||
def __init__(self, url, auth_token, serialize_format=None,
|
||||
deserialize_format=None):
|
||||
"""
|
||||
@param url: Base URL for the compute service
|
||||
@type url: String
|
||||
@param auth_token: Auth token to be used for all requests
|
||||
@type auth_token: String
|
||||
@param serialize_format: Format for serializing requests
|
||||
@type serialize_format: String
|
||||
@param deserialize_format: Format for de-serializing responses
|
||||
@type deserialize_format: String
|
||||
"""
|
||||
super(HypervisorsClient, self).__init__(serialize_format,
|
||||
deserialize_format)
|
||||
self.auth_token = auth_token
|
||||
self.default_headers['X-Auth-Token'] = auth_token
|
||||
ct = ''.join(['application/', self.serialize_format])
|
||||
accept = ''.join(['application/', self.deserialize_format])
|
||||
self.default_headers['Content-Type'] = ct
|
||||
self.default_headers['Accept'] = accept
|
||||
self.url = url
|
||||
|
||||
def list_hypervisors(self, requestslib_kwargs=None):
|
||||
"""
|
||||
@summary: Returns a list of hypervisors
|
||||
@return: List of hypervisors
|
||||
@rtype: C{list}
|
||||
"""
|
||||
url = "{url}/os-hypervisors".format(url=self.url)
|
||||
hypervisor_res = self.request('GET', url,
|
||||
response_entity_type=Hypervisor,
|
||||
requestslib_kwargs=requestslib_kwargs)
|
||||
return hypervisor_res
|
||||
|
||||
def list_hypervisor_servers(self, hypervisor_hostname,
|
||||
requestslib_kwargs=None):
|
||||
"""
|
||||
@summary: Returns a list of servers in a hypervisor host
|
||||
@return: List of servers
|
||||
@rtype: C{list}
|
||||
"""
|
||||
url = "{url}/os-hypervisors/{hypervisor_hostname}/servers".\
|
||||
format(url=self.url, hypervisor_hostname=hypervisor_hostname)
|
||||
hypervisor_res = self.request('GET', url,
|
||||
response_entity_type=Hypervisor,
|
||||
requestslib_kwargs=requestslib_kwargs)
|
||||
return hypervisor_res
|
||||
15
cloudcafe/compute/hypervisors_api/model/__init__.py
Normal file
15
cloudcafe/compute/hypervisors_api/model/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
"""
|
||||
Copyright 2013 Rackspace
|
||||
|
||||
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.
|
||||
"""
|
||||
110
cloudcafe/compute/hypervisors_api/model/hypervisors.py
Normal file
110
cloudcafe/compute/hypervisors_api/model/hypervisors.py
Normal file
@@ -0,0 +1,110 @@
|
||||
"""
|
||||
Copyright 2013 Rackspace
|
||||
|
||||
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 json
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from cafe.engine.models.base import AutoMarshallingModel
|
||||
from cloudcafe.compute.common.equality_tools import EqualityTools
|
||||
from cloudcafe.compute.servers_api.models.servers import Server
|
||||
|
||||
|
||||
class Hypervisor(AutoMarshallingModel):
|
||||
|
||||
def __init__(self, id=None, hypervisor_hostname=None,
|
||||
servers=None):
|
||||
super(Hypervisor, self).__init__()
|
||||
self.id = id
|
||||
self.hypervisor_hostname = hypervisor_hostname
|
||||
self.servers = servers
|
||||
|
||||
def __eq__(self, other):
|
||||
"""
|
||||
@summary: Overrides the default equals
|
||||
@param other: Hypervisor object to compare with
|
||||
@type other: Hypervisor
|
||||
@return: True if Host objects are equal, False otherwise
|
||||
@rtype: bool
|
||||
"""
|
||||
return EqualityTools.are_objects_equal(self, other)
|
||||
|
||||
def __ne__(self, other):
|
||||
"""
|
||||
@summary: Overrides the default not-equals
|
||||
@param other: Hypervisor object to compare with
|
||||
@type other: Hypervisor
|
||||
@return: True if Hypervisor objects are not equal, False otherwise
|
||||
@rtype: bool
|
||||
"""
|
||||
return not self.__eq__(other)
|
||||
|
||||
@classmethod
|
||||
def _json_to_obj(cls, serialized_str):
|
||||
"""
|
||||
@summary: Returns an instance of a Hypervisor
|
||||
based on the json serialized_str
|
||||
passed in.
|
||||
@param serialized_str: JSON serialized string
|
||||
@type serialized_str: String
|
||||
@return: List of Hypervisors
|
||||
@rtype: List
|
||||
"""
|
||||
json_dict = json.loads(serialized_str)
|
||||
hypervisors = []
|
||||
for hypervisor_dict in json_dict['hypervisors']:
|
||||
if 'servers' in hypervisor_dict.keys():
|
||||
servers = []
|
||||
for server_dict in hypervisor_dict['servers']:
|
||||
servers.append(Server._dict_to_obj(server_dict))
|
||||
else:
|
||||
servers = None
|
||||
hypervisor_dict.update({"servers": servers})
|
||||
id = hypervisor_dict['id']
|
||||
hypervisor_hostname = hypervisor_dict['hypervisor_hostname']
|
||||
hypervisors.append(Hypervisor(id,
|
||||
hypervisor_hostname,
|
||||
servers))
|
||||
return hypervisors
|
||||
|
||||
@classmethod
|
||||
def _xml_to_obj(cls, serialized_str):
|
||||
"""
|
||||
@summary: Returns an instance of a Hypervisor
|
||||
based on the xml serialized_str
|
||||
passed in.
|
||||
@param serialized_str: XML serialized string
|
||||
@type serialized_str: String
|
||||
@return: List of Hypervisors
|
||||
@rtype: List
|
||||
"""
|
||||
element = ET.fromstring(serialized_str)
|
||||
hypervisors = []
|
||||
for hypervisor in element.findall('hypervisor'):
|
||||
if "servers" in [elem.tag for elem in hypervisor.iter()]:
|
||||
for server in hypervisor.iter('server'):
|
||||
servers = [Server._dict_to_obj(server.attrib)]
|
||||
else:
|
||||
servers = None
|
||||
hypervisor.attrib.update({"servers": servers})
|
||||
|
||||
hypervisor_dict = hypervisor.attrib
|
||||
id = hypervisor_dict['id']
|
||||
hypervisor_hostname = hypervisor_dict['hypervisor_hostname']
|
||||
hypervisors.append(Hypervisor(id,
|
||||
hypervisor_hostname,
|
||||
servers))
|
||||
|
||||
return hypervisors
|
||||
@@ -174,7 +174,7 @@ class Server(AutoMarshallingModel):
|
||||
metadata = Metadata._dict_to_obj(server_dict['metadata'])
|
||||
|
||||
server = Server(
|
||||
id=server_dict.get('id'),
|
||||
id=server_dict.get('id') or server_dict.get('uuid'),
|
||||
disk_config=server_dict.get('OS-DCF:diskConfig'),
|
||||
power_state=server_dict.get('OS-EXT-STS:power_state'),
|
||||
progress=server_dict.get('progress', 0),
|
||||
|
||||
Reference in New Issue
Block a user