|
| 1 | +"""Remove VLANs trunked to this server.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +import SoftLayer |
| 7 | +from SoftLayer.CLI import environment |
| 8 | +from SoftLayer.CLI import exceptions |
| 9 | +from SoftLayer.CLI import helpers |
| 10 | + |
| 11 | + |
| 12 | +@click.command(cls=SoftLayer.CLI.command.SLCommand, ) |
| 13 | +@click.argument('hardware', nargs=1) |
| 14 | +@click.argument('vlans', nargs=-1) |
| 15 | +@click.option('--all', 'all_vlans', is_flag=True, default=False, help="Remove ALL trunked vlans from this server.") |
| 16 | +@environment.pass_env |
| 17 | +def cli(env, hardware, vlans, all_vlans): |
| 18 | + """Remove VLANs trunked to this server. |
| 19 | +
|
| 20 | + HARDWARE is the id of the server |
| 21 | + VLANS is the ID, name, or number of the VLANs you want to remove. Multiple vlans can be removed at the same time. |
| 22 | + It is recommended to use the vlan ID, especially if you have multiple vlans with the same name/number. |
| 23 | + """ |
| 24 | + if not vlans and not all_vlans: |
| 25 | + raise exceptions.ArgumentError("Error: Missing argument 'VLANS'.") |
| 26 | + h_mgr = SoftLayer.HardwareManager(env.client) |
| 27 | + n_mgr = SoftLayer.NetworkManager(env.client) |
| 28 | + hw_id = helpers.resolve_id(h_mgr.resolve_ids, hardware, 'hardware') |
| 29 | + |
| 30 | + if all_vlans: |
| 31 | + h_mgr.clear_vlan(hw_id) |
| 32 | + env.fout("Done.") |
| 33 | + return |
| 34 | + |
| 35 | + # Enclosing in quotes is required for any input that has a space in it. |
| 36 | + # "Public DAL10" for example needs to be sent to search as \"Public DAL10\" |
| 37 | + sl_vlans = n_mgr.search_for_vlan(" ".join(f"\"{v}\"" for v in vlans)) |
| 38 | + if not sl_vlans: |
| 39 | + raise exceptions.ArgumentError(f"No vlans found matching {' '.join(vlans)}") |
| 40 | + del_vlans = parse_vlans(sl_vlans) |
| 41 | + component_mask = "mask[id, name, port, macAddress, primaryIpAddress]" |
| 42 | + # NEXT: Add nice output / exception handling |
| 43 | + if len(del_vlans['public']) > 0: |
| 44 | + components = h_mgr.get_network_components(hw_id, mask=component_mask, space='public') |
| 45 | + for c in components: |
| 46 | + if c.get('primaryIpAddress'): |
| 47 | + h_mgr.remove_vlan(c.get('id'), del_vlans['public']) |
| 48 | + if len(del_vlans['private']) > 0: |
| 49 | + components = h_mgr.get_network_components(hw_id, mask=component_mask, space='private') |
| 50 | + for c in components: |
| 51 | + if c.get('primaryIpAddress'): |
| 52 | + h_mgr.remove_vlan(c.get('id'), del_vlans['private']) |
| 53 | + |
| 54 | + |
| 55 | +def parse_vlans(vlans): |
| 56 | + """returns a dictionary mapping for public / private vlans""" |
| 57 | + |
| 58 | + pub_vlan = [] |
| 59 | + pri_vlan = [] |
| 60 | + for vlan in vlans: |
| 61 | + if vlan.get('networkSpace') == "PUBLIC": |
| 62 | + pub_vlan.append(vlan) |
| 63 | + else: |
| 64 | + pri_vlan.append(vlan) |
| 65 | + return {"public": pub_vlan, "private": pri_vlan} |
0 commit comments