From 4ed62af4847e91d738c0c04bda0b883274992574 Mon Sep 17 00:00:00 2001 From: Anthony Corbacho Date: Fri, 25 Nov 2016 22:35:05 +0900 Subject: [PATCH 1/2] Implement NotebookRepo settings option in ZeppelinHub notebook Repository. This change will allow users to directly switch from instance without switching token in zeppelin-env.sh. --- .../repo/zeppelinhub/ZeppelinHubRepo.java | 108 ++++++++++++++++-- 1 file changed, 97 insertions(+), 11 deletions(-) diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java index e941c0d0ba1..a660b515de2 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java @@ -40,6 +40,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; @@ -156,18 +157,30 @@ String getZeppelinHubUrl(ZeppelinConfiguration conf) { } /** - * Get Token directly from Zeppelinhub. + * Get list of user instances from Zeppelinhub. * This will avoid and remove the needs of setting up token in zeppelin-env.sh. */ - private String getUserZeppelinInstanceToken(String ticket) throws IOException { + private List getUserInstances(String ticket) throws IOException { if (StringUtils.isBlank(ticket)) { - return ""; + return Collections.emptyList(); } + return restApiClient.getInstances(ticket); + } - List instances = restApiClient.getInstances(ticket); - // TODO(anthony): Implement NotebookRepo Setting to let user switch token at runtime. + /** + * Get user default instance. + * From now, it will be from the first instance from the list, + * But later we can think about marking a default one and return it instead :) + */ + private String getDefaultZeppelinInstanceToken(String ticket) throws IOException { + List instances = getUserInstances(ticket); + if (instances.isEmpty()) { + return StringUtils.EMPTY; + } - token = instances.isEmpty() ? StringUtils.EMPTY : instances.get(0).token; + String token = instances.get(0).token; + LOG.debug("The following instance has been assigned {} with token {}", instances.get(0).name, + token); return token; } @@ -179,14 +192,13 @@ private String getUserToken(String principal) { if (StringUtils.isBlank(token)) { String ticket = UserSessionContainer.instance.getSession(principal); try { - token = getUserZeppelinInstanceToken(ticket); + token = getDefaultZeppelinInstanceToken(ticket); usersToken.putIfAbsent(principal, token); } catch (IOException e) { LOG.error("Cannot get user token", e); token = StringUtils.EMPTY; } } - return token; } @@ -305,13 +317,87 @@ public List revisionHistory(String noteId, AuthenticationInfo subject) @Override public List getSettings(AuthenticationInfo subject) { - LOG.warn("Method not implemented"); - return Collections.emptyList(); + if (!isSubjectValid(subject)) { + return Collections.emptyList(); + } + + List settings = Lists.newArrayList(); + String user = subject.getUser(); + String zeppelinHubUserSession = UserSessionContainer.instance.getSession(user); + String userToken = getUserToken(user); + List instances; + List> values = Lists.newLinkedList(); + + try { + instances = getUserInstances(zeppelinHubUserSession); + } catch (IOException e) { + // user not logged + //TODO(xxx): handle this case. + instances = Collections.emptyList(); + } + + NotebookRepoSettingsInfo repoSetting = NotebookRepoSettingsInfo.newInstance(); + repoSetting.type = NotebookRepoSettingsInfo.Type.DROPDOWN; + for (Instance instance : instances) { + if (instance.token.equals(userToken)) { + repoSetting.selected = Integer.toString(instance.id); + } + values.add(ImmutableMap.of("name", instance.name, "value", Integer.toString(instance.id))); + } + + repoSetting.value = values; + repoSetting.name = "Instance"; + settings.add(repoSetting); + return settings; + } + + private void changeToken(int instanceId, String user) { + if (instanceId <= 0) { + LOG.error("User {} tried to switch to a non valid instance {}", user, instanceId); + return; + } + + LOG.info("User {} will switch instance", user); + String ticket = UserSessionContainer.instance.getSession(user); + List instances; + try { + instances = getUserInstances(ticket); + if (instances.isEmpty()) { + return; + } + + for (Instance instance : instances) { + if (instance.id == instanceId) { + LOG.info("User {} switched to instance {}", user, instances.get(0).name); + usersToken.put(user, instance.token); + break; + } + } + } catch (IOException e) { + LOG.error("Cannot switch instance for user {}", user, e); + } } @Override public void updateSettings(Map settings, AuthenticationInfo subject) { - LOG.warn("Method not implemented"); + if (!isSubjectValid(subject)) { + LOG.error("Invalid subject, cannot update Zeppelinhub settings"); + return; + } + if (settings == null || settings.isEmpty()) { + LOG.error("Cannot update ZeppelinHub repo settings because of invalid settings"); + return; + } + + int instanceId = 0; + if (settings.containsKey("Instance")) { + try { + instanceId = Integer.parseInt(settings.get("Instance")); + } catch (NumberFormatException e) { + LOG.error("ZeppelinHub Instance Id in not a valid integer", e); + } + } + changeToken(instanceId, subject.getUser()); } } From fe86558d726c841a59ab067a94fdfbedc31d1c04 Mon Sep 17 00:00:00 2001 From: Anthony Corbacho Date: Tue, 29 Nov 2016 16:17:09 +0900 Subject: [PATCH 2/2] Add logger in getUserInstances try-catch --- .../zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java index a660b515de2..9c9f7f785cc 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/zeppelinhub/ZeppelinHubRepo.java @@ -331,6 +331,8 @@ public List getSettings(AuthenticationInfo subject) { try { instances = getUserInstances(zeppelinHubUserSession); } catch (IOException e) { + LOG.warn("Couldnt find instances for the session {}, returning empty collection", + zeppelinHubUserSession); // user not logged //TODO(xxx): handle this case. instances = Collections.emptyList();