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

Reconfigure Gson to parse more complex theia preference object #14442

Merged
merged 1 commit into from
Sep 10, 2019
Merged
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 @@ -40,5 +40,18 @@ public final class Warnings {
COMMAND_IS_CONFIGURED_IN_PLUGIN_WITH_MULTIPLY_CONTAINERS_WARNING_MESSAGE_FMT =
"There are configured commands for plugin '%s' that has multiply containers. Commands will be configured to be run in first container";

public static final int JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_WARNING_CODE =
4108;
public static final String JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_MESSAGE_FMT =
"Unable to provision git configuration into runtime. "
+ "Json object in user preferences is not a valid representation for an object of type map: '%s'";

public static final int
INTERNAL_SERVER_ERROR_OCCURRED_DURING_OPERATING_WITH_USER_PREFERENCES_WARNING_CODE = 4109;
public static final String
INTERNAL_SERVER_ERROR_OCCURRED_DURING_OPERATING_WITH_USER_PREFERENCES_MESSAGE_FMT =
"Unable to provision git configuration into runtime. "
+ "Internal server error occurred during operating with user preferences: '%s'";

private Warnings() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
package org.eclipse.che.workspace.infrastructure.kubernetes.provision;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static java.util.Collections.singletonMap;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
Expand All @@ -36,8 +38,10 @@
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.user.server.PreferenceManager;
import org.eclipse.che.api.workspace.server.model.impl.WarningImpl;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.workspace.infrastructure.kubernetes.Warnings;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;

@Singleton
Expand All @@ -63,14 +67,45 @@ public GitUserProfileProvisioner(PreferenceManager preferenceManager) {
@Override
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
throws InfrastructureException {
try {
doInternalProvision(k8sEnv, identity);
} catch (ServerException e) {
String warnMsg =
format(
Warnings
.INTERNAL_SERVER_ERROR_OCCURRED_DURING_OPERATING_WITH_USER_PREFERENCES_MESSAGE_FMT,
e.getMessage());
k8sEnv
.getWarnings()
.add(
new WarningImpl(
Warnings
.INTERNAL_SERVER_ERROR_OCCURRED_DURING_OPERATING_WITH_USER_PREFERENCES_WARNING_CODE,
warnMsg));
} catch (JsonSyntaxException e) {
String warnMsg =
format(
Warnings.JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_MESSAGE_FMT,
e.getMessage());
k8sEnv
.getWarnings()
.add(
new WarningImpl(
Warnings.JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_WARNING_CODE,
warnMsg));
}
}

private void doInternalProvision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
throws ServerException, JsonSyntaxException {
getPreferenceValue(PREFERENCES_KEY_FILTER)
.ifPresent(
preferenceJsonValue -> {
Map<String, String> theiaPreferences = getMapFromJsonObject(preferenceJsonValue);
Map<String, Object> theiaPreferences = getMapFromJsonObject(preferenceJsonValue);

getGlobalGitConfigFileContent(
theiaPreferences.get(GIT_USER_NAME_PROPERTY),
theiaPreferences.get(GIT_USER_EMAIL_PROPERTY))
getStringValueOrNull(theiaPreferences, GIT_USER_NAME_PROPERTY),
getStringValueOrNull(theiaPreferences, GIT_USER_EMAIL_PROPERTY))
.ifPresent(
gitConfigFileContent -> {
String gitConfigMapName =
Expand All @@ -81,21 +116,29 @@ public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
});
}

private Map<String, String> getMapFromJsonObject(String json) {
Type stringMapType = new TypeToken<Map<String, String>>() {}.getType();
private String getStringValueOrNull(Map<String, Object> map, String key) {
Object value = map.get(key);

return new Gson().fromJson(json, stringMapType);
return value instanceof String ? (String) value : null;
}

private Optional<String> getPreferenceValue(String keyFilter) throws InfrastructureException {
try {
String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
Map<String, String> preferencesMap = preferenceManager.find(userId, keyFilter);
private Map<String, Object> getMapFromJsonObject(String json) throws JsonSyntaxException {
Type typeToken = new TypeToken<Map<String, Object>>() {}.getType();

return ofNullable(preferencesMap.get(keyFilter));
} catch (ServerException e) {
throw new InfrastructureException(e);
}
/*
The main idea to implicit cast map from string:object to string:string is that,
we're looking for a two specific properties in the end, that have 100% string values,
so we don't care about what we have in other properties.
*/

return new Gson().fromJson(json, typeToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about how this is done; it looks like an implicit cast to Map<String, String>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's implicit, when we get parsed and implicitly casted map, we will look for two specific properties, that we're 100% sure that have string value. We can add additional checking for a string type, but it will reduce code readability. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's up to you. It's not clear to me from reading the code that we can be 100% it's a string value, but I'm also not familiar with all the details here.

Is it possible for me to set my git.user.name to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically - yes, but it's hard to do, you need to update preferences by hand and escape the whole json object to place it as string value in user preferences. But when eclipse-che/che-theia#424 will be merged, these two properties will be configured through the UI and there will be an only option to setup them as string value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment, describes the idea of this hack

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make that any error in this class does not prevent workspace from starting.
To notify a user about an error you can provision a warning into k8s Environment, not sure if it's handled by Theia - if no - feel free to register an issue.

}

private Optional<String> getPreferenceValue(String keyFilter) throws ServerException {
String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
Map<String, String> preferencesMap = preferenceManager.find(userId, keyFilter);

return ofNullable(preferencesMap.get(keyFilter));
}

private Optional<String> getGlobalGitConfigFileContent(String userName, String userEmail) {
Expand Down
Loading