def cli(env, identifier, name, minimum, maximum, userdata, userfile, cpu, memory): """Edits an Autoscale group.""" template = {} autoscale = AutoScaleManager(env.client) group = autoscale.details(identifier) template['name'] = name template['minimumMemberCount'] = minimum template['maximumMemberCount'] = maximum virt_template = {} if userdata: virt_template['userData'] = [{"value": userdata}] elif userfile: with open(userfile, 'r', encoding="utf-8") as userfile_obj: virt_template['userData'] = [{"value": userfile_obj.read()}] virt_template['startCpus'] = cpu virt_template['maxMemory'] = memory # Remove any entries that are `None` as the API will complain about them. template['virtualGuestMemberTemplate'] = clean_dict(virt_template) clean_template = clean_dict(template) # If there are any values edited in the template, we need to get the OLD template values and replace them. if template['virtualGuestMemberTemplate']: # Update old template with new values for key, value in clean_template['virtualGuestMemberTemplate'].items(): group['virtualGuestMemberTemplate'][key] = value clean_template['virtualGuestMemberTemplate'] = group[ 'virtualGuestMemberTemplate'] autoscale.edit(identifier, clean_template) click.echo("Done")
def cli(env, identifier): """Get details of an Autoscale groups.""" autoscale = AutoScaleManager(env.client) group = autoscale.details(identifier) # Group Config Table table = formatting.KeyValueTable(["Group", "Value"]) table.align['Group'] = 'l' table.align['Value'] = 'l' table.add_row(['Id', group.get('id')]) # Ideally we would use regionalGroup->preferredDatacenter, but that generates an error. table.add_row(['Datacenter', group['regionalGroup']['locations'][0]['longName']]) table.add_row(['Termination', utils.lookup(group, 'terminationPolicy', 'name')]) table.add_row(['Minimum Members', group.get('minimumMemberCount')]) table.add_row(['Maximum Members', group.get('maximumMemberCount')]) table.add_row(['Current Members', group.get('virtualGuestMemberCount')]) table.add_row(['Cooldown', "{} seconds".format(group.get('cooldown'))]) table.add_row(['Last Action', utils.clean_time(group.get('lastActionDate'))]) for network in group.get('networkVlans', []): network_type = utils.lookup(network, 'networkVlan', 'networkSpace') router = utils.lookup(network, 'networkVlan', 'primaryRouter', 'hostname') vlan_number = utils.lookup(network, 'networkVlan', 'vlanNumber') vlan_name = "{}.{}".format(router, vlan_number) table.add_row([network_type, vlan_name]) env.fout(table) # Template Config Table config_table = formatting.KeyValueTable(["Template", "Value"]) config_table.align['Template'] = 'l' config_table.align['Value'] = 'l' template = group.get('virtualGuestMemberTemplate') config_table.add_row(['Hostname', template.get('hostname')]) config_table.add_row(['Domain', template.get('domain')]) config_table.add_row(['Core', template.get('startCpus')]) config_table.add_row(['Ram', template.get('maxMemory')]) network = template.get('networkComponents') config_table.add_row(['Network', network[0]['maxSpeed'] if network else 'Default']) ssh_keys = template.get('sshKeys', []) ssh_manager = SoftLayer.SshKeyManager(env.client) for key in ssh_keys: # Label isn't included when retrieved from the AutoScale group... ssh_key = ssh_manager.get_key(key.get('id')) config_table.add_row(['SSH Key {}'.format(ssh_key.get('id')), ssh_key.get('label')]) disks = template.get('blockDevices', []) disk_type = "Local" if template.get('localDiskFlag') else "SAN" for disk in disks: disk_image = disk.get('diskImage') config_table.add_row(['{} Disk {}'.format(disk_type, disk.get('device')), disk_image.get('capacity')]) config_table.add_row(['OS', template.get('operatingSystemReferenceCode')]) config_table.add_row(['Post Install', template.get('postInstallScriptUri') or 'None']) env.fout(config_table) # Policy Config Table policy_table = formatting.KeyValueTable(["Policy", "Cooldown"]) policies = group.get('policies', []) for policy in policies: policy_table.add_row([policy.get('name'), policy.get('cooldown') or group.get('cooldown')]) env.fout(policy_table) # Active Guests member_table = formatting.Table(['Id', 'Hostname', 'Created'], title="Active Guests") guests = group.get('virtualGuestMembers', []) for guest in guests: real_guest = guest.get('virtualGuest') member_table.add_row([ real_guest.get('id'), real_guest.get('hostname'), utils.clean_time(real_guest.get('provisionDate')) ]) env.fout(member_table)
class AutoScaleTests(testing.TestCase): def set_up(self): self.autoscale = AutoScaleManager(self.client) def test_autoscale_list(self): self.autoscale.list() self.assert_called_with('SoftLayer_Account', 'getScaleGroups') def test_autoscale_list_with_mask(self): self.autoscale.list(mask='mask[status,virtualGuestMemberCount]') self.assert_called_with('SoftLayer_Account', 'getScaleGroups') def test_autoscale_details(self): self.autoscale.details(11111) self.assert_called_with('SoftLayer_Scale_Group', 'getObject', identifier=11111) def test_autoscale_details_with_mask(self): self.autoscale.details( 11111, mask= 'mask[virtualGuestMembers[id,virtualGuest[hostname,domain,provisionDate]], ' 'terminationPolicy,virtualGuestMemberCount]') self.assert_called_with('SoftLayer_Scale_Group', 'getObject', identifier=11111) def test_autoscale_policy(self): self.autoscale.get_policy(11111) self.assert_called_with('SoftLayer_Scale_Policy', 'getObject', identifier=11111) def test_autoscale_policy_with_mask(self): self.autoscale.get_policy( 11111, mask='mask[cooldown, createDate, id, name, actions, triggers[type]]' ) self.assert_called_with('SoftLayer_Scale_Policy', 'getObject', identifier=11111) def test_autoscale_scale(self): self.autoscale.scale(11111, 3) self.assert_called_with('SoftLayer_Scale_Group', 'scale', identifier=11111) def test_autoscale_scaleTo(self): self.autoscale.scale_to(11111, 3) self.assert_called_with('SoftLayer_Scale_Group', 'scaleTo', identifier=11111) def test_autoscale_getLogs(self): self.autoscale.get_logs(11111) self.assert_called_with('SoftLayer_Scale_Group', 'getLogs', identifier=11111) def test_autoscale_get_virtual_guests(self): self.autoscale.get_virtual_guests(11111) self.assert_called_with('SoftLayer_Scale_Group', 'getVirtualGuestMembers', identifier=11111, mask=None) def test_autoscale_get_virtual_guests_mask(self): test_mask = "mask[id]" self.autoscale.get_virtual_guests(11111, mask=test_mask) self.assert_called_with('SoftLayer_Scale_Group', 'getVirtualGuestMembers', identifier=11111, mask=test_mask) def test_edit_object(self): template = {'name': 'test'} self.autoscale.edit(12345, template) self.assert_called_with('SoftLayer_Scale_Group', 'editObject', args=(template, ), identifier=12345)
class AutoScaleTests(testing.TestCase): def set_up(self): self.autoscale = AutoScaleManager(self.client) def test_autoscale_list(self): self.autoscale.list() self.assert_called_with('SoftLayer_Account', 'getScaleGroups') def test_autoscale_list_with_mask(self): self.autoscale.list(mask='mask[status,virtualGuestMemberCount]') self.assert_called_with('SoftLayer_Account', 'getScaleGroups') def test_autoscale_details(self): self.autoscale.details(11111) self.assert_called_with('SoftLayer_Scale_Group', 'getObject', identifier=11111) def test_autoscale_details_with_mask(self): self.autoscale.details( 11111, mask= 'mask[virtualGuestMembers[id,virtualGuest[hostname,domain,provisionDate]], ' 'terminationPolicy,virtualGuestMemberCount]') self.assert_called_with('SoftLayer_Scale_Group', 'getObject', identifier=11111) def test_autoscale_policy(self): self.autoscale.get_policy(11111) self.assert_called_with('SoftLayer_Scale_Policy', 'getObject', identifier=11111) def test_autoscale_policy_with_mask(self): self.autoscale.get_policy( 11111, mask='mask[cooldown, createDate, id, name, actions, triggers[type]]' ) self.assert_called_with('SoftLayer_Scale_Policy', 'getObject', identifier=11111) def test_autoscale_scale(self): self.autoscale.scale(11111, 3) self.assert_called_with('SoftLayer_Scale_Group', 'scale', identifier=11111) def test_autoscale_scaleTo(self): self.autoscale.scale_to(11111, 3) self.assert_called_with('SoftLayer_Scale_Group', 'scaleTo', identifier=11111) def test_autoscale_getLogs(self): self.autoscale.get_logs(11111) self.assert_called_with('SoftLayer_Scale_Group', 'getLogs', identifier=11111) def test_autoscale_get_virtual_guests(self): self.autoscale.get_virtual_guests(11111) self.assert_called_with('SoftLayer_Scale_Group', 'getVirtualGuestMembers', identifier=11111, mask=None) def test_autoscale_get_virtual_guests_mask(self): test_mask = "mask[id]" self.autoscale.get_virtual_guests(11111, mask=test_mask) self.assert_called_with('SoftLayer_Scale_Group', 'getVirtualGuestMembers', identifier=11111, mask=test_mask) def test_edit_object(self): template = {'name': 'test'} self.autoscale.edit(12345, template) self.assert_called_with('SoftLayer_Scale_Group', 'editObject', args=(template, ), identifier=12345) def test_create_object(self): template = { 'name': 'test', 'cooldown': 3600, 'maximumMemberCount': 5, 'minimumMemberCount': 1, 'regionalGroupId': 4568, 'suspendedFlag': False, 'balancedTerminationFlag': False, 'virtualGuestMemberTemplate': { 'domain': 'test.com', 'hostname': 'testvs', 'operatingSystemReferenceCode': 'CENTOS_7_64', 'maxMemory': 2048, 'datacenter': { 'id': 265592 }, 'startCpus': 2, 'blockDevices': [{ 'diskImage': { 'capacity': '100' }, 'device': 0 }], 'hourlyBillingFlag': True, 'networkComponents': [{ 'maxSpeed': 100 }], 'localDiskFlag': True, 'typeId': 1, 'userData': [{ 'value': 'the userData' }] }, 'virtualGuestMemberCount': 0, 'terminationPolicyId': '2', } self.autoscale.create(template) self.assert_called_with('SoftLayer_Scale_Group', 'createObject', args=(template, )) def test_delete_object(self): self.autoscale.delete(12345) self.assert_called_with('SoftLayer_Scale_Group', 'forceDeleteObject')