From 35c39a95b4063ffc13f41f2c8873c8fe6dcb1a70 Mon Sep 17 00:00:00 2001 From: Matheus Cruz Date: Sun, 8 Dec 2024 16:09:23 -0300 Subject: [PATCH] Convert azure-functions to use @ConfigMapping --- extensions/azure-functions/deployment/pom.xml | 3 - .../deployment/AzureFunctionsConfig.java | 131 ++++++++---------- .../AzureFunctionsDeployCommand.java | 42 +++--- .../deployment/AzureFunctionsProcessor.java | 2 +- .../deployment/AzureFunctionsRunCommand.java | 6 +- extensions/azure-functions/runtime/pom.xml | 3 - 6 files changed, 84 insertions(+), 103 deletions(-) diff --git a/extensions/azure-functions/deployment/pom.xml b/extensions/azure-functions/deployment/pom.xml index 10437cac5561b..f01b722b1d4d1 100644 --- a/extensions/azure-functions/deployment/pom.xml +++ b/extensions/azure-functions/deployment/pom.xml @@ -132,9 +132,6 @@ ${project.version} - - -AlegacyConfigRoot=true - diff --git a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsConfig.java b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsConfig.java index b2994e55bd132..75e3954f397cf 100644 --- a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsConfig.java +++ b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsConfig.java @@ -9,7 +9,6 @@ import java.util.Properties; import org.apache.commons.lang3.StringUtils; -import org.jboss.logging.Logger; import com.azure.core.management.AzureEnvironment; import com.microsoft.azure.toolkit.lib.Azure; @@ -31,27 +30,29 @@ import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; /** * Azure Functions configuration. * Most options supported and name similarly to azure-functions-maven-plugin config */ @ConfigRoot(phase = ConfigPhase.BUILD_TIME) -public class AzureFunctionsConfig { +@ConfigMapping(prefix = "quarkus.azure-functions") +public interface AzureFunctionsConfig { /** * App name for azure function project. This is required setting. * * Defaults to the base artifact name */ - @ConfigItem - public Optional appName; + Optional appName(); /** * Azure Resource Group for your Azure Functions */ - @ConfigItem(defaultValue = "quarkus") - public String resourceGroup; + @WithDefault("quarkus") + String resourceGroup(); /** * Specifies the region where your Azure Functions will be hosted; default value is westus. @@ -59,114 +60,105 @@ public class AzureFunctionsConfig { * "https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions">Valid * values */ - @ConfigItem(defaultValue = "westus") - public String region; + @WithDefault("westus") + String region(); /** * Specifies whether to disable application insights for your function app */ - @ConfigItem(defaultValue = "false") - public boolean disableAppInsights; + @WithDefault("false") + boolean disableAppInsights(); /** * Specifies the instrumentation key of application insights which will bind to your function app */ - @ConfigItem - public Optional appInsightsKey; + Optional appInsightsKey(); - public RuntimeConfig runtime; + RuntimeConfig runtime(); - public AuthConfig auth; + AuthConfig auth(); /** * Specifies the name of the existing App Service Plan when you do not want to create a new one. */ - @ConfigItem(defaultValue = "java-functions-app-service-plan") - public String appServicePlanName; + @WithDefault("java-functions-app-service-plan") + String appServicePlanName(); /** * The app service plan resource group. */ - @ConfigItem - public Optional appServicePlanResourceGroup; + Optional appServicePlanResourceGroup(); /** * Azure subscription id. Required only if there are more than one subscription in your account */ - @ConfigItem - public Optional subscriptionId; + Optional subscriptionId(); /** * The pricing tier. */ - @ConfigItem - public Optional pricingTier; + Optional pricingTier(); /** * Port to run azure function in local runtime. * Will default to quarkus.http.test-port or 8081 */ - @ConfigItem - public Optional funcPort; + Optional funcPort(); /** * Config String for local debug */ - @ConfigItem(defaultValue = "transport=dt_socket,server=y,suspend=n,address=5005") - public String localDebugConfig; + @WithDefault("transport=dt_socket,server=y,suspend=n,address=5005") + String localDebugConfig(); /** * Specifies the application settings for your Azure Functions, which are defined in name-value pairs */ @ConfigItem @ConfigDocMapKey("setting-name") - public Map appSettings = Collections.emptyMap(); + Map appSettings = Collections.emptyMap(); @ConfigGroup - public static class RuntimeConfig { + interface RuntimeConfig { /** * Valid values are linux, windows, and docker */ - @ConfigItem(defaultValue = "linux") - public String os; + @WithDefault("linux") + String os(); /** * Valid values are 8, 11, and 17 */ - @ConfigItem(defaultValue = "11") - public String javaVersion; + @WithDefault("11") + String javaVersion(); /** * URL of docker image if deploying via docker */ - @ConfigItem - public Optional image; + Optional image(); /** * If using docker, url of registry */ - @ConfigItem - public Optional registryUrl; + Optional registryUrl(); } - public FunctionAppConfig toFunctionAppConfig(String subscriptionId, String appName) { + default FunctionAppConfig toFunctionAppConfig(String subscriptionId, String appName) { Map appSettings = this.appSettings; if (appSettings.isEmpty()) { appSettings = new HashMap<>(); appSettings.put("FUNCTIONS_EXTENSION_VERSION", "~4"); } return (FunctionAppConfig) new FunctionAppConfig() - .disableAppInsights(disableAppInsights) - .appInsightsKey(appInsightsKey.orElse(null)) - .appInsightsInstance(appInsightsKey.orElse(null)) + .disableAppInsights(disableAppInsights()) + .appInsightsKey(appInsightsKey().orElse(null)) + .appInsightsInstance(appInsightsKey().orElse(null)) .subscriptionId(subscriptionId) - .resourceGroup(resourceGroup) + .resourceGroup(resourceGroup()) .appName(appName) - .servicePlanName(appServicePlanName) - .servicePlanResourceGroup(appServicePlanResourceGroup.orElse(null)) - //.deploymentSlotName(getDeploymentSlotName()) - //.deploymentSlotConfigurationSource(getDeploymentSlotConfigurationSource()) + .servicePlanName(appServicePlanName()) + .servicePlanResourceGroup(appServicePlanResourceGroup().orElse(null)) .pricingTier(getParsedPricingTier(subscriptionId)) .region(getParsedRegion()) .runtime(getRuntimeConfig(subscriptionId)) @@ -174,42 +166,42 @@ public FunctionAppConfig toFunctionAppConfig(String subscriptionId, String appNa } private PricingTier getParsedPricingTier(String subscriptionId) { - return Optional.ofNullable(this.pricingTier.orElse(null)).map(PricingTier::fromString) + return Optional.ofNullable(this.pricingTier().orElse(null)).map(PricingTier::fromString) .orElseGet(() -> Optional.ofNullable(getServicePlan(subscriptionId)).map(AppServicePlan::getPricingTier) .orElse(null)); } private AppServicePlan getServicePlan(String subscriptionId) { - final String servicePlan = this.appServicePlanName; - final String servicePlanGroup = StringUtils.firstNonBlank(this.appServicePlanResourceGroup.orElse(null), - this.resourceGroup); + final String servicePlan = this.appServicePlanName(); + final String servicePlanGroup = StringUtils.firstNonBlank(this.appServicePlanResourceGroup().orElse(null), + this.resourceGroup()); return StringUtils.isAnyBlank(subscriptionId, servicePlan, servicePlanGroup) ? null : Azure.az(AzureAppService.class).plans(subscriptionId).get(servicePlan, servicePlanGroup); } private com.microsoft.azure.toolkit.lib.appservice.config.RuntimeConfig getRuntimeConfig(String subscriptionId) { - final RuntimeConfig runtime = this.runtime; + final RuntimeConfig runtime = this.runtime(); if (runtime == null) { return null; } - final OperatingSystem os = Optional.ofNullable(runtime.os).map(OperatingSystem::fromString) + final OperatingSystem os = Optional.ofNullable(runtime.os()).map(OperatingSystem::fromString) .orElseGet( () -> Optional.ofNullable(getServicePlan(subscriptionId)).map(AppServicePlan::getOperatingSystem) .orElse(null)); - final JavaVersion javaVersion = Optional.ofNullable(runtime.javaVersion).map(JavaVersion::fromString).orElse(null); + final JavaVersion javaVersion = Optional.ofNullable(runtime.javaVersion()).map(JavaVersion::fromString).orElse(null); final com.microsoft.azure.toolkit.lib.appservice.config.RuntimeConfig result = new com.microsoft.azure.toolkit.lib.appservice.config.RuntimeConfig() .os(os) .javaVersion(javaVersion).webContainer(WebContainer.JAVA_OFF) - .image(runtime.image.orElse(null)).registryUrl(runtime.registryUrl.orElse(null)); + .image(runtime.image().orElse(null)).registryUrl(runtime.registryUrl().orElse(null)); return result; } private Region getParsedRegion() { - return Optional.ofNullable(region).map(Region::fromName).orElse(null); + return Optional.ofNullable(region()).map(Region::fromName).orElse(null); } @ConfigGroup - public static class AuthConfig { + interface AuthConfig { /** * Description of each type can be found @@ -234,28 +226,23 @@ public static class AuthConfig { * * Defaults to "azure_cli" for authentication */ - @ConfigItem(defaultValue = "azure_cli") - public String type; + @WithDefault("azure_cli") + String type(); /** * Filesystem path to properties file if using file type */ - @ConfigItem - public Optional path; + Optional path(); /** * Client or App Id required if using managed_identity type */ - @ConfigItem - public Optional client; + Optional client(); /** - * Tenant Id required if using oauth2 or device_code type + * Tenant ID required if using oauth2 or device_code type */ - @ConfigItem - public Optional tenant; - - private static final Logger log = Logger.getLogger(AzureFunctionsConfig.class); + Optional tenant(); private static String findValue(Properties props, String key) { if (props.contains(key)) @@ -301,15 +288,15 @@ private static AuthConfiguration fromFile(Optional path) { } } - public AuthConfiguration toAuthConfiguration() { + default AuthConfiguration toAuthConfiguration() { try { - if (this.type.equalsIgnoreCase("file")) { - return fromFile(this.path); + if (this.type().equalsIgnoreCase("file")) { + return fromFile(this.path()); } - final AuthType type = AuthType.parseAuthType(this.type); + final AuthType type = AuthType.parseAuthType(this.type()); final AuthConfiguration authConfiguration = new AuthConfiguration(type); - authConfiguration.setClient(client.orElse(null)); - authConfiguration.setTenant(tenant.orElse(null)); + authConfiguration.setClient(client().orElse(null)); + authConfiguration.setTenant(tenant().orElse(null)); authConfiguration.setEnvironment(AzureEnvironmentUtils.azureEnvironmentToString(AzureEnvironment.AZURE)); return authConfiguration; } catch (InvalidConfigurationException e) { diff --git a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsDeployCommand.java b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsDeployCommand.java index aff10a43c92f2..544240480aead 100644 --- a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsDeployCommand.java +++ b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsDeployCommand.java @@ -167,22 +167,22 @@ protected void validateParameters(AzureFunctionsConfig config, String appName) t throw new BuildException(INVALID_APP_NAME); } // resource group - if (StringUtils.isBlank(config.resourceGroup)) { + if (StringUtils.isBlank(config.resourceGroup())) { throw new BuildException(EMPTY_RESOURCE_GROUP); } - if (config.resourceGroup.endsWith(".") || !config.resourceGroup.matches(RESOURCE_GROUP_PATTERN)) { + if (config.resourceGroup().endsWith(".") || !config.resourceGroup().matches(RESOURCE_GROUP_PATTERN)) { throw new BuildException(INVALID_RESOURCE_GROUP_NAME); } // asp name & resource group - if (StringUtils.isNotEmpty(config.appServicePlanName) - && !config.appServicePlanName.matches(APP_SERVICE_PLAN_NAME_PATTERN)) { + if (StringUtils.isNotEmpty(config.appServicePlanName()) + && !config.appServicePlanName().matches(APP_SERVICE_PLAN_NAME_PATTERN)) { throw new BuildException(String.format(INVALID_SERVICE_PLAN_NAME, APP_SERVICE_PLAN_NAME_PATTERN)); } - if (config.appServicePlanResourceGroup.isPresent() - && StringUtils.isNotEmpty(config.appServicePlanResourceGroup.orElse(null)) + if (config.appServicePlanResourceGroup().isPresent() + && StringUtils.isNotEmpty(config.appServicePlanResourceGroup().orElse(null)) && - (config.appServicePlanResourceGroup.orElse(null).endsWith(".") - || !config.appServicePlanResourceGroup.orElse(null).matches(RESOURCE_GROUP_PATTERN))) { + (config.appServicePlanResourceGroup().orElse(null).endsWith(".") + || !config.appServicePlanResourceGroup().orElse(null).matches(RESOURCE_GROUP_PATTERN))) { throw new BuildException(INVALID_SERVICE_PLAN_RESOURCE_GROUP_NAME); } // slot name @@ -196,26 +196,26 @@ protected void validateParameters(AzureFunctionsConfig config, String appName) t * */ // region - if (StringUtils.isNotEmpty(config.region) && Region.fromName(config.region).isExpandedValue()) { - log.warn(String.format(EXPANDABLE_REGION_WARNING, config.region)); + if (StringUtils.isNotEmpty(config.region()) && Region.fromName(config.region()).isExpandedValue()) { + log.warn(String.format(EXPANDABLE_REGION_WARNING, config.region())); } // os - if (StringUtils.isNotEmpty(config.runtime.os) && OperatingSystem.fromString(config.runtime.os) == null) { + if (StringUtils.isNotEmpty(config.runtime().os()) && OperatingSystem.fromString(config.runtime().os()) == null) { throw new BuildException(INVALID_OS); } // java version - if (StringUtils.isNotEmpty(config.runtime.javaVersion) - && JavaVersion.fromString(config.runtime.javaVersion).isExpandedValue()) { - log.warn(String.format(EXPANDABLE_JAVA_VERSION_WARNING, config.runtime.javaVersion)); + if (StringUtils.isNotEmpty(config.runtime().javaVersion()) + && JavaVersion.fromString(config.runtime().javaVersion()).isExpandedValue()) { + log.warn(String.format(EXPANDABLE_JAVA_VERSION_WARNING, config.runtime().javaVersion())); } // pricing tier - if (config.pricingTier.isPresent() && StringUtils.isNotEmpty(config.pricingTier.orElse(null)) - && PricingTier.fromString(config.pricingTier.orElse(null)).isExpandedValue()) { - log.warn(String.format(EXPANDABLE_PRICING_TIER_WARNING, config.pricingTier.orElse(null))); + if (config.pricingTier().isPresent() && StringUtils.isNotEmpty(config.pricingTier().orElse(null)) + && PricingTier.fromString(config.pricingTier().orElse(null)).isExpandedValue()) { + log.warn(String.format(EXPANDABLE_PRICING_TIER_WARNING, config.pricingTier().orElse(null))); } // docker image - if (OperatingSystem.fromString(config.runtime.os) == OperatingSystem.DOCKER - && StringUtils.isEmpty(config.runtime.image.orElse(null))) { + if (OperatingSystem.fromString(config.runtime().os()) == OperatingSystem.DOCKER + && StringUtils.isEmpty(config.runtime().image().orElse(null))) { throw new BuildException(EMPTY_IMAGE_NAME); } } @@ -226,9 +226,9 @@ protected void validateParameters(AzureFunctionsConfig config, String appName) t protected AzureAppService initAzureAppServiceClient(AzureFunctionsConfig config) throws BuildException { if (appServiceClient == null) { - final Account account = loginAzure(config.auth); + final Account account = loginAzure(config.auth()); final List subscriptions = account.getSubscriptions(); - final String targetSubscriptionId = getTargetSubscriptionId(config.subscriptionId.orElse(null), subscriptions, + final String targetSubscriptionId = getTargetSubscriptionId(config.subscriptionId().orElse(null), subscriptions, account.getSelectedSubscriptions()); checkSubscription(subscriptions, targetSubscriptionId); com.microsoft.azure.toolkit.lib.Azure.az(AzureAccount.class).account() diff --git a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsProcessor.java b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsProcessor.java index 04ef96151d917..438d5a70fe8f6 100644 --- a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsProcessor.java +++ b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsProcessor.java @@ -71,7 +71,7 @@ FeatureBuildItem feature() { @BuildStep AzureFunctionsAppNameBuildItem appName(OutputTargetBuildItem output, AzureFunctionsConfig functionsConfig) { - String appName = functionsConfig.appName.orElse(output.getBaseName()); + String appName = functionsConfig.appName().orElse(output.getBaseName()); return new AzureFunctionsAppNameBuildItem(appName); } diff --git a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsRunCommand.java b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsRunCommand.java index 7797fd7b49901..890e08a6f7ec5 100644 --- a/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsRunCommand.java +++ b/extensions/azure-functions/deployment/src/main/java/io/quarkus/azure/functions/deployment/AzureFunctionsRunCommand.java @@ -77,15 +77,15 @@ protected String getCheckRuntimeCommand() { protected String getStartFunctionHostCommand(AzureFunctionsConfig azureConfig) { int funcPort; - if (azureConfig.funcPort.isPresent()) { - funcPort = azureConfig.funcPort.get(); + if (azureConfig.funcPort().isPresent()) { + funcPort = azureConfig.funcPort().get(); } else { Config config = ConfigProviderResolver.instance().getConfig(); funcPort = config.getValue("quarkus.http.test-port", OptionalInt.class).orElse(8081); } final String enableDebug = System.getProperty("enableDebug"); if (StringUtils.isNotEmpty(enableDebug) && enableDebug.equalsIgnoreCase("true")) { - return String.format(FUNC_HOST_START_WITH_DEBUG_CMD, funcPort, azureConfig.localDebugConfig); + return String.format(FUNC_HOST_START_WITH_DEBUG_CMD, funcPort, azureConfig.localDebugConfig()); } else { return String.format(FUNC_HOST_START_CMD, funcPort); } diff --git a/extensions/azure-functions/runtime/pom.xml b/extensions/azure-functions/runtime/pom.xml index 5440764ba4778..0901929c5192b 100644 --- a/extensions/azure-functions/runtime/pom.xml +++ b/extensions/azure-functions/runtime/pom.xml @@ -53,9 +53,6 @@ ${project.version} - - -AlegacyConfigRoot=true -