Skip to content

Commit

Permalink
add the logic to allow user to disable creating new azure resources
Browse files Browse the repository at this point in the history
  • Loading branch information
andxu committed Sep 23, 2021
1 parent 111688c commit da7f914
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ public abstract class AbstractWebAppMojo extends AbstractAppServiceMojo {
@Parameter(property = "webapp.skip", defaultValue = "false")
protected boolean skip;

/**
* TODO(andxu): move this flag to AbstractAzureMojo
*/
@JsonIgnore
@Parameter(property = "azure.resource.create.skip", defaultValue = "false")
protected boolean skipAzureResourceCreate;

/**
* TODO(andxu): move this flag to AbstractAzureMojo
*/
@JsonIgnore
@Parameter(property = "skipCreateAzureResource")
protected boolean skipCreateAzureResource;

/**
* App Service region, which will only be used to create App Service at the first time.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.microsoft.azure.maven.webapp;

import com.azure.core.management.exception.ManagementException;
import com.microsoft.azure.maven.model.DeploymentResource;
import com.microsoft.azure.maven.webapp.configuration.DeploymentSlotConfig;
import com.microsoft.azure.maven.webapp.task.DeployExternalResourcesTask;
Expand All @@ -23,8 +24,10 @@
import com.microsoft.azure.toolkit.lib.appservice.utils.AppServiceConfigUtils;
import com.microsoft.azure.toolkit.lib.common.bundle.AzureString;
import com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException;
import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException;
import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager;
import com.microsoft.azure.toolkit.lib.common.utils.Utils;
import com.microsoft.azure.toolkit.lib.resource.AzureGroup;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.versioning.ComparableVersion;
Expand Down Expand Up @@ -63,12 +66,34 @@ private IWebAppBase<?> createOrUpdateResource() throws AzureExecutionException {
final AppServiceConfig appServiceConfig = getConfigParser().getAppServiceConfig();
IWebApp app = Azure.az(AzureAppService.class).webapp(appServiceConfig.resourceGroup(), appServiceConfig.appName());
final boolean newWebApp = !app.exists();
final boolean skipCreate = skipAzureResourceCreate || skipCreateAzureResource;
if (skipCreate) {
if (newWebApp) {
throwForbidCreateResourceWarning("Web app", appName);
}
if (StringUtils.isNotBlank(appServiceConfig.servicePlanResourceGroup())) {
final AzureGroup az = Azure.az(AzureGroup.class).subscription(subscriptionId);
try {
// check servicePlanResourceGroup for existence
az.getByName(appServiceConfig.servicePlanResourceGroup());
} catch (ManagementException e) {
if (e.getResponse().getStatusCode() == 404) {
throwForbidCreateResourceWarning("Resource group", appServiceConfig.servicePlanResourceGroup());
}
}
}
}
AppServiceConfig defaultConfig = !newWebApp ? fromAppService(app, app.plan()) : buildDefaultConfig(appServiceConfig.subscriptionId(),
appServiceConfig.resourceGroup(), appServiceConfig.appName());
mergeAppServiceConfig(appServiceConfig, defaultConfig);
if (appServiceConfig.pricingTier() == null) {
appServiceConfig.pricingTier(appServiceConfig.runtime().webContainer() == WebContainer.JBOSS_7 ? PricingTier.PREMIUM_P1V3 : PricingTier.PREMIUM_P1V2);
}
if (skipCreate) {
if (!Azure.az(AzureAppService.class).appServicePlan(appServiceConfig.servicePlanResourceGroup(), appServiceConfig.servicePlanName()).exists()) {
throwForbidCreateResourceWarning("Service plan", appServiceConfig.servicePlanName());
}
}
return new CreateOrUpdateWebAppTask(appServiceConfig).execute();
} else {
// todo: New CreateOrUpdateDeploymentSlotTask
Expand All @@ -78,6 +103,11 @@ private IWebAppBase<?> createOrUpdateResource() throws AzureExecutionException {
}
}

private static void throwForbidCreateResourceWarning(String resourceType, String name) {
throw new AzureToolkitRuntimeException(String.format("%s(%s) cannot be found, if you want to create please remove these maven arguments: " +
"`-Dazure.resource.create.skip=true` or `-DskipCreateAzureResource`.", resourceType, name));
}

private AppServiceConfig buildDefaultConfig(String subscriptionId, String resourceGroup, String appName) {
ComparableVersion javaVersionForProject = null;
final String outputFileName = project.getBuild().getFinalName() + "." + project.getPackaging();
Expand Down

0 comments on commit da7f914

Please sign in to comment.