From 30b6b179b0ff75a562bd223b59a4c423af65e688 Mon Sep 17 00:00:00 2001 From: Adam Price Date: Thu, 4 Aug 2011 11:43:57 -0400 Subject: [PATCH] adding 'update', 'facts', and 'packages' calls to katello cli client --- cli/bin/katello | 4 +- cli/src/katello/client/api/system.py | 9 ++- cli/src/katello/client/core/system.py | 108 +++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 5 deletions(-) diff --git a/cli/bin/katello b/cli/bin/katello index 0d5bb04fe92..4a501a14a02 100755 --- a/cli/bin/katello +++ b/cli/bin/katello @@ -106,7 +106,9 @@ def setup_admin(admin): system_cmd.add_action('register', system.Register()) system_cmd.add_action('unregister', system.Unregister()) system_cmd.add_action('info', system.Info()) - system_cmd.add_action('installed-packages', system.InstalledPackages()) + system_cmd.add_action('packages', system.InstalledPackages()) + system_cmd.add_action('facts', system.Facts()) + system_cmd.add_action('update', system.Update()) admin.add_command('system', system_cmd) template_cmd = template.Template() diff --git a/cli/src/katello/client/api/system.py b/cli/src/katello/client/api/system.py index 68f2847bdb3..0ad20a38b47 100644 --- a/cli/src/katello/client/api/system.py +++ b/cli/src/katello/client/api/system.py @@ -35,8 +35,9 @@ def register(self, name, org, envName, cp_type): "cp_type": cp_type, "facts": { "distribution.name": "Fedora" - } - })[1] + } + })[1] + def unregister(self, system_id): path = "/api/systems/" + str(system_id) return self.server.DELETE(path)[1] @@ -48,6 +49,10 @@ def system(self, system_id): def packages(self, system_id): path="/api/systems/%s/packages" % system_id return self.server.GET(path)[1] + + def update(self, system_id, params = {}): + path = "/api/systems/%s" % system_id + return self.server.PUT(path, params)[1] def systems_by_org(self, orgId, query = {}): path = "/api/organizations/%s/systems" % orgId diff --git a/cli/src/katello/client/core/system.py b/cli/src/katello/client/core/system.py index bc5c6a62d7a..7c546765285 100644 --- a/cli/src/katello/client/core/system.py +++ b/cli/src/katello/client/core/system.py @@ -113,7 +113,8 @@ def run(self): printer.addColumn('name') printer.addColumn('uuid') printer.addColumn('location') - printer.addColumn('created_at', 'Registered') + printer.addColumn('created_at', 'Registered', time_format=True) + printer.addColumn('updated_at', 'Last updated', time_format=True) printer.addColumn('description', multiline=True) printer.printItem(system) @@ -153,7 +154,7 @@ def run(self): if not systems: return os.EX_DATAERR - packages = self.api.packages(self.api.system(systems[0]['uuid'])['uuid']) # magic + packages = self.api.packages(systems[0]['uuid']) printer.addColumn('name') @@ -169,6 +170,53 @@ def run(self): printer.printItems(packages) return os.EX_OK + +class Facts(SystemAction): + + description = _('display a the hardware facts of a system') + + def setup_parser(self): + self.parser.add_option('--org', dest='org', + help=_("organization name eg: foo.example.com (required)")) + self.parser.add_option('--name', dest='name', + help=_("system name (required)")) + self.parser.add_option('--environment', dest='environment', + help=_("environment name")) + + def check_options(self): + self.require_option('org') + self.require_option('name') + + def run(self): + org_name = self.get_option('org') + env_name = self.get_option('environment') + sys_name = self.get_option('name') + # info is always grep friendly + printer = Printer(False) + + if env_name is None: + printer.setHeader(_("System Facts For System %s in Org %s") % (sys_name, org_name)) + systems = self.api.systems_by_org(org_name, {'name': sys_name}) + else: + printer.setHeader(_("System Facts For System %s in Environment %s in Org %s") % (sys_name, env_name, org_name)) + systems = self.api.systems_by_env(org_name, env_name, + {'name': sys_name}) + + if not systems: + return os.EX_DATAERR + + # get system details + system = self.api.system(systems[0]['uuid']) + + facts_hash = system['facts'] + facts_tuples_sorted = [(k, facts_hash[k]) for k in sorted(facts_hash.keys())] + for k, v in facts_tuples_sorted: + printer.addColumn(k) + system[k] = v + + printer.printItem(system) + + return os.EX_OK class Register(SystemAction): @@ -224,6 +272,62 @@ def run(self): result = self.api.unregister(systems[0]['uuid']) print _("Successfully unregistered System [ %s ]") % name return os.EX_OK + +class Update(SystemAction): + + description = _('update a system') + + def setup_parser(self): + self.parser.add_option('--org', dest='org', + help=_('organization name (required)')) + self.parser.add_option('--name', dest='name', + help=_('system name (required)')) + self.parser.add_option('--environment', dest='environment', + help=_("environment name")) + + self.parser.add_option('--new-name', dest='new_name', + help=_('a new name for the system')) + self.parser.add_option('--description', dest='description', + help=_('a description of the system')) +# self.parser.add_option('--location', dest='location', +# help=_("location of the system")) + + def check_options(self): + self.require_option('org') + self.require_option('name') + + def run(self): + org_name = self.get_option('org') + env_name = self.get_option('environment') + sys_name = self.get_option('name') + new_name = self.get_option('new_name') + new_description = self.get_option('description') +# new_location = self.get_option('location') + + if env_name is None: + systems = self.api.systems_by_org(org_name, {'name': sys_name}) + else: + systems = self.api.systems_by_env(org_name, env_name, + {'name': sys_name}) + + if not systems: + return os.EX_DATAERR + + system_uuid = systems[0]['uuid'] + updates = {} + if new_name: updates['name'] = new_name + if new_description: updates['description'] = new_description +# if new_location: updates['location'] = new_location + + response = self.api.update(system_uuid, updates) + + if is_valid_record(response): + print _("Successfully updated system [ %s ]") % systems[0]['name'] + else: + print _("Could not update system [ %s ]") % systems[0]['name'] + + return os.EX_OK + class System(Command):