How to skip a value when it is Null, Blank

Solved
eroche
Getting noticed

How to skip a value when it is Null, Blank

I put together a script to update a switchport through the API. There are times when all the port values do not have to updated so for the field I would like to leave blank then when the updates are submitted through  dashboard.switch.updateDeviceSwitchPort, I would like for it skip it and not update. Is this possible? Here is the section of my code that is in questions:

 

# Update settings   
    def update_values():
        print ('----------------------------------------------------------------' + Style.RESET_ALL)
        print (Fore.YELLOW,Style.BRIGHT +'\n' + 'Please enter the updated values:  ' + '\n')
        port_name = input ('\n' + 'Port Name:  ')
        enable = input ('Enable Port [True or False]:  ')
        port_type = input ('Port Type [Trunk or Access]:  ')
        
		d_vlan = input ('Data VLAN:  ')
        if port_type == ('Access'):
			v_vlan = input ('Voice VLAN:  ')
		if port_type == ('Trunk'):
			v_allowed = input ('Allowed VLANs [1,3 or 1-3 or All]: ')
        rstp_enabled = input ('RSTP Enable [True or False]:  ')
        stp_guard = input ('STP Guard [Disabled, Root Guard, BPDU guard, Loop Guard]:  ')
		access_policy = input ('Access Policy [Open, MAC Allowed, Stickey MAC MAC Allowed, Internal DOT1x]:  ')
		if access_policy == ('MAC Allowed'):
			mac_allowed = input ('MAC Address [xx:xx:xx:xx:xx:xx, xx:xx:xx:xx:xx:xx): ')
		if access_policy == ('Sticky MAC Allowed list'):
			sticky_mac_allowed = input ('MAC Address [xx:xx:xx:xx:xx:xx, xx:xx:xx:xx:xx:xx): ')
			mac_allowed_limit = input {'How many MAC Address Allowed: ')
        poe_enabled = input ('PoE Enabled [True or False]:  ')
        
        
        print ('\n' + 'Here are the values you entered')
        print ('\n' + 'Port Name: ' + port_name + '\n' + 'Port Enabled: ' + enable + '\n' + 'Port Type: ' + port_type + '\n' + 'Data VLAN: ' + d_vlan + '\n' + 'Voice VLAN: ' + v_vlan + '\n'
			+ 'VLANs Allowed: ' + v_allowed + 'RSTP: ' + rstp_enabled + '\n' + 'STP Guard: ' + stp_guard + '\n' + 'Access Policy: ' + access_policy + '\n' + 'MACs Aloowed: ' + mac_allowed + \n
			'Sticky MACs Allowed: ' + sticky_mac_allowed + \n + 'MAC Allows Limit: ' mac_allowed_limit + \n + 'PoE Enabled: ' + poe_enabled)
        
        validate = input ('\n' + 'Are all values correct [Y or N]:  ')
 
        
        if validate in {'Y', 'y'}:
            response = dashboard.switch.updateDeviceSwitchPort(
                serial, port_id,
                name = port_name,
                enabled = enable,
				type = port_type,
				vlan = d_vlan,
				voiceVlan = v_vlan,
				allowedVlans = v_allowed,
				rstpEnables = rstp_enabled,
				stpGuard = stp_guard,
				accessPolicyType = access_policy,
				macAllowedList = mac_allowed,
				stickyMacAllowedList = sticky_mac_allowed,
				stickyMacAllowListLimit = mac_allowed_limit,
				poeEnabled = poe_enabled,
				
                )
                
            response = dashboard.switch.getDeviceSwitchPort(serial, port_id)
    
            fields = ['portId', 'name', 'enabled', 'poeEnabled', 'type', 'vlan', 'voiceVlan', 'allowedVlans', 'isolationEnabled', 'rstpEnabled', 'stpGuard', 'accessPolicyType', 'macAllowedList', 'stickyMacAllowListLimit', 'linkNegotiation']
    
            for field in fields:
                print ('\n' + 'Here are the New Settings: ' + '\n')
                print (f"{field}: {response[field]}")
            input  ('\n' + 'Press Enter to return to Main Menu: ')
            main_menu()
    
        elif validate in {'N', 'n'}:
            input ('\n' + 'Darn, Ok lets try this again, press Enter: ')
            clearconsole()
            print ('\n' + 'Current Values:' + '\n')
            
            response = dashboard.switch.getDeviceSwitchPort(serial, port_id)
    
            fields = ['portId', 'name', 'enabled', 'poeEnabled', 'type', 'vlan', 'voiceVlan', 'allowedVlans', 'isolationEnabled', 'rstpEnabled', 'stpGuard', 'linkNegotiation']
    
            for field in fields:
                print (f"{field}: {response[field]}")
                
            update_values()
            
        else:
            print ('you need to select Y,y,N or n')
            
            
    update_values()

 

1 Accepted Solution
eroche
Getting noticed

Closing figured out what I was doing wrong in my test above but still working on the filtering of fields with blank values. Gonna open another discussion for that.

View solution in original post

4 Replies 4
252_Masked_Nett
Getting noticed

You can update the script to check for a consistent blank field for the ports with null or blank fields (i.e. 'name') (i.e. if not port ["name"]) and to skip the port if the specific field is blank.

eroche
Getting noticed

Ok a little detail....my goal is to do a complete configure of a port not just one element (ex: name) so I present like 6 or 7 questions to collect the setting values. Currently, depending on the value type it is expecting (Int, str, bool) it will either throw an error or just take that blank and overwrite the current setting in the dashboard. If I leave one blank on purpuse becasue I don't want to update that value. So what I need to figure out is how append the below line with the fields I want to update based on whether a value.

 

fields = ['portId', 'name', 'enabled', 'poeEnabled', 'type', 'vlan', 'voiceVlan', 'allowedVlans', 'isolationEnabled', 'rstpEnabled', 'stpGuard', 'linkNegotiation']

eroche
Getting noticed

Would the following work?

 

fields = []

if name is not Null:
    fields.append ('name')

if vlan is not Null:
    fields.append ('vlan')

#fields would then equal [name,vlan]
eroche
Getting noticed

Closing figured out what I was doing wrong in my test above but still working on the filtering of fields with blank values. Gonna open another discussion for that.

Get notified when there are additional replies to this discussion.