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

LeonardoMapper and LeonardoLabelHelper refactoring #8834

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
@@ -1,17 +1,16 @@
package org.pmiops.workbench.api;

import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_AOU_CONFIG;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_IS_RUNTIME;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_IS_RUNTIME_TRUE;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_WORKSPACE_NAME;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_WORKSPACE_NAMESPACE;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.hasValidRuntimeConfigurationLabel;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.upsertLeonardoLabel;

import com.google.common.base.Strings;
import jakarta.annotation.Nullable;
import jakarta.inject.Provider;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Logger;
Expand Down Expand Up @@ -134,13 +133,7 @@ private Runtime getOverrideFromListRuntimes(String googleProject) {
LeonardoListRuntimeResponse mostRecentRuntime =
mostRecentRuntimeMaybe.orElseThrow(NotFoundException::new);

@SuppressWarnings("unchecked")
Map<String, String> runtimeLabels = (Map<String, String>) mostRecentRuntime.getLabels();

if (runtimeLabels != null
&& LeonardoMapper.RUNTIME_CONFIGURATION_TYPE_ENUM_TO_STORAGE_MAP
.values()
.contains(runtimeLabels.get(LEONARDO_LABEL_AOU_CONFIG))) {
if (hasValidRuntimeConfigurationLabel(mostRecentRuntime.getLabels())) {
try {
Runtime runtime = leonardoMapper.toApiRuntime(mostRecentRuntime);
if (!RuntimeStatus.DELETED.equals(runtime.getStatus())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.pmiops.workbench.leonardo.LeonardoCustomEnvVarUtils.OWNER_EMAIL_ENV_KEY;
import static org.pmiops.workbench.leonardo.LeonardoCustomEnvVarUtils.WORKSPACE_NAME_ENV_KEY;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_DISK_LABEL_KEYS;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_AOU_CONFIG;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.appTypeToLabelValue;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.upsertLeonardoLabel;

Expand Down Expand Up @@ -303,9 +304,7 @@ private Map<String, String> buildRuntimeConfigurationLabels(
RuntimeConfigurationType runtimeConfigurationType) {
if (runtimeConfigurationType != null) {
return Collections.singletonMap(
LeonardoLabelHelper.LEONARDO_LABEL_AOU_CONFIG,
LeonardoMapper.RUNTIME_CONFIGURATION_TYPE_ENUM_TO_STORAGE_MAP.get(
runtimeConfigurationType));
LEONARDO_LABEL_AOU_CONFIG, leonardoMapper.toConfigurationLabel(runtimeConfigurationType));
} else {
return new HashMap<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.pmiops.workbench.model.AppType;

/** Helper class for setting Leonardo labels. */
Expand All @@ -26,6 +27,40 @@ private LeonardoLabelHelper() {}
public static final String LEONARDO_LABEL_WORKSPACE_NAMESPACE = "saturnWorkspaceNamespace";
public static final String LEONARDO_LABEL_WORKSPACE_NAME = "saturnWorkspaceName";

// Important: keep these string constants in sync with LeonardoMapper
// toConfigurationType() and toConfigurationLabel()

public static String USER_OVERRIDE = "user-override";
public static String GENERAL_ANALYSIS = "preset-general-analysis";
public static String HAIL_GENOMIC_ANALYSIS = "preset-hail-genomic-analysis";

@Nullable
public static String getRuntimeConfigurationLabel(@Nullable Map<String, String> labels) {
if (labels == null) {
return null;
}
return labels.get(LEONARDO_LABEL_AOU_CONFIG);
}

@Nullable
@SuppressWarnings("unchecked")
public static String getRuntimeConfigurationLabel(@Nullable Object labels) {
if (labels == null) {
return null;
}

return getRuntimeConfigurationLabel((Map<String, String>) labels);
}

public static boolean hasValidRuntimeConfigurationLabel(@Nullable Object labels) {
String s = getRuntimeConfigurationLabel(labels);
if (s == null) {
return false;
}

return Set.of(USER_OVERRIDE, GENERAL_ANALYSIS, HAIL_GENOMIC_ANALYSIS).contains(s);
}

public static String appTypeToLabelValue(AppType appType) {
return appType.toString().toLowerCase();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.pmiops.workbench.utils.mappers;

import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.getRuntimeConfigurationLabel;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.hasValidRuntimeConfigurationLabel;

import com.google.gson.Gson;
import jakarta.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
Expand Down Expand Up @@ -57,11 +57,15 @@
@Mapper(config = MapStructConfig.class)
public interface LeonardoMapper {

BiMap<RuntimeConfigurationType, String> RUNTIME_CONFIGURATION_TYPE_ENUM_TO_STORAGE_MAP =
ImmutableBiMap.of(
RuntimeConfigurationType.USEROVERRIDE, "user-override",
RuntimeConfigurationType.GENERALANALYSIS, "preset-general-analysis",
RuntimeConfigurationType.HAILGENOMICANALYSIS, "preset-hail-genomic-analysis");
@ValueMapping(source = "user-override", target = "USEROVERRIDE")
@ValueMapping(source = "preset-general-analysis", target = "GENERALANALYSIS")
@ValueMapping(source = "preset-hail-genomic-analysis", target = "HAILGENOMICANALYSIS")
RuntimeConfigurationType toConfigurationType(String runtimeConfigurationLabel);

@ValueMapping(source = "USEROVERRIDE", target = "user-override")
@ValueMapping(source = "GENERALANALYSIS", target = "preset-general-analysis")
@ValueMapping(source = "HAILGENOMICANALYSIS", target = "preset-hail-genomic-analysis")
String toConfigurationLabel(RuntimeConfigurationType runtimeConfigurationType);

DataprocConfig toDataprocConfig(LeonardoMachineConfig leonardoMachineConfig);

Expand Down Expand Up @@ -258,18 +262,13 @@ default void setAppType(UserAppEnvironment appEnvironment, @Nullable Object appL
}

default void mapRuntimeLabels(Runtime runtime, Object runtimeLabelsObj) {
@SuppressWarnings("unchecked")
final Map<String, String> runtimeLabels = (Map<String, String>) runtimeLabelsObj;
if (runtimeLabels == null
|| runtimeLabels.get(LeonardoLabelHelper.LEONARDO_LABEL_AOU_CONFIG) == null) {
// If there's no label, fall back onto the old behavior where every Runtime was created with a
// default Dataproc config
runtime.setConfigurationType(RuntimeConfigurationType.HAILGENOMICANALYSIS);
} else {
if (hasValidRuntimeConfigurationLabel(runtimeLabelsObj)) {
runtime.setConfigurationType(
RUNTIME_CONFIGURATION_TYPE_ENUM_TO_STORAGE_MAP
.inverse()
.get(runtimeLabels.get(LeonardoLabelHelper.LEONARDO_LABEL_AOU_CONFIG)));
toConfigurationType(getRuntimeConfigurationLabel(runtimeLabelsObj)));
} else {
// If there's no valid label, fall back onto the old behavior where every Runtime was created
// with a default Dataproc config
runtime.setConfigurationType(RuntimeConfigurationType.HAILGENOMICANALYSIS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.GENERAL_ANALYSIS;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.HAIL_GENOMIC_ANALYSIS;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_AOU_CONFIG;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_IS_RUNTIME;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_IS_RUNTIME_TRUE;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_WORKSPACE_NAME;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.LEONARDO_LABEL_WORKSPACE_NAMESPACE;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.USER_OVERRIDE;
import static org.pmiops.workbench.leonardo.LeonardoLabelHelper.getRuntimeConfigurationLabel;
import static org.pmiops.workbench.utils.TestMockFactory.createControlledTier;

import com.google.cloud.Date;
Expand Down Expand Up @@ -473,7 +477,7 @@ public void testGetRuntime_noLabel() throws ApiException {

@Test
public void testGetRuntime_defaultLabel_hail() throws ApiException {
testLeoRuntime.setLabels(ImmutableMap.of("all-of-us-config", "preset-hail-genomic-analysis"));
testLeoRuntime.setLabels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, HAIL_GENOMIC_ANALYSIS));

when(mockUserRuntimesApi.getRuntime(GOOGLE_PROJECT_ID, getRuntimeName()))
.thenReturn(testLeoRuntime);
Expand All @@ -484,7 +488,7 @@ public void testGetRuntime_defaultLabel_hail() throws ApiException {

@Test
public void testGetRuntime_defaultLabel_generalAnalysis() throws ApiException {
testLeoRuntime.setLabels(ImmutableMap.of("all-of-us-config", "preset-general-analysis"));
testLeoRuntime.setLabels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, GENERAL_ANALYSIS));

when(mockUserRuntimesApi.getRuntime(GOOGLE_PROJECT_ID, getRuntimeName()))
.thenReturn(testLeoRuntime);
Expand All @@ -495,7 +499,7 @@ public void testGetRuntime_defaultLabel_generalAnalysis() throws ApiException {

@Test
public void testGetRuntime_overrideLabel() throws ApiException {
testLeoRuntime.setLabels(ImmutableMap.of("all-of-us-config", "user-override"));
testLeoRuntime.setLabels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE));

when(mockUserRuntimesApi.getRuntime(GOOGLE_PROJECT_ID, getRuntimeName()))
.thenReturn(testLeoRuntime);
Expand Down Expand Up @@ -531,7 +535,7 @@ public void testGetRuntime_fromListRuntimes() throws ApiException {
.runtimeName("expected-runtime")
.status(LeonardoRuntimeStatus.CREATING)
.auditInfo(new LeonardoAuditInfo().createdDate(timestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE))));

Runtime runtime = runtimeController.getRuntime(WORKSPACE_NS).getBody();

Expand All @@ -552,7 +556,7 @@ public void testGetRuntime_fromListRuntimes_invalidRuntime() throws ApiException
ImmutableList.of(
new LeonardoListRuntimeResponse()
.runtimeConfig(dataprocConfigObj)
.labels(ImmutableMap.of("all-of-us-config", "user-override"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE))));

assertThrows(NotFoundException.class, () -> runtimeController.getRuntime(WORKSPACE_NS));
}
Expand All @@ -569,7 +573,7 @@ public void testGetRuntime_fromListRuntimes_gceConfig() throws ApiException {
new LeonardoListRuntimeResponse()
.runtimeConfig(gceConfigObj)
.auditInfo(new LeonardoAuditInfo().createdDate(timestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE))));

Runtime runtime = runtimeController.getRuntime(WORKSPACE_NS).getBody();

Expand Down Expand Up @@ -599,7 +603,7 @@ public void testGetRuntime_fromListRuntimes_dataprocConfig() throws ApiException
new LeonardoListRuntimeResponse()
.runtimeConfig(dataProcConfigObj)
.auditInfo(new LeonardoAuditInfo().createdDate(timestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE))));

Runtime runtime = runtimeController.getRuntime(WORKSPACE_NS).getBody();

Expand All @@ -626,11 +630,11 @@ public void testGetRuntime_fromListRuntimes_checkMostRecent() throws ApiExceptio
new LeonardoListRuntimeResponse()
.runtimeName("expected-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(newerTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override")),
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE)),
new LeonardoListRuntimeResponse()
.runtimeName("default-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(olderTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "default"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, "default"))));

assertThat(runtimeController.getRuntime(WORKSPACE_NS).getBody().getRuntimeName())
.isEqualTo("expected-runtime");
Expand All @@ -648,10 +652,10 @@ public void testGetRuntime_fromListRuntimes_checkMostRecent_nullAuditInfo() thro
new LeonardoListRuntimeResponse()
.runtimeName("expected-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(newerTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override")),
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE)),
new LeonardoListRuntimeResponse()
.runtimeName("default-runtime")
.labels(ImmutableMap.of("all-of-us-config", "default"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, "default"))));

assertThat(runtimeController.getRuntime(WORKSPACE_NS).getBody().getRuntimeName())
.isEqualTo("expected-runtime");
Expand All @@ -669,11 +673,11 @@ public void testGetRuntime_fromListRuntimes_checkMostRecent_nullTimestamp() thro
new LeonardoListRuntimeResponse()
.runtimeName("expected-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(newerTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override")),
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE)),
new LeonardoListRuntimeResponse()
.runtimeName("default-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(null))
.labels(ImmutableMap.of("all-of-us-config", "default"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, "default"))));

assertThat(runtimeController.getRuntime(WORKSPACE_NS).getBody().getRuntimeName())
.isEqualTo("expected-runtime");
Expand All @@ -691,11 +695,11 @@ public void testGetRuntime_fromListRuntimes_checkMostRecent_emptyTimestamp() thr
new LeonardoListRuntimeResponse()
.runtimeName("expected-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(newerTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override")),
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE)),
new LeonardoListRuntimeResponse()
.runtimeName("default-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(""))
.labels(ImmutableMap.of("all-of-us-config", "default"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, "default"))));

assertThat(runtimeController.getRuntime(WORKSPACE_NS).getBody().getRuntimeName())
.isEqualTo("expected-runtime");
Expand All @@ -714,11 +718,11 @@ public void testGetRuntime_fromListRuntime_mostRecentIsDefaultLabel() throws Api
new LeonardoListRuntimeResponse()
.runtimeName("override-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(olderTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "user-override")),
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, USER_OVERRIDE)),
new LeonardoListRuntimeResponse()
.runtimeName("default-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(newerTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "default"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, "default"))));

assertThrows(NotFoundException.class, () -> runtimeController.getRuntime(WORKSPACE_NS));
}
Expand All @@ -739,7 +743,7 @@ public void testGetRuntime_fromListRuntime_mostRecentHasNoLabel() throws ApiExce
new LeonardoListRuntimeResponse()
.runtimeName("default-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(olderTimestamp))
.labels(ImmutableMap.of("all-of-us-config", "default"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, "default"))));

assertThrows(NotFoundException.class, () -> runtimeController.getRuntime(WORKSPACE_NS));
}
Expand All @@ -756,7 +760,7 @@ public void testGetRuntime_fromListRuntime_returnPresets() throws ApiException {
new LeonardoListRuntimeResponse()
.runtimeName("preset-runtime")
.auditInfo(new LeonardoAuditInfo().createdDate(timestamp))
.labels(ImmutableMap.of("all-of-us-config", "preset-general-analysis"))));
.labels(ImmutableMap.of(LEONARDO_LABEL_AOU_CONFIG, GENERAL_ANALYSIS))));

assertThat(runtimeController.getRuntime(WORKSPACE_NS).getBody().getRuntimeName())
.isEqualTo("preset-runtime");
Expand Down Expand Up @@ -1047,8 +1051,8 @@ public void testCreateRuntime_defaultLabel_hail() throws ApiException {
eq(GOOGLE_PROJECT_ID), eq(getRuntimeName()), createRuntimeRequestCaptor.capture());

LeonardoCreateRuntimeRequest createRuntimeRequest = createRuntimeRequestCaptor.getValue();
assertThat(((Map<String, String>) createRuntimeRequest.getLabels()).get("all-of-us-config"))
.isEqualTo("preset-hail-genomic-analysis");
assertThat(getRuntimeConfigurationLabel(createRuntimeRequest.getLabels()))
.isEqualTo(HAIL_GENOMIC_ANALYSIS);
}

@Test
Expand All @@ -1067,8 +1071,8 @@ public void testCreateRuntime_defaultLabel_generalAnalysis() throws ApiException
eq(GOOGLE_PROJECT_ID), eq(getRuntimeName()), createRuntimeRequestCaptor.capture());

LeonardoCreateRuntimeRequest createRuntimeRequest = createRuntimeRequestCaptor.getValue();
assertThat(((Map<String, String>) createRuntimeRequest.getLabels()).get("all-of-us-config"))
.isEqualTo("preset-general-analysis");
assertThat(getRuntimeConfigurationLabel(createRuntimeRequest.getLabels()))
.isEqualTo(GENERAL_ANALYSIS);
}

@Test
Expand All @@ -1087,8 +1091,8 @@ public void testCreateRuntime_overrideLabel() throws ApiException {
eq(GOOGLE_PROJECT_ID), eq(getRuntimeName()), createRuntimeRequestCaptor.capture());

LeonardoCreateRuntimeRequest createRuntimeRequest = createRuntimeRequestCaptor.getValue();
assertThat(((Map<String, String>) createRuntimeRequest.getLabels()).get("all-of-us-config"))
.isEqualTo("user-override");
assertThat(getRuntimeConfigurationLabel(createRuntimeRequest.getLabels()))
.isEqualTo(USER_OVERRIDE);
}

@Test
Expand Down Expand Up @@ -1308,8 +1312,7 @@ public void testUpdateRuntime() throws ApiException {
.isEqualTo(
Collections.singletonMap(
LEONARDO_LABEL_AOU_CONFIG,
LeonardoMapper.RUNTIME_CONFIGURATION_TYPE_ENUM_TO_STORAGE_MAP.get(
RuntimeConfigurationType.USEROVERRIDE)));
leonardoMapper.toConfigurationLabel(RuntimeConfigurationType.USEROVERRIDE)));
}

@Test
Expand Down