-
Notifications
You must be signed in to change notification settings - Fork 194
dedicatedhost cancel, cancel-guests, and list-guests commands #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
746dd22
3052686
2ef45d8
1cfac08
72d0843
4a82445
2987fd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Cancel a dedicated host.""" | ||
# :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 | ||
|
||
|
||
@click.command() | ||
@click.argument('identifier') | ||
@environment.pass_env | ||
def cli(env, identifier): | ||
"""Cancel a dedicated host server immediately""" | ||
|
||
mgr = SoftLayer.DedicatedHostManager(env.client) | ||
|
||
host_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'dedicated host') | ||
|
||
if not (env.skip_confirmations or formatting.no_going_back(host_id)): | ||
raise exceptions.CLIAbort('Aborted') | ||
|
||
mgr.cancel_host(host_id) | ||
|
||
click.secho('Dedicated Host %s was cancelled' % host_id, fg='green') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Cancel a dedicated host.""" | ||
# :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 | ||
|
||
|
||
@click.command() | ||
@click.argument('identifier') | ||
@environment.pass_env | ||
def cli(env, identifier): | ||
"""Cancel all virtual guests of the dedicated host immediately. | ||
|
||
Use the 'slcli vs cancel' command to cancel an specific guest | ||
""" | ||
|
||
dh_mgr = SoftLayer.DedicatedHostManager(env.client) | ||
|
||
host_id = helpers.resolve_id(dh_mgr.resolve_ids, identifier, 'dedicated host') | ||
|
||
if not (env.skip_confirmations or formatting.no_going_back(host_id)): | ||
raise exceptions.CLIAbort('Aborted') | ||
|
||
table = formatting.Table(['id', 'server name', 'status']) | ||
|
||
result = dh_mgr.cancel_guests(host_id) | ||
|
||
if result: | ||
for status in result: | ||
table.add_row([ | ||
status['id'], | ||
status['fqdn'], | ||
status['status'] | ||
]) | ||
|
||
env.fout(table) | ||
else: | ||
click.secho('There is not any guest into the dedicated host %s' % host_id, fg='red') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""List guests which are in a dedicated host server.""" | ||
# :license: MIT, see LICENSE for more details. | ||
|
||
import click | ||
|
||
import SoftLayer | ||
from SoftLayer.CLI import columns as column_helper | ||
from SoftLayer.CLI import environment | ||
from SoftLayer.CLI import formatting | ||
from SoftLayer.CLI import helpers | ||
|
||
COLUMNS = [ | ||
column_helper.Column('guid', ('globalIdentifier',)), | ||
column_helper.Column('cpu', ('maxCpu',)), | ||
column_helper.Column('memory', ('maxMemory',)), | ||
column_helper.Column('datacenter', ('datacenter', 'name')), | ||
column_helper.Column('primary_ip', ('primaryIpAddress',)), | ||
column_helper.Column('backend_ip', ('primaryBackendIpAddress',)), | ||
column_helper.Column( | ||
'created_by', | ||
('billingItem', 'orderItem', 'order', 'userRecord', 'username')), | ||
column_helper.Column('power_state', ('powerState', 'name')), | ||
column_helper.Column( | ||
'tags', | ||
lambda server: formatting.tags(server.get('tagReferences')), | ||
mask="tagReferences.tag.name"), | ||
] | ||
|
||
DEFAULT_COLUMNS = [ | ||
'id', | ||
'hostname', | ||
'domain', | ||
'primary_ip', | ||
'backend_ip', | ||
'power_state' | ||
] | ||
|
||
|
||
@click.command() | ||
@click.argument('identifier') | ||
@click.option('--cpu', '-c', help='Number of CPU cores', type=click.INT) | ||
@click.option('--domain', '-D', help='Domain portion of the FQDN') | ||
@click.option('--hostname', '-H', help='Host portion of the FQDN') | ||
@click.option('--memory', '-m', help='Memory in mebibytes', type=click.INT) | ||
@helpers.multi_option('--tag', help='Filter by tags') | ||
@click.option('--sortby', | ||
help='Column to sort by', | ||
default='hostname', | ||
show_default=True) | ||
@click.option('--columns', | ||
callback=column_helper.get_formatter(COLUMNS), | ||
help='Columns to display. [options: %s]' | ||
% ', '.join(column.name for column in COLUMNS), | ||
default=','.join(DEFAULT_COLUMNS), | ||
show_default=True) | ||
@environment.pass_env | ||
def cli(env, identifier, sortby, cpu, domain, hostname, memory, tag, columns): | ||
"""List guests which are in a dedicated host server.""" | ||
|
||
mgr = SoftLayer.DedicatedHostManager(env.client) | ||
guests = mgr.list_guests(host_id=identifier, | ||
cpus=cpu, | ||
hostname=hostname, | ||
domain=domain, | ||
memory=memory, | ||
tags=tag, | ||
mask=columns.mask()) | ||
|
||
table = formatting.Table(columns.columns) | ||
table.sortby = sortby | ||
|
||
for guest in guests: | ||
table.add_row([value or formatting.blank() | ||
for value in columns.row(guest)]) | ||
|
||
env.fout(table) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,10 +33,144 @@ def __init__(self, client, ordering_manager=None): | |
self.client = client | ||
self.account = client['Account'] | ||
self.host = client['Virtual_DedicatedHost'] | ||
self.guest = client['Virtual_Guest'] | ||
|
||
if ordering_manager is None: | ||
self.ordering_manager = ordering.OrderingManager(client) | ||
|
||
def cancel_host(self, host_id): | ||
"""Cancel a dedicated host immediately, it fails if there are still guests in the host. | ||
|
||
:param host_id: The ID of the dedicated host to be cancelled. | ||
:return: True on success or an exception | ||
|
||
Example:: | ||
# Cancels dedicated host id 12345 | ||
result = mgr.cancel_host(12345) | ||
|
||
""" | ||
return self.host.deleteObject(id=host_id) | ||
|
||
def cancel_guests(self, host_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this function, I'd like it to be able to return a structure that has a bit more information about what guests were canceled so the Ideally id, FQDN, and whether the guest was canceled or got an error
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, it returns a dictionary and it is empty if there isn't any guest in the dedicated host. The |
||
"""Cancel all guests into the dedicated host immediately. | ||
|
||
To cancel an specified guest use the method VSManager.cancel_instance() | ||
|
||
:param host_id: The ID of the dedicated host. | ||
:return: The id, fqdn and status of all guests into a dictionary. The status | ||
could be 'Cancelled' or an exception message, The dictionary is empty | ||
if there isn't any guest in the dedicated host. | ||
|
||
Example:: | ||
# Cancel guests of dedicated host id 12345 | ||
result = mgr.cancel_guests(12345) | ||
""" | ||
result = [] | ||
|
||
guests = self.host.getGuests(id=host_id, mask='id,fullyQualifiedDomainName') | ||
|
||
if guests: | ||
for vs in guests: | ||
status_info = { | ||
'id': vs['id'], | ||
'fqdn': vs['fullyQualifiedDomainName'], | ||
'status': self._delete_guest(vs['id']) | ||
} | ||
result.append(status_info) | ||
|
||
return result | ||
|
||
def list_guests(self, host_id, tags=None, cpus=None, memory=None, hostname=None, | ||
domain=None, local_disk=None, nic_speed=None, public_ip=None, | ||
private_ip=None, **kwargs): | ||
"""Retrieve a list of all virtual servers on the dedicated host. | ||
|
||
Example:: | ||
|
||
# Print out a list of instances with 4 cpu cores in the host id 12345. | ||
|
||
for vsi in mgr.list_guests(host_id=12345, cpus=4): | ||
print vsi['fullyQualifiedDomainName'], vsi['primaryIpAddress'] | ||
|
||
# Using a custom object-mask. Will get ONLY what is specified | ||
object_mask = "mask[hostname,monitoringRobot[robotStatus]]" | ||
for vsi in mgr.list_guests(mask=object_mask,cpus=4): | ||
print vsi | ||
|
||
:param integer host_id: the identifier of dedicated host | ||
:param list tags: filter based on list of tags | ||
:param integer cpus: filter based on number of CPUS | ||
:param integer memory: filter based on amount of memory | ||
:param string hostname: filter based on hostname | ||
:param string domain: filter based on domain | ||
:param string local_disk: filter based on local_disk | ||
:param integer nic_speed: filter based on network speed (in MBPS) | ||
:param string public_ip: filter based on public ip address | ||
:param string private_ip: filter based on private ip address | ||
:param dict \\*\\*kwargs: response-level options (mask, limit, etc.) | ||
:returns: Returns a list of dictionaries representing the matching | ||
virtual servers | ||
""" | ||
if 'mask' not in kwargs: | ||
items = [ | ||
'id', | ||
'globalIdentifier', | ||
'hostname', | ||
'domain', | ||
'fullyQualifiedDomainName', | ||
'primaryBackendIpAddress', | ||
'primaryIpAddress', | ||
'lastKnownPowerState.name', | ||
'hourlyBillingFlag', | ||
'powerState', | ||
'maxCpu', | ||
'maxMemory', | ||
'datacenter', | ||
'activeTransaction.transactionStatus[friendlyName,name]', | ||
'status', | ||
] | ||
kwargs['mask'] = "mask[%s]" % ','.join(items) | ||
|
||
_filter = utils.NestedDict(kwargs.get('filter') or {}) | ||
|
||
if tags: | ||
_filter['guests']['tagReferences']['tag']['name'] = { | ||
'operation': 'in', | ||
'options': [{'name': 'data', 'value': tags}], | ||
} | ||
|
||
if cpus: | ||
_filter['guests']['maxCpu'] = utils.query_filter(cpus) | ||
|
||
if memory: | ||
_filter['guests']['maxMemory'] = utils.query_filter(memory) | ||
|
||
if hostname: | ||
_filter['guests']['hostname'] = utils.query_filter(hostname) | ||
|
||
if domain: | ||
_filter['guests']['domain'] = utils.query_filter(domain) | ||
|
||
if local_disk is not None: | ||
_filter['guests']['localDiskFlag'] = ( | ||
utils.query_filter(bool(local_disk))) | ||
|
||
if nic_speed: | ||
_filter['guests']['networkComponents']['maxSpeed'] = ( | ||
utils.query_filter(nic_speed)) | ||
|
||
if public_ip: | ||
_filter['guests']['primaryIpAddress'] = ( | ||
utils.query_filter(public_ip)) | ||
|
||
if private_ip: | ||
_filter['guests']['primaryBackendIpAddress'] = ( | ||
utils.query_filter(private_ip)) | ||
|
||
kwargs['filter'] = _filter.to_dict() | ||
kwargs['iter'] = True | ||
return self.host.getGuests(id=host_id, **kwargs) | ||
|
||
def list_instances(self, tags=None, cpus=None, memory=None, hostname=None, | ||
disk=None, datacenter=None, **kwargs): | ||
"""Retrieve a list of all dedicated hosts on the account | ||
|
@@ -384,3 +518,13 @@ def get_router_options(self, datacenter=None, flavor=None): | |
item = self._get_item(package, flavor) | ||
|
||
return self._get_backend_router(location['location']['locationPackageDetails'], item) | ||
|
||
def _delete_guest(self, guest_id): | ||
"""Deletes a guest and returns 'Cancelled' or and Exception message""" | ||
msg = 'Cancelled' | ||
try: | ||
self.guest.deleteObject(id=guest_id) | ||
except SoftLayer.SoftLayerAPIError as e: | ||
msg = 'Exception: ' + e.faultString | ||
|
||
return msg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After updated the manager function, just add a way to print out a nice table with the results of each guests' cancelation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. It prints a table like below