Skip to content

Commit 04730e6

Browse files
author
Dean Troyer
committed
Add 'list service' command and common modules
1 parent 2f2191b commit 04730e6

5 files changed

Lines changed: 227 additions & 0 deletions

File tree

openstackclient/common/command.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2011 OpenStack LLC.
2+
# All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
#
16+
# vim: tabstop=4 shiftwidth=4 softtabstop=4
17+
18+
"""
19+
OpenStack base command
20+
"""
21+
22+
from cliff.command import Command
23+
24+
25+
class OpenStackCommand(Command):
26+
"""Base class for OpenStack commands
27+
"""
28+
29+
api = None
30+
31+
def run(self, parsed_args):
32+
if not self.api:
33+
return
34+
else:
35+
return super(OpenStackCommand, self).run(parsed_args)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Copyright 2012 OpenStack LLC.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
#
16+
# vim: tabstop=4 shiftwidth=4 softtabstop=4
17+
18+
"""
19+
Exception definitions.
20+
"""
21+
22+
23+
class CommandError(Exception):
24+
pass
25+
26+
27+
class AuthorizationFailure(Exception):
28+
pass
29+
30+
31+
class NoTokenLookupException(Exception):
32+
"""This form of authentication does not support looking up
33+
endpoints from an existing token."""
34+
pass
35+
36+
37+
class EndpointNotFound(Exception):
38+
"""Could not find Service or Region in Service Catalog."""
39+
pass
40+
41+
42+
class ClientException(Exception):
43+
"""
44+
The base exception class for all exceptions this library raises.
45+
"""
46+
def __init__(self, code, message=None, details=None):
47+
self.code = code
48+
self.message = message or self.__class__.message
49+
self.details = details
50+
51+
def __str__(self):
52+
return "%s (HTTP %s)" % (self.message, self.code)
53+
54+
55+
class BadRequest(ClientException):
56+
"""
57+
HTTP 400 - Bad request: you sent some malformed data.
58+
"""
59+
http_status = 400
60+
message = "Bad request"
61+
62+
63+
class Unauthorized(ClientException):
64+
"""
65+
HTTP 401 - Unauthorized: bad credentials.
66+
"""
67+
http_status = 401
68+
message = "Unauthorized"
69+
70+
71+
class Forbidden(ClientException):
72+
"""
73+
HTTP 403 - Forbidden: your credentials don't give you access to this
74+
resource.
75+
"""
76+
http_status = 403
77+
message = "Forbidden"
78+
79+
80+
class NotFound(ClientException):
81+
"""
82+
HTTP 404 - Not found
83+
"""
84+
http_status = 404
85+
message = "Not found"
86+
87+
88+
class Conflict(ClientException):
89+
"""
90+
HTTP 409 - Conflict
91+
"""
92+
http_status = 409
93+
message = "Conflict"
94+
95+
96+
class OverLimit(ClientException):
97+
"""
98+
HTTP 413 - Over limit: you're over the API limits for this time period.
99+
"""
100+
http_status = 413
101+
message = "Over limit"
102+
103+
104+
# NotImplemented is a python keyword.
105+
class HTTPNotImplemented(ClientException):
106+
"""
107+
HTTP 501 - Not Implemented: the server does not support this operation.
108+
"""
109+
http_status = 501
110+
message = "Not Implemented"
111+
112+
113+
# In Python 2.4 Exception is old-style and thus doesn't have a __subclasses__()
114+
# so we can do this:
115+
# _code_map = dict((c.http_status, c)
116+
# for c in ClientException.__subclasses__())
117+
#
118+
# Instead, we have to hardcode it:
119+
_code_map = dict((c.http_status, c) for c in [BadRequest, Unauthorized,
120+
Forbidden, NotFound, OverLimit, HTTPNotImplemented])
121+
122+
123+
def from_response(response, body):
124+
"""
125+
Return an instance of an ClientException or subclass
126+
based on an httplib2 response.
127+
128+
Usage::
129+
130+
resp, body = http.request(...)
131+
if resp.status != 200:
132+
raise exception_from_response(resp, body)
133+
"""
134+
cls = _code_map.get(response.status, ClientException)
135+
if body:
136+
if hasattr(body, 'keys'):
137+
error = body[body.keys()[0]]
138+
message = error.get('message', None)
139+
details = error.get('details', None)
140+
else:
141+
# If we didn't get back a properly formed error message we
142+
# probably couldn't communicate with Keystone at all.
143+
message = "Unable to communicate with image service: %s." % body
144+
details = None
145+
return cls(code=response.status, message=message, details=details)
146+
else:
147+
return cls(code=response.status)

openstackclient/identity/v2_0/__init__.py

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2012 OpenStack LLC.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
#
16+
# vim: tabstop=4 shiftwidth=4 softtabstop=4
17+
18+
"""
19+
Service action implementations
20+
"""
21+
22+
import logging
23+
24+
from openstackclient.common import command
25+
from openstackclient.common import utils
26+
27+
28+
class List_Service(command.OpenStackCommand):
29+
"List service command."
30+
31+
api = 'identity'
32+
log = logging.getLogger(__name__)
33+
34+
def get_parser(self, prog_name):
35+
parser = super(List_Service, self).get_parser(prog_name)
36+
parser.add_argument(
37+
'--long',
38+
action='store_true',
39+
default=False,
40+
help='Additional fields are listed in output')
41+
return parser
42+
43+
def run(self, parsed_args):
44+
self.log.info('v2.List_Service.run(%s)' % parsed_args)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def read(fname):
4949
'openstack.cli': [
5050
'list_server = openstackclient.compute.v2.server:List_Server',
5151
'show_server = openstackclient.compute.v2.server:Show_Server',
52+
'list_service = openstackclient.identity.v2_0.service:List_Service',
5253
]
5354
}
5455
)

0 commit comments

Comments
 (0)