Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Reorganize landscape page #1008

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,29 @@ void main() {

// check that we transitioned to the LandscapePage
l10n = tester.l10n<LandscapePage>();
final selfHostedRadio = find.ancestor(
of: find.text(l10n.landscapeQuickSetupSelfHosted),
matching: find.byType(YaruSelectableContainer),
);
final continueButton = find.button(l10n.buttonNext);
final continueButton = find.button(l10n.landscapeRegister);

// check that invalid input disables continue
await tester.tap(selfHostedRadio);
await tester.tap(find.text(l10n.landscapeSetupManual));
await tester.pump();
final fqdnInput = find.ancestor(
of: find.text(l10n.landscapeFQDNLabel),
matching: find.byType(TextField),
);
await tester.enterText(fqdnInput, '::');
await tester.pump();
expect(tester.widget<ElevatedButton>(continueButton).enabled, isFalse);
expect(
tester.widget<ButtonStyleButton>(continueButton).enabled,
isFalse,
);

// check that valid input enabled continue, and continue
await tester.enterText(fqdnInput, 'localhost');
await tester.pump();
expect(tester.widget<ElevatedButton>(continueButton).enabled, isTrue);
expect(
tester.widget<ButtonStyleButton>(continueButton).enabled,
isTrue,
);
await tester.tap(continueButton);
await tester.pumpAndSettle();

Expand Down Expand Up @@ -324,19 +326,15 @@ landscape:
await tester.tap(landscapeButton);
await tester.pumpAndSettle();
final landscapeL10n = tester.l10n<LandscapePage>();
final selfHosted = find.ancestor(
of: find.text(landscapeL10n.landscapeQuickSetupSelfHosted),
matching: find.byType(YaruSelectableContainer),
);
await tester.tap(selfHosted);
await tester.tap(find.text(l10n.landscapeSetupManual));
final fqdnInput = find.ancestor(
of: find.text(landscapeL10n.landscapeFQDNLabel),
matching: find.byType(TextField),
);
await tester.tap(fqdnInput);
await tester.enterText(fqdnInput, 'localhost');
await tester.pump();
final continueButton = find.button(landscapeL10n.buttonNext);
final continueButton = find.button(landscapeL10n.landscapeRegister);
await tester.tap(continueButton);
await tester.pumpAndSettle();

Expand Down
19 changes: 8 additions & 11 deletions gui/packages/ubuntupro/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,14 @@
}
}
},
"landscapeQuickSetupSaas": "Landscape SaaS configuration",
"landscapeQuickSetupSaasHint": "Register with landscape.canonical.com",
"landscapeQuickSetupSelfHosted": "Self-hosted Landscape configuration",
"landscapeQuickSetupSelfHostedHint": "Register with your own Landscape server",
"landscapeFQDNLabel": "Landscape server address",
"landscapeSetupManual": "Manual configuration",
"landscapeSetupManualHint": "Register with your own Landscape server",
"landscapeSetupCustom": "Advanced Configuration",
"landscapeSetupCustomHint": "Load a custom Landscape client configuration file",
"landscapeFQDNLabel": "Landscape FQDN",
"landscapeFQDNError": "Invalid URI. Format should be a hostname or IP address.",
"landscapeAccountNameLabel": "Landscape Account Name",
"landscapeAccountNameError": "Invalid account name",
"landscapeKeyLabel": "Registration Key",
"landscapeCustomSetup": "Advanced Configuration",
"landscapeCustomSetupHint": "Load a custom Landscape client configuration file",
"landscapeSSLKeyLabel": "Server SSL public key",
"landscapeKeyLabel": "Registration Key (optional)",
"landscapeSSLKeyLabel": "Server SSL public key (optional)",
"landscapeFileLabel": "Config file path",
"landscapeFileTooLarge": "Configuration file is too large",
"landscapeFileEmptyPath": "A path must be specified",
Expand All @@ -98,6 +94,7 @@
}
}
},
"landscapeRegister": "Register",

"buttonNext": "Next",
"buttonSkip": "Skip",
Expand Down
146 changes: 33 additions & 113 deletions gui/packages/ubuntupro/lib/pages/landscape/landscape_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@

/// The current configuration type, allowing the UI to show the correct form.
LandscapeConfigType get configType => _current;
LandscapeConfigType _current = LandscapeConfigType.selfHosted;
LandscapeConfigType _current = LandscapeConfigType.manual;

// The active configuration form data, a shortcut to reduce some switch statements
// and avoid relying on ducktyping when serializing the config or checking for completeness.
late LandscapeConfig _active = selfHosted;
late LandscapeConfig _active = manual;

/// The configuration form data for the SaaS configuration.
final LandscapeSaasConfig saas = LandscapeSaasConfig();
/// The configuration form data for the manual configuration.
final LandscapeManualConfig manual = LandscapeManualConfig();

// TODO: Remove this condition when Landscape SaaS starts supporting WSL.
bool get isSaaSSupported => kDebugMode;

/// The configuration form data for the self-hosted configuration.
final LandscapeSelfHostedConfig selfHosted = LandscapeSelfHostedConfig();
// TODO: Remove this condition when Landscape manual/SaaS starts supporting WSL.
bool get isManualSupported => kDebugMode;

Check warning on line 39 in gui/packages/ubuntupro/lib/pages/landscape/landscape_model.dart

View check run for this annotation

Codecov / codecov/patch

gui/packages/ubuntupro/lib/pages/landscape/landscape_model.dart#L39

Added line #L39 was not covered by tests

/// The configuration form data for the custom configuration.
final LandscapeCustomConfig custom = LandscapeCustomConfig();
Expand All @@ -49,57 +46,36 @@
if (value == null) return;
_current = value;
switch (configType) {
case LandscapeConfigType.saas:
_active = saas;
case LandscapeConfigType.selfHosted:
_active = selfHosted;
case LandscapeConfigType.manual:
_active = manual;
case LandscapeConfigType.custom:
_active = custom;
}
notifyListeners();
}

/// Sets (and validates) the account name for the SaaS configuration.
void setAccountName(String? accountName) {
// While calling this method when the active configuration is not the SaaS is harmless,
// allowing it could hide a bug in the UI logic, thus a debug time assertion.
assert(_active is LandscapeSaasConfig);
if (accountName == null) return;
saas.accountName = accountName;

// A relevant piece of state changed: notify the UI.
notifyListeners();
}

/// Sets the registration key for the SaaS configurations.
void setSaasRegistrationKey(String? registrationKey) {
assert(_active is LandscapeSaasConfig);
if (registrationKey == null) return;
saas.registrationKey = registrationKey;
notifyListeners();
}

/// Sets the registration key for the self-hosted configuration.
void setSelfHostedRegistrationKey(String? registrationKey) {
assert(_active is LandscapeSelfHostedConfig);
/// Sets the registration key for the manual configurations.
void setManualRegistrationKey(String? registrationKey) {
assert(_active is LandscapeManualConfig);
if (registrationKey == null) return;
selfHosted.registrationKey = registrationKey;
manual.registrationKey = registrationKey;
notifyListeners();
}

/// Sets (and validates) the FQDN for the self-hosted configuration.
void setFqdn(String? fqdn) {
assert(_active is LandscapeSelfHostedConfig);
assert(_active is LandscapeManualConfig);
if (fqdn == null) return;
selfHosted.fqdn = fqdn;
manual.fqdn = fqdn;
notifyListeners();
}

/// Sets (and validates) the SSL key path for the self-hosted configuration.
void setSslKeyPath(String? sslKeyPath) {
assert(_active is LandscapeSelfHostedConfig);
assert(_active is LandscapeManualConfig);
if (sslKeyPath == null) return;
selfHosted.sslKeyPath = sslKeyPath;
manual.sslKeyPath = sslKeyPath;
notifyListeners();
}

Expand All @@ -126,7 +102,7 @@
}

/// The different types of Landscape configurations, modelled as an enum to make it easy on the UI side to switch between them.
enum LandscapeConfigType { saas, selfHosted, custom }
enum LandscapeConfigType { manual, custom }

/// The alternative errors we could encounter when validating file paths submitted as part of any subform data.
enum FileError {
Expand All @@ -139,8 +115,7 @@
invalidFormat,
}

const landscapeSaas = 'landscape.canonical.com';
const standalone = 'standalone';
const validCertExtensions = ['cer', 'crt', 'der', 'pem'];

/// The base class for the closed set of Landscape configuration form types.
sealed class LandscapeConfig {
Expand All @@ -151,73 +126,25 @@
String? config();
}

/// The SaaS configuration form data: only the account name is mandatory and must not be 'standalone'.
class LandscapeSaasConfig extends LandscapeConfig {
String registrationKey = '';

bool _accountNameError = false;
bool get accountNameError => _accountNameError;
String _accountName = '';
String get accountName => _accountName;

/// Account name can't be standalone for the SaaS.
bool _validateAccountName(String value) {
_accountNameError = value == standalone;
return !_accountNameError;
}

set accountName(String value) {
if (value == _accountName) {
return;
}
if (_validateAccountName(value)) {
_accountName = value;
}
}

// Avoid spamming the user with 'account name cannot be empty' messages.
@override
bool get isComplete => !accountNameError && accountName.isNotEmpty;

@override
String? config() {
if (!isComplete) return null;
final uri = Uri.https(landscapeSaas);

final registrationKeyLine =
registrationKey.isEmpty ? '' : 'registration_key = $registrationKey';

return '''
[host]
url = ${uri.replace(port: 6554).authority}
[client]
account_name = $accountName
url = ${uri.replace(path: '/message-system')}
ping_url = ${uri.replace(scheme: 'http').replace(path: '/ping')}
log_level = info
$registrationKeyLine
'''
.trimRight();
}
}

const validCertExtensions = ['cer', 'crt', 'der', 'pem'];

/// The self-hosted configuration form data: only the FQDN is mandatory and must not be the SaaS URL.
class LandscapeSelfHostedConfig extends LandscapeConfig {
String registrationKey = '';

/// The SaaS configuration form data: only the FQDN is mandatory.
class LandscapeManualConfig extends LandscapeConfig {
String _fqdn = '';
String get fqdn => _fqdn;
bool _fqdnError = false;
bool get fqdnError => _fqdnError;
// FQDN must be a valid URL (without an explicit port) and must not be the Landscape SaaS URL.

String registrationKey = '';

String _sslKeyPath = '';
String get sslKeyPath => _sslKeyPath;

FileError _fileError = FileError.none;
FileError get fileError => _fileError;

// FQDN must be a valid URL (without an explicit port) and must not be the Landscape URL.
bool _validateFQDN(String value) {
final uri = Uri.tryParse(value);
_fqdnError = value.isEmpty ||
uri == null ||
uri.hasPort ||
value.endsWith(landscapeSaas);
_fqdnError = value.isEmpty || uri == null || uri.hasPort;

return !_fqdnError;
}
Expand All @@ -235,11 +162,6 @@
}
}

String _sslKeyPath = '';
String get sslKeyPath => _sslKeyPath;
FileError _fileError = FileError.none;
FileError get fileError => _fileError;

// If a path is provided, then it must exist and be a non-empty file.
bool _validatePath(String path) {
// Empty paths are allowed, since this field is optional.
Expand Down Expand Up @@ -294,22 +216,20 @@

@override
bool get isComplete =>
!fqdnError && fileError == FileError.none && fqdn.isNotEmpty;
!fqdnError && fqdn.isNotEmpty && fileError == FileError.none;

@override
String? config() {
if (!isComplete) return null;

final uri = Uri.parse(_fqdn);
final sslKeyLine = sslKeyPath.isEmpty ? '' : 'ssl_public_key = $sslKeyPath';
final registrationKeyLine =
registrationKey.isEmpty ? '' : 'registration_key = $registrationKey';

final uri = Uri.parse(_fqdn);
return '''
[host]
url = ${uri.replace(port: 6554).authority}
[client]
account_name = $standalone
url = ${uri.replace(path: '/message-system')}
ping_url = ${uri.replace(scheme: 'http').replace(path: '/ping')}
log_level = info
Expand Down
Loading
Loading