Skip to content
Merged
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
40 changes: 40 additions & 0 deletions SoftLayer/CLI/hardware/guests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Lists the Virtual Guests running on this server."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers
from SoftLayer import utils


@click.command()
@click.argument('identifier')
@environment.pass_env
def cli(env, identifier):
"""Lists the Virtual Guests running on this server."""

mgr = SoftLayer.HardwareManager(env.client)
hw_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'hardware')
hw_guests = mgr.get_hardware_guests(hw_id)

if not hw_guests:
raise exceptions.CLIAbort("No Virtual Guests found.")

table = formatting.Table(['id', 'hostname', 'CPU', 'Memory', 'Start Date', 'Status', 'powerState'])
table.sortby = 'hostname'
for guest in hw_guests:
table.add_row([
guest['id'],
guest['hostname'],
'%i %s' % (guest['maxCpu'], guest['maxCpuUnits']),
guest['maxMemory'],
utils.clean_time(guest['createDate']),
guest['status']['keyName'],
guest['powerState']['keyName']
])

env.fout(table)
1 change: 1 addition & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
('hardware:detail', 'SoftLayer.CLI.hardware.detail:cli'),
('hardware:billing', 'SoftLayer.CLI.hardware.billing:cli'),
('hardware:edit', 'SoftLayer.CLI.hardware.edit:cli'),
('hardware:guests', 'SoftLayer.CLI.hardware.guests:cli'),
('hardware:list', 'SoftLayer.CLI.hardware.list:cli'),
('hardware:power-cycle', 'SoftLayer.CLI.hardware.power:power_cycle'),
('hardware:power-off', 'SoftLayer.CLI.hardware.power:power_off'),
Expand Down
21 changes: 20 additions & 1 deletion SoftLayer/CLI/virt/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers

from SoftLayer import utils

# pylint: disable=unnecessary-lambda

Expand Down Expand Up @@ -93,3 +93,22 @@ def cli(env, sortby, cpu, domain, datacenter, hostname, memory, network,
for value in columns.row(guest)])

env.fout(table)

hardware_guests = vsi.get_hardware_guests()
for hardware in hardware_guests:
if hardware['virtualHost']['guests']:
title = "Hardware(id = {hardwareId}) guests associated".format(hardwareId=hardware['id'])
table_hardware_guest = formatting.Table(['id', 'hostname', 'CPU', 'Memory', 'Start Date', 'Status',
'powerState'], title=title)
table_hardware_guest.sortby = 'hostname'
for guest in hardware['virtualHost']['guests']:
table_hardware_guest.add_row([
guest['id'],
guest['hostname'],
'%i %s' % (guest['maxCpu'], guest['maxCpuUnits']),
guest['maxMemory'],
utils.clean_time(guest['createDate']),
guest['status']['keyName'],
guest['powerState']['keyName']
])
env.fout(table_hardware_guest)
44 changes: 42 additions & 2 deletions SoftLayer/fixtures/SoftLayer_Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@
'id': 6660
}
},
"virtualHost": {
"accountId": 11111,
"id": 22222,
"name": "vmware.chechu.com",
"guests": [
{
"accountId": 11111,
"createDate": "2019-09-05T17:03:42-06:00",
"hostname": "NSX-T Manager",
"id": 33333,
"maxCpu": 16,
"maxCpuUnits": "CORE",
"maxMemory": 49152,
"powerState": {
"keyName": "RUNNING",
"name": "Running"
},
"status": {
"keyName": "ACTIVE",
"name": "Active"
}
}]}
}, {
'id': 1001,
'metricTrackingObject': {'id': 4},
Expand Down Expand Up @@ -190,7 +212,13 @@
'vlanNumber': 3672,
'id': 19082
},
]
],
"virtualHost": {
"accountId": 11111,
"id": 22222,
"name": "host14.vmware.chechu.com",
"guests": []
}
}, {
'id': 1002,
'metricTrackingObject': {'id': 5},
Expand Down Expand Up @@ -234,9 +262,21 @@
'vlanNumber': 3672,
'id': 19082
},
]
],
"virtualHost": {
"accountId": 11111,
"id": 22222,
"name": "host14.vmware.chechu.com",
"guests": []
}
}, {
'id': 1003,
"virtualHost": {
"accountId": 11111,
"id": 22222,
"name": "host14.vmware.chechu.com",
"guests": []
}
}]
getDomains = [{'name': 'example.com',
'id': 12345,
Expand Down
20 changes: 20 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Hardware_Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,23 @@
}
}
]

getVirtualHost = {
"accountId": 11111,
"createDate": "2018-10-08T10:54:48-06:00",
"description": "host16.vmware.chechu.com",
"hardwareId": 22222,
"id": 33333,
"name": "host16.vmware.chechu.com",
"uuid": "00000000-0000-0000-0000-0cc11111",
"hardware": {
"accountId": 11111,
"domain": "chechu.com",
"hostname": "host16.vmware",
"id": 22222,
"hardwareStatus": {
"id": 5,
"status": "ACTIVE"
}
}
}
40 changes: 40 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Virtual_Host.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
getGuests = [
{
"accountId": 11111,
"createDate": "2019-09-05T17:03:42-06:00",
"fullyQualifiedDomainName": "NSX-T Manager",
"hostname": "NSX-T Manager",
"id": 22222,
"maxCpu": 16,
"maxCpuUnits": "CORE",
"maxMemory": 49152,
"startCpus": 16,
"powerState": {
"keyName": "RUNNING",
"name": "Running"
},
"status": {
"keyName": "ACTIVE",
"name": "Active"
}
},
{
"accountId": 11111,
"createDate": "2019-09-23T06:00:53-06:00",
"hostname": "NSX-T Manager2",
"id": 33333,
"maxCpu": 12,
"maxCpuUnits": "CORE",
"maxMemory": 49152,
"startCpus": 12,
"statusId": 1001,
"powerState": {
"keyName": "RUNNING",
"name": "Running"
},
"status": {
"keyName": "ACTIVE",
"name": "Active"
}
}
]
12 changes: 12 additions & 0 deletions SoftLayer/managers/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,18 @@ def get_hard_drives(self, instance_id):
"""
return self.hardware.getHardDrives(id=instance_id)

def get_hardware_guests(self, instance_id):
"""Returns the hardware server guests.

:param int instance_id: Id of the hardware server.
"""
mask = "mask[id]"
virtual_host = self.hardware.getVirtualHost(mask=mask, id=instance_id)
if virtual_host:
return self.client.call('SoftLayer_Virtual_Host', 'getGuests', mask='mask[powerState]',
id=virtual_host['id'])
return virtual_host


def _get_bandwidth_key(items, hourly=True, no_public=False, location=None):
"""Picks a valid Bandwidth Item, returns the KeyName"""
Expand Down
9 changes: 9 additions & 0 deletions SoftLayer/managers/vs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,3 +1158,12 @@ def get_local_disks(self, instance_id):
"""
mask = 'mask[diskImage]'
return self.guest.getBlockDevices(mask=mask, id=instance_id)

def get_hardware_guests(self):
"""Returns all virtualHost capable hardware objects and their guests.

:return SoftLayer_Hardware[].
"""
object_filter = {"hardware": {"virtualHost": {"id": {"operation": "not null"}}}}
mask = "mask[virtualHost[guests[powerState]]]"
return self.client.call('SoftLayer_Account', 'getHardware', mask=mask, filter=object_filter)
4 changes: 4 additions & 0 deletions docs/cli/hardware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ This function updates the firmware of a server. If already at the latest version
.. click:: SoftLayer.CLI.hardware.storage:cli
:prog: hardware storage
:show-nested:

.. click:: SoftLayer.CLI.hardware.guests:cli
:prog: hardware guests
:show-nested:
12 changes: 12 additions & 0 deletions tests/CLI/modules/server_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,15 @@ def test_create_hw_no_confirm(self, confirm_mock):
'--network=TEST_NETWORK', '--os=UBUNTU_12_64'])

self.assertEqual(result.exit_code, 2)

def test_get_hardware_guests(self):
result = self.run_command(['hw', 'guests', '123456'])
self.assert_no_fail(result)

def test_hardware_guests_empty(self):
mock = self.set_mock('SoftLayer_Virtual_Host', 'getGuests')
mock.return_value = None

result = self.run_command(['hw', 'guests', '123456'])
self.assertEqual(result.exit_code, 2)
self.assertIsInstance(result.exception, exceptions.CLIAbort)
13 changes: 0 additions & 13 deletions tests/CLI/modules/vs/vs_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,6 @@ def test_list_vs(self):
result = self.run_command(['vs', 'list', '--tag=tag'])

self.assert_no_fail(result)
self.assertEqual(json.loads(result.output),
[{'datacenter': 'TEST00',
'primary_ip': '172.16.240.2',
'hostname': 'vs-test1',
'action': None,
'id': 100,
'backend_ip': '10.45.19.37'},
{'datacenter': 'TEST00',
'primary_ip': '172.16.240.7',
'hostname': 'vs-test2',
'action': None,
'id': 104,
'backend_ip': '10.45.19.35'}])

@mock.patch('SoftLayer.utils.lookup')
def test_detail_vs_empty_billing(self, mock_lookup):
Expand Down
32 changes: 32 additions & 0 deletions tests/managers/hardware_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,38 @@ def test_get_hard_drive_empty(self):

self.assertEqual([], result)

def test_get_hardware_guests_empty_virtualHost(self):
mock = self.set_mock('SoftLayer_Hardware_Server', 'getVirtualHost')
mock.return_value = None

result = self.hardware.get_hardware_guests(1234)

self.assertEqual(None, result)

def test_get_hardware_guests(self):
mock = self.set_mock('SoftLayer_Virtual_Host', 'getGuests')
mock.return_value = [
{
"accountId": 11111,
"hostname": "NSX-T Manager",
"id": 22222,
"maxCpu": 16,
"maxCpuUnits": "CORE",
"maxMemory": 49152,
"powerState": {
"keyName": "RUNNING",
"name": "Running"
},
"status": {
"keyName": "ACTIVE",
"name": "Active"
}
}]

result = self.hardware.get_hardware_guests(1234)

self.assertEqual("NSX-T Manager", result[0]['hostname'])


class HardwareHelperTests(testing.TestCase):

Expand Down
33 changes: 33 additions & 0 deletions tests/managers/vs/vs_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,3 +1132,36 @@ def test_get_local_disks_swap(self):
}
}
], result)

def test_get_hardware_guests(self):
mock = self.set_mock('SoftLayer_Account', 'getHardware')
mock.return_value = [{
"accountId": 11111,
"domain": "vmware.chechu.com",
"hostname": "host14",
"id": 22222,
"virtualHost": {
"accountId": 11111,
"id": 33333,
"name": "host14.vmware.chechu.com",
"guests": [
{
"accountId": 11111,
"hostname": "NSX-T Manager",
"id": 44444,
"maxCpu": 16,
"maxCpuUnits": "CORE",
"maxMemory": 49152,
"powerState": {
"keyName": "RUNNING",
"name": "Running"
},
"status": {
"keyName": "ACTIVE",
"name": "Active"
}
}]}}]

result = self.vs.get_hardware_guests()

self.assertEqual("NSX-T Manager", result[0]['virtualHost']['guests'][0]['hostname'])