-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2105 from allmightyspiff/vlanTrunk
Added vlan trunks on the server detail page
- Loading branch information
Showing
18 changed files
with
847 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Trunk a VLAN to this server.""" | ||
# :license: MIT, see LICENSE for more details. | ||
|
||
import click | ||
|
||
import SoftLayer | ||
from SoftLayer.CLI import environment | ||
from SoftLayer.CLI import exceptions | ||
from SoftLayer.CLI import helpers | ||
|
||
|
||
@click.command(cls=SoftLayer.CLI.command.SLCommand, ) | ||
@click.argument('hardware', nargs=1) | ||
@click.argument('vlans', nargs=-1) | ||
@environment.pass_env | ||
def cli(env, hardware, vlans): | ||
"""Trunk a VLAN to this server. | ||
HARDWARE is the id of the server | ||
VLANS is the ID, name, or number of the VLANs you want to add. Multiple vlans can be added at the same time. | ||
It is recommended to use the vlan ID, especially if you have multiple vlans with the same name/number. | ||
""" | ||
|
||
if not vlans: | ||
raise exceptions.ArgumentError("Error: Missing argument 'VLANS'.") | ||
h_mgr = SoftLayer.HardwareManager(env.client) | ||
n_mgr = SoftLayer.NetworkManager(env.client) | ||
hw_id = helpers.resolve_id(h_mgr.resolve_ids, hardware, 'hardware') | ||
# Enclosing in quotes is required for any input that has a space in it. | ||
# "Public DAL10" for example needs to be sent to search as \"Public DAL10\" | ||
sl_vlans = n_mgr.search_for_vlan(" ".join(f"\"{v}\"" for v in vlans)) | ||
if not sl_vlans: | ||
raise exceptions.ArgumentError(f"No vlans found matching {' '.join(vlans)}") | ||
add_vlans = parse_vlans(sl_vlans) | ||
component_mask = "mask[id, name, port, macAddress, primaryIpAddress]" | ||
# NEXT: Add nice output / exception handling | ||
if len(add_vlans['public']) > 0: | ||
components = h_mgr.get_network_components(hw_id, mask=component_mask, space='public') | ||
for c in components: | ||
if c.get('primaryIpAddress'): | ||
h_mgr.trunk_vlan(c.get('id'), add_vlans['public']) | ||
if len(add_vlans['private']) > 0: | ||
components = h_mgr.get_network_components(hw_id, mask=component_mask, space='private') | ||
for c in components: | ||
if c.get('primaryIpAddress'): | ||
h_mgr.trunk_vlan(c.get('id'), add_vlans['private']) | ||
|
||
|
||
def parse_vlans(vlans): | ||
"""returns a dictionary mapping for public / private vlans""" | ||
|
||
pub_vlan = [] | ||
pri_vlan = [] | ||
for vlan in vlans: | ||
print(f"{vlan.get('networkSpace')} | {vlan.get('id')} -> {vlan.get('vlanNumber')}") | ||
if vlan.get('networkSpace') == "PUBLIC": | ||
pub_vlan.append(vlan) | ||
else: | ||
pri_vlan.append(vlan) | ||
return {"public": pub_vlan, "private": pri_vlan} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"""Remove VLANs trunked to this server.""" | ||
# :license: MIT, see LICENSE for more details. | ||
|
||
import click | ||
|
||
import SoftLayer | ||
from SoftLayer.CLI import environment | ||
from SoftLayer.CLI import exceptions | ||
from SoftLayer.CLI import helpers | ||
|
||
|
||
@click.command(cls=SoftLayer.CLI.command.SLCommand, ) | ||
@click.argument('hardware', nargs=1) | ||
@click.argument('vlans', nargs=-1) | ||
@click.option('--all', 'all_vlans', is_flag=True, default=False, help="Remove ALL trunked vlans from this server.") | ||
@environment.pass_env | ||
def cli(env, hardware, vlans, all_vlans): | ||
"""Remove VLANs trunked to this server. | ||
HARDWARE is the id of the server | ||
VLANS is the ID, name, or number of the VLANs you want to remove. Multiple vlans can be removed at the same time. | ||
It is recommended to use the vlan ID, especially if you have multiple vlans with the same name/number. | ||
""" | ||
if not vlans and not all_vlans: | ||
raise exceptions.ArgumentError("Error: Missing argument 'VLANS'.") | ||
h_mgr = SoftLayer.HardwareManager(env.client) | ||
n_mgr = SoftLayer.NetworkManager(env.client) | ||
hw_id = helpers.resolve_id(h_mgr.resolve_ids, hardware, 'hardware') | ||
|
||
if all_vlans: | ||
h_mgr.clear_vlan(hw_id) | ||
env.fout("Done.") | ||
return | ||
|
||
# Enclosing in quotes is required for any input that has a space in it. | ||
# "Public DAL10" for example needs to be sent to search as \"Public DAL10\" | ||
sl_vlans = n_mgr.search_for_vlan(" ".join(f"\"{v}\"" for v in vlans)) | ||
if not sl_vlans: | ||
raise exceptions.ArgumentError(f"No vlans found matching {' '.join(vlans)}") | ||
del_vlans = parse_vlans(sl_vlans) | ||
component_mask = "mask[id, name, port, macAddress, primaryIpAddress]" | ||
# NEXT: Add nice output / exception handling | ||
if len(del_vlans['public']) > 0: | ||
components = h_mgr.get_network_components(hw_id, mask=component_mask, space='public') | ||
for c in components: | ||
if c.get('primaryIpAddress'): | ||
h_mgr.remove_vlan(c.get('id'), del_vlans['public']) | ||
if len(del_vlans['private']) > 0: | ||
components = h_mgr.get_network_components(hw_id, mask=component_mask, space='private') | ||
for c in components: | ||
if c.get('primaryIpAddress'): | ||
h_mgr.remove_vlan(c.get('id'), del_vlans['private']) | ||
|
||
|
||
def parse_vlans(vlans): | ||
"""returns a dictionary mapping for public / private vlans""" | ||
|
||
pub_vlan = [] | ||
pri_vlan = [] | ||
for vlan in vlans: | ||
if vlan.get('networkSpace') == "PUBLIC": | ||
pub_vlan.append(vlan) | ||
else: | ||
pri_vlan.append(vlan) | ||
return {"public": pub_vlan, "private": pri_vlan} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""List VLANs this server can be attached to.""" | ||
# :license: MIT, see LICENSE for more details. | ||
|
||
import click | ||
|
||
import SoftLayer | ||
from SoftLayer.CLI import environment | ||
from SoftLayer.CLI import formatting | ||
from SoftLayer.CLI import helpers | ||
|
||
|
||
@click.command(cls=SoftLayer.CLI.command.SLCommand, ) | ||
@click.argument('hardware') | ||
@environment.pass_env | ||
def cli(env, hardware): | ||
"""List VLANs this server can be attached to.""" | ||
|
||
mgr = SoftLayer.HardwareManager(env.client) | ||
hw_id = helpers.resolve_id(mgr.resolve_ids, hardware, 'hardware') | ||
mask = ( | ||
"mask[id,primaryIpAddress," | ||
"networkVlansTrunkable[id,name,vlanNumber,fullyQualifiedName,networkSpace]]" | ||
) | ||
table = formatting.Table(["ID", "VLAN", "Name", "Space"]) | ||
table.set_empty_message("No trunkable vlans found.") | ||
hw_components = env.client.call('SoftLayer_Hardware_Server', 'getNetworkComponents', id=hw_id, mask=mask) | ||
|
||
for component in hw_components: | ||
if component.get('primaryIpAddress'): | ||
for vlan in component.get('networkVlansTrunkable', []): | ||
table.add_row([ | ||
vlan.get('id'), vlan.get('fullyQualifiedName'), vlan.get('name'), vlan.get('networkSpace') | ||
]) | ||
|
||
env.fout(table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.