Clients and Models for hosts api

Adding HostsClient and Host model
for making host calls to api.

Change-Id: I934029b88803e619ac4ee5c633b32fa24fcdac37
This commit is contained in:
Sumanth Nagadavalli
2013-05-10 11:47:32 +05:30
parent dcd02fb704
commit 4979b46976
4 changed files with 179 additions and 0 deletions

View 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.
"""

View File

@@ -0,0 +1,57 @@
"""
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.hosts_api.models.hosts import \
Host
class HostsClient(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(HostsClient, 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_hosts(self, requestslib_kwargs=None):
"""
@summary: Returns a list of hosts
@return: List of hosts
@rtype: C{list}
"""
url = '%s/os-hosts' % (self.url)
host_response = self.request('GET', url,
response_entity_type=Host,
requestslib_kwargs=requestslib_kwargs)
return host_response

View 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.
"""

View File

@@ -0,0 +1,92 @@
"""
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
class Host(AutoMarshallingModel):
def __init__(self, name, service, zone):
self.name = name
self.service = service
self.zone = zone
def __eq__(self, other):
"""
@summary: Overrides the default equals
@param other: Host object to compare with
@type other: Host
@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: Host object to compare with
@type other: Host
@return: True if Host objects are not equal, False otherwise
@rtype: bool
"""
return EqualityTools.are_not_equal(self, other)
@classmethod
def _json_to_obj(cls, serialized_str):
json_dict = json.loads(serialized_str)
if 'host' in json_dict.keys():
host = cls._dict_to_obj(json_dict['host'])
return host
if 'hosts' in json_dict.keys():
hosts = []
for host_dict in json_dict['hosts']:
hosts.append(cls._dict_to_obj(host_dict['host']))
return hosts
@classmethod
def _dict_to_obj(cls, json_dict):
host = Host(json_dict.get('host_name'), json_dict.get('service'),
json_dict.get('zone'))
return host
@classmethod
def _xml_to_obj(cls, serialized_str):
'''Returns an instance of a Host based on the xml serialized_str
passed in.'''
element = ET.fromstring(serialized_str)
if element.tag == 'host':
host = cls._xml_ele_to_obj(element)
return host
if element.tag == 'hosts':
hosts = []
for host in element.findall('host'):
host = cls._xml_ele_to_obj(host)
hosts.append(host)
return hosts
@classmethod
def _xml_ele_to_obj(cls, element):
host_dict = element.attrib
host = Host(host_dict.get('host_name'), host_dict.get('service'),
host_dict.get('zone'))
return host