From f60dc8658f801088eefbc8aa66802bec44930036 Mon Sep 17 00:00:00 2001 From: Jesse Andrews Date: Sun, 24 Apr 2011 23:30:35 -0700 Subject: pythonizing --- echo/echo/echo.py | 5 +- echo/echo_client.py | 22 ++++---- keystone/auth_protocol/auth_protocol_token.py | 2 +- keystone/logic/types/auth.py | 2 +- keystone/logic/types/fault.py | 2 +- keystone/logic/types/tenant.py | 72 ++++++++------------------- 6 files changed, 39 insertions(+), 66 deletions(-) diff --git a/echo/echo/echo.py b/echo/echo/echo.py index 5b6b571b..1219f877 100644 --- a/echo/echo/echo.py +++ b/echo/echo/echo.py @@ -20,12 +20,9 @@ import sys import eventlet from eventlet import wsgi #from httplib2 import Http +import json from lxml import etree from paste.deploy import loadapp -try: - import simplejson as json -except ImportError: - import json import urllib # If ../echo/__init__.py exists, add ../ to Python search path, so that diff --git a/echo/echo_client.py b/echo/echo_client.py index 61f0fbff..fc4b62fa 100644 --- a/echo/echo_client.py +++ b/echo/echo_client.py @@ -18,21 +18,26 @@ Implement a client for Echo service using Identity service """ import httplib -import simplejson +import json def get_auth_token(username, password, tenant): headers = {"Content-type": "application/json", "Accept": "text/json"} - params = '{"passwordCredentials": { "username": "' + username + '", "password": "' + password + '", "tenantId": "1"}}' + params = {"passwordCredentials": {"username": username, + "password": password, + "tenantId": "1"}} conn = httplib.HTTPConnection("localhost:8080") - conn.request("POST", "/v1.0/token", params, headers=headers) + conn.request("POST", "/v1.0/token", json.dumps(params), headers=headers) response = conn.getresponse() data = response.read() ret = data return ret + def call_service(token): - headers = {"X-Auth-Token": token, "Content-type": "application/json", "Accept": "text/json"} + headers = {"X-Auth-Token": token, + "Content-type": "application/json", + "Accept": "text/json"} params = '{"ping": "abcdefg"}' conn = httplib.HTTPConnection("localhost:8090") conn.request("POST", "/", params, headers=headers) @@ -42,13 +47,12 @@ def call_service(token): return ret if __name__ == '__main__': - # Call the keystone service to get a token (assumes the test_setup.sql script has loaded this user) + # Call the keystone service to get a token + # NOTE: assumes the test_setup.sql script has loaded this user auth = get_auth_token("joeuser", "secrete", "1") - obj = simplejson.loads(auth) + obj = json.loads(auth) token = obj["auth"]["token"]["id"] print "Token:", token - - # Use that token to call an OpenStack service (we're using the Echo sample service for now) + # Use that token to call an OpenStack service (echo) data = call_service(token) print "Response:", data - \ No newline at end of file diff --git a/keystone/auth_protocol/auth_protocol_token.py b/keystone/auth_protocol/auth_protocol_token.py index b330b376..dc35bf32 100644 --- a/keystone/auth_protocol/auth_protocol_token.py +++ b/keystone/auth_protocol/auth_protocol_token.py @@ -15,7 +15,7 @@ # limitations under the License. # Not Yet PEP8 standardized -import simplejson as json +import json from webob.exc import HTTPUnauthorized, Request from keystone.common.bufferedhttp import http_connect_raw as http_connect diff --git a/keystone/logic/types/auth.py b/keystone/logic/types/auth.py index 69f285d3..b73876d6 100644 --- a/keystone/logic/types/auth.py +++ b/keystone/logic/types/auth.py @@ -16,7 +16,7 @@ from datetime import datetime from abc import ABCMeta -import simplejson as json +import json import keystone.logic.types.fault as fault from lxml import etree diff --git a/keystone/logic/types/fault.py b/keystone/logic/types/fault.py index bd5b970b..4591d92c 100644 --- a/keystone/logic/types/fault.py +++ b/keystone/logic/types/fault.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import simplejson as json +import json from lxml import etree diff --git a/keystone/logic/types/tenant.py b/keystone/logic/types/tenant.py index e8a689dc..cad1c3af 100644 --- a/keystone/logic/types/tenant.py +++ b/keystone/logic/types/tenant.py @@ -13,22 +13,22 @@ # See the License for the specific language governing permissions and # limitations under the License. -import string as s -import simplejson as json +import json import keystone.logic.types.fault as fault from lxml import etree +import string class Tenant(object): "Describes a tenant in the auth system" def __init__(self, tenant_id, description, enabled): - self.__tenant_id = tenant_id - self.__description = description + self.tenant_id = tenant_id + self.description = description if enabled: - self.__enabled = True + self.enabled = True else: - self.__enabled = False + self.enabled = False @staticmethod def from_xml(xml_str): @@ -52,7 +52,7 @@ class Tenant(object): raise fault.BadRequestFault("Expecting Tenant Description") return Tenant(tenant_id, desc.text, set_enabled) except etree.LxmlError as e: - raise fault.BadRequestFault("Cannot parse Tenant", e.__str__()) + raise fault.BadRequestFault("Cannot parse Tenant", str(e)) @staticmethod def from_json(json_str): @@ -75,28 +75,16 @@ class Tenant(object): description = tenant["description"] return Tenant(tenant_id, description, set_enabled) except (json.decoder.JSONDecodeError, TypeError) as e: - raise fault.BadRequestFault("Cannot parse Tenant", e.__str__()) - - @property - def tenant_id(self): - return self.__tenant_id - - @property - def description(self): - return self.__description - - @property - def enabled(self): - return self.__enabled + raise fault.BadRequestFault("Cannot parse Tenant", str(e)) def to_dom(self): dom = etree.Element("tenant", xmlns="http://docs.openstack.org/idm/api/v1.0", - enabled=s.lower(self.__enabled.__str__())) - if self.__tenant_id != None: - dom.set("id", self.__tenant_id) + enabled=string.lower(str(self.enabled))) + if self.tenant_id: + dom.set("id", self.tenant_id) desc = etree.Element("description") - desc.text = self.__description + desc.text = self.description dom.append(desc) return dom @@ -105,13 +93,11 @@ class Tenant(object): def to_dict(self): tenant = {} - if self.__tenant_id != None: - tenant["id"] = self.__tenant_id - tenant["description"] = self.__description - tenant["enabled"] = self.__enabled - ret = {} - ret["tenant"] = tenant - return ret + if self.tenant_id: + tenant["id"] = self.tenant_id + tenant["description"] = self.description + tenant["enabled"] = self.enabled + return {'tenant': tenant} def to_json(self): return json.dumps(self.to_dict()) @@ -121,30 +107,16 @@ class Tenants(object): "A collection of tenants." def __init__(self, values, links): - self.__values = values - self.__links = links - - @property - def values(self): - return self.__values - - @property - def links(self): - return self.__links + self.values = values + self.links = links def to_xml(self): dom = etree.Element("tenants", xmlns="http://docs.openstack.org/idm/api/v1.0") - for t in self.__values: + for t in self.values: dom.append(t.to_dom()) return etree.tostring(dom) def to_json(self): - values = [] - for t in self.__values: - values.append(t.to_dict()["tenant"]) - v = {} - v["values"] = values - ret = {} - ret["tenants"] = v - return json.dumps(ret) + values = [t.to_dict()["tenant"] for t in self.values] + return json.dumps({"tenants": {"values": values}}) -- cgit