Set up common utils
This commit is contained in:
parent
11d3ba4570
commit
06f82305b5
@ -12,13 +12,19 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
"""
|
||||
Common client utilities
|
||||
"""
|
||||
|
||||
import os
|
||||
import uuid
|
||||
|
||||
import prettytable
|
||||
|
||||
from glanceclient.common import exceptions
|
||||
from openstackclient.common import exceptions
|
||||
|
||||
|
||||
# Decorator for cli-args
|
||||
|
@ -12,8 +12,14 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
from glanceclient.common import utils
|
||||
"""
|
||||
Server action implementations
|
||||
"""
|
||||
|
||||
from openstackclient.common import utils
|
||||
|
||||
|
||||
def _find_server(cs, server):
|
||||
|
@ -1,131 +0,0 @@
|
||||
# Copyright 2011 OpenStack LLC.
|
||||
# All Rights Reserved
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
"""
|
||||
Utility functions for OpenStack Client
|
||||
"""
|
||||
|
||||
import copy
|
||||
|
||||
import prettytable
|
||||
|
||||
#from novaclient import utils
|
||||
|
||||
|
||||
# lifted from glance/common/utils.py
|
||||
def bool_from_string(subject):
|
||||
"""
|
||||
Interpret a string as a boolean.
|
||||
|
||||
Any string value in:
|
||||
('True', 'true', 'On', 'on', '1')
|
||||
is interpreted as a boolean True.
|
||||
|
||||
Useful for JSON-decoded stuff and config file parsing
|
||||
"""
|
||||
if isinstance(subject, bool):
|
||||
return subject
|
||||
elif isinstance(subject, int):
|
||||
return subject == 1
|
||||
if hasattr(subject, 'startswith'): # str or unicode...
|
||||
if subject.strip().lower() in ('true', 'on', '1'):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# lifted from keystoneclient/base.py
|
||||
def getid(obj):
|
||||
"""
|
||||
Abstracts the common pattern of allowing both an object or an object's ID
|
||||
(UUID) as a parameter when dealing with relationships.
|
||||
"""
|
||||
|
||||
# Try to return the object's UUID first, if we have a UUID.
|
||||
try:
|
||||
if obj.uuid:
|
||||
return obj.uuid
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
return obj.id
|
||||
except AttributeError:
|
||||
return obj
|
||||
|
||||
|
||||
def show_object(manager, id, fields=None):
|
||||
"""Check id, lookup object, display result fields"""
|
||||
if not id:
|
||||
print "no id specified"
|
||||
return
|
||||
obj = manager.get(id)
|
||||
print_obj_fields(obj, fields)
|
||||
|
||||
|
||||
def print_obj_fields(obj, fields=[]):
|
||||
"""Print specified object fields"""
|
||||
# Select the fields to print, then passthrough to novaclient
|
||||
a = {name: getattr(obj, name, '') for name in fields}
|
||||
utils.print_dict(a)
|
||||
|
||||
|
||||
def print_dict_fields(obj, fields=[]):
|
||||
"""Print specified object fields"""
|
||||
# Select the fields to print, then passthrough to novaclient
|
||||
a = {name: obj[name] for name in fields}
|
||||
utils.print_dict(a)
|
||||
|
||||
|
||||
def print_dict_list(objs, fields, formatters={}):
|
||||
"""Print list of dicts"""
|
||||
mixed_case_fields = []
|
||||
pt = prettytable.PrettyTable([f for f in fields], caching=False)
|
||||
pt.aligns = ['l' for f in fields]
|
||||
|
||||
for o in objs:
|
||||
row = []
|
||||
for field in fields:
|
||||
if field in formatters:
|
||||
row.append(formatters[field](o))
|
||||
else:
|
||||
if field in mixed_case_fields:
|
||||
field_name = field.replace(' ', '_')
|
||||
else:
|
||||
field_name = field.lower().replace(' ', '_')
|
||||
data = o[field_name]
|
||||
row.append(data)
|
||||
pt.add_row(row)
|
||||
|
||||
pt.printt(sortby=fields[0])
|
||||
|
||||
|
||||
def print_list(objs, fields, formatters={}):
|
||||
"""Print list of objects"""
|
||||
# Passthrough to novaclient
|
||||
utils.print_list(objs, fields, formatters=formatters)
|
||||
|
||||
|
||||
def expand_meta(objs, field):
|
||||
"""Expand metadata fields in an object"""
|
||||
ret = []
|
||||
for oldobj in objs:
|
||||
newobj = copy.deepcopy(oldobj)
|
||||
ex = getattr(newobj, field, {})
|
||||
for f in ex.keys():
|
||||
setattr(newobj, f, ex[f])
|
||||
delattr(newobj, field)
|
||||
ret.append(newobj)
|
||||
return ret
|
@ -1,7 +1,21 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
import time
|
||||
|
||||
import mox
|
||||
import unittest
|
||||
|
||||
|
||||
class TestCase(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def setUp(self):
|
||||
super(TestCase, self).setUp()
|
||||
self.mox = mox.Mox()
|
||||
self._original_time = time.time
|
||||
time.time = lambda: 1234
|
||||
|
||||
def tearDown(self):
|
||||
time.time = self._original_time
|
||||
super(TestCase, self).tearDown()
|
||||
self.mox.UnsetStubs()
|
||||
self.mox.VerifyAll()
|
||||
|
Loading…
Reference in New Issue
Block a user