👌 IMPROVE: allow specifying the A records to update #3

Open
jonas wants to merge 1 commits from jonas/DynDns:feature/allow-specifying-records-to-update into master
2 changed files with 25 additions and 19 deletions

View File

@ -1,3 +1,4 @@
HpLogin=
HpPass=
Domain=
Records=

View File

@ -42,32 +42,37 @@ class HpUpdater():
driver.implicitly_wait(10)
tableRows = driver.find_element_by_id('dns-record-list').find_elements_by_class_name('b-record')
recordRows = driver.find_element_by_id('dns-record-list').find_elements_by_class_name('b-record')
# The last two entries are my A records
for e in tableRows[-2:]:
try:
e.find_element_by_name('edit').click()
# For some reason with each iteration it return one more such element
editForm = driver.find_elements_by_class_name('dns-record-list-edit')
editForm[-1].find_element_by_name('ip_address').clear()
editForm[-1].find_element_by_name('ip_address').send_keys(ip)
editForm[-1].find_element_by_name('apply').click()
except Exception as e:
logger.error(f'Exception during update: {str(e)}')
logger.info(f'Changed A records ip to {ip}')
for recordToUpdate in os.environ.get('Records').split(','):
Review

Add .split(',').strip() for the case that there are whitespaces surrounding the record (happens when defined as firstRecord, secondRecord, ...)

Add `.split(',').strip()` for the case that there are whitespaces surrounding the record (happens when defined as `firstRecord, secondRecord, ...`)
logger.debug(f'recordToUpdate: {recordToUpdate}')
for record in recordRows:
recordId = None
Review

No need to declare recordId here

No need to declare `recordId` here
recordChilds = record.find_elements_by_tag_name('td')
if recordChilds[1].text == recordToUpdate and recordChilds[2].text == 'A':
recordId = record.get_attribute('id')
logger.debug(f'recordId: {recordId}')
try:
record.find_element_by_name('edit').click()
editForm = driver.find_element_by_id('edit' + recordId)
editForm.find_element_by_name('ip_address').clear()
editForm.find_element_by_name('ip_address').send_keys(ip)
editForm.find_element_by_name('apply').click()
time.sleep(10)
logger.info(f'Changed {recordToUpdate} A record ip to {ip}')
except Exception as e:
logger.error(f'Exception during update: {str(e)}')
finally:
break
logger.info(f'Changed all A records ip to {ip}')
try:
e = driver.find_element_by_name('execute_now')
driver.execute_script("arguments[0].click();", e)
# Give the site some time to save
time.sleep(10)
time.sleep(15)
logger.info('Successfully saved changes')