~ubuntu-branches/ubuntu/saucy/keystone/saucy-proposed

« back to all changes in this revision

Viewing changes to keystone/logic/types/endpoint.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-08-23 10:18:22 UTC
  • Revision ID: james.westby@ubuntu.com-20110823101822-enve6zceb3lqhuvj
Tags: upstream-1.0~d4~20110823.1078
ImportĀ upstreamĀ versionĀ 1.0~d4~20110823.1078

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2010-2011 OpenStack, LLC.
 
2
#
 
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
 
6
#
 
7
#    http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
import json
 
17
from lxml import etree
 
18
 
 
19
from keystone.logic.types import fault
 
20
 
 
21
 
 
22
class EndpointTemplate(object):
 
23
    """Document me!"""
 
24
 
 
25
    @staticmethod
 
26
    def from_xml(xml_str):
 
27
        try:
 
28
            dom = etree.Element("root")
 
29
            dom.append(etree.fromstring(xml_str))
 
30
            root = dom.find("{http://docs.openstack.org/identity/api/v2.0}" \
 
31
                            "endpointTemplate")
 
32
            if root == None:
 
33
                raise fault.BadRequestFault("Expecting endpointTemplate")
 
34
            id = root.get("id")
 
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))
 
47
 
 
48
    @staticmethod
 
49
    def from_json(json_str):
 
50
        try:
 
51
            obj = json.loads(json_str)
 
52
            region = None
 
53
            service = None
 
54
            public_url = None
 
55
            admin_url = None
 
56
            internal_url = None
 
57
            enabled = None
 
58
            is_global = None
 
59
 
 
60
            if not "endpointTemplate" in obj:
 
61
                raise fault.BadRequestFault("Expecting endpointTemplate")
 
62
            endpoint_template = obj["endpointTemplate"]
 
63
 
 
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']]
 
68
            if invalid != []:
 
69
                raise fault.BadRequestFault("Invalid attribute(s): %s"
 
70
                                            % invalid)
 
71
 
 
72
            if not "id" in endpoint_template:
 
73
                id = None
 
74
            else:
 
75
                id = endpoint_template["id"]
 
76
 
 
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"]
 
91
 
 
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))
 
97
 
 
98
    def __init__(self, id, region, service, public_url, admin_url,
 
99
                 internal_url, enabled, is_global):
 
100
        self.id = id
 
101
        self.region = region
 
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
 
108
 
 
109
    def to_dom(self):
 
110
        dom = etree.Element("endpointTemplate",
 
111
                        xmlns="http://docs.openstack.org/identity/api/v2.0")
 
112
        if self.id:
 
113
            dom.set("id", str(self.id))
 
114
        if self.region:
 
115
            dom.set("region", self.region)
 
116
        if self.service:
 
117
            dom.set("serviceId", self.service)
 
118
        if self.public_url:
 
119
            dom.set("publicURL", self.public_url)
 
120
        if self.admin_url:
 
121
            dom.set("adminURL", self.admin_url)
 
122
        if self.internal_url:
 
123
            dom.set("internalURL", self.internal_url)
 
124
        if self.enabled:
 
125
            dom.set("enabled", 'true')
 
126
        if self.is_global:
 
127
            dom.set("global", 'true')
 
128
        return dom
 
129
 
 
130
    def to_xml(self):
 
131
        return etree.tostring(self.to_dom())
 
132
 
 
133
    def to_dict(self):
 
134
        endpoint_template = {}
 
135
        if self.id:
 
136
            endpoint_template["id"] = self.id
 
137
        if self.region:
 
138
            endpoint_template["region"] = self.region
 
139
        if self.service:
 
140
            endpoint_template["serviceId"] = self.service
 
141
        if self.public_url:
 
142
            endpoint_template["publicURL"] = self.public_url
 
143
        if self.admin_url:
 
144
            endpoint_template["adminURL"] = self.admin_url
 
145
        if self.internal_url:
 
146
            endpoint_template["internalURL"] = self.internal_url
 
147
        if self.enabled:
 
148
            endpoint_template["enabled"] = self.enabled
 
149
        if self.is_global:
 
150
            endpoint_template["global"] = self.is_global
 
151
        return {'endpointTemplate': endpoint_template}
 
152
 
 
153
    def to_json(self):
 
154
        return json.dumps(self.to_dict())
 
155
 
 
156
 
 
157
class EndpointTemplates(object):
 
158
    """A collection of endpointTemplates."""
 
159
 
 
160
    def __init__(self, values, links):
 
161
        self.values = values
 
162
        self.links = links
 
163
 
 
164
    def to_xml(self):
 
165
        dom = etree.Element("endpointTemplates")
 
166
        dom.set(u"xmlns", "http://docs.openstack.org/identity/api/v2.0")
 
167
 
 
168
        for t in self.values:
 
169
            dom.append(t.to_dom())
 
170
 
 
171
        for t in self.links:
 
172
            dom.append(t.to_dom())
 
173
 
 
174
        return etree.tostring(dom)
 
175
 
 
176
    def to_json(self):
 
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}})
 
181
 
 
182
 
 
183
class Endpoint(object):
 
184
    """Document me!"""
 
185
 
 
186
    def __init__(self, id, href):
 
187
        self.id = id
 
188
        self.href = href
 
189
 
 
190
    def to_dom(self):
 
191
        dom = etree.Element("endpoint",
 
192
                        xmlns="http://docs.openstack.org/identity/api/v2.0")
 
193
        if self.id:
 
194
            dom.set("id", str(self.id))
 
195
        if self.href:
 
196
            dom.set("href", self.href)
 
197
        return dom
 
198
 
 
199
    def to_xml(self):
 
200
        return etree.tostring(self.to_dom())
 
201
 
 
202
    def to_dict(self):
 
203
        endpoint = {}
 
204
        if self.id:
 
205
            endpoint["id"] = self.id
 
206
        if self.href:
 
207
            endpoint["href"] = self.href
 
208
        return {'endpoint': endpoint}
 
209
 
 
210
    def to_json(self):
 
211
        return json.dumps(self.to_dict())
 
212
 
 
213
 
 
214
class Endpoints(object):
 
215
    """A collection of endpoints."""
 
216
 
 
217
    def __init__(self, values, links):
 
218
        self.values = values
 
219
        self.links = links
 
220
 
 
221
    def to_xml(self):
 
222
        dom = etree.Element("endpoints")
 
223
        dom.set(u"xmlns", "http://docs.openstack.org/identity/api/v2.0")
 
224
 
 
225
        for t in self.values:
 
226
            dom.append(t.to_dom())
 
227
 
 
228
        for t in self.links:
 
229
            dom.append(t.to_dom())
 
230
 
 
231
        return etree.tostring(dom)
 
232
 
 
233
    def to_json(self):
 
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}})