Skip to content

Commit ef27235

Browse files
add metadata, hostname, and domain editing for hardware and CCI
Added docblocks for edit
1 parent b18f294 commit ef27235

4 files changed

Lines changed: 161 additions & 1 deletion

File tree

SoftLayer/CLI/modules/cci.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
The available commands are:
77
network Manage network settings
8+
edit Edit details of a CCI
89
create Order and create a CCI
910
(see `sl cci create-options` for choices)
1011
manage Manage active CCI
@@ -762,3 +763,48 @@ def sync_ptr_record():
762763

763764
if both or args.get('--PTR'):
764765
sync_ptr_record()
766+
767+
768+
class EditCCI(CLIRunnable):
769+
"""
770+
usage: sl cci edit <identifier> [options]
771+
772+
Edit CCI details
773+
774+
Options:
775+
-H --hostname=HOST Host portion of the FQDN. example: server
776+
-D --domain=DOMAIN Domain portion of the FQDN example: example.com
777+
-u --userdata=DATA User defined metadata string
778+
-F --userfile=FILE Read userdata from file
779+
"""
780+
action = 'edit'
781+
782+
@staticmethod
783+
def execute(client, args):
784+
data = {}
785+
786+
if args['--userdata'] and args['--userfile']:
787+
raise ArgumentError('[-u | --userdata] not allowed with '
788+
'[-F | --userfile]')
789+
if args['--userfile']:
790+
if not os.path.exists(args['--userfile']):
791+
raise ArgumentError(
792+
'File does not exist [-u | --userfile] = %s'
793+
% args['--userfile'])
794+
795+
if args.get('--userdata'):
796+
data['userdata'] = args['--userdata']
797+
elif args.get('--userfile'):
798+
f = open(args['--userfile'], 'r')
799+
try:
800+
data['userdata'] = f.read()
801+
finally:
802+
f.close()
803+
804+
data['hostname'] = args.get('--hostname')
805+
data['domain'] = args.get('--domain')
806+
807+
cci = CCIManager(client)
808+
cci_id = resolve_id(cci.resolve_ids, args.get('<identifier>'), 'CCI')
809+
if not cci.edit(cci_id, **data):
810+
raise CLIAbort("Failed to update CCI")

SoftLayer/CLI/modules/hardware.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
hostname or the ip address for a piece of hardware.
2020
"""
2121
import re
22+
import os
2223
from os import linesep
2324
from SoftLayer.CLI.helpers import (
2425
CLIRunnable, Table, FormattedItem, NestedDict, CLIAbort, blank, listing,
25-
SequentialOutput, gb, no_going_back, resolve_id, confirm)
26+
SequentialOutput, gb, no_going_back, resolve_id, confirm, ArgumentError)
2627
from SoftLayer import HardwareManager
2728

2829

@@ -746,3 +747,49 @@ def _get_price_id_from_options(cls, ds_options, option, value):
746747
price_id = item_options[1]
747748

748749
return price_id
750+
751+
752+
class EditHardware(CLIRunnable):
753+
"""
754+
usage: sl hardware edit <identifier> [options]
755+
756+
Edit hardware details
757+
758+
Options:
759+
-H --hostname=HOST Host portion of the FQDN. example: server
760+
-D --domain=DOMAIN Domain portion of the FQDN example: example.com
761+
-u --userdata=DATA User defined metadata string
762+
-F --userfile=FILE Read userdata from file
763+
"""
764+
action = 'edit'
765+
766+
@staticmethod
767+
def execute(client, args):
768+
data = {}
769+
770+
if args['--userdata'] and args['--userfile']:
771+
raise ArgumentError('[-u | --userdata] not allowed with '
772+
'[-F | --userfile]')
773+
if args['--userfile']:
774+
if not os.path.exists(args['--userfile']):
775+
raise ArgumentError(
776+
'File does not exist [-u | --userfile] = %s'
777+
% args['--userfile'])
778+
779+
if args.get('--userdata'):
780+
data['userdata'] = args['--userdata']
781+
elif args.get('--userfile'):
782+
f = open(args['--userfile'], 'r')
783+
try:
784+
data['userdata'] = f.read()
785+
finally:
786+
f.close()
787+
788+
data['hostname'] = args.get('--hostname')
789+
data['domain'] = args.get('--domain')
790+
791+
hw = HardwareManager(client)
792+
hw_id = resolve_id(hw.resolve_ids, args.get('<identifier>'),
793+
'hardware')
794+
if not hw.edit(hw_id, **data):
795+
raise CLIAbort("Failed to update hardware")

SoftLayer/managers/cci.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def get_instance(self, id, **kwargs):
143143
'activeTransaction.id',
144144
'blockDevices',
145145
'blockDeviceTemplateGroup[id, name]',
146+
'userData',
146147
'status.name',
147148
'operatingSystem.softwareLicense.'
148149
'softwareDescription[manufacturer,name,version,referenceCode]',
@@ -311,3 +312,35 @@ def _get_ids_from_ip(self, ip):
311312
results = self.list_instances(private_ip=ip, mask="id")
312313
if results:
313314
return [result['id'] for result in results]
315+
316+
def edit(self, id, userdata=None, hostname=None, domain=None, notes=None):
317+
""" Edit hostname, domain name, notes, and/or the user data of a CCI
318+
319+
Parameters set to None will be ignored and not attempted to be updated.
320+
321+
:param integer id: the instance ID to edit
322+
:param string userdata: user data on CCI to edit.
323+
If none exist it will be created
324+
:param string hostname: valid hostname
325+
:param string domain: valid domain namem
326+
:param string notes: notes about this particular CCI
327+
328+
"""
329+
330+
obj = {}
331+
if userdata:
332+
self.guest.setUserMetadata([userdata], id=id)
333+
334+
if hostname:
335+
obj['hostname'] = hostname
336+
337+
if domain:
338+
obj['domain'] = domain
339+
340+
if notes:
341+
obj['notes'] = notes
342+
343+
if not obj:
344+
return True
345+
346+
return self.guest.editObject(obj, id=id)

SoftLayer/managers/hardware.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def get_hardware(self, id, **kwargs):
217217
'notes',
218218
'primaryBackendIpAddress',
219219
'primaryIpAddress',
220+
'userData',
220221
'datacenter.name',
221222
'networkComponents[id, status, speed, maxSpeed, name,'
222223
'ipmiMacAddress, ipmiIpAddress, macAddress, primaryIpAddress,'
@@ -476,6 +477,39 @@ def _parse_package_data(self, id):
476477

477478
return results
478479

480+
def edit(self, id, userdata=None, hostname=None, domain=None, notes=None):
481+
""" Edit hostname, domain name, notes, and/or the
482+
user data of the hardware
483+
484+
Parameters set to None will be ignored and not attempted to be updated.
485+
486+
:param integer id: the instance ID to edit
487+
:param string userdata: user data on the hardware to edit.
488+
If none exist it will be created
489+
:param string hostname: valid hostname
490+
:param string domain: valid domain namem
491+
:param string notes: notes about this particular hardware
492+
493+
"""
494+
495+
obj = {}
496+
if userdata:
497+
self.hardware.setUserMetadata([userdata], id=id)
498+
499+
if hostname:
500+
obj['hostname'] = hostname
501+
502+
if domain:
503+
obj['domain'] = domain
504+
505+
if notes:
506+
obj['notes'] = notes
507+
508+
if not obj:
509+
return True
510+
511+
return self.hardware.editObject(obj, id=id)
512+
479513

480514
def get_default_value(package_options, category):
481515
if category not in package_options['categories']:

0 commit comments

Comments
 (0)