@@ -48,6 +48,15 @@ def get_thresholds_list(self):
4848
4949 def get_resources_list (self ):
5050 return list (self .resources )
51+
52+ def get_crm_config (self ):
53+ configdb = self .cfgdb
54+ if configdb is None :
55+ namespaces = multi_asic .get_namespace_list ()
56+ configdb = ConfigDBConnector (namespace = namespaces [0 ])
57+ configdb .connect ()
58+
59+ return configdb .get_entry ('CRM' , 'Config' )
5160
5261 @multi_asic_util .run_on_multi_asic
5362 def config (self , attr , val ):
@@ -63,15 +72,7 @@ def show_summary(self):
6372 CRM Handler to display general information.
6473 """
6574
66- configdb = self .cfgdb
67- if configdb is None :
68- # Get the namespace list
69- namespaces = multi_asic .get_namespace_list ()
70-
71- configdb = ConfigDBConnector (namespace = namespaces [0 ])
72- configdb .connect ()
73-
74- crm_info = configdb .get_entry ('CRM' , 'Config' )
75+ crm_info = self .get_crm_config ()
7576
7677 if crm_info :
7778 try :
@@ -86,15 +87,8 @@ def show_thresholds(self, resource):
8687 """
8788 CRM Handler to display thresholds information.
8889 """
89- configdb = self .cfgdb
90- if configdb is None :
91- # Get the namespace list
92- namespaces = multi_asic .get_namespace_list ()
9390
94- configdb = ConfigDBConnector (namespace = namespaces [0 ])
95- configdb .connect ()
96-
97- crm_info = configdb .get_entry ('CRM' , 'Config' )
91+ crm_info = self .get_crm_config ()
9892
9993 header = ("Resource Name" , "Threshold Type" , "Low Threshold" , "High Threshold" )
10094 data = []
@@ -418,27 +412,72 @@ def type(ctx, value):
418412@click .pass_context
419413def low (ctx , value ):
420414 """CRM low threshold configuration"""
421- attr = ''
422415
416+ type_attr = ''
423417 if ctx .obj ["crm" ].addr_family != None :
424- attr += ctx .obj ["crm" ].addr_family + '_'
418+ type_attr += ctx .obj ["crm" ].addr_family + '_'
419+ type_attr += ctx .obj ["crm" ].res_type + '_' + 'threshold_type'
425420
426- attr += ctx .obj ["crm" ].res_type + '_' + 'low_threshold'
421+ high_attr = ''
422+ if ctx .obj ["crm" ].addr_family != None :
423+ high_attr += ctx .obj ["crm" ].addr_family + '_'
424+ high_attr += ctx .obj ["crm" ].res_type + '_' + 'high_threshold'
425+
426+ crm_info = ctx .obj ["crm" ].get_crm_config ()
427+ threshold_type = crm_info .get (type_attr , 'percentage' )
428+ if threshold_type == 'percentage' :
429+ if value < 0 or value > 100 :
430+ raise click .ClickException (
431+ 'Error: Low threshold value must be between 0 and 100 for '
432+ 'percentage type.' )
433+
434+ high_value = crm_info .get (high_attr , None )
435+ if high_value is not None and value >= int (high_value ):
436+ raise click .ClickException (
437+ 'Error: Low threshold value must be less than high threshold '
438+ f'value: { high_value } for percentage type.' )
427439
440+ attr = ''
441+ if ctx .obj ["crm" ].addr_family != None :
442+ attr += ctx .obj ["crm" ].addr_family + '_'
443+ attr += ctx .obj ["crm" ].res_type + '_' + 'low_threshold'
428444 ctx .obj ["crm" ].config (attr , value )
429445
446+
430447@click .command ()
431448@click .argument ('value' , type = click .INT )
432449@click .pass_context
433450def high (ctx , value ):
434451 """CRM high threshold configuration"""
435- attr = ''
436452
453+ type_attr = ''
437454 if ctx .obj ["crm" ].addr_family != None :
438- attr += ctx .obj ["crm" ].addr_family + '_'
455+ type_attr += ctx .obj ["crm" ].addr_family + '_'
456+ type_attr += ctx .obj ["crm" ].res_type + '_' + 'threshold_type'
439457
440- attr += ctx .obj ["crm" ].res_type + '_' + 'high_threshold'
458+ low_attr = ''
459+ if ctx .obj ["crm" ].addr_family != None :
460+ low_attr += ctx .obj ["crm" ].addr_family + '_'
461+ low_attr += ctx .obj ["crm" ].res_type + '_' + 'low_threshold'
462+
463+ crm_info = ctx .obj ["crm" ].get_crm_config ()
464+ threshold_type = crm_info .get (type_attr , 'percentage' )
465+ if threshold_type == 'percentage' :
466+ if value < 0 or value > 100 :
467+ raise click .ClickException (
468+ 'Error: High threshold value must be between 0 and 100 for '
469+ 'percentage type.' )
470+
471+ low_value = crm_info .get (low_attr , None )
472+ if low_value is not None and value <= int (low_value ):
473+ raise click .ClickException (
474+ 'Error: Low threshold value must be less than high threshold '
475+ f'value: { low_value } for percentage type.' )
441476
477+ attr = ''
478+ if ctx .obj ["crm" ].addr_family != None :
479+ attr += ctx .obj ["crm" ].addr_family + '_'
480+ attr += ctx .obj ["crm" ].res_type + '_' + 'low_threshold'
442481 ctx .obj ["crm" ].config (attr , value )
443482
444483route .add_command (type )
0 commit comments