Skip to content

Commit

Permalink
Allow changing base urls
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed Jan 14, 2025
1 parent 71e8187 commit 5dda7ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/middlewared/middlewared/api/v25_04_0/tn_connect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import IPvAnyAddress
from pydantic import IPvAnyAddress, model_validator

from middlewared.api.base import BaseModel, ForUpdateMetaclass, NonEmptyString, single_argument_args

Expand Down Expand Up @@ -26,7 +26,19 @@ class TNCEntry(BaseModel):
class TNCUpdateArgs(BaseModel, metaclass=ForUpdateMetaclass):
enabled: bool
ips: list[IPvAnyAddress]
# TODO: Ensure endpoints start with https:// and normalize them with a trailing slash if they don't have it
account_service_base_url: NonEmptyString
leca_service_base_url: NonEmptyString
tnc_base_url: NonEmptyString

@model_validator(mode='after')
def validate_attrs(self):
for k in ('account_service_base_url', 'leca_service_base_url', 'tnc_base_url'):
value = getattr(self, k)
if not value.startswith('https://'):
raise ValueError(f'{k} must start with https://')
if not value.endswith('/'):
setattr(self, k, value + '/')
return self


class TNCUpdateResult(BaseModel):
Expand Down
8 changes: 8 additions & 0 deletions src/middlewared/middlewared/plugins/truenas_connect/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ async def validate_data(self, old_config, data):
'IPs cannot be changed when TrueNAS Connect is in a state other than disabled or completely configured'
)

if data['enabled'] and old_config['enabled']:
for k in ('account_service_base_url', 'leca_service_base_url', 'tnc_base_url'):
if data[k] != old_config[k]:
verrors.add(
f'tn_connect_update.{k}', 'This field cannot be changed when TrueNAS Connect is enabled'
)

verrors.check()

@api_method(TNCUpdateArgs, TNCUpdateResult)
Expand All @@ -88,6 +95,7 @@ async def do_update(self, data):
"""
config = await self.config()
data = config | data

await self.validate_data(config, data)

db_payload = {'enabled': data['enabled'], 'ips': data['ips']}
Expand Down

0 comments on commit 5dda7ab

Please sign in to comment.