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
2 changes: 1 addition & 1 deletion SoftLayer/CLI/tags/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""Manage Tags"""
"""Manage Tags"""
19 changes: 11 additions & 8 deletions SoftLayer/CLI/tags/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

import click

from SoftLayer.exceptions import SoftLayerAPIError
from SoftLayer.managers.tags import TagManager
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.exceptions import SoftLayerAPIError
from SoftLayer.managers.tags import TagManager
from SoftLayer import utils

# pylint: disable=unnecessary-lambda

from pprint import pprint as pp

@click.command()
@click.option('--detail', '-d', is_flag=True, default=False,
Expand All @@ -21,7 +20,7 @@ def cli(env, detail):
"""List Tags."""

tag_manager = TagManager(env.client)

if detail:
tables = detailed_table(tag_manager)
for table in tables:
Expand All @@ -33,7 +32,9 @@ def cli(env, detail):


def tag_row(tag):
return [tag.get('id'), tag.get('name'), tag.get('referenceCount',0)]
"""Format a tag table row"""
return [tag.get('id'), tag.get('name'), tag.get('referenceCount', 0)]


def detailed_table(tag_manager):
"""Creates a table for each tag, with details about resources using it"""
Expand All @@ -52,6 +53,7 @@ def detailed_table(tag_manager):

return tables


def simple_table(tag_manager):
"""Just tags and how many resources on each"""
tags = tag_manager.list_tags()
Expand All @@ -62,6 +64,7 @@ def simple_table(tag_manager):
table.add_row(tag_row(tag))
return table


def get_resource_name(tag_manager, resource_id, tag_type):
"""Returns a string to identify a resource"""
try:
Expand All @@ -70,6 +73,6 @@ def get_resource_name(tag_manager, resource_id, tag_type):
resource_row = resource.get('primaryIpAddress')
else:
resource_row = resource.get('fullyQualifiedDomainName')
except SoftLayerAPIError as e:
resource_row = "{}".format(e.reason)
return resource_row
except SoftLayerAPIError as exception:
resource_row = "{}".format(exception.reason)
return resource_row
59 changes: 59 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Hardware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
getObject = {
'id': 1234,
'globalIdentifier': 'xxxxc-asd',
'datacenter': {'id': 12, 'name': 'DALLAS21',
'description': 'Dallas 21'},
'billingItem': {
'id': 6327,
'recurringFee': 1.54,
'nextInvoiceTotalRecurringAmount': 16.08,
'children': [
{'description': 'test', 'nextInvoiceTotalRecurringAmount': 1},
],
'orderItem': {
'order': {
'userRecord': {
'username': 'bob',
}
}
}
},
'primaryIpAddress': '4.4.4.4',
'hostname': 'testtest1',
'domain': 'test.sftlyr.ws',
'bareMetalInstanceFlag': True,
'fullyQualifiedDomainName': 'testtest1.test.sftlyr.ws',
'processorPhysicalCoreAmount': 4,
'memoryCapacity': 4,
'primaryBackendIpAddress': '10.4.4.4',
'networkManagementIpAddress': '10.4.4.4',
'hardwareStatus': {'status': 'ACTIVE'},
'primaryNetworkComponent': {'maxSpeed': 1000, 'speed': 1000},
'provisionDate': '2020-08-01 15:23:45',
'notes': 'NOTES NOTES NOTES',
'operatingSystem': {
'softwareLicense': {
'softwareDescription': {
'referenceCode': 'UBUNTU_20_64',
'name': 'Ubuntu',
'version': 'Ubuntu 20.04 LTS',
}
},
'passwords': [
{'username': 'root', 'password': 'xxxxxxxxxxxx'}
],
},
'remoteManagementAccounts': [
{'username': 'root', 'password': 'zzzzzzzzzzzzzz'}
],
'networkVlans': [
{
'networkSpace': 'PRIVATE',
'vlanNumber': 1234,
'id': 11111
},
],
'tagReferences': [
{'tag': {'name': 'a tag'}}
],
}
32 changes: 16 additions & 16 deletions SoftLayer/fixtures/SoftLayer_Network_Storage_Allowed_Host.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@
getObject = TEST_ALLOWED_HOST

getSubnetsInAcl = [{
'id': 12345678,
'accountId': 1234,
'networkIdentifier': '10.11.12.13',
'cidr': '14',
'billingRecordId': None,
'parentId': None,
'networkVlanId': None,
'createDate': '2020-01-02 00:00:01',
'modifyDate': None,
'subnetType': 'SECONDARY_ON_VLAN',
'restrictAllocationFlag': 0,
'leafFlag': 1,
'ownerId': 1,
'ipAddressBegin': 129123,
'ipAddressEnd': 129145,
'purgeFlag': 0
'id': 12345678,
'accountId': 1234,
'networkIdentifier': '10.11.12.13',
'cidr': '14',
'billingRecordId': None,
'parentId': None,
'networkVlanId': None,
'createDate': '2020-01-02 00:00:01',
'modifyDate': None,
'subnetType': 'SECONDARY_ON_VLAN',
'restrictAllocationFlag': 0,
'leafFlag': 1,
'ownerId': 1,
'ipAddressBegin': 129123,
'ipAddressEnd': 129145,
'purgeFlag': 0
}]

assignSubnetsToAcl = [
Expand Down
16 changes: 16 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
getUnattachedTagsForCurrentUser = [{'id': 287895, 'name': 'coreos', 'referenceCount': 0}]
getAttachedTagsForCurrentUser = [{'id': 1286571, 'name': 'bs_test_instance', 'referenceCount': 5}]
getReferences = [
{
'id': 73009305,
'resourceTableId': 33488921,
'tag': {
'id': 1286571,
'name': 'bs_test_instance',
},
'tagId': 1286571,
'tagType': {'description': 'CCI', 'keyName': 'GUEST'},
'tagTypeId': 2,
'usrRecordId': 6625205
}
]
28 changes: 18 additions & 10 deletions SoftLayer/managers/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from SoftLayer.exceptions import SoftLayerAPIError


class TagManager(object):
"""Manager for Tag functions."""

Expand All @@ -25,21 +26,31 @@ def list_tags(self, mask=None):
unattached = self.get_unattached_tags(mask)
attached = self.get_attached_tags(mask)
return {'attached': attached, 'unattached': unattached}
# return [unattached, attached]

def get_unattached_tags(self, mask=None):
"""Calls SoftLayer_Tag::getUnattachedTagsForCurrentUser()"""
"""Calls SoftLayer_Tag::getUnattachedTagsForCurrentUser()

:params string mask: Mask to use.
"""
return self.client.call('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser',
mask=mask, iter=True)

def get_attached_tags(self, mask=None):
"""Calls SoftLayer_Tag::getAttachedTagsForCurrentUser()"""
"""Calls SoftLayer_Tag::getAttachedTagsForCurrentUser()

:params string mask: Mask to use.
"""
return self.client.call('SoftLayer_Tag', 'getAttachedTagsForCurrentUser',
mask=mask, iter=True)

def get_tag_references(self, tag_id, mask=None):
"""Calls SoftLayer_Tag::getReferences(id=tag_id)

:params int tag_id: Tag id to get references from
:params string mask: Mask to use.
"""
if mask is None:
mask="mask[tagType]"
mask = "mask[tagType]"
return self.client.call('SoftLayer_Tag', 'getReferences', id=tag_id, mask=mask, iter=True)

def reference_lookup(self, resource_table_id, tag_type):
Expand All @@ -50,8 +61,8 @@ def reference_lookup(self, resource_table_id, tag_type):

From SoftLayer_Tag::getAllTagTypes()

|Type |Service |
| ----------------------------- | ------ |
|Type |Service |
| ----------------------------- | ------ |
|Hardware |HARDWARE|
|CCI |GUEST|
|Account Document |ACCOUNT_DOCUMENT|
Expand All @@ -67,7 +78,7 @@ def reference_lookup(self, resource_table_id, tag_type):
if tag_type in ['ACCOUNT_DOCUMENT', 'CONTRACT']:
raise SoftLayerAPIError(404, "Unable to lookup {} types".format(tag_type))

if tag_type == 'APPLICATION_DELIVERY_CONTROLLER' :
if tag_type == 'APPLICATION_DELIVERY_CONTROLLER':
service = 'Network_Application_Delivery_Controller'
elif tag_type == 'GUEST':
service = 'Virtual_Guest'
Expand All @@ -82,6 +93,3 @@ def reference_lookup(self, resource_table_id, tag_type):

# return {}
return self.client.call(service, 'getObject', id=resource_table_id)



2 changes: 1 addition & 1 deletion tests/CLI/modules/block_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_volume_detail_name_identifier(self):
'keyName': {'operation': '*= BLOCK_STORAGE'}
},
'username': {'operation': '_= SL-12345'}
}
}
}

self.assert_called_with('SoftLayer_Account', 'getIscsiNetworkStorage', filter=expected_filter)
Expand Down
36 changes: 18 additions & 18 deletions tests/CLI/modules/server_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,19 +694,19 @@ def test_dns_sync_both(self, confirm_mock):
'getResourceRecords')
getResourceRecords.return_value = []
createAargs = ({
'type': 'a',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '172.16.1.100',
'ttl': 7200
},)
'type': 'a',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '172.16.1.100',
'ttl': 7200
},)
createPTRargs = ({
'type': 'ptr',
'host': '100',
'domainId': 123456,
'data': 'hardware-test1.test.sftlyr.ws',
'ttl': 7200
},)
'type': 'ptr',
'host': '100',
'domainId': 123456,
'data': 'hardware-test1.test.sftlyr.ws',
'ttl': 7200
},)

result = self.run_command(['hw', 'dns-sync', '1000'])

Expand Down Expand Up @@ -749,12 +749,12 @@ def test_dns_sync_v6(self, confirm_mock):
}
}
createV6args = ({
'type': 'aaaa',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
'ttl': 7200
},)
'type': 'aaaa',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
'ttl': 7200
},)
server.return_value = test_server
result = self.run_command(['hw', 'dns-sync', '--aaaa-record', '1000'])
self.assert_no_fail(result)
Expand Down
26 changes: 26 additions & 0 deletions tests/CLI/modules/tag_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
SoftLayer.tests.CLI.modules.tag_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Tests for the user cli command
"""
from SoftLayer import testing


class TagCLITests(testing.TestCase):

def test_list(self):
result = self.run_command(['tags', 'list'])
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
self.assert_called_with('SoftLayer_Tag', 'getAttachedTagsForCurrentUser')
self.assertIn('coreos', result.output)

def test_list_detail(self):
result = self.run_command(['tags', 'list', '-d'])
self.assert_no_fail(result)
self.assertIn('"vs-test1.test.sftlyr.ws', result.output) # From fixtures/virutal_guest.getObject
# self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
self.assert_called_with('SoftLayer_Tag', 'getAttachedTagsForCurrentUser')
self.assert_called_with('SoftLayer_Tag', 'getReferences', identifier=1286571)
self.assert_called_with('SoftLayer_Virtual_Guest', 'getObject', identifier=33488921)
36 changes: 18 additions & 18 deletions tests/CLI/modules/vs/vs_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,19 @@ def test_dns_sync_both(self, confirm_mock):
'getResourceRecords')
getResourceRecords.return_value = []
createAargs = ({
'type': 'a',
'host': 'vs-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '172.16.240.2',
'ttl': 7200
},)
'type': 'a',
'host': 'vs-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '172.16.240.2',
'ttl': 7200
},)
createPTRargs = ({
'type': 'ptr',
'host': '2',
'domainId': 123456,
'data': 'vs-test1.test.sftlyr.ws',
'ttl': 7200
},)
'type': 'ptr',
'host': '2',
'domainId': 123456,
'data': 'vs-test1.test.sftlyr.ws',
'ttl': 7200
},)

result = self.run_command(['vs', 'dns-sync', '100'])

Expand Down Expand Up @@ -425,12 +425,12 @@ def test_dns_sync_v6(self, confirm_mock):
}
}
createV6args = ({
'type': 'aaaa',
'host': 'vs-test1',
'domainId': 12345,
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
'ttl': 7200
},)
'type': 'aaaa',
'host': 'vs-test1',
'domainId': 12345,
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
'ttl': 7200
},)
guest.return_value = test_guest
result = self.run_command(['vs', 'dns-sync', '--aaaa-record', '100'])
self.assert_no_fail(result)
Expand Down
Loading