From da7f914d43fb5d6dab132ca1ec1249f542b9ae92 Mon Sep 17 00:00:00 2001 From: andxu Date: Wed, 22 Sep 2021 14:51:39 +0800 Subject: [PATCH] add the logic to allow user to disable creating new azure resources --- .../maven/webapp/AbstractWebAppMojo.java | 14 +++++++++ .../azure/maven/webapp/DeployMojo.java | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/AbstractWebAppMojo.java b/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/AbstractWebAppMojo.java index bb6a68c429..6cfedd9f87 100644 --- a/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/AbstractWebAppMojo.java +++ b/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/AbstractWebAppMojo.java @@ -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. */ diff --git a/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/DeployMojo.java b/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/DeployMojo.java index 6344a1304e..f796f5c60b 100644 --- a/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/DeployMojo.java +++ b/azure-webapp-maven-plugin/src/main/java/com/microsoft/azure/maven/webapp/DeployMojo.java @@ -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; @@ -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; @@ -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 @@ -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();