Skip to content

Commit

Permalink
Merge pull request #1762 from microsoft/andy-skip-webapp-create
Browse files Browse the repository at this point in the history
[Task 1872016] support flag to skip webapp create
  • Loading branch information
andxu authored Sep 23, 2021
2 parents 3691515 + 2da4e6d commit f3b5c24
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public abstract class AbstractFunctionMojo extends AbstractAppServiceMojo {
/**
* App Service region, which will only be used to create App Service at the first time.
*/
@Parameter(property = "functions.region", defaultValue = "westeurope")
@Parameter(property = "functions.region")
protected String region;

@Parameter(property = "functions.runtime")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.microsoft.azure.toolkit.lib.common.task.AzureTask;
import com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemetry;
import com.microsoft.azure.toolkit.lib.resource.task.CreateResourceGroupTask;
import lombok.Setter;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Flux;
Expand All @@ -36,6 +37,8 @@
import java.util.HashMap;
import java.util.List;

import static com.microsoft.azure.toolkit.lib.appservice.utils.Utils.throwForbidCreateResourceWarning;

public class CreateOrUpdateWebAppTask extends AzureTask<IWebApp> {
private static final String CREATE_NEW_WEB_APP = "createNewWebApp";

Expand All @@ -47,6 +50,9 @@ public class CreateOrUpdateWebAppTask extends AzureTask<IWebApp> {
private final AppServiceConfig config;
private final List<AzureTask<?>> subTasks;

@Setter
private boolean skipCreateAzureResource;

public CreateOrUpdateWebAppTask(AppServiceConfig config) {
this.config = config;
this.subTasks = this.initTasks();
Expand All @@ -60,6 +66,9 @@ private List<AzureTask<?>> initTasks() {
final IWebApp target = az.subscription(config.subscriptionId())
.webapp(config.resourceGroup(), config.appName());
if (!target.exists()) {
if (skipCreateAzureResource) {
throwForbidCreateResourceWarning("Web app", config.appName());
}
CheckNameAvailabilityResultEntity result = az.checkNameAvailability(config.subscriptionId(), config.appName());
if (!result.isAvailable()) {
throw new AzureToolkitRuntimeException(AzureString.format("Cannot create webapp {0} due to error: {1}",
Expand Down Expand Up @@ -102,6 +111,11 @@ private IWebApp update(final IWebApp webApp) {
final IAppServicePlan currentPlan = webApp.plan();
final AppServicePlanConfig servicePlanConfig = config.getServicePlanConfig();

if (skipCreateAzureResource && !Azure.az(AzureAppService.class).appServicePlan(servicePlanConfig.servicePlanResourceGroup(), servicePlanConfig.servicePlanName()).exists()) {
throwForbidCreateResourceWarning("Service plan", servicePlanConfig.servicePlanResourceGroup() + "/" + servicePlanConfig.servicePlanName());
}
throwForbidCreateResourceWarning("Web app", config.appName());

final Runtime runtime = getRuntime(config.runtime());
final IAppServicePlan appServicePlan = new CreateOrUpdateAppServicePlanTask(servicePlanConfig).execute();
final IAppServiceUpdater<? extends IWebApp> draft = webApp.update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public static <T> void mergeObjects(T to, T from) throws IllegalAccessException
}
}
}

public static void throwForbidCreateResourceWarning(String resourceType, String name) {
throw new AzureToolkitRuntimeException(String.format("%s(%s) cannot be found, if you want to create azure resources please remove command line arguments: " +
"`-Dazure.resource.create.skip=true` or `-DskipCreateAzureResource`.", resourceType, name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ default List<Subscription> getSubscriptions() {
return Azure.az(IAzureAccount.class).account().getSelectedSubscriptions();
}

@SuppressWarnings("checkstyle:Indentation")
default List<Region> listSupportedRegions(String subscriptionId) {
String[] names = StringUtils.split(name(), "/");
if (names.length != 2) {
Expand Down
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 @@ -36,6 +36,7 @@

import static com.microsoft.azure.toolkit.lib.appservice.utils.AppServiceConfigUtils.fromAppService;
import static com.microsoft.azure.toolkit.lib.appservice.utils.AppServiceConfigUtils.mergeAppServiceConfig;
import static com.microsoft.azure.toolkit.lib.appservice.utils.Utils.throwForbidCreateResourceWarning;

/**
* Deploy an Azure Web App, either Windows-based or Linux-based.
Expand All @@ -59,6 +60,7 @@ protected void doExecute() throws AzureExecutionException {
}

private IWebAppBase<?> createOrUpdateResource() throws AzureExecutionException {
final boolean skipCreate = skipAzureResourceCreate || skipCreateAzureResource;
if (!isDeployToDeploymentSlot()) {
final AppServiceConfig appServiceConfig = getConfigParser().getAppServiceConfig();
IWebApp app = Azure.az(AzureAppService.class).webapp(appServiceConfig.resourceGroup(), appServiceConfig.appName());
Expand All @@ -69,12 +71,18 @@ private IWebAppBase<?> createOrUpdateResource() throws AzureExecutionException {
if (appServiceConfig.pricingTier() == null) {
appServiceConfig.pricingTier(appServiceConfig.runtime().webContainer() == WebContainer.JBOSS_7 ? PricingTier.PREMIUM_P1V3 : PricingTier.PREMIUM_P1V2);
}
return new CreateOrUpdateWebAppTask(appServiceConfig).execute();
final CreateOrUpdateWebAppTask task = new CreateOrUpdateWebAppTask(appServiceConfig);
task.setSkipCreateAzureResource(skipCreate);
return task.execute();
} else {
// todo: New CreateOrUpdateDeploymentSlotTask
final DeploymentSlotConfig config = getConfigParser().getDeploymentSlotConfig();
final IWebAppDeploymentSlot slot = getDeploymentSlot(config);
return slot.exists() ? updateDeploymentSlot(slot, config) : createDeploymentSlot(slot, config);
final boolean slotExists = slot.exists();
if (!slotExists && skipCreate) {
throwForbidCreateResourceWarning("Deployment slot", config.getName());
}
return slotExists ? updateDeploymentSlot(slot, config) : createDeploymentSlot(slot, config);
}
}

Expand Down Expand Up @@ -117,6 +125,7 @@ private IWebAppDeploymentSlot createDeploymentSlot(final IWebAppDeploymentSlot s

// update existing slot is not supported in current version, will implement it later
private IWebAppDeploymentSlot updateDeploymentSlot(final IWebAppDeploymentSlot slot, final DeploymentSlotConfig slotConfig) {
AzureMessager.getMessager().warning("update existing slot is not supported in current version");
return slot;
}

Expand Down

0 comments on commit f3b5c24

Please sign in to comment.