diff --git a/kollacli/api/group.py b/kollacli/api/group.py index d173fb7..aa71690 100644 --- a/kollacli/api/group.py +++ b/kollacli/api/group.py @@ -11,6 +11,8 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import kollacli.i18n as u + from kollacli.api.exceptions import MissingArgument from kollacli.common.inventory import Inventory from kollacli.common.utils import safe_decode @@ -18,15 +20,45 @@ from kollacli.common.utils import safe_decode 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): - """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: string + :type groupname: string """ if not groupname: - raise MissingArgument('group name') + raise MissingArgument('Group name') groupname = safe_decode(groupname) inventory = Inventory.load() @@ -34,16 +66,50 @@ class GroupApi(object): Inventory.save(inventory) 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: string + :type groupname: string """ if not groupname: - raise MissingArgument('group name') + raise MissingArgument('Group name') inventory = Inventory.load() groupname = safe_decode(groupname) inventory.remove_group(groupname) 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 diff --git a/kollacli/api/service.py b/kollacli/api/service.py index c30d7c9..6464fcd 100644 --- a/kollacli/api/service.py +++ b/kollacli/api/service.py @@ -15,6 +15,7 @@ import kollacli.i18n as u from kollacli.api.exceptions import MissingArgument from kollacli.common.inventory import Inventory +from kollacli.common.utils import safe_decode class ServiceApi(object): @@ -94,6 +95,7 @@ class ServiceApi(object): """ if servicenames is None: raise(MissingArgument(u._('Service names'))) + servicenames = safe_decode(servicenames) return self._get_services(servicenames) def _get_services(self, servicenames, get_all=False): diff --git a/kollacli/commands/group.py b/kollacli/commands/group.py index 2d8a1ed..3be1a0b 100644 --- a/kollacli/commands/group.py +++ b/kollacli/commands/group.py @@ -123,18 +123,16 @@ class GroupListhosts(Lister): def take_action(self, parsed_args): try: - inventory = Inventory.load() - - data = [] - group_hosts = inventory.get_group_hosts() - if group_hosts: - for (groupname, hostnames) in group_hosts.items(): - data.append((groupname, hostnames)) - else: - data.append(('', '')) + data = [('', '')] + groups = CLIENT.group_get_all() + if groups: + data = [] + for group in groups: + data.append((group.get_name(), + sorted(group.get_hostnames()))) return ((u._('Group'), u._('Hosts')), sorted(data)) - except CommandError as e: - raise e + except ClientException as e: + raise CommandError(str(e)) except Exception as e: raise Exception(traceback.format_exc()) @@ -197,17 +195,15 @@ class GroupListservices(Lister): def take_action(self, parsed_args): try: - inventory = Inventory.load() - - data = [] - group_services = inventory.get_group_services() - if group_services: - for (groupname, servicenames) in group_services.items(): - data.append((groupname, sorted(servicenames))) - else: - data.append(('', '')) + data = [('', '')] + groups = CLIENT.group_get_all() + if groups: + data = [] + for group in groups: + data.append((group.get_name(), + sorted(group.get_servicenames()))) return ((u._('Group'), u._('Services')), sorted(data)) - except CommandError as e: - raise e + except ClientException as e: + raise CommandError(str(e)) except Exception as e: raise Exception(traceback.format_exc())