|
2 | 2 | # :license: MIT, see LICENSE for more details. |
3 | 3 |
|
4 | 4 | import click |
5 | | - |
6 | 5 | import SoftLayer |
7 | 6 | from SoftLayer.CLI import environment |
8 | 7 | from SoftLayer.CLI import formatting |
| 8 | +from SoftLayer import utils |
9 | 9 |
|
10 | 10 |
|
11 | 11 | @click.command(cls=SoftLayer.CLI.command.SLCommand, ) |
| 12 | +@click.argument('identifier') |
12 | 13 | @environment.pass_env |
13 | | -def cli(env): |
| 14 | +def cli(env, identifier): |
14 | 15 | """List object storage endpoints.""" |
15 | 16 |
|
16 | 17 | mgr = SoftLayer.ObjectStorageManager(env.client) |
17 | | - endpoints = mgr.list_endpoints() |
| 18 | + endpoints = mgr.list_endpoints(identifier) |
| 19 | + |
| 20 | + final_end_points = [] |
18 | 21 |
|
19 | | - table = formatting.Table(['datacenter', 'public', 'private']) |
| 22 | + table = formatting.Table(['Location/Region', 'Url', 'EndPoint Type', 'Public/Private', 'Legacy']) |
| 23 | + table.align['Location/Region'] = 'l' |
| 24 | + table.align['Url'] = 'l' |
20 | 25 | for endpoint in endpoints: |
21 | | - table.add_row([ |
22 | | - endpoint['datacenter']['name'], |
23 | | - endpoint['public'], |
24 | | - endpoint['private'], |
25 | | - ]) |
| 26 | + data = { |
| 27 | + 'Location/Region': location_region(endpoint), |
| 28 | + 'Url': endpoint['url'], |
| 29 | + 'EndPoint Type': end_point_return(endpoint['region']), |
| 30 | + 'Public/Private': public_private(endpoint['type']), |
| 31 | + 'Legacy': endpoint['legacy'] |
| 32 | + } |
| 33 | + final_end_points.append(data) |
| 34 | + |
| 35 | + final_end_points = sort_endpoint(final_end_points) |
| 36 | + table = add_array_to_table(table, final_end_points) |
26 | 37 |
|
27 | 38 | env.fout(table) |
| 39 | + |
| 40 | + |
| 41 | +def add_array_to_table(table, array_datas): |
| 42 | + """Add an array to a table""" |
| 43 | + for array in array_datas: |
| 44 | + table.add_row([array['Location/Region'], |
| 45 | + array['Url'], |
| 46 | + array['EndPoint Type'], |
| 47 | + array['Public/Private'], |
| 48 | + array['Legacy']]) |
| 49 | + return table |
| 50 | + |
| 51 | + |
| 52 | +def end_point_return(endpoint): |
| 53 | + """Returns end point type""" |
| 54 | + if endpoint == 'singleSite': |
| 55 | + return 'Single Site' |
| 56 | + if endpoint == 'regional': |
| 57 | + return 'Region' |
| 58 | + return 'Cross Region' |
| 59 | + |
| 60 | + |
| 61 | +def public_private(data): |
| 62 | + """Returns public or private in capital letter""" |
| 63 | + if data == 'public': |
| 64 | + return 'Public' |
| 65 | + return 'Private' |
| 66 | + |
| 67 | + |
| 68 | +def location_region(endpoint): |
| 69 | + """Returns location if it exists otherwise region""" |
| 70 | + if utils.lookup(endpoint, 'location'): |
| 71 | + return endpoint['location'] |
| 72 | + return endpoint['region'] |
| 73 | + |
| 74 | + |
| 75 | +def sort_endpoint(endpoints): |
| 76 | + """Sort the all endpoints for public or private""" |
| 77 | + first_data = 0 |
| 78 | + endpoint_type = '' |
| 79 | + if len(endpoints) > 0: |
| 80 | + endpoint_type = endpoints[first_data]['EndPoint Type'] |
| 81 | + public = [] |
| 82 | + private = [] |
| 83 | + array_final = [] |
| 84 | + for endpoint in endpoints: |
| 85 | + if endpoint['EndPoint Type'] != endpoint_type: |
| 86 | + endpoint_type = endpoint['EndPoint Type'] |
| 87 | + array_final = array_final + public + private |
| 88 | + public.clear() |
| 89 | + private.clear() |
| 90 | + if endpoint['Public/Private'] == 'Public': |
| 91 | + public.append(endpoint) |
| 92 | + else: |
| 93 | + private.append(endpoint) |
| 94 | + |
| 95 | + array_final = array_final + public + private |
| 96 | + return array_final |
0 commit comments