Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

add json format when listing vms #312

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ gandi.1
# gh-pages
node_modules/

# Other
bin/
include/
local/
man/
share/


# ArchLinux sources and binaries
packages/archlinux/pkg/
packages/archlinux/src/
16 changes: 12 additions & 4 deletions gandi/cli/commands/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from gandi.cli.core.cli import cli
from gandi.cli.core.utils import (
output_vm, output_image, output_generic, output_datacenter,
output_kernels, output_metric,
output_kernels, output_metric, output_json,
DatacenterLimited
)
from gandi.cli.core.utils.size import disk_check_size
Expand All @@ -28,8 +28,10 @@ def vm(gandi):
@click.option('--id', help='Display ids.', is_flag=True)
@click.option('--limit', help='Limit number of results.', default=100,
show_default=True)
@click.option('--format', type=click.Choice(['json', 'pretty-json']),
required=False, help="Choose the output format")
@pass_gandi
def list(gandi, state, id, limit, datacenter):
def list(gandi, state, id, limit, datacenter, format):
"""List virtual machines."""
options = {
'items_per_page': limit,
Expand All @@ -45,9 +47,15 @@ def list(gandi, state, id, limit, datacenter):

result = gandi.iaas.list(options)
for num, vm in enumerate(result):
if num:
if num and not format:
gandi.separator_line()
output_vm(gandi, vm, [], output_keys)

vm['ifaces'] = [ gandi.iface.info(iface_id) for iface_id in vm['ifaces_id'] ]

if format:
output_json(gandi, format, vm)
else:
output_vm(gandi, vm, [], output_keys)

return result

Expand Down
33 changes: 33 additions & 0 deletions gandi/cli/tests/commands/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ def test_list_id(self):
""")
self.assertEqual(result.exit_code, 0)

def test_list_json(self):
args = ['--format', 'json']
result = self.invoke_with_exceptions(vm.list, args)

self.assertEqual(result.output, """\
{"ai_active": 0, "console": 0, "cores": 1, "datacenter_id": 3,\
"date_created": "20141008T16:13:59", "date_updated": "20150319T11:11:31",\
"description": null, "disks_id": [4969232], "flex_shares": 0, "hostname": "vm1426759833",\
"id": 152966, "ifaces": [{"bandwidth": 204800.0, "datacenter_id": 1, "date_created": "20150105T00:00:00",\
"date_updated": "20150105T00:00:00", "id": 156572, "ips": [{"datacenter_id": 3, "date_created": "20150319T11:10:34",\
"date_updated": "20150319T11:10:36", "id": 204557, "iface_id": 156572, "ip": "10.50.10.10", "num": 0, "reverse": "xvm6-dc2-fece-e25f.ghst.net",\
"state": "created", "version": 4}], "ips_id": [204557], "num": null, "state": "free", "type": "private", "vlan": null, "vm_id": null}], "ifaces_id": [156572],\
"memory": 256, "state": "running", "vm_max_memory": 2048}
{"ai_active": 0, "console": 0, "cores": 1, "datacenter_id": 3,\
"date_created": "20141008T16:13:59", "date_updated": "20150319T11:11:31", "description": null,\
"disks_id": [4969232], "flex_shares": 0, "hostname": "vm1426759844", "id": 152964, "ifaces": [{"bandwidth": 204800.0,\
"datacenter_id": 1, "date_created": "20150105T00:00:00", "date_updated": "20150105T00:00:00", "id": 156572, "ips": [{"datacenter_id": 3, "date_created": "20150319T11:10:34",\
"date_updated": "20150319T11:10:36", "id": 204557, "iface_id": 156572, "ip": "10.50.10.10", "num": 0, "reverse": "xvm6-dc2-fece-e25f.ghst.net", "state": "created", "version": 4}],\
"ips_id": [204557], "num": null, "state": "free", "type": "private", "vlan": null, "vm_id": null}], "ifaces_id": [156572],\
"memory": 256, "state": "running", "vm_max_memory": 2048}
{"ai_active": 0, "console": 0, "cores": 1, "datacenter_id": 1, "date_created": "20150319T11:14:13", "date_updated": "20150319T11:14:55",\
"description": null, "disks_id": [4969249], "flex_shares": 0, "hostname": "server01", "id": 152967, "ifaces": [{"bandwidth": 102400.0,\
"datacenter_id": 1, "date_created": "20140423T00:00:00", "date_updated": "20140423T00:00:00", "id": 156573, "ips": [{"datacenter_id": 1,\
"date_created": "20150317T16:20:10", "date_updated": "20150319T11:14:13", "id": 203968, "iface_id": 156573, "ip": "95.142.160.181", "num": 0, "reverse": "xvm-160-181.dc0.ghst.net",\
"state": "created", "version": 4}, {"datacenter_id": 1, "date_created": "20150319T11:14:16", "date_updated": "20150319T11:14:16", "id": 204558,\
"iface_id": 156573, "ip": "2001:4b98:dc0:47:216:3eff:feb2:3862", "num": 1, "reverse": "xvm6-dc0-feb2-3862.ghst.net", "state": "created", "version": 6}],\
"ips_id": [203968, 204558], "num": 0, "state": "used", "type": "public", "vlan": null, "vm_id": 152967}], "ifaces_id": [156573],\
"memory": 256, "state": "running", "vm_max_memory": 2048}
{"ai_active": 0, "console": 0, "cores": 1, "datacenter_id": 1, "date_created": "20150319T11:14:13", "date_updated": "20150319T11:14:55",\
"description": null, "disks_id": [4969250], "flex_shares": 0, "hostname": "server02", "id": 152968, "ifaces": [null], "ifaces_id": [156574],\
"memory": 256, "state": "halted", "vm_max_memory": 2048}
""")

def test_list_filter_state(self):

result = self.invoke_with_exceptions(vm.list, ['--state', 'halted'])
Expand Down
3 changes: 2 additions & 1 deletion gandi/cli/tests/fixtures/_hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,8 @@ def iface_info(iface_id):

ifaces = iface_list({})
ifaces = dict([(iface['id'], iface) for iface in ifaces])
return ifaces[iface_id]

return ifaces.get(iface_id, None)


def ip_list(options):
Expand Down