Skip to content

Commit

Permalink
Flag Advanced Settings (#53)
Browse files Browse the repository at this point in the history
* break settings model into parts and add a flag to include only the credentials section (false by default)
  • Loading branch information
foodprocessor authored Aug 29, 2024
1 parent cb12033 commit 4850f77
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/plugin/settings/device_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>

#define NX_PRINT_PREFIX (this->logUtils.printPrefix)
#include <nx/kit/debug.h>
#include <nx/kit/utils.h>
#include <nx/sdk/helpers/active_setting_changed_response.h>
Expand Down
63 changes: 39 additions & 24 deletions src/plugin/settings/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "cloudfuse_helper.h"

// TODO: get NX_PRINT_PREFIX working with non-member helper functions
// #define NX_PRINT_PREFIX (this->logUtils.printPrefix)
#include <nx/kit/debug.h>
#include <nx/kit/ini_config.h>
#include <nx/sdk/helpers/active_setting_changed_response.h>
Expand Down Expand Up @@ -200,24 +202,27 @@ bool Engine::settingsChanged()
{
return true;
}
// endpoint
if (m_prevSettings[kEndpointUrlTextFieldId] != newValues[kEndpointUrlTextFieldId])
if (!credentialsOnly)
{
// if they're different, but both amount to the same thing, then there is no effective change
bool prevIsDefault = m_prevSettings[kEndpointUrlTextFieldId] == kDefaultEndpoint ||
m_prevSettings[kEndpointUrlTextFieldId] == "";
bool newIsDefault =
newValues[kEndpointUrlTextFieldId] == kDefaultEndpoint || newValues[kEndpointUrlTextFieldId] == "";
if (!prevIsDefault || !newIsDefault)
// endpoint
if (m_prevSettings[kEndpointUrlTextFieldId] != newValues[kEndpointUrlTextFieldId])
{
// if they're different, but both amount to the same thing, then there is no effective change
bool prevIsDefault = m_prevSettings[kEndpointUrlTextFieldId] == kDefaultEndpoint ||
m_prevSettings[kEndpointUrlTextFieldId] == "";
bool newIsDefault =
newValues[kEndpointUrlTextFieldId] == kDefaultEndpoint || newValues[kEndpointUrlTextFieldId] == "";
if (!prevIsDefault || !newIsDefault)
{
return true;
}
}
// bucket name
if (m_prevSettings[kBucketNameTextFieldId] != newValues[kBucketNameTextFieldId])
{
return true;
}
}
// bucket name
if (m_prevSettings[kBucketNameTextFieldId] != newValues[kBucketNameTextFieldId])
{
return true;
}
// nothing we care about changed
return false;
}
Expand Down Expand Up @@ -343,9 +348,14 @@ nx::sdk::Error Engine::validateMount()
std::map<std::string, std::string> values = currentSettings();
std::string keyId = values[kKeyIdTextFieldId];
std::string secretKey = values[kSecretKeyPasswordFieldId];
std::string endpointUrl = values[kEndpointUrlTextFieldId];
std::string bucketName = values[kBucketNameTextFieldId]; // The default empty string will cause cloudfuse
// to select first available bucket
std::string endpointUrl = kDefaultEndpoint;
std::string bucketName = "";
if (!credentialsOnly)
{
endpointUrl = values[kEndpointUrlTextFieldId];
bucketName = values[kBucketNameTextFieldId]; // The default empty string will cause cloudfuse
// to select first available bucket
}
std::string mountDir = m_cfManager.getMountDir();
std::string fileCacheDir = m_cfManager.getFileCacheDir();
// Unmount before mounting
Expand Down Expand Up @@ -478,9 +488,7 @@ nx::sdk::Error Engine::spawnMount()
#endif
if (mountRet.errCode != 0)
{
std::string errorMessage = "Unable to launch mount with error: " + mountRet.output;
NX_PRINT << errorMessage;
return error(ErrorCode::internalError, errorMessage);
return error(ErrorCode::internalError, "Unable to launch mount with error: " + mountRet.output);
}

// Mount might not show up immediately, so wait for mount to appear
Expand All @@ -495,9 +503,7 @@ nx::sdk::Error Engine::spawnMount()

if (!m_cfManager.isMounted())
{
std::string errorMessage = "Cloudfuse was not able to successfully mount";
NX_PRINT << errorMessage;
return error(ErrorCode::internalError, errorMessage);
return error(ErrorCode::internalError, "Cloudfuse was not able to successfully mount");
}

return Error(ErrorCode::noError, nullptr);
Expand All @@ -517,15 +523,24 @@ void Engine::doGetSettingsOnActiveSettingChange(Result<const IActiveSettingChang
{
NX_PRINT << "cloudfuse Engine::doGetSettingsOnActiveSettingChange";
std::string parseError;
Json::object model = Json::parse(activeSettingChangedAction->settingsModel(), parseError).object_items();
Json model = Json::parse(activeSettingChangedAction->settingsModel(), parseError);
if (parseError != "")
{
std::string errorMessage = "Failed to parse activeSettingChangedAction model JSON. Here's why: " + parseError;
NX_PRINT << errorMessage;
*outResult = error(ErrorCode::internalError, errorMessage);
return;
}

Json::object modelObject = model.object_items();

const std::string settingId(activeSettingChangedAction->activeSettingName());

std::map<std::string, std::string> values = toStdMap(shareToPtr(activeSettingChangedAction->settingsValues()));

const auto settingsResponse = makePtr<SettingsResponse>();
settingsResponse->setValues(makePtr<StringMap>(values));
settingsResponse->setModel(makePtr<String>(Json(model).dump()));
settingsResponse->setModel(makePtr<String>(Json(modelObject).dump()));

const nx::sdk::Ptr<nx::sdk::ActionResponse> actionResponse =
generateActionResponse(settingId, activeSettingChangedAction->params());
Expand Down
94 changes: 54 additions & 40 deletions src/plugin/settings/settings_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,16 @@ static const std::string kGermanCitiesSettingsModelPart = /*suppress newline*/ 1
static const std::string kRegularSettingsModelPart2 = /*suppress newline*/ 1 + R"json(")json";

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]
// ------------------------------------------------------------------------------------------------

// status
static const std::string kStatusBannerId = "connectionStatus";
static const std::string kStatusSuccess = R"json(
{
"type": "Banner",
"name": ")json" + kStatusBannerId +
R"json(",
"icon": "info",
"text": "Cloud storage connected successfully!"
}
)json";

static const std::string kStatusFailure = R"json(
{
"type": "Banner",
"name": ")json" + kStatusBannerId +
R"json(",
"icon": "warning",
"text": "Cloud storage connection failed!"
}
)json";
// Enable this flag hide all but the credentials section
// NOTE: enabling this will prevent the user from changing the default endpoint (kDefaultEndpoint)
// Only set this flag true if you want to tie your users to a specific cloud storage endpoint
static const bool credentialsOnly = false;

// credentials
static const std::string kKeyIdTextFieldId = "keyId";
static const std::string kSecretKeyPasswordFieldId = "secretKey";
static const std::string kCheckCredentialsButtonId = "checkCredentialsButton";
// advanced
static const std::string kEndpointUrlTextFieldId = "endpointUrl";
static const std::string kDefaultEndpoint = "https://s3.us-east-1.lyvecloud.seagate.com";
static const std::string kBucketNameTextFieldId = "bucketName";
// top-level settings model
static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json(
{
"type": "Settings",
"items":
[
static const std::string kCredentialGroupBox = R"json(
{
"type": "GroupBox",
"caption": "Credentials",
Expand All @@ -95,7 +69,7 @@ static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json(
{
"type": "TextField",
"name": ")json" + kKeyIdTextFieldId +
R"json(",
R"json(",
"caption": "Access Key ID",
"description": "Cloud bucket access key ID",
"defaultValue": "",
Expand All @@ -105,52 +79,92 @@ static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json(
{
"type": "PasswordField",
"name": ")json" + kSecretKeyPasswordFieldId +
R"json(",
R"json(",
"caption": "Secret Key",
"description": "Cloud bucket secret key",
"defaultValue": "",
"validationErrorMessage": "Secret key must be 32 or 40 alphanumeric-plus-slash characters",
"validationRegex": "^[A-Za-z0-9\/+=]{32,128}$"
}
]
},
})json";

// advanced
static const std::string kEndpointUrlTextFieldId = "endpointUrl";
static const std::string kDefaultEndpoint = "https://s3.us-east-1.lyvecloud.seagate.com";
static const std::string kBucketNameTextFieldId = "bucketName";
static const std::string kAdvancedGroupBox = R"json(
{
"type": "GroupBox",
"caption": ")json" + kAdvancedSettingsGroupBoxCaption +
R"json(",
R"json(",
"items":
[
{
"type": "TextField",
"name": ")json" + kEndpointUrlTextFieldId +
R"json(",
R"json(",
"caption": "Endpoint URL",
"description": "Set a different endpoint (different region or service)",
"defaultValue": ")json" + kDefaultEndpoint +
R"json(",
R"json(",
"validationErrorMessage": "Endpoint must be a URL (begin with 'http[s]://').",
"validationRegex": "(^$)|(^https?:\/\/.+$)",
"validationRegexFlags": "i"
},
{
"type": "TextField",
"name": ")json" + kBucketNameTextFieldId +
R"json(",
R"json(",
"caption": "Bucket Name",
"description": "Specify a bucket name (leave empty to let the system automatically detect your bucket)",
"defaultValue": "",
"validationErrorMessage": "Bucket name can only contain lowercase letters, numbers, dashes, and dots.",
"validationRegex": "^[-.a-z0-9]*$"
}
]
},
})json";

static const std::string kPluginWebsiteLink = R"json(
{
"type": "Link",
"caption": "Plugin Website",
"url": "https://github.com/Seagate/nx-lyve-cloud-plugin"
}
})json";

// gather settings items together
static const std::string kSettingsItems =
kCredentialGroupBox + (credentialsOnly ? "" : ("," + kAdvancedGroupBox + "," + kPluginWebsiteLink));

// top-level settings model
static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json(

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 140 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]
{
"type": "Settings",
"items":
[)json" + kSettingsItems + R"json(
]
}
)json";

// status
static const std::string kStatusBannerId = "connectionStatus";
static const std::string kStatusSuccess = R"json(
{
"type": "Banner",
"name": ")json" + kStatusBannerId +
R"json(",
"icon": "info",
"text": "Cloud storage connected successfully!"
}
)json";
static const std::string kStatusFailure = R"json(
{
"type": "Banner",
"name": ")json" + kStatusBannerId +
R"json(",
"icon": "warning",
"text": "Cloud storage connection failed!"
}
)json";

} // namespace settings

0 comments on commit 4850f77

Please sign in to comment.