Skip to content

Commit a48c7c5

Browse files
caberoscaberos
authored andcommitted
create a new commands on slcli that create/cancel a VMware licenses simulate to IBMCloud portal
1 parent c52b9f0 commit a48c7c5

File tree

10 files changed

+269
-0
lines changed

10 files changed

+269
-0
lines changed

SoftLayer/CLI/licenses/__init__.py

Whitespace-only changes.

SoftLayer/CLI/licenses/cancel.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Cancel VMware licenses."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
from SoftLayer import utils
6+
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import exceptions
9+
from SoftLayer.managers.license import LicensesManager
10+
11+
12+
@click.command()
13+
@click.argument('key')
14+
@click.option('--immediate', is_flag=True, help='Immediate cancellation')
15+
@environment.pass_env
16+
def cli(env, key, immediate):
17+
"""Cancel VMware license."""
18+
19+
if not immediate:
20+
immediate = False
21+
vmware_find = False
22+
license = LicensesManager(env.client)
23+
24+
vmware_licenses = license.get_all_objects()
25+
26+
for vmware in vmware_licenses:
27+
if vmware.get('key') == key:
28+
vmware_find = True
29+
license.cancel_item(utils.lookup(vmware, 'billingItem', 'id'),
30+
immediate,
31+
'Cancel by cli command',
32+
'Cancel by cli command')
33+
break
34+
35+
if not vmware_find:
36+
raise exceptions.CLIAbort(
37+
"The VMware not found, try whit another key")

SoftLayer/CLI/licenses/create.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Order/create a vwmare licenses."""
2+
# :licenses: MIT, see LICENSE for more details.
3+
4+
import click
5+
from SoftLayer.managers import ordering
6+
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import formatting
9+
10+
11+
@click.command()
12+
@click.option('--key', '-k', required=True, prompt=True, help="The License Key for this specific Account License.")
13+
@click.option('--datacenter', '-d', required=True, prompt=True, help="Datacenter shortname")
14+
@environment.pass_env
15+
def cli(env, key, datacenter):
16+
"""Order/create a vlan instance."""
17+
18+
complex_type = 'SoftLayer_Container_Product_Order_Software_License'
19+
item_package = [key]
20+
21+
ordering_manager = ordering.OrderingManager(env.client)
22+
result = ordering_manager.place_order(package_keyname='SOFTWARE_LICENSE_PACKAGE',
23+
location=datacenter,
24+
item_keynames=item_package,
25+
complex_type=complex_type,
26+
hourly=False)
27+
table = formatting.KeyValueTable(['name', 'value'])
28+
table.align['name'] = 'r'
29+
table.align['value'] = 'l'
30+
table.add_row(['id', result['orderId']])
31+
table.add_row(['created', result['orderDate']])
32+
33+
env.fout(table)

SoftLayer/CLI/routes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@
217217
('nas:list', 'SoftLayer.CLI.nas.list:cli'),
218218
('nas:credentials', 'SoftLayer.CLI.nas.credentials:cli'),
219219

220+
('licenses', 'SoftLayer.CLI.licenses'),
221+
('licenses:create', 'SoftLayer.CLI.licenses.create:cli'),
222+
('licenses:cancel', 'SoftLayer.CLI.licenses.cancel:cli'),
223+
220224
('object-storage', 'SoftLayer.CLI.object_storage'),
221225

222226
('object-storage:accounts', 'SoftLayer.CLI.object_storage.list_accounts:cli'),

SoftLayer/fixtures/SoftLayer_Product_Order.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,35 @@
252252
]
253253
}
254254
}
255+
256+
wmware_placeOrder = {
257+
"orderDate": "2021-06-02 15:23:47",
258+
"orderId": 123456,
259+
"prices": [
260+
{
261+
"id": 176535,
262+
"itemId": 8109,
263+
"categories": [
264+
{
265+
"categoryCode": "software_license",
266+
"id": 438,
267+
"name": "Software License"
268+
}
269+
],
270+
"item": {
271+
"capacity": "1",
272+
"description": "VMware vSAN Advanced Tier III 64 - 124 TB 6.x",
273+
"id": 8109,
274+
"keyName": "VMWARE_VSAN_ADVANCE_TIER_III_64_124_6_X",
275+
"softwareDescription": {
276+
"id": 1795,
277+
},
278+
"thirdPartyPolicyAssignments": [
279+
{
280+
"id": 29263,
281+
"policyName": "3rd Party Software Terms VMWare v4"
282+
}
283+
]
284+
}
285+
}
286+
]}

SoftLayer/fixtures/SoftLayer_Product_Package.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,3 +2055,29 @@
20552055
"categoryCode": "dedicated_virtual_hosts"
20562056
}
20572057
}]}
2058+
2059+
getItems_vmware = [{
2060+
"capacity": "2",
2061+
"description": "VMware vSAN Enterprise Tier III 65 - 124 TB 6.x",
2062+
"id": 9567,
2063+
"itemTaxCategoryId": 166,
2064+
"keyName": "VMWARE_VSAN_ENTERPRISE_TIER_III_65_124_TB_6_X_2",
2065+
"softwareDescriptionId": 1979,
2066+
"units": "CPU",
2067+
"itemCategory": {
2068+
"categoryCode": "software_license",
2069+
"id": 438,
2070+
"name": "Software License",
2071+
"quantityLimit": 1,
2072+
},
2073+
"prices": [
2074+
{
2075+
"id": 245164,
2076+
"itemId": 9567,
2077+
"laborFee": "0",
2078+
"locationGroupId": None,
2079+
"oneTimeFee": "0",
2080+
"setupFee": "0",
2081+
"sort": 0,
2082+
}
2083+
]}]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
getAllObjects = [{
2+
"capacity": "4",
3+
"key": "ABCDE-6CJ8L-J8R9H-000R0-CDR70",
4+
"units": "CPU",
5+
"billingItem": {
6+
"allowCancellationFlag": 1,
7+
"categoryCode": "software_license",
8+
"createDate": "2018-10-22T11:16:48-06:00",
9+
"cycleStartDate": "2021-06-03T23:11:22-06:00",
10+
"description": "vCenter Server Appliance 6.0",
11+
"id": 123654789,
12+
"lastBillDate": "2021-06-03T23:11:22-06:00",
13+
"modifyDate": "2021-06-03T23:11:22-06:00",
14+
"nextBillDate": "2021-07-03T23:00:00-06:00",
15+
"orderItemId": 385054741,
16+
"recurringMonths": 1,
17+
"serviceProviderId": 1,
18+
},
19+
"softwareDescription": {
20+
"id": 1529,
21+
"longDescription": "VMware vCenter 6.0",
22+
"manufacturer": "VMware",
23+
"name": "vCenter",
24+
"version": "6.0",
25+
"requiredUser": "administrator@vsphere.local"
26+
}
27+
},
28+
{
29+
"capacity": "1",
30+
"key": "CBERT-4RL92-K8999-031K4-AJF5J",
31+
"units": "CPU",
32+
"billingItem": {
33+
"allowCancellationFlag": 1,
34+
"categoryCode": "software_license",
35+
"createDate": "2021-06-09T14:51:38-06:00",
36+
"cycleStartDate": "2021-06-09T14:51:38-06:00",
37+
"description": "VMware vSAN Advanced Tier III 64 - 124 TB 6.x",
38+
"id": 369852174,
39+
"nextBillDate": "2021-07-03T23:00:00-06:00",
40+
"orderItemId": 836502628,
41+
"recurringMonths": 1,
42+
"serviceProviderId": 1,
43+
},
44+
"softwareDescription": {
45+
"id": 1795,
46+
"longDescription": "VMware Virtual SAN Advanced Tier III 6.2",
47+
"manufacturer": "VMware",
48+
"name": "Virtual SAN Advanced Tier III",
49+
"version": "6.2",
50+
}
51+
}]

SoftLayer/managers/license.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
SoftLayer.license
3+
~~~~~~~~~~~~~~~
4+
License Manager
5+
6+
:license: MIT, see LICENSE for more details.
7+
"""
8+
9+
10+
# pylint: disable=too-many-public-methods
11+
12+
13+
class LicensesManager(object):
14+
"""Manages account lincese."""
15+
16+
def __init__(self, client):
17+
self.client = client
18+
19+
def get_all_objects(self):
20+
"""Show the all VM ware licenses of account.
21+
22+
"""
23+
_mask = '''softwareDescription,billingItem'''
24+
25+
return self.client.call('SoftLayer_Software_AccountLicense',
26+
'getAllObjects', mask=_mask)
27+
28+
def cancel_item(self, identifier, cancel_immediately,
29+
reason_cancel, customer_note):
30+
"""Cancel a billing item immediately, deleting all its data.
31+
32+
:param integer identifier: the instance ID to cancel
33+
:param string reason_cancel: reason cancel
34+
"""
35+
return self.client.call('SoftLayer_Billing_Item', 'cancelItem',
36+
cancel_immediately,
37+
True,
38+
reason_cancel,
39+
customer_note,
40+
id=identifier)

docs/cli/licenses.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. _cli_licenses:
2+
3+
licenses Commands
4+
=================
5+
6+
.. click:: SoftLayer.CLI.licenses.create:cli
7+
:prog: licenses create
8+
:show-nested:
9+
10+
.. click:: SoftLayer.CLI.licenses.cancel:cli
11+
:prog: licenses cancel
12+
:show-nested:

tests/CLI/modules/licenses_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
SoftLayer.tests.CLI.modules.licenses_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
:license: MIT, see LICENSE for more details.
6+
"""
7+
8+
from SoftLayer.fixtures import SoftLayer_Product_Order
9+
from SoftLayer.fixtures import SoftLayer_Product_Package
10+
11+
from SoftLayer import testing
12+
13+
14+
class LicensesTests(testing.TestCase):
15+
16+
def test_create(self):
17+
_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
18+
19+
_mock.return_value = SoftLayer_Product_Package.getItems_vmware
20+
21+
order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
22+
order_mock.return_value = SoftLayer_Product_Order.wmware_placeOrder
23+
result = self.run_command(['licenses',
24+
'create',
25+
'-k', 'VMWARE_VSAN_ENTERPRISE_TIER_III_65_124_TB_6_X_2',
26+
'-d dal03'])
27+
self.assert_no_fail(result)
28+
29+
def test_cancel(self):
30+
result = self.run_command(['licenses',
31+
'cancel',
32+
'ABCDE-6CJ8L-J8R9H-000R0-CDR70',
33+
'--immediate'])
34+
self.assert_no_fail(result)

0 commit comments

Comments
 (0)