Added client and model for the UsedLimitsForAdmin extension

Created client and model to describe the UsedLimitsForAdmin extension
Changed the location to extensions under compute

Change-Id: Ia8e275664ebd7c1aaf8d75403850b75d7c1d6140
This commit is contained in:
Sumanth Nagadavalli 2013-07-03 16:33:27 +05:30
parent 55aea4c8b2
commit 661925b0a3
5 changed files with 280 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,58 @@
"""
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.extensions.used_limits.model.used_limits \
import UsedLimits
class UsedLimitsClient(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(UsedLimitsClient, self).__init__(serialize_format,
deserialize_format)
self.auth_token = auth_token
self.default_headers['X-Auth-Token'] = auth_token
ct = 'application/{0}'.format(self.serialize_format)
accept = 'application/{0}'.format(self.serialize_format)
self.default_headers['Content-Type'] = ct
self.default_headers['Accept'] = accept
self.url = url
def get_used_limits_for_user(self, user_tenant_id,
requestslib_kwargs=None):
"""
@summary: Returns the used limits of a particular tenant
@return: Used Limits Response
@rtype: Response
"""
url = "{url}/limits?tenant_id={user_tenant_id}".\
format(url=self.url, user_tenant_id=user_tenant_id)
used_limits_res = self.request('GET', url,
response_entity_type=UsedLimits,
requestslib_kwargs=requestslib_kwargs)
return used_limits_res

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,72 @@
"""
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.models.base import AutoMarshallingModel
class Absolute(AutoMarshallingModel):
def __init__(self, max_server_meta=None, max_personality=None,
max_image_meta=None, max_personality_size=None,
max_security_group_rules=None, max_total_keypairs=None,
total_ram_used=None, total_instances_used=None,
max_security_groups=None, total_floating_ips_used=None,
max_total_cores=None, total_security_groups_used=None,
max_total_floating_ips=None, max_total_instances=None,
total_cores_used=None, max_total_ram_size=None):
super(Absolute, self).__init__()
self.max_server_meta = max_server_meta
self.max_personality = max_personality
self.max_image_meta = max_image_meta
self.max_personality_size = max_personality_size
self.max_security_group_rules = max_security_group_rules
self.max_total_key_pairs = max_total_keypairs
self.total_ram_used = total_ram_used
self.total_instances_used = total_instances_used
self.max_security_groups = max_security_groups
self.total_floating_ips_used = total_floating_ips_used
self.max_total_cores = max_total_cores
self.total_security_groups_used = total_security_groups_used
self.max_total_floating_ips = max_total_floating_ips
self.max_total_instances = max_total_instances
self.total_cores_used = total_cores_used
self.max_total_ram_size = max_total_ram_size
@classmethod
def _dict_to_obj(cls, absolute_dict):
return Absolute(absolute_dict.get('maxServerMeta'),
absolute_dict.get('maxPersonality'),
absolute_dict.get('maxImageMeta'),
absolute_dict.get('maxPersonalitySize'),
absolute_dict.get('maxSecurityGroupRules'),
absolute_dict.get('maxTotalKeypairs'),
absolute_dict.get('totalRAMUsed'),
absolute_dict.get('totalInstancesUsed'),
absolute_dict.get('maxSecurityGroups'),
absolute_dict.get('totalFloatingIpsUsed'),
absolute_dict.get('maxTotalCores'),
absolute_dict.get('totalSecurityGroupsUsed'),
absolute_dict.get('maxTotalFloatingIps'),
absolute_dict.get('maxTotalInstances'),
absolute_dict.get('totalCoresUsed'),
absolute_dict.get('maxTotalRAMSize'))
@classmethod
def _xml_ele_to_obj(cls, absolute_xml):
limit = absolute_xml.findall('limit')
absolute_dict = dict((l.attrib['name'], l.attrib['value'])
for l in limit)
return cls._dict_to_obj(absolute_dict)

View File

@ -0,0 +1,120 @@
"""
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 cloudcafe.compute.common.equality_tools import EqualityTools
from cafe.engine.models.base import AutoMarshallingModel
from cloudcafe.compute.extensions.used_limits.model.absolute import Absolute
class Rate(AutoMarshallingModel):
def __init__(self, regex=None,
limit=None, uri=None):
super(Rate, self).__init__()
self.regex = regex
self.limit = limit
self.uri = uri
@classmethod
def _dict_to_obj(cls, rate_dict):
rate = rate_dict[0]
limits = []
for limit in rate.get('limit'):
limits.append(Limit._dict_to_obj(limit))
return Rate(rate.get('regex'), limits,
rate.get('uri'))
@classmethod
def _xml_ele_to_obj(cls, rates):
rate = rates.findall('rate')[0]
limits = []
for limit in rate.findall('limit'):
limits.append(Limit._dict_to_obj(limit.attrib))
return Rate(rate.attrib.get('regex'), limits,
rate.attrib.get('uri'))
class Limit(AutoMarshallingModel):
def __init__(self, next_available=None,
unit=None, verb=None,
remaining=None, value=None):
super(Limit, self).__init__()
self.next_available = next_available
self.unit = unit
self.verb = verb
self.remaining = remaining
self.value = value
@classmethod
def _dict_to_obj(cls, limit_dict):
return Limit(limit_dict.get('next-available'),
limit_dict.get('unit'), limit_dict.get('verb'),
limit_dict.get('remaining'), limit_dict.get('value'))
class UsedLimits(AutoMarshallingModel):
def __init__(self, rate=None, absolute=None):
super(UsedLimits, self).__init__()
self.rate = rate
self.absolute = absolute
def __eq__(self, other):
"""
@summary: Overrides the default equals
@param other: UsedLimits object to compare with
@type other: UsedLimits
@return: True if UsedLimits 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: UsedLimits object to compare with
@type other: UsedLimits
@return: True if UsedLimits objects are not equal, False otherwise
@rtype: bool
"""
return not self.__eq__(other)
@classmethod
def _json_to_obj(cls, serialized_str):
json_dict = json.loads(serialized_str)
rate_dict = json_dict.get('limits').get('rate')
rates = Rate._dict_to_obj(rate_dict)
absolute_dict = json_dict.get('limits').get('absolute')
absolute = Absolute._dict_to_obj(absolute_dict)
usedLimitsForAdmin = UsedLimits(rates, absolute)
return usedLimitsForAdmin
@classmethod
def _xml_to_obj(cls, serialized_str):
limits = ET.fromstring(serialized_str)
rates_xml = limits.find('rates')
rates = Rate._xml_ele_to_obj(rates_xml)
absolute_xml = limits.find('absolute')
absolute = Absolute._xml_ele_to_obj(absolute_xml)
usedLimitsForAdmin = UsedLimits(rates, absolute)
return usedLimitsForAdmin