Skip to content

Commit 11d3ba4

Browse files
author
Dean Troyer
committed
Add openstackclient bits
1 parent f4b5ef3 commit 11d3ba4

15 files changed

Lines changed: 680 additions & 0 deletions

File tree

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.log
2+
*.pyc
3+
*.swp
4+
*~
5+
.openstackclient-venv
6+
.venv
7+
build
8+
dist
9+
python_openstackclient.egg-info

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include AUTHORS
2+
include LICENSE
3+
include README.rst
4+
recursive-inlcude docs *
5+
recursive-include tests *

openstackclient/__init__.py

Whitespace-only changes.

openstackclient/common/__init__.py

Whitespace-only changes.

openstackclient/common/utils.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
import os
17+
import uuid
18+
19+
import prettytable
20+
21+
from glanceclient.common import exceptions
22+
23+
24+
# Decorator for cli-args
25+
def arg(*args, **kwargs):
26+
def _decorator(func):
27+
# Because of the sematics of decorator composition if we just append
28+
# to the options list positional options will appear to be backwards.
29+
func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
30+
return func
31+
return _decorator
32+
33+
34+
def pretty_choice_list(l):
35+
return ', '.join("'%s'" % i for i in l)
36+
37+
38+
def print_list(objs, fields, formatters={}):
39+
pt = prettytable.PrettyTable([f for f in fields], caching=False)
40+
pt.aligns = ['l' for f in fields]
41+
42+
for o in objs:
43+
row = []
44+
for field in fields:
45+
if field in formatters:
46+
row.append(formatters[field](o))
47+
else:
48+
field_name = field.lower().replace(' ', '_')
49+
data = getattr(o, field_name, '')
50+
row.append(data)
51+
pt.add_row(row)
52+
53+
pt.printt(sortby=fields[0])
54+
55+
56+
def print_dict(d):
57+
pt = prettytable.PrettyTable(['Property', 'Value'], caching=False)
58+
pt.aligns = ['l', 'l']
59+
[pt.add_row(list(r)) for r in d.iteritems()]
60+
pt.printt(sortby='Property')
61+
62+
63+
def find_resource(manager, name_or_id):
64+
"""Helper for the _find_* methods."""
65+
# first try to get entity as integer id
66+
try:
67+
if isinstance(name_or_id, int) or name_or_id.isdigit():
68+
return manager.get(int(name_or_id))
69+
except exceptions.NotFound:
70+
pass
71+
72+
# now try to get entity as uuid
73+
try:
74+
uuid.UUID(str(name_or_id))
75+
return manager.get(name_or_id)
76+
except (ValueError, exceptions.NotFound):
77+
pass
78+
79+
# finally try to find entity by name
80+
try:
81+
return manager.find(name=name_or_id)
82+
except exceptions.NotFound:
83+
msg = "No %s with a name or ID of '%s' exists." % \
84+
(manager.resource_class.__name__.lower(), name_or_id)
85+
raise exceptions.CommandError(msg)
86+
87+
88+
def skip_authentication(f):
89+
"""Function decorator used to indicate a caller may be unauthenticated."""
90+
f.require_authentication = False
91+
return f
92+
93+
94+
def is_authentication_required(f):
95+
"""Checks to see if the function requires authentication.
96+
97+
Use the skip_authentication decorator to indicate a caller may
98+
skip the authentication step.
99+
"""
100+
return getattr(f, 'require_authentication', True)
101+
102+
103+
def string_to_bool(arg):
104+
return arg.strip().lower() in ('t', 'true', 'yes', '1')
105+
106+
107+
def env(*vars, **kwargs):
108+
"""Search for the first defined of possibly many env vars
109+
110+
Returns the first environment variable defined in vars, or
111+
returns the default defined in kwargs.
112+
"""
113+
for v in vars:
114+
value = os.environ.get(v, None)
115+
if value:
116+
return value
117+
return kwargs.get('default', '')

openstackclient/compute/__init__.py

Whitespace-only changes.

openstackclient/compute/v2/__init__.py

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
from glanceclient.common import utils
17+
18+
19+
def _find_server(cs, server):
20+
"""Get a server by name or ID."""
21+
return utils.find_resource(cs.servers, server)
22+
23+
def _print_server(cs, server):
24+
# By default when searching via name we will do a
25+
# findall(name=blah) and due a REST /details which is not the same
26+
# as a .get() and doesn't get the information about flavors and
27+
# images. This fix it as we redo the call with the id which does a
28+
# .get() to get all informations.
29+
if not 'flavor' in server._info:
30+
server = _find_server(cs, server.id)
31+
32+
networks = server.networks
33+
info = server._info.copy()
34+
for network_label, address_list in networks.items():
35+
info['%s network' % network_label] = ', '.join(address_list)
36+
37+
flavor = info.get('flavor', {})
38+
flavor_id = flavor.get('id', '')
39+
info['flavor'] = _find_flavor(cs, flavor_id).name
40+
41+
image = info.get('image', {})
42+
image_id = image.get('id', '')
43+
info['image'] = _find_image(cs, image_id).name
44+
45+
info.pop('links', None)
46+
info.pop('addresses', None)
47+
48+
utils.print_dict(info)
49+
50+
@utils.arg('server', metavar='<server>', help='Name or ID of server.')
51+
def do_show_server(cs, args):
52+
"""Show details about the given server."""
53+
print "do_show_server(%s)" % args.server
54+
#s = _find_server(cs, args.server)
55+
#_print_server(cs, s)

openstackclient/identity/__init__.py

Whitespace-only changes.

openstackclient/image/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)