Skip to content

Commit

Permalink
adding 'update', 'facts', and 'packages' calls to katello cli client
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Price committed Aug 4, 2011
1 parent 2a4b918 commit 30b6b17
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cli/bin/katello
Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions cli/src/katello/client/api/system.py
Expand Up @@ -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]
Expand All @@ -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
Expand Down
108 changes: 106 additions & 2 deletions cli/src/katello/client/core/system.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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')

Expand All @@ -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):

Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit 30b6b17

Please sign in to comment.