Core: Services: cable-guy: Complete migration from old settings to Pydantic to ensure migrations#3232
Conversation
Reviewer's Guide by SourceryThis pull request migrates the cable-guy service settings to Pydantic to improve settings management and ensure compatibility across different BlueOS versions. It refactors the settings loading and saving logic, centralizes service configuration, and resolves settings migration issues that previously caused network configuration problems when downgrading BlueOS versions. Sequence diagram for loading settings during initializationsequenceDiagram
participant EM as EthernetManager
participant PM as PydanticManager
participant S as SettingsV1
EM->>PM: _settings (property)
activate PM
PM->>S: Returns settings
deactivate PM
EM->>EM: Load previous settings
loop For each item in settings.content
EM->>EM: set_configuration(item)
end
EM->>PM: save()
activate PM
PM->>S: save()
deactivate PM
Sequence diagram for updating interface settingssequenceDiagram
participant EM as EthernetManager
participant S as SettingsV1
participant PM as PydanticManager
EM->>EM: _update_interface_settings(interface_name, updated_interface)
EM->>S: _settings.content
S-->>EM: Returns content
EM->>EM: Filter out old interface
EM->>S: _settings.content.append(updated_interface)
EM->>PM: _manager.save()
activate PM
PM->>S: save()
deactivate PM
Updated class diagram for SettingsV1classDiagram
class SettingsV1 {
- content: List~NetworkInterface~
+ migrate(data: Dict~str, Any~): None
+ on_settings_created(file_path: pathlib.Path): None
}
class PydanticSettings {
}
SettingsV1 --|> PydanticSettings
class NetworkInterface {
- name: str
- addresses: List~InterfaceAddress~
- info: Optional~InterfaceInfo~
- priority: Optional~int~
}
SettingsV1 -- NetworkInterface : contains
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
903508a to
19a30a0
Compare
0806396 to
4590b02
Compare
f7388b7 to
965a604
Compare
There was a problem hiding this comment.
Hey @joaomariolago - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a unit test for the
sanitize_old_settings_filefunction to ensure it correctly handles the settings migration. - The
on_settings_createdmethod includes a try/except block that simply passes on exception - consider logging the exception to provide better visibility into potential issues.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| """ | ||
| # We don't want to write in disk if there is nothing different to write | ||
| if self.root["content"] == content: | ||
| def on_settings_created(self, file_path: pathlib.Path) -> None: |
There was a problem hiding this comment.
issue (complexity): Consider extracting the migration logic and file parsing within on_settings_created into a separate helper function to reduce nesting and improve readability
Consider extracting migration logic and file parsing into a separate helper to reduce nesting. For example, you can move the inner logic of on_settings_created to a dedicated function:
def migrate_old_settings(file_path: pathlib.Path, settings: SettingsV1) -> None:
old_settings_file_path = file_path.parent / "settings.json"
try:
with open(old_settings_file_path, "r", encoding="utf-8") as file:
settings.content = [NetworkInterface.parse_obj(iface) for iface in json.load(file)["content"]]
sanitize_old_settings_file(old_settings_file_path)
except Exception:
passThen call it in your method:
def on_settings_created(self, file_path: pathlib.Path) -> None:
if self.VERSION != SettingsV1.STATIC_VERSION:
return
migrate_old_settings(file_path, self)This refactoring isolates migration details, reducing complexity in the primary class while preserving all functionality.
* Move all uses of old plain dict settings to new pydantic settings
965a604 to
8e8a19e
Compare
patrickelectric
left a comment
There was a problem hiding this comment.
Tested on 1.3.1, 1.4.0-beta.16 and the PR BlueOS version itself.
Will wait for someone else to review and test before merging.
Fix #3231
Depends on #3235
When updating to one of 1.4.0-beta.17 or 1.4.0-beta.18 and after downgrading it to other lower version it makes cable-guy break due to invalid settings and not configure network properly.
NOTE: Tested on PI4, but aditional test would be good before merge, mainly to make sure cable-guy functionality was not affected.
Summary by Sourcery
Migrate cable-guy service settings to Pydantic to improve settings management and ensure compatibility across different versions
Bug Fixes:
Enhancements:
Chores:
Summary by Sourcery
Migrate cable-guy service settings to Pydantic for improved configuration management and version compatibility
New Features:
Bug Fixes:
Enhancements:
Chores: