add get and get_all methods to group api

- add new Group object
- add get and get_all
- update the 2 CLI group list methods to use the new API
- add safe decode to service get method

Jira-Issue: OSTACKDEV-17
This commit is contained in:
Steve Noyes 2016-03-23 16:17:16 -04:00
parent 8d9e66a281
commit ee80b1b5f2
3 changed files with 92 additions and 28 deletions

View File

@ -11,6 +11,8 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import kollacli.i18n as u
from kollacli.api.exceptions import MissingArgument from kollacli.api.exceptions import MissingArgument
from kollacli.common.inventory import Inventory from kollacli.common.inventory import Inventory
from kollacli.common.utils import safe_decode from kollacli.common.utils import safe_decode
@ -18,15 +20,45 @@ from kollacli.common.utils import safe_decode
class GroupApi(object): class GroupApi(object):
class Group(object):
def __init__(self, groupname, servicenames, hostnames):
self.name = groupname
self.servicenames = servicenames
self.hostnames = hostnames
def get_name(self):
"""Get name
:return: group name
:rtype: string
"""
return self.name
def get_servicenames(self):
"""Get service names associated with this group.
:return: service names
:rtype: list of strings
"""
return self.servicenames
def get_hostnames(self):
"""Get host names associated with this group.
:return: host names
:rtype: list of strings
"""
return self.hostnames
def group_add(self, groupname): def group_add(self, groupname):
"""add a group to the inventory """Add a group to the inventory
:param groupname: name of the group to add to the inventory :param groupname: name of the group to add to the inventory
:param groupname: string :type groupname: string
""" """
if not groupname: if not groupname:
raise MissingArgument('group name') raise MissingArgument('Group name')
groupname = safe_decode(groupname) groupname = safe_decode(groupname)
inventory = Inventory.load() inventory = Inventory.load()
@ -34,16 +66,50 @@ class GroupApi(object):
Inventory.save(inventory) Inventory.save(inventory)
def group_remove(self, groupname): def group_remove(self, groupname):
"""remove a group from the inventory """Remove a group from the inventory
:param groupname: name of the group to remove from the inventory :param groupname: name of the group to remove from the inventory
:param groupname: string :type groupname: string
""" """
if not groupname: if not groupname:
raise MissingArgument('group name') raise MissingArgument('Group name')
inventory = Inventory.load() inventory = Inventory.load()
groupname = safe_decode(groupname) groupname = safe_decode(groupname)
inventory.remove_group(groupname) inventory.remove_group(groupname)
Inventory.save(inventory) Inventory.save(inventory)
def group_get_all(self):
"""Get all groups in the inventory
:return: groups
:rtype: list of Group objects
"""
return self._get_groups(None, get_all=True)
def group_get(self, groupnames):
"""Get selected groups in the inventory
:param groupnames: names of groups to be read
:type groupnames: list of strings
:return: groups
:rtype: list of Group objects
"""
if groupnames is None:
raise(MissingArgument(u._('Group names')))
groupnames = safe_decode(groupnames)
return self._get_groups(groupnames)
def _get_groups(self, groupnames, get_all=False):
groups = []
inventory = Inventory.load()
group_services = inventory.get_group_services()
inv_groups = inventory.get_groups()
for inv_group in inv_groups:
if get_all or inv_group.name in groupnames:
group = self.Group(inv_group.name,
group_services[inv_group.name],
inv_group.get_hostnames())
groups.append(group)
return groups

View File

@ -15,6 +15,7 @@ import kollacli.i18n as u
from kollacli.api.exceptions import MissingArgument from kollacli.api.exceptions import MissingArgument
from kollacli.common.inventory import Inventory from kollacli.common.inventory import Inventory
from kollacli.common.utils import safe_decode
class ServiceApi(object): class ServiceApi(object):
@ -94,6 +95,7 @@ class ServiceApi(object):
""" """
if servicenames is None: if servicenames is None:
raise(MissingArgument(u._('Service names'))) raise(MissingArgument(u._('Service names')))
servicenames = safe_decode(servicenames)
return self._get_services(servicenames) return self._get_services(servicenames)
def _get_services(self, servicenames, get_all=False): def _get_services(self, servicenames, get_all=False):

View File

@ -123,18 +123,16 @@ class GroupListhosts(Lister):
def take_action(self, parsed_args): def take_action(self, parsed_args):
try: try:
inventory = Inventory.load() data = [('', '')]
groups = CLIENT.group_get_all()
if groups:
data = [] data = []
group_hosts = inventory.get_group_hosts() for group in groups:
if group_hosts: data.append((group.get_name(),
for (groupname, hostnames) in group_hosts.items(): sorted(group.get_hostnames())))
data.append((groupname, hostnames))
else:
data.append(('', ''))
return ((u._('Group'), u._('Hosts')), sorted(data)) return ((u._('Group'), u._('Hosts')), sorted(data))
except CommandError as e: except ClientException as e:
raise e raise CommandError(str(e))
except Exception as e: except Exception as e:
raise Exception(traceback.format_exc()) raise Exception(traceback.format_exc())
@ -197,17 +195,15 @@ class GroupListservices(Lister):
def take_action(self, parsed_args): def take_action(self, parsed_args):
try: try:
inventory = Inventory.load() data = [('', '')]
groups = CLIENT.group_get_all()
if groups:
data = [] data = []
group_services = inventory.get_group_services() for group in groups:
if group_services: data.append((group.get_name(),
for (groupname, servicenames) in group_services.items(): sorted(group.get_servicenames())))
data.append((groupname, sorted(servicenames)))
else:
data.append(('', ''))
return ((u._('Group'), u._('Services')), sorted(data)) return ((u._('Group'), u._('Services')), sorted(data))
except CommandError as e: except ClientException as e:
raise e raise CommandError(str(e))
except Exception as e: except Exception as e:
raise Exception(traceback.format_exc()) raise Exception(traceback.format_exc())