Skip to content

Commit 1f25d96

Browse files
committed
Fix possible NPE when load app settings from non-existing function app
1 parent b44dab6 commit 1f25d96

File tree

1 file changed

+16
-13
lines changed
  • PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appservice/src/main/java/com/microsoft/azure/toolkit/intellij/legacy/function/runner/deploy/ui

1 file changed

+16
-13
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appservice/src/main/java/com/microsoft/azure/toolkit/intellij/legacy/function/runner/deploy/ui/FunctionDeploymentPanel.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,24 @@
2424
import com.microsoft.azure.toolkit.lib.Azure;
2525
import com.microsoft.azure.toolkit.lib.appservice.AppServiceAppBase;
2626
import com.microsoft.azure.toolkit.lib.appservice.function.AzureFunctions;
27-
import com.microsoft.azure.toolkit.lib.appservice.function.FunctionApp;
28-
import com.microsoft.azure.toolkit.lib.appservice.function.FunctionAppBase;
2927
import com.microsoft.azure.toolkit.lib.common.form.AzureFormInput;
3028
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
3129
import com.microsoft.azure.toolkit.lib.common.model.AbstractAzResource;
3230
import com.microsoft.azure.toolkit.lib.common.task.AzureTask;
3331
import com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager;
3432
import org.apache.commons.collections.MapUtils;
33+
import org.apache.commons.lang3.ObjectUtils;
3534
import org.apache.commons.lang3.StringUtils;
3635
import org.jetbrains.annotations.NotNull;
3736
import org.jetbrains.annotations.Nullable;
3837
import org.jetbrains.idea.maven.project.MavenProject;
3938

4039
import javax.annotation.Nonnull;
4140
import javax.swing.*;
42-
import java.awt.event.ItemEvent;
4341
import java.nio.file.Paths;
4442
import java.util.Arrays;
4543
import java.util.Collections;
4644
import java.util.List;
47-
import java.util.Map;
4845
import java.util.Objects;
4946
import java.util.Optional;
5047
import java.util.UUID;
@@ -85,7 +82,7 @@ public void customize(JList list, Module module, int i, boolean b, boolean b1) {
8582
}
8683
});
8784
functionAppComboBox.setRequired(true);
88-
chkSlot.addItemListener(e -> onSelectSlot());
85+
chkSlot.addItemListener(e -> onSlotCheckBoxChanged());
8986

9087
lblModule.setLabelFor(cbFunctionModule);
9188
lblFunction.setLabelFor(functionAppComboBox);
@@ -202,24 +199,23 @@ private void onSelectFunctionApp(final FunctionAppConfig value) {
202199
return;
203200
}
204201
// disable slot for draft function
202+
this.chkSlot.setEnabled(StringUtils.isNotEmpty(value.getResourceId()));
205203
if (StringUtils.isEmpty(value.getResourceId())) {
206204
this.chkSlot.setSelected(false);
207205
}
208-
this.chkSlot.setEnabled(StringUtils.isNotEmpty(value.getResourceId()));
209-
this.toggleDeploymentSlot(chkSlot.isSelected());
210206
this.cbDeploymentSlot.setAppService(value.getResourceId());
211207
if (!this.chkSlot.isSelected()) {
212208
loadAppSettings(getResourceId(value, null));
213209
}
214210
}
215211

216-
private void loadAppSettings(@Nonnull final String resourceId) {
212+
private void loadAppSettings(@Nullable final String resourceId) {
217213
if (StringUtils.equalsIgnoreCase(resourceId, this.appSettingsResourceId) && MapUtils.isNotEmpty(this.appSettingsTable.getAppSettings())) {
218214
return;
219215
}
220216
this.appSettingsResourceId = resourceId;
221217
this.appSettingsTable.loadAppSettings(() -> {
222-
final AbstractAzResource<?, ?, ?> resource = Azure.az().getById(resourceId);
218+
final AbstractAzResource<?, ?, ?> resource = StringUtils.isBlank(resourceId) ? null : Azure.az().getById(resourceId);
223219
return resource instanceof AppServiceAppBase<?, ?, ?> ? ((AppServiceAppBase<?, ?, ?>) resource).getAppSettings() : Collections.emptyMap();
224220
});
225221
}
@@ -247,10 +243,14 @@ private void selectModule(final Module target) {
247243
}
248244
}
249245

250-
private void onSelectSlot() {
246+
private void onSlotCheckBoxChanged() {
251247
toggleDeploymentSlot(chkSlot.isSelected());
252-
if (!chkSlot.isSelected() && functionAppComboBox.getValue() != null) {
253-
// reload app settings for function app
248+
final FunctionAppConfig function = functionAppComboBox.getValue();
249+
final DeploymentSlotConfig slot = cbDeploymentSlot.getValue();
250+
// reload app settings when switch slot configuration
251+
if (chkSlot.isSelected() && ObjectUtils.allNotNull(function, slot)) {
252+
loadAppSettings(getResourceId(functionAppComboBox.getValue(), slot));
253+
} else if (!chkSlot.isSelected() && Objects.nonNull(function)) {
254254
loadAppSettings(getResourceId(functionAppComboBox.getValue(), null));
255255
}
256256
}
@@ -269,12 +269,15 @@ public List<AzureValidationInfo> getAllValidationInfos(final boolean revalidateI
269269
.filter(Objects::nonNull).collect(Collectors.toList());
270270
}
271271

272+
@Nullable
272273
private String getResourceId(@Nonnull FunctionAppConfig config, @Nullable DeploymentSlotConfig slotConfig) {
273274
if (Objects.isNull(slotConfig)) {
274275
return StringUtils.isNoneBlank(config.getResourceId()) ? config.getResourceId() :
275276
Azure.az(AzureFunctions.class).functionApps(config.getSubscriptionId()).getOrTemp(config.getName(), config.getResourceGroupName()).getId();
276277
} else {
277-
return Azure.az(AzureFunctions.class).functionApp(config.getResourceId()).slots().getOrTemp(slotConfig.getName(), null).getId();
278+
return Optional.ofNullable(Azure.az(AzureFunctions.class).functionApp(config.getResourceId()))
279+
.map(func -> func.slots().getOrTemp(slotConfig.getName(), null).getId())
280+
.orElse(null);
278281
}
279282
}
280283

0 commit comments

Comments
 (0)