com.azure.resourcemanager.appplatform.implementation.SpringAppImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-resourcemanager-appplatform Show documentation
Show all versions of azure-resourcemanager-appplatform Show documentation
This package contains Microsoft Azure App Platform Management SDK. For documentation on how to use this package, please see https://aka.ms/azsdk/java/mgmt
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.resourcemanager.appplatform.implementation;
import com.azure.core.util.CoreUtils;
import com.azure.resourcemanager.appplatform.AppPlatformManager;
import com.azure.resourcemanager.appplatform.fluent.models.AppResourceInner;
import com.azure.resourcemanager.appplatform.models.ActiveDeploymentCollection;
import com.azure.resourcemanager.appplatform.models.AppResourceProperties;
import com.azure.resourcemanager.appplatform.models.BindingResourceProperties;
import com.azure.resourcemanager.appplatform.models.CustomDomainProperties;
import com.azure.resourcemanager.appplatform.models.ManagedIdentityProperties;
import com.azure.resourcemanager.appplatform.models.PersistentDisk;
import com.azure.resourcemanager.appplatform.models.ResourceUploadDefinition;
import com.azure.resourcemanager.appplatform.models.SpringApp;
import com.azure.resourcemanager.appplatform.models.SpringAppDeployment;
import com.azure.resourcemanager.appplatform.models.SpringAppDeployments;
import com.azure.resourcemanager.appplatform.models.SpringAppDomains;
import com.azure.resourcemanager.appplatform.models.SpringAppServiceBindings;
import com.azure.resourcemanager.appplatform.models.SpringConfigurationService;
import com.azure.resourcemanager.appplatform.models.SpringService;
import com.azure.resourcemanager.appplatform.models.SpringServiceRegistry;
import com.azure.resourcemanager.appplatform.models.TemporaryDisk;
import com.azure.resourcemanager.appplatform.models.UserSourceType;
import com.azure.resourcemanager.resources.fluentcore.arm.models.implementation.ExternalChildResourceImpl;
import com.azure.resourcemanager.resources.fluentcore.dag.FunctionalTaskItem;
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class SpringAppImpl
extends ExternalChildResourceImpl
implements SpringApp, SpringApp.Definition, SpringApp.Update {
private Creatable springAppDeploymentToCreate = null;
private final SpringAppDeploymentsImpl deployments = new SpringAppDeploymentsImpl(this);
private final SpringAppServiceBindingsImpl serviceBindings = new SpringAppServiceBindingsImpl(this);
private final SpringAppDomainsImpl domains = new SpringAppDomainsImpl(this);
private FunctionalTaskItem setActiveDeploymentTask = null;
SpringAppImpl(String name, SpringServiceImpl parent, AppResourceInner innerObject) {
super(name, parent, innerObject);
}
@Override
public boolean isPublic() {
if (innerModel().properties() == null) {
return false;
}
return innerModel().properties().publicProperty();
}
@Override
public boolean isHttpsOnly() {
if (innerModel().properties() == null) {
return false;
}
return innerModel().properties().httpsOnly();
}
@Override
public String url() {
if (innerModel().properties() == null) {
return null;
}
return innerModel().properties().url();
}
@Override
public String fqdn() {
if (innerModel().properties() == null) {
return null;
}
return innerModel().properties().fqdn();
}
@Override
public TemporaryDisk temporaryDisk() {
if (innerModel().properties() == null) {
return null;
}
return innerModel().properties().temporaryDisk();
}
@Override
public PersistentDisk persistentDisk() {
if (innerModel().properties() == null) {
return null;
}
return innerModel().properties().persistentDisk();
}
@Override
public ManagedIdentityProperties identity() {
return innerModel().identity();
}
@Override
public String activeDeploymentName() {
// get the first active deployment
Optional deployment = deployments.list().stream().filter(SpringAppDeployment::isActive).findFirst();
return deployment.map(SpringAppDeployment::appName).orElse(null);
}
@Override
public SpringAppDeployment getActiveDeployment() {
return getActiveDeploymentAsync().block();
}
@Override
public Mono getActiveDeploymentAsync() {
return deployments.listAsync().filter(SpringAppDeployment::isActive).singleOrEmpty();
}
@Override
@SuppressWarnings("unchecked")
public > SpringAppDeployments deployments() {
return (SpringAppDeployments) deployments;
}
@Override
public SpringAppServiceBindings serviceBindings() {
return serviceBindings;
}
@Override
public SpringAppDomains customDomains() {
return domains;
}
@Override
public Mono getResourceUploadUrlAsync() {
return manager().serviceClient().getApps().getResourceUploadUrlAsync(
parent().resourceGroupName(), parent().name(), name());
}
@Override
public ResourceUploadDefinition getResourceUploadUrl() {
return getResourceUploadUrlAsync().block();
}
private void ensureProperty() {
if (innerModel().properties() == null) {
innerModel().withProperties(new AppResourceProperties());
}
}
@Override
public boolean hasConfigurationServiceBinding() {
Map> addonConfigs = innerModel().properties().addonConfigs();
if (addonConfigs == null) {
return false;
}
SpringConfigurationService configurationService = parent().getDefaultConfigurationService();
if (configurationService == null) {
return false;
}
return addonConfigs.get(Constants.APPLICATION_CONFIGURATION_SERVICE_KEY) != null
&& configurationService.id().equalsIgnoreCase((String) addonConfigs.get(Constants.APPLICATION_CONFIGURATION_SERVICE_KEY).get(Constants.BINDING_RESOURCE_ID));
}
@Override
public boolean hasServiceRegistryBinding() {
Map> addonConfigs = innerModel().properties().addonConfigs();
if (addonConfigs == null) {
return false;
}
SpringServiceRegistry serviceRegistry = parent().getDefaultServiceRegistry();
if (serviceRegistry == null) {
return false;
}
return addonConfigs.get(Constants.SERVICE_REGISTRY_KEY) != null
&& serviceRegistry.id().equalsIgnoreCase((String) addonConfigs.get(Constants.SERVICE_REGISTRY_KEY).get(Constants.BINDING_RESOURCE_ID));
}
@Override
public SpringAppImpl withDefaultPublicEndpoint() {
ensureProperty();
innerModel().properties().withPublicProperty(true);
return this;
}
@Override
public SpringAppImpl withoutDefaultPublicEndpoint() {
ensureProperty();
innerModel().properties().withPublicProperty(false);
return this;
}
@Override
public SpringAppImpl withCustomDomain(String domain) {
domains.prepareCreateOrUpdate(domain, new CustomDomainProperties());
return this;
}
@Override
public SpringAppImpl withCustomDomain(String domain, String certThumbprint) {
domains.prepareCreateOrUpdate(domain, new CustomDomainProperties().withThumbprint(certThumbprint));
return this;
}
@Override
public Update withoutCustomDomain(String domain) {
domains.prepareDelete(domain);
return this;
}
@Override
public SpringAppImpl withHttpsOnly() {
ensureProperty();
innerModel().properties().withHttpsOnly(true);
return this;
}
@Override
public SpringAppImpl withoutHttpsOnly() {
ensureProperty();
innerModel().properties().withHttpsOnly(false);
return this;
}
@Override
public SpringAppImpl withTemporaryDisk(int sizeInGB, String mountPath) {
ensureProperty();
innerModel().properties().withTemporaryDisk(
new TemporaryDisk().withSizeInGB(sizeInGB).withMountPath(mountPath));
return this;
}
@Override
public SpringAppImpl withPersistentDisk(int sizeInGB, String mountPath) {
ensureProperty();
innerModel().properties().withPersistentDisk(
new PersistentDisk().withSizeInGB(sizeInGB).withMountPath(mountPath));
return this;
}
@Override
public SpringAppImpl withActiveDeployment(String name) {
if (CoreUtils.isNullOrEmpty(name)) {
return this;
}
this.setActiveDeploymentTask =
context -> manager().serviceClient().getApps()
.setActiveDeploymentsAsync(parent().resourceGroupName(), parent().name(), name(), new ActiveDeploymentCollection().withActiveDeploymentNames(Arrays.asList(name)))
.then(context.voidMono());
return this;
}
@Override
public void beforeGroupCreateOrUpdate() {
if (setActiveDeploymentTask != null) {
this.addPostRunDependent(setActiveDeploymentTask);
}
setActiveDeploymentTask = null;
}
@Override
public Mono createResourceAsync() {
if (springAppDeploymentToCreate == null) {
withDefaultActiveDeployment();
}
return manager().serviceClient().getApps().createOrUpdateAsync(
parent().resourceGroupName(), parent().name(), name(), innerModel())
.map(inner -> {
setInner(inner);
return this;
})
.thenMany(springAppDeploymentToCreate.createAsync())
.then(Mono.just(this));
}
@Override
public Mono updateResourceAsync() {
return manager().serviceClient().getApps().updateAsync(
parent().resourceGroupName(), parent().name(), name(), innerModel())
.map(inner -> {
setInner(inner);
return this;
});
}
@Override
public Mono deleteResourceAsync() {
return manager().serviceClient().getApps().deleteAsync(parent().resourceGroupName(), parent().name(), name());
}
@Override
protected Mono getInnerAsync() {
return manager().serviceClient().getApps().getAsync(parent().resourceGroupName(), parent().name(), name());
}
@Override
public String id() {
return innerModel().id();
}
@Override
public SpringAppImpl update() {
prepareUpdate();
return this;
}
public AppPlatformManager manager() {
return parent().manager();
}
@Override
public SpringAppImpl withServiceBinding(String name, BindingResourceProperties bindingProperties) {
serviceBindings.prepareCreateOrUpdate(name, bindingProperties);
return this;
}
@Override
public SpringAppImpl withoutServiceBinding(String name) {
serviceBindings.prepareDelete(name);
return this;
}
@Override
public SpringAppImpl withDefaultActiveDeployment() {
String defaultDeploymentName = "default";
springAppDeploymentToCreate = deployments().define(defaultDeploymentName)
.withExistingSource(UserSourceType.JAR, String.format("<%s>", defaultDeploymentName))
.withActivation();
return this;
}
@Override
@SuppressWarnings("unchecked")
public >
SpringAppDeployment.DefinitionStages.Blank defineActiveDeployment(String name) {
return (SpringAppDeployment.DefinitionStages.Blank) deployments.define(name).withActivation();
}
SpringAppImpl addDeployment(SpringAppDeploymentImpl deployment) {
springAppDeploymentToCreate = deployment;
return this;
}
@Override
public SpringAppImpl withConfigurationServiceBinding() {
ensureProperty();
Map> addonConfigs = innerModel().properties().addonConfigs();
if (addonConfigs == null) {
addonConfigs = new HashMap<>();
innerModel().properties().withAddonConfigs(addonConfigs);
}
SpringConfigurationService configurationService = parent().getDefaultConfigurationService();
if (configurationService != null) {
Map configurationServiceConfigs = addonConfigs.computeIfAbsent(Constants.APPLICATION_CONFIGURATION_SERVICE_KEY, k -> new HashMap<>());
configurationServiceConfigs.put(Constants.BINDING_RESOURCE_ID, configurationService.id());
}
return this;
}
@Override
public SpringAppImpl withoutConfigurationServiceBinding() {
if (innerModel().properties() == null) {
return this;
}
Map> addonConfigs = innerModel().properties().addonConfigs();
if (addonConfigs == null) {
return this;
}
Map configurationServiceConfigs = addonConfigs.get(Constants.APPLICATION_CONFIGURATION_SERVICE_KEY);
if (configurationServiceConfigs == null) {
return this;
}
configurationServiceConfigs.put(Constants.BINDING_RESOURCE_ID, "");
return this;
}
@Override
public SpringAppImpl withServiceRegistryBinding() {
ensureProperty();
Map> addonConfigs = innerModel().properties().addonConfigs();
if (addonConfigs == null) {
addonConfigs = new HashMap<>();
innerModel().properties().withAddonConfigs(addonConfigs);
}
SpringServiceRegistry serviceRegistry = parent().getDefaultServiceRegistry();
if (serviceRegistry != null) {
Map serviceRegistryConfigs = addonConfigs.computeIfAbsent(Constants.SERVICE_REGISTRY_KEY, k -> new HashMap<>());
serviceRegistryConfigs.put(Constants.BINDING_RESOURCE_ID, serviceRegistry.id());
}
return this;
}
@Override
public SpringAppImpl withoutServiceRegistryBinding() {
if (innerModel().properties() == null) {
return this;
}
Map> addonConfigs = innerModel().properties().addonConfigs();
if (addonConfigs == null) {
return this;
}
Map configurationServiceConfigs = addonConfigs.get(Constants.SERVICE_REGISTRY_KEY);
if (configurationServiceConfigs == null) {
return this;
}
configurationServiceConfigs.put(Constants.BINDING_RESOURCE_ID, "");
return this;
}
}