Repository: softlayer/softlayer-python
Pull request #1878 (2023-03-15 16:24:29)

new command account hooks
[#issue1877](https://github.com/softlayer/softlayer-python/issues/1877)

![image](https://user-images.githubusercontent.com/22535437/225375117-f125d493-7db8-44b4-a707-fc1eff1216fe.png)



Patch:
diff --git a/SoftLayer/CLI/account/hooks.py b/SoftLayer/CLI/account/hooks.py
new file mode 100644
index 00000000..582ae4c2
--- /dev/null
+++ b/SoftLayer/CLI/account/hooks.py
@@ -0,0 +1,25 @@
+"""Show all Provisioning Scripts."""
+# :license: MIT, see LICENSE for more details.
+import click
+
+
+from SoftLayer.CLI.command import SLCommand as SLCommand
+from SoftLayer.CLI import environment
+from SoftLayer.CLI import formatting
+from SoftLayer.managers import account
+
+
+@click.command(cls=SLCommand)
+@environment.pass_env
+def cli(env):
+    """Show all Provisioning Scripts."""
+
+    manager = account.AccountManager(env.client)
+    hooks = manager.get_provisioning_scripts()
+
+    table = formatting.Table(["Id", "Name", "Uri"])
+
+    for hook in hooks:
+        table.add_row([hook.get('id'), hook.get('name'), hook.get('uri')])
+
+    env.fout(table)
diff --git a/SoftLayer/CLI/hardware/list.py b/SoftLayer/CLI/hardware/list.py
index 42b29815..cb3243e3 100644
--- a/SoftLayer/CLI/hardware/list.py
+++ b/SoftLayer/CLI/hardware/list.py
@@ -9,6 +9,7 @@
 from SoftLayer.CLI import formatting
 from SoftLayer.CLI import helpers
 
+
 # pylint: disable=unnecessary-lambda
 
 COLUMNS = [
@@ -46,9 +47,6 @@
 @click.option('--hostname', '-H', help='Filter by hostname')
 @click.option('--memory', '-m', help='Filter by memory in gigabytes')
 @click.option('--network', '-n', help='Filter by network port speed in Mbps')
-@click.option('--search', is_flag=False, flag_value="", default=None,
-              help="Use the more flexible Search API to list instances. See `slcli search --types` for list " +
-                   "of searchable fields.")
 @helpers.multi_option('--tag', help='Filter by tags')
 @click.option('--sortby', help='Column to sort by', default='hostname', show_default=True)
 @click.option('--columns',
@@ -61,26 +59,19 @@
               default=100,
               show_default=True)
 @environment.pass_env
-def cli(env, sortby, cpu, domain, datacenter, hostname, memory, network, search, tag, columns, limit):
+def cli(env, sortby, cpu, domain, datacenter, hostname, memory, network, tag, columns, limit):
     """List hardware servers."""
 
-    if search is not None:
-        object_mask = "mask[resource(SoftLayer_Hardware)]"
-        search_manager = SoftLayer.SearchManager(env.client)
-        servers = search_manager.search_hadrware_instances(hostname=hostname, domain=domain, datacenter=datacenter,
-                                                           tags=tag, search_string=search, mask=object_mask)
-
-    else:
-        manager = SoftLayer.HardwareManager(env.client)
-        servers = manager.list_hardware(hostname=hostname,
-                                        domain=domain,
-                                        cpus=cpu,
-                                        memory=memory,
-                                        datacenter=datacenter,
-                                        nic_speed=network,
-                                        tags=tag,
-                                        mask="mask(SoftLayer_Hardware_Server)[%s]" % columns.mask(),
-                                        limit=limit)
+    manager = SoftLayer.HardwareManager(env.client)
+    servers = manager.list_hardware(hostname=hostname,
+                                    domain=domain,
+                                    cpus=cpu,
+                                    memory=memory,
+                                    datacenter=datacenter,
+                                    nic_speed=network,
+                                    tags=tag,
+                                    mask="mask(SoftLayer_Hardware_Server)[%s]" % columns.mask(),
+                                    limit=limit)
 
     table = formatting.Table(columns.columns)
     table.sortby = sortby
diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py
index d236fc0b..1795978c 100644
--- a/SoftLayer/CLI/routes.py
+++ b/SoftLayer/CLI/routes.py
@@ -24,6 +24,7 @@
     ('account:cancel-item', 'SoftLayer.CLI.account.cancel_item:cli'),
     ('account:orders', 'SoftLayer.CLI.account.orders:cli'),
     ('account:bandwidth-pools', 'SoftLayer.CLI.account.bandwidth_pools:cli'),
+    ('account:hooks', 'SoftLayer.CLI.account.hooks:cli'),
 
     ('virtual', 'SoftLayer.CLI.virt'),
     ('virtual:bandwidth', 'SoftLayer.CLI.virt.bandwidth:cli'),
diff --git a/SoftLayer/fixtures/SoftLayer_Account.py b/SoftLayer/fixtures/SoftLayer_Account.py
index 6a5ade1f..d8f7fc72 100644
--- a/SoftLayer/fixtures/SoftLayer_Account.py
+++ b/SoftLayer/fixtures/SoftLayer_Account.py
@@ -1469,3 +1469,14 @@
         'keyName': 'ACTIVE'
     }
 }]
+
+getPostProvisioningHooks = [{
+    "id": 147258369,
+    "name": "testProv",
+    "uri": "http://test.com"
+},
+    {
+        "id": 123654,
+        "name": "GettestProv12345",
+        "uri": "http://test.com"
+    }]
diff --git a/SoftLayer/managers/account.py b/SoftLayer/managers/account.py
index c052f0ab..72744fd3 100644
--- a/SoftLayer/managers/account.py
+++ b/SoftLayer/managers/account.py
@@ -387,3 +387,11 @@ def getBandwidthDetail(self, identifier):
         virtualGuests[outboundPublicBandwidthUsage,bandwidthAllotmentDetail[allocation]],
         bareMetalInstances[outboundBandwidthUsage,bandwidthAllotmentDetail[allocation]]"""
         return self.client['SoftLayer_Network_Bandwidth_Version1_Allotment'].getObject(id=identifier, mask=_mask)
+
+    def get_provisioning_scripts(self):
+        """Gets a provisioning hooks.
+
+        :returns: provisioning hook
+        """
+
+        return self.client.call('Account', 'getPostProvisioningHooks')
diff --git a/SoftLayer/managers/search.py b/SoftLayer/managers/search.py
index 78c5d412..a0ae8c33 100644
--- a/SoftLayer/managers/search.py
+++ b/SoftLayer/managers/search.py
@@ -61,36 +61,3 @@ def search_instances(self, search_string, mask=None, **kwargs):
             guests.append(resource.get('resource'))
 
         return guests
-
-    def search_hadrware_instances(self, search_string, mask=None, **kwargs):
-        """Lists hardwares based in the search_string.
-
-                Also takes in a few search terms as **kwargs. such as hostname, datacenter, domain and tags
-                """
-
-        # This forces the Search API to do a fuzzy search on our term, kinda. Not sure why the ** are
-        # Required but it will do an exact search without them.
-        if search_string:
-            search_string = f"*{search_string}*"
-        search_string = f"_objectType:SoftLayer_Hardware {search_string}"
-        if kwargs.get('hostname'):
-            search_string = f"{search_string} hostname: *{kwargs.get('hostname')}*"
-        if kwargs.get('domain'):
-            search_string = f"{search_string} domain: *{kwargs.get('domain')}*"
-        if kwargs.get('cpu'):
-            search_string = f"{search_string} cpu: *{kwargs.get('cpu')}*"
-        if kwargs.get('network'):
-            search_string = f"{search_string} network: *{kwargs.get('network')}*"
-        if kwargs.get('memory'):
-            search_string = f"{search_string} memory: *{kwargs.get('memory')}*"
-        if kwargs.get('datacenter'):
-            search_string = f"{search_string} datacenter.longName: *{kwargs.get('datacenter')}*"
-        if kwargs.get('tags'):
-            tags = " ".join(kwargs.get("tags", []))
-            search_string = f"{search_string} internalTagReferences.tag.name: {tags}"
-        result = self.search_manager.advancedSearch(search_string, mask=mask)
-        servers = []
-        for resource in result:
-            servers.append(resource.get('resource'))
-
-        return servers
diff --git a/docs/cli/account.rst b/docs/cli/account.rst
index c215d708..33ac4e6b 100644
--- a/docs/cli/account.rst
+++ b/docs/cli/account.rst
@@ -51,3 +51,7 @@ Account Commands
 .. click:: SoftLayer.CLI.account.bandwidth_pools_detail:cli
     :prog: account bandwidth-pools-detail
     :show-nested:
+
+.. click:: SoftLayer.CLI.account.hooks:cli
+    :prog: account hooks
+    :show-nested:
diff --git a/setup.py b/setup.py
index a2c012a0..332cf006 100644
--- a/setup.py
+++ b/setup.py
@@ -40,7 +40,7 @@
         'prompt_toolkit >= 2',
         'pygments >= 2.0.0',
         'urllib3 >= 1.24',
-        'rich == 13.3.2'
+        'rich == 13.3.1'
     ],
     keywords=['softlayer', 'cloud', 'slcli'],
     classifiers=[
diff --git a/tools/requirements.txt b/tools/requirements.txt
index 0446899b..b5757421 100644
--- a/tools/requirements.txt
+++ b/tools/requirements.txt
@@ -4,6 +4,6 @@ requests >= 2.20.0
 prompt_toolkit >= 2
 pygments >= 2.0.0
 urllib3 >= 1.24
-rich == 13.3.2
+rich == 13.3.1
 # only used for soap transport
 # softlayer-zeep >= 5.0.0


Test patch:
diff --git a/tests/CLI/modules/account_tests.py b/tests/CLI/modules/account_tests.py
index b8932a8f..022ef540 100644
--- a/tests/CLI/modules/account_tests.py
+++ b/tests/CLI/modules/account_tests.py
@@ -159,3 +159,8 @@ def test_acccount_bandwidth_pool_detail(self):
         result = self.run_command(['account', 'bandwidth-pools-detail', '123456'])
         self.assert_no_fail(result)
         self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject')
+
+    def test_acccount_provisioning_hook(self):
+        result = self.run_command(['account', 'hooks'])
+        self.assert_no_fail(result)
+        self.assert_called_with('SoftLayer_Account', 'getPostProvisioningHooks')
diff --git a/tests/CLI/modules/server_tests.py b/tests/CLI/modules/server_tests.py
index d0a0d8ba..1d0109e8 100644
--- a/tests/CLI/modules/server_tests.py
+++ b/tests/CLI/modules/server_tests.py
@@ -1057,14 +1057,3 @@ def test_create_credential(self):
                                    '--username', 'testslcli', '--password', 'test-123456',
                                    '--notes', 'test slcli', '--system', 'system'])
         self.assert_no_fail(result)
-
-    def test_list_hw_search_noargs(self):
-        result = self.run_command(['hw', 'list', '--search'])
-        self.assert_no_fail(result)
-        self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=('_objectType:SoftLayer_Hardware ',))
-
-    def test_list_hw_search_noargs_domain(self):
-        result = self.run_command(['hw', 'list', '--search', '-Dtest'])
-        self.assert_no_fail(result)
-        self.assert_called_with('SoftLayer_Search', 'advancedSearch',
-                                args=('_objectType:SoftLayer_Hardware  domain: *test*',))
diff --git a/tests/managers/account_tests.py b/tests/managers/account_tests.py
index b32c223f..efacbf69 100644
--- a/tests/managers/account_tests.py
+++ b/tests/managers/account_tests.py
@@ -176,3 +176,7 @@ def test_get_bandwidth_pool_counts(self):
         total = self.manager.get_bandwidth_pool_counts(1234)
         self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject', identifier=1234)
         self.assertEqual(total, 2)
+
+    def test_get_provisioning_scripts(self):
+        self.manager.get_provisioning_scripts()
+        self.assert_called_with("SoftLayer_Account", "getPostProvisioningHooks")