diff --git a/src/plugin/settings/device_agent.cpp b/src/plugin/settings/device_agent.cpp index a89fff0..8f50e5d 100644 --- a/src/plugin/settings/device_agent.cpp +++ b/src/plugin/settings/device_agent.cpp @@ -5,6 +5,7 @@ #include +#define NX_PRINT_PREFIX (this->logUtils.printPrefix) #include #include #include diff --git a/src/plugin/settings/engine.cpp b/src/plugin/settings/engine.cpp index 75cd5fe..c1ead54 100644 --- a/src/plugin/settings/engine.cpp +++ b/src/plugin/settings/engine.cpp @@ -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 #include #include @@ -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; } @@ -343,9 +348,14 @@ nx::sdk::Error Engine::validateMount() std::map 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 @@ -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 @@ -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); @@ -517,7 +523,16 @@ void Engine::doGetSettingsOnActiveSettingChange(ResultsettingsModel(), 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()); @@ -525,7 +540,7 @@ void Engine::doGetSettingsOnActiveSettingChange(Result(); settingsResponse->setValues(makePtr(values)); - settingsResponse->setModel(makePtr(Json(model).dump())); + settingsResponse->setModel(makePtr(Json(modelObject).dump())); const nx::sdk::Ptr actionResponse = generateActionResponse(settingId, activeSettingChangedAction->params()); diff --git a/src/plugin/settings/settings_model.h b/src/plugin/settings/settings_model.h index 4595272..32083de 100644 --- a/src/plugin/settings/settings_model.h +++ b/src/plugin/settings/settings_model.h @@ -51,42 +51,16 @@ static const std::string kGermanCitiesSettingsModelPart = /*suppress newline*/ 1 static const std::string kRegularSettingsModelPart2 = /*suppress newline*/ 1 + 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"; +// 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", @@ -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": "", @@ -105,7 +79,7 @@ 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": "", @@ -113,21 +87,27 @@ static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json( "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" @@ -135,7 +115,7 @@ static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json( { "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": "", @@ -143,14 +123,48 @@ static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json( "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( +{ + "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