1
# Copyright (c) 2010-2011 OpenStack, LLC.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
17
from lxml import etree
19
from keystone.logic.types import fault
22
class EndpointTemplate(object):
26
def from_xml(xml_str):
28
dom = etree.Element("root")
29
dom.append(etree.fromstring(xml_str))
30
root = dom.find("{http://docs.openstack.org/identity/api/v2.0}" \
33
raise fault.BadRequestFault("Expecting endpointTemplate")
35
region = root.get("region")
36
service = root.get("serviceId")
37
public_url = root.get("publicURL")
38
admin_url = root.get("adminURL")
39
internal_url = root.get("internalURL")
40
enabled = root.get("enabled")
41
is_global = root.get("global")
42
return EndpointTemplate(id, region, service, public_url, admin_url,
43
internal_url, enabled, is_global)
44
except etree.LxmlError as e:
45
raise fault.BadRequestFault(\
46
"Cannot parse endpointTemplate", str(e))
49
def from_json(json_str):
51
obj = json.loads(json_str)
60
if not "endpointTemplate" in obj:
61
raise fault.BadRequestFault("Expecting endpointTemplate")
62
endpoint_template = obj["endpointTemplate"]
64
# Check that fields are valid
65
invalid = [key for key in endpoint_template if key not in
66
['id', 'region', 'serviceId', 'publicURL',
67
'adminURL', 'internalURL', 'enabled', 'global']]
69
raise fault.BadRequestFault("Invalid attribute(s): %s"
72
if not "id" in endpoint_template:
75
id = endpoint_template["id"]
77
if 'region' in endpoint_template:
78
region = endpoint_template["region"]
79
if 'serviceId' in endpoint_template:
80
service = endpoint_template["serviceId"]
81
if 'publicURL' in endpoint_template:
82
public_url = endpoint_template["publicURL"]
83
if 'adminURL' in endpoint_template:
84
admin_url = endpoint_template["adminURL"]
85
if 'internalURL' in endpoint_template:
86
internal_url = endpoint_template["internalURL"]
87
if 'enabled' in endpoint_template:
88
enabled = endpoint_template["enabled"]
89
if 'global' in endpoint_template:
90
is_global = endpoint_template["global"]
92
return EndpointTemplate(id, region, service, public_url, admin_url,
93
internal_url, enabled, is_global)
94
except (ValueError, TypeError) as e:
95
raise fault.BadRequestFault(\
96
"Cannot parse endpointTemplate", str(e))
98
def __init__(self, id, region, service, public_url, admin_url,
99
internal_url, enabled, is_global):
102
self.service = service
103
self.public_url = public_url
104
self.admin_url = admin_url
105
self.internal_url = internal_url
106
self.enabled = enabled
107
self.is_global = is_global
110
dom = etree.Element("endpointTemplate",
111
xmlns="http://docs.openstack.org/identity/api/v2.0")
113
dom.set("id", str(self.id))
115
dom.set("region", self.region)
117
dom.set("serviceId", self.service)
119
dom.set("publicURL", self.public_url)
121
dom.set("adminURL", self.admin_url)
122
if self.internal_url:
123
dom.set("internalURL", self.internal_url)
125
dom.set("enabled", 'true')
127
dom.set("global", 'true')
131
return etree.tostring(self.to_dom())
134
endpoint_template = {}
136
endpoint_template["id"] = self.id
138
endpoint_template["region"] = self.region
140
endpoint_template["serviceId"] = self.service
142
endpoint_template["publicURL"] = self.public_url
144
endpoint_template["adminURL"] = self.admin_url
145
if self.internal_url:
146
endpoint_template["internalURL"] = self.internal_url
148
endpoint_template["enabled"] = self.enabled
150
endpoint_template["global"] = self.is_global
151
return {'endpointTemplate': endpoint_template}
154
return json.dumps(self.to_dict())
157
class EndpointTemplates(object):
158
"""A collection of endpointTemplates."""
160
def __init__(self, values, links):
165
dom = etree.Element("endpointTemplates")
166
dom.set(u"xmlns", "http://docs.openstack.org/identity/api/v2.0")
168
for t in self.values:
169
dom.append(t.to_dom())
172
dom.append(t.to_dom())
174
return etree.tostring(dom)
177
values = [t.to_dict()["endpointTemplate"] for t in self.values]
178
links = [t.to_dict()["links"] for t in self.links]
179
return json.dumps({"endpointTemplates":\
180
{"values": values, "links": links}})
183
class Endpoint(object):
186
def __init__(self, id, href):
191
dom = etree.Element("endpoint",
192
xmlns="http://docs.openstack.org/identity/api/v2.0")
194
dom.set("id", str(self.id))
196
dom.set("href", self.href)
200
return etree.tostring(self.to_dom())
205
endpoint["id"] = self.id
207
endpoint["href"] = self.href
208
return {'endpoint': endpoint}
211
return json.dumps(self.to_dict())
214
class Endpoints(object):
215
"""A collection of endpoints."""
217
def __init__(self, values, links):
222
dom = etree.Element("endpoints")
223
dom.set(u"xmlns", "http://docs.openstack.org/identity/api/v2.0")
225
for t in self.values:
226
dom.append(t.to_dom())
229
dom.append(t.to_dom())
231
return etree.tostring(dom)
234
values = [t.to_dict()["endpoint"] for t in self.values]
235
links = [t.to_dict()["links"] for t in self.links]
236
return json.dumps({"endpoints": {"values": values, "links": links}})