👌 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
Contributor
  • this allows to explicitely define which A records to update rather than just modifying the two last ones
  • the records are defined by their complete (sub)domain in a comma separated list as environment variable 'Records'
- this allows to explicitely define which A records to update rather than just modifying the two last ones - the records are defined by their complete (sub)domain in a comma separated list as environment variable 'Records'
jeanclaude approved these changes 2020-12-24 14:24:00 +01:00
jeanclaude left a comment
Owner

As already discussed, using sets instead of lists is more efficient.

Somnething like this should work (I have not run it, so I am not sure if I got everything right):

recordRows = driver.find_element_by_id('dns-record-list').find_elements_by_class_name('b-record')

records = set(os.environ.get('Records').split(','))

for record in recordRows:
    logger.debug(f'record: {record}')
    recordChilds = record.find_elements_by_tag_name('td')

    if recordChilds[1].text not in records or recordChilds[2].text != 'A':
        continue

    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 {recordChilds[1].text} A record ip to {ip}')
    except Exception as e:
        logger.error(f'Exception during update: {str(e)}')
    finally:
        break
As already discussed, using sets instead of lists is more efficient. Somnething like this should work (I have not run it, so I am not sure if I got everything right): ``` recordRows = driver.find_element_by_id('dns-record-list').find_elements_by_class_name('b-record') records = set(os.environ.get('Records').split(',')) for record in recordRows: logger.debug(f'record: {record}') recordChilds = record.find_elements_by_tag_name('td') if recordChilds[1].text not in records or recordChilds[2].text != 'A': continue 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 {recordChilds[1].text} A record ip to {ip}') except Exception as e: logger.error(f'Exception during update: {str(e)}') finally: break ```
@ -62,1 +45,3 @@
logger.info(f'Changed A records ip to {ip}')
recordRows = driver.find_element_by_id('dns-record-list').find_elements_by_class_name('b-record')
for recordToUpdate in os.environ.get('Records').split(','):
Owner

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, ...`)
@ -63,0 +47,4 @@
for recordToUpdate in os.environ.get('Records').split(','):
logger.debug(f'recordToUpdate: {recordToUpdate}')
for record in recordRows:
recordId = None
Owner

No need to declare recordId here

No need to declare `recordId` here
Owner

Oh, the text above is not formatted properly. Here it is again:

As already discussed, using sets instead of lists is more efficient in this case.

Somnething like this should work (I have not run it, so I am not sure if I got everything right):

recordRows = driver.find_element_by_id('dns-record-list').find_elements_by_class_name('b-record')

records = set(os.environ.get('Records').split(','))

for record in recordRows:
    logger.debug(f'record: {record}')
    recordChilds = record.find_elements_by_tag_name('td')

    if recordChilds[1].text not in records or recordChilds[2].text != 'A':
        continue

    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 {recordChilds[1].text} A record ip to {ip}')
    except Exception as e:
        logger.error(f'Exception during update: {str(e)}')
    finally:
        break
Oh, the text above is not formatted properly. Here it is again: As already discussed, using sets instead of lists is more efficient in this case. Somnething like this should work (I have not run it, so I am not sure if I got everything right): ``` recordRows = driver.find_element_by_id('dns-record-list').find_elements_by_class_name('b-record') records = set(os.environ.get('Records').split(',')) for record in recordRows: logger.debug(f'record: {record}') recordChilds = record.find_elements_by_tag_name('td') if recordChilds[1].text not in records or recordChilds[2].text != 'A': continue 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 {recordChilds[1].text} A record ip to {ip}') except Exception as e: logger.error(f'Exception during update: {str(e)}') finally: break ```
This pull request can be merged automatically.
You are not authorized to merge this pull request.
You can also view command line instructions.

Step 1:

From your project repository, check out a new branch and test the changes.
git checkout -b jonas-feature/allow-specifying-records-to-update master
git pull feature/allow-specifying-records-to-update

Step 2:

Merge the changes and update on Gitea.
git checkout master
git merge --no-ff jonas-feature/allow-specifying-records-to-update
git push origin master
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: jeanclaude/DynDns#3
No description provided.