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

Merged
merged 12 commits into from
Dec 17, 2024
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",
CarlosNihelton marked this conversation as resolved.
Show resolved Hide resolved
"landscapeFQDNError": "Invalid URI. Format should be a hostname or IP address.",
CarlosNihelton marked this conversation as resolved.
Show resolved Hide resolved
"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
150 changes: 37 additions & 113 deletions gui/packages/ubuntupro/lib/pages/landscape/landscape_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/foundation.dart' show ChangeNotifier, kDebugMode;
import 'package:flutter/foundation.dart' show ChangeNotifier;
import 'package:grpc/grpc.dart' show GrpcError;
import 'package:pkcs7/pkcs7.dart';

import '/core/agent_api_client.dart';

const landscapeSaasUri = 'landscape.canonical.com';
CarlosNihelton marked this conversation as resolved.
Show resolved Hide resolved
const standaloneAN = 'standalone';

/// The view model for the Landscape configuration page.
/// This class is responsible for managing the state of the Landscape configuration form, including its subforms
/// and submit the active form data when complete, disregarding the inactive ones.
Expand All @@ -26,20 +29,14 @@ class LandscapeModel extends ChangeNotifier {

/// 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;

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

// 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();
/// The configuration form data for the manual configuration.
final LandscapeManualConfig manual = LandscapeManualConfig();

/// The configuration form data for the custom configuration.
final LandscapeCustomConfig custom = LandscapeCustomConfig();
Expand All @@ -49,57 +46,36 @@ class LandscapeModel extends ChangeNotifier {
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.
/// Sets (and validates) the FQDN for the manual 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.
/// Sets (and validates) the SSL key path for the manual 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 @@ class LandscapeModel extends ChangeNotifier {
}

/// 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 }
CarlosNihelton marked this conversation as resolved.
Show resolved Hide resolved

/// 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 @@ enum FileError {
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,28 @@ sealed class LandscapeConfig {
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 manual 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;

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 SaaS URL.
bool _validateFQDN(String value) {
final uri = Uri.tryParse(value);
_fqdnError = value.isEmpty ||
uri == null ||
uri.hasPort ||
value.endsWith(landscapeSaas);
value.endsWith(landscapeSaasUri);

return !_fqdnError;
}
Expand All @@ -235,11 +165,6 @@ class LandscapeSelfHostedConfig extends LandscapeConfig {
}
}

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 +219,21 @@ class LandscapeSelfHostedConfig extends LandscapeConfig {

@override
bool get isComplete =>
!fqdnError && fileError == FileError.none && fqdn.isNotEmpty;
!fqdnError && fqdn.isNotEmpty && fileError == FileError.none;
CarlosNihelton marked this conversation as resolved.
Show resolved Hide resolved

@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
CarlosNihelton marked this conversation as resolved.
Show resolved Hide resolved
account_name = $standaloneAN
url = ${uri.replace(path: '/message-system')}
ping_url = ${uri.replace(scheme: 'http').replace(path: '/ping')}
log_level = info
Expand Down
Loading
Loading