Repository: softlayer/softlayer-python
Pull request #2000 (2023-06-21 18:24:28)

slcli bandwidth pools-create with single locations as option #1990
slcli bandwidth pools-create with single locations as option

Fixes: #1990

This PR provides users with an option to specify single as well as multiple regions.

For example. 

SJC/DAL/WDC/TOR/MON A user can specify any of those to get a pool in that region.
slcli bandwidth pools-create --region MON
slcli bandwidth pools-create --region SJC/DAL/WDC/TOR/MON

Both of the above commands will work.

Thanks,
Jayasilan


Patch:
diff --git a/SoftLayer/CLI/bandwidth/pools_create.py b/SoftLayer/CLI/bandwidth/pools_create.py
index 98aacb36..477ce222 100644
--- a/SoftLayer/CLI/bandwidth/pools_create.py
+++ b/SoftLayer/CLI/bandwidth/pools_create.py
@@ -21,16 +21,42 @@
     "FRA": "FRA"
 }
 
+regions = ['SJC/DAL/WDC/TOR/MON', 'AMS/LON/MAD/PAR', 'SNG/HKG/OSA/TOK', 'SYD', 'MEX', 'SAO', 'CHE', 'MIL', 'SEO', 'FRA']
+
+
+def check_region_param(ctx, param, value):  # pylint: disable=unused-argument
+    """Check if provided region is region group or part of region"""
+
+    # :params string value: Region or Region-Groups
+    # return string Region-Groups
+
+    region_group = None
+    for key in location_groups:
+        if value in key or value is key:
+            region_group = key
+
+    if region_group:
+        return region_group
+    else:
+        raise click.BadParameter(f"{value} is not a region or part of any region."
+                                 " Available Choices: ['SJC/DAL/WDC/TOR/MON', 'AMS/LON/MAD/PAR',"
+                                 " 'SNG/HKG/OSA/TOK', 'SYD', 'MEX', 'SAO', 'CHE', 'MIL', 'SEO', 'FRA']")
+
 
 @click.command(cls=SLCommand)
 @click.option('--name', required=True, help="Pool name")
 @click.option('--region', required=True,
-              type=click.Choice(['SJC/DAL/WDC/TOR/MON', 'AMS/LON/MAD/PAR', 'SNG/HKG/OSA/TOK',
-                                'SYD', 'MEX', 'SAO', 'CHE', 'MIL', 'SEO', 'FRA']),
-              help="Region selected")
+              help=f"Choose Region/Region-Group {regions}", callback=check_region_param)
+@click.help_option('--help', '-h')
 @environment.pass_env
 def cli(env, name, region):
-    """Create bandwidth pool."""
+    """Create bandwidth pool.
+
+    Region can be the full zone name 'SJC/DAL/WDC/TOR/MON', or just a single datacenter like 'SJC'.
+    Example::
+        slcli bandwidth pool-create --name testPool --region DAL
+        slcli bandwidth pool-create --name testPool --region SJC/DAL/WDC/TOR/MON
+        """
 
     manager = BandwidthManager(env.client)
     locations = manager.get_location_group()


Test patch:
diff --git a/slcli b/slcli
old mode 100644
new mode 100755
diff --git a/tests/CLI/modules/bandwidth_tests.py b/tests/CLI/modules/bandwidth_tests.py
index 6cc96622..ccc4f572 100644
--- a/tests/CLI/modules/bandwidth_tests.py
+++ b/tests/CLI/modules/bandwidth_tests.py
@@ -68,6 +68,15 @@ def test_delete_bandwidth(self):
         json_output = json.loads(result.output)
         self.assertEqual("Bandwidth pool 123456 has been scheduled for deletion.", json_output)
 
+    def test_create_bandwidth_single_region(self):
+        result = self.run_command(['bandwidth', 'pools-create', '--name=NewRegion', '--region=AMS'])
+        self.assert_no_fail(result)
+        self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'createObject')
+        json_output = json.loads(result.output)
+        self.assertEqual(123456789, json_output['Id'])
+        self.assertEqual('NewRegion', json_output['Name Pool'])
+        self.assertEqual('AMS/LON/MAD/PAR', json_output['Region'])
+
 
 def _bandwidth_advanced_search():
     result = [