Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions config/console.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
import utilities_common.cli as clicommon

from .validated_config_db_connector import ValidatedConfigDBConnector
from jsonpatch import JsonPatchConflict
#
# 'console' group ('config console ...')
#
Expand All @@ -16,14 +17,18 @@ def console():
@clicommon.pass_db
def enable_console_switch(db):
"""Enable console switch"""
config_db = db.cfgdb
config_db = ValidatedConfigDBConnector(db.cfgdb)

table = "CONSOLE_SWITCH"
dataKey1 = 'console_mgmt'
dataKey2 = 'enabled'

data = { dataKey2 : "yes" }
config_db.mod_entry(table, dataKey1, data)
try:
config_db.mod_entry(table, dataKey1, data)
except ValueError as e:
ctx = click.get_current_context()
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'console disable' group ('config console disable')
Expand All @@ -32,14 +37,18 @@ def enable_console_switch(db):
@clicommon.pass_db
def disable_console_switch(db):
"""Disable console switch"""
config_db = db.cfgdb
config_db = ValidatedConfigDBConnector(db.cfgdb)

table = "CONSOLE_SWITCH"
dataKey1 = 'console_mgmt'
dataKey2 = 'enabled'

data = { dataKey2 : "no" }
config_db.mod_entry(table, dataKey1, data)
try:
config_db.mod_entry(table, dataKey1, data)
except ValueError as e:
ctx = click.get_current_context()
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'console add' group ('config console add ...')
Expand All @@ -52,7 +61,7 @@ def disable_console_switch(db):
@click.option('--devicename', '-d', metavar='<device_name>', required=False)
def add_console_setting(db, linenum, baud, flowcontrol, devicename):
"""Add Console-realted configuration tasks"""
config_db = db.cfgdb
config_db = ValidatedConfigDBConnector(db.cfgdb)

table = "CONSOLE_PORT"
dataKey1 = 'baud_rate'
Expand All @@ -72,7 +81,10 @@ def add_console_setting(db, linenum, baud, flowcontrol, devicename):
ctx.fail("Given device name {} has been used. Please enter a valid device name or remove the existing one !!".format(devicename))
console_entry[dataKey3] = devicename

config_db.set_entry(table, linenum, console_entry)
try:
config_db.set_entry(table, linenum, console_entry)
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))


#
Expand All @@ -83,15 +95,18 @@ def add_console_setting(db, linenum, baud, flowcontrol, devicename):
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
def remove_console_setting(db, linenum):
"""Remove Console-related configuration tasks"""
config_db = db.cfgdb
config_db = ValidatedConfigDBConnector(db.cfgdb)
ctx = click.get_current_context()

table = "CONSOLE_PORT"

data = config_db.get_entry(table, linenum)
if data:
config_db.mod_entry(table, linenum, None)
try:
config_db.set_entry(table, linenum, None)
except JsonPatchConflict as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
else:
ctx = click.get_current_context()
ctx.fail("Trying to delete console port setting, which is not present.")

#
Expand All @@ -103,7 +118,7 @@ def remove_console_setting(db, linenum):
@click.argument('devicename', metavar='<device_name>', required=False)
def upate_console_remote_device_name(db, linenum, devicename):
"""Update remote device name for a console line"""
config_db = db.cfgdb
config_db = ValidatedConfigDBConnector(db.cfgdb)
ctx = click.get_current_context()

table = "CONSOLE_PORT"
Expand All @@ -117,12 +132,18 @@ def upate_console_remote_device_name(db, linenum, devicename):
elif not devicename:
# remove configuration key from console setting if user not give a remote device name
data.pop(dataKey, None)
config_db.mod_entry(table, linenum, data)
try:
config_db.mod_entry(table, linenum, data)
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
elif isExistingSameDevice(config_db, devicename, table):
ctx.fail("Given device name {} has been used. Please enter a valid device name or remove the existing one !!".format(devicename))
else:
data[dataKey] = devicename
config_db.mod_entry(table, linenum, data)
try:
config_db.mod_entry(table, linenum, data)
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
else:
ctx.fail("Trying to update console port setting, which is not present.")

Expand All @@ -135,7 +156,7 @@ def upate_console_remote_device_name(db, linenum, devicename):
@click.argument('baud', metavar='<baud>', required=True, type=click.INT)
def update_console_baud(db, linenum, baud):
"""Update baud for a console line"""
config_db = db.cfgdb
config_db = ValidatedConfigDBConnector(db.cfgdb)
ctx = click.get_current_context()

table = "CONSOLE_PORT"
Expand All @@ -149,7 +170,10 @@ def update_console_baud(db, linenum, baud):
return
else:
data[dataKey] = baud
config_db.mod_entry(table, linenum, data)
try:
config_db.mod_entry(table, linenum, data)
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
else:
ctx.fail("Trying to update console port setting, which is not present.")

Expand All @@ -162,7 +186,7 @@ def update_console_baud(db, linenum, baud):
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
def update_console_flow_control(db, mode, linenum):
"""Update flow control setting for a console line"""
config_db = db.cfgdb
config_db = ValidatedConfigDBConnector(db.cfgdb)
ctx = click.get_current_context()

table = "CONSOLE_PORT"
Expand All @@ -177,7 +201,10 @@ def update_console_flow_control(db, mode, linenum):
return
else:
data[dataKey] = innerMode
config_db.mod_entry(table, linenum, data)
try:
config_db.mod_entry(table, linenum, data)
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
else:
ctx.fail("Trying to update console port setting, which is not present.")

Expand Down
15 changes: 12 additions & 3 deletions config/kube.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click

from utilities_common.cli import AbbreviationGroup, pass_db
from .validated_config_db_connector import ValidatedConfigDBConnector

from .utils import log

Expand All @@ -21,22 +22,30 @@
KUBE_LABEL_SET_KEY = "SET"

def _update_kube_server(db, field, val):
db_data = db.cfgdb.get_entry(KUBE_SERVER_TABLE_NAME, KUBE_SERVER_TABLE_KEY)
config_db = ValidatedConfigDBConnector(db.cfgdb)
db_data = config_db.get_entry(KUBE_SERVER_TABLE_NAME, KUBE_SERVER_TABLE_KEY)
def_data = {
KUBE_SERVER_IP: "",
KUBE_SERVER_PORT: "6443",
KUBE_SERVER_INSECURE: "True",
KUBE_SERVER_DISABLE: "False"
}
ctx = click.get_current_context()
for f in def_data:
if db_data and f in db_data:
if f == field and db_data[f] != val:
db.cfgdb.mod_entry(KUBE_SERVER_TABLE_NAME, KUBE_SERVER_TABLE_KEY, {field: val})
try:
config_db.mod_entry(KUBE_SERVER_TABLE_NAME, KUBE_SERVER_TABLE_KEY, {field: val})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
log.log_info("modify kubernetes server entry {}={}".format(field,val))
else:
# Missing field. Set to default or given value
v = val if f == field else def_data[f]
db.cfgdb.mod_entry(KUBE_SERVER_TABLE_NAME, KUBE_SERVER_TABLE_KEY, {f: v})
try:
config_db.mod_entry(KUBE_SERVER_TABLE_NAME, KUBE_SERVER_TABLE_KEY, {f: v})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
log.log_info("set kubernetes server entry {}={}".format(f,v))


Expand Down
61 changes: 40 additions & 21 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import copy

from jsonpatch import JsonPatchConflict
from jsonpointer import JsonPointerException
from collections import OrderedDict
from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat
from minigraph import parse_device_desc_xml, minigraph_encoder
Expand Down Expand Up @@ -4149,7 +4150,7 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load
raise click.Abort()

# Get the config_db connector
config_db = ctx.obj['config_db']
config_db = ValidatedConfigDBConnector(ctx.obj['config_db'])

target_brkout_mode = mode

Expand Down Expand Up @@ -4228,7 +4229,10 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load
if interface_name not in brkout_cfg_keys:
click.secho("[ERROR] {} is not present in 'BREAKOUT_CFG' Table!".format(interface_name), fg='red')
raise click.Abort()
config_db.set_entry("BREAKOUT_CFG", interface_name, {'brkout_mode': target_brkout_mode})
try:
config_db.set_entry("BREAKOUT_CFG", interface_name, {'brkout_mode': target_brkout_mode})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
click.secho("Breakout process got successfully completed."
.format(interface_name), fg="cyan", underline=True)
click.echo("Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`.")
Expand Down Expand Up @@ -6375,15 +6379,19 @@ def ntp(ctx):
@click.pass_context
def add_ntp_server(ctx, ntp_ip_address):
""" Add NTP server IP """
if not clicommon.is_ipaddress(ntp_ip_address):
ctx.fail('Invalid ip address')
db = ctx.obj['db']
if ADHOC_VALIDATION:
if not clicommon.is_ipaddress(ntp_ip_address):
ctx.fail('Invalid IP address')
db = ValidatedConfigDBConnector(ctx.obj['db'])
ntp_servers = db.get_table("NTP_SERVER")
if ntp_ip_address in ntp_servers:
click.echo("NTP server {} is already configured".format(ntp_ip_address))
return
else:
db.set_entry('NTP_SERVER', ntp_ip_address, {'NULL': 'NULL'})
try:
db.set_entry('NTP_SERVER', ntp_ip_address, {'NULL': 'NULL'})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
click.echo("NTP server {} added to configuration".format(ntp_ip_address))
try:
click.echo("Restarting ntp-config service...")
Expand All @@ -6396,12 +6404,16 @@ def add_ntp_server(ctx, ntp_ip_address):
@click.pass_context
def del_ntp_server(ctx, ntp_ip_address):
""" Delete NTP server IP """
if not clicommon.is_ipaddress(ntp_ip_address):
ctx.fail('Invalid IP address')
db = ctx.obj['db']
if ADHOC_VALIDATION:
if not clicommon.is_ipaddress(ntp_ip_address):
ctx.fail('Invalid IP address')
db = ValidatedConfigDBConnector(ctx.obj['db'])
ntp_servers = db.get_table("NTP_SERVER")
if ntp_ip_address in ntp_servers:
db.set_entry('NTP_SERVER', '{}'.format(ntp_ip_address), None)
try:
db.set_entry('NTP_SERVER', '{}'.format(ntp_ip_address), None)
except JsonPatchConflict as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
click.echo("NTP server {} removed from configuration".format(ntp_ip_address))
else:
ctx.fail("NTP server {} is not configured.".format(ntp_ip_address))
Expand Down Expand Up @@ -6654,16 +6666,19 @@ def add(ctx, name, ipaddr, port, vrf):
if not is_valid_collector_info(name, ipaddr, port, vrf):
return

config_db = ctx.obj['db']
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
collector_tbl = config_db.get_table('SFLOW_COLLECTOR')

if (collector_tbl and name not in collector_tbl and len(collector_tbl) == 2):
click.echo("Only 2 collectors can be configured, please delete one")
return

config_db.mod_entry('SFLOW_COLLECTOR', name,
{"collector_ip": ipaddr, "collector_port": port,
"collector_vrf": vrf})

try:
config_db.mod_entry('SFLOW_COLLECTOR', name,
{"collector_ip": ipaddr, "collector_port": port,
"collector_vrf": vrf})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
return

#
Expand All @@ -6674,14 +6689,18 @@ def add(ctx, name, ipaddr, port, vrf):
@click.pass_context
def del_collector(ctx, name):
"""Delete a sFlow collector"""
config_db = ctx.obj['db']
collector_tbl = config_db.get_table('SFLOW_COLLECTOR')
config_db = ValidatedConfigDBConnector(ctx.obj['db'])
if ADHOC_VALIDATION:
collector_tbl = config_db.get_table('SFLOW_COLLECTOR')

if name not in collector_tbl:
click.echo("Collector: {} not configured".format(name))
return
if name not in collector_tbl:
click.echo("Collector: {} not configured".format(name))
return

config_db.mod_entry('SFLOW_COLLECTOR', name, None)
try:
config_db.set_entry('SFLOW_COLLECTOR', name, None)
except (JsonPatchConflict, JsonPointerException) as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))

#
# 'sflow agent-id' group
Expand Down
Loading