Skip to content

Commit ae99ac1

Browse files
author
Yingchao Huang
committed
dedicatedhost Edit and show tags
1. Add new cli command to edit tags of dedicatedhost 2. Show tags in dedicatedhost detail e.g. ``` slcli dedicatedhost edit --tag nicetag 258803 slcli --format=json dedicatedhost detail 258803 slcli --format=json dedicatedhost detail 258803 { "modify date": "", "name": "dev-ryans-cluster0650-cluster-service-compute0-0", "cpu count": 56, "router hostname": "bcr01a.dal10", "memory capacity": 242, "tags": [ "nicetag" ], "disk capacity": 1200, "guest count": 0, "create date": "2018-10-10T12:46:35-06:00", "router id": 843613, "owner": "[email protected]_2018-08-02-13.24.01", "datacenter": "dal10", "id": 258803 } ```
1 parent 3cb2917 commit ae99ac1

5 files changed

Lines changed: 124 additions & 1 deletion

File tree

SoftLayer/CLI/dedicatedhost/detail.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def cli(env, identifier, price=False, guests=False):
4040
table.add_row(['router hostname', result['backendRouter']['hostname']])
4141
table.add_row(['owner', formatting.FormattedItem(
4242
utils.lookup(result, 'billingItem', 'orderItem', 'order', 'userRecord', 'username') or formatting.blank(),)])
43+
table.add_row(['tags', formatting.tags(result['tagReferences'])])
4344

4445
if price:
4546
total_price = utils.lookup(result,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Edit dedicated host details."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import exceptions
9+
from SoftLayer.CLI import helpers
10+
11+
12+
@click.command()
13+
@click.argument('identifier')
14+
@click.option('--domain', '-D', help="Domain portion of the FQDN")
15+
@click.option('--userfile', '-F',
16+
help="Read userdata from file",
17+
type=click.Path(exists=True, readable=True, resolve_path=True))
18+
@click.option('--tag', '-g',
19+
multiple=True,
20+
help="Tags to set or empty string to remove all")
21+
@click.option('--hostname', '-H', help="Host portion of the FQDN")
22+
@click.option('--userdata', '-u', help="User defined metadata string")
23+
@click.option('--public-speed',
24+
help="Public port speed.",
25+
default=None,
26+
type=click.Choice(['0', '10', '100', '1000', '10000']))
27+
@click.option('--private-speed',
28+
help="Private port speed.",
29+
default=None,
30+
type=click.Choice(['0', '10', '100', '1000', '10000']))
31+
@environment.pass_env
32+
def cli(env, identifier, domain, userfile, tag, hostname, userdata,
33+
public_speed, private_speed):
34+
"""Edit dedicated host details."""
35+
36+
if userdata and userfile:
37+
raise exceptions.ArgumentError(
38+
'[-u | --userdata] not allowed with [-F | --userfile]')
39+
40+
data = {
41+
'hostname': hostname,
42+
'domain': domain,
43+
}
44+
if userdata:
45+
data['userdata'] = userdata
46+
elif userfile:
47+
with open(userfile, 'r') as userfile_obj:
48+
data['userdata'] = userfile_obj.read()
49+
if tag:
50+
data['tags'] = ','.join(tag)
51+
52+
mgr = SoftLayer.DedicatedHostManager(env.client)
53+
54+
if not mgr.edit(identifier, **data):
55+
raise exceptions.CLIAbort("Failed to update dedicated host")
56+
57+
if public_speed is not None:
58+
mgr.change_port_speed(identifier, True, int(public_speed))
59+
60+
if private_speed is not None:
61+
mgr.change_port_speed(identifier, False, int(private_speed))

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
('dedicatedhost:cancel', 'SoftLayer.CLI.dedicatedhost.cancel:cli'),
5151
('dedicatedhost:cancel-guests', 'SoftLayer.CLI.dedicatedhost.cancel_guests:cli'),
5252
('dedicatedhost:list-guests', 'SoftLayer.CLI.dedicatedhost.list_guests:cli'),
53+
('dedicatedhost:edit', 'SoftLayer.CLI.dedicatedhost.edit:cli'),
5354

5455
('cdn', 'SoftLayer.CLI.cdn'),
5556
('cdn:detail', 'SoftLayer.CLI.cdn.detail:cli'),

SoftLayer/managers/dedicated_host.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,50 @@ def list_guests(self, host_id, tags=None, cpus=None, memory=None, hostname=None,
171171
kwargs['iter'] = True
172172
return self.host.getGuests(id=host_id, **kwargs)
173173

174+
def edit(self, host_id, userdata=None, hostname=None, domain=None,
175+
notes=None, tags=None):
176+
"""Edit hostname, domain name, notes, user data of the dedicated host.
177+
178+
Parameters set to None will be ignored and not attempted to be updated.
179+
180+
:param integer host_id: the instance ID to edit
181+
:param string userdata: user data on the dedicated host to edit.
182+
If none exist it will be created
183+
:param string hostname: valid hostname
184+
:param string domain: valid domain name
185+
:param string notes: notes about this particular dedicated host
186+
:param string tags: tags to set on the dedicated host as a comma
187+
separated list. Use the empty string to remove all
188+
tags.
189+
190+
Example::
191+
192+
# Change the hostname on instance 12345 to 'something'
193+
result = mgr.edit(host_id=12345 , hostname="something")
194+
#result will be True or an Exception
195+
"""
196+
197+
obj = {}
198+
if userdata:
199+
self.host.setUserMetadata([userdata], id=host_id)
200+
201+
if tags is not None:
202+
self.host.setTags(tags, id=host_id)
203+
204+
if hostname:
205+
obj['hostname'] = hostname
206+
207+
if domain:
208+
obj['domain'] = domain
209+
210+
if notes:
211+
obj['notes'] = notes
212+
213+
if not obj:
214+
return True
215+
216+
return self.host.editObject(obj, id=host_id)
217+
174218
def list_instances(self, tags=None, cpus=None, memory=None, hostname=None,
175219
disk=None, datacenter=None, **kwargs):
176220
"""Retrieve a list of all dedicated hosts on the account
@@ -305,7 +349,14 @@ def get_host(self, host_id, **kwargs):
305349
domain,
306350
uuid
307351
],
308-
guestCount
352+
guestCount,
353+
tagReferences[
354+
id,
355+
tag[
356+
name,
357+
id
358+
]
359+
]
309360
''')
310361

311362
return self.host.getObject(id=host_id, **kwargs)
@@ -553,3 +604,11 @@ def _delete_guest(self, guest_id):
553604
msg = 'Exception: ' + e.faultString
554605

555606
return msg
607+
608+
# @retry(logger=LOGGER)
609+
def set_tags(self, tags, host_id):
610+
"""Sets tags on a dedicated_host with a retry decorator
611+
612+
Just calls guest.setTags, but if it fails from an APIError will retry
613+
"""
614+
self.host.setTags(tags, id=host_id)

tools/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
prettytable >= 0.7.0
12
six >= 1.7.0
23
ptable >= 0.9.2
34
click >= 7

0 commit comments

Comments
 (0)