Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage common property configuration supports all Storage features #29094

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
*/
String prefix() default "";

/**
* The prefixes that should be applied to each property. The prefixes automatically ends
* with a dot if not specified. A valid prefix is defined by one or more words
* separated with dots (e.g. {@code "spring.cloud.azure"}).
* @return the prefix array
*/
String[] prefixes() default {};

/**
* The name of the properties to test. If a prefix has been defined, it is applied to
* compute the full key of each property. For instance if the prefix is {@code "spring.cloud.azure"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private static class Spec {

private final String prefix;

private final String[] prefixes;

private final String havingValue;

private final String[] names;
Expand All @@ -61,6 +63,7 @@ private static class Spec {
Spec(AnnotationAttributes annotationAttributes) {
String prefixAttr = annotationAttributes.getString("prefix");
prefix = AzureStringUtils.ensureEndsWithSuffix(prefixAttr.trim(), PROPERTY_SUFFIX);
hui1110 marked this conversation as resolved.
Show resolved Hide resolved
this.prefixes = getPrefixes(annotationAttributes);
saragluna marked this conversation as resolved.
Show resolved Hide resolved
this.havingValue = annotationAttributes.getString("havingValue");
this.names = getNames(annotationAttributes);
this.matchIfMissing = annotationAttributes.getBoolean("matchIfMissing");
Expand All @@ -76,6 +79,14 @@ private String[] getNames(Map<String, Object> annotationAttributes) {
return (value.length > 0) ? value : name;
}

private String[] getPrefixes(Map<String, Object> annotationAttributes) {
String[] prefixesAttr = (String[]) annotationAttributes.get("prefixes");
for (int i = 0; i < prefixesAttr.length; i++) {
prefixesAttr[i] = AzureStringUtils.ensureEndsWithSuffix(prefixesAttr[i].trim(), PROPERTY_SUFFIX);
}
return prefixesAttr;
}

private void collectProperties(PropertyResolver resolver, List<String> missing, List<String> nonMatching,
List<String> matching) {
for (String name : this.names) {
Expand All @@ -94,6 +105,24 @@ private void collectProperties(PropertyResolver resolver, List<String> missing,
}
}
}
for (String prefix : this.prefixes) {
for (String name : this.names) {
String key = prefix + name;
if (resolver.containsProperty(key)) {
if (!isMatch(resolver.getProperty(key), this.havingValue)) {
nonMatching.add(name);
} else {
matching.add(name);
}
} else {
if (!this.matchIfMissing) {
missing.add(name);
} else {
matching.add(name);
}
}
}
}
}

private boolean isMatch(String value, String requiredValue) {
Expand All @@ -107,7 +136,17 @@ private boolean isMatch(String value, String requiredValue) {
public String toString() {
StringBuilder result = new StringBuilder();
result.append("(");
result.append(this.prefix);
if (this.prefix.length() > 1) {
result.append(this.prefix);
} else if (this.prefixes.length > 0) {
if (this.prefixes.length == 1) {
result.append(this.prefixes[0]);
} else {
result.append("[");
result.append(StringUtils.arrayToCommaDelimitedString(this.prefixes));
result.append("]");
}
}
if (this.names.length == 1) {
result.append(this.names[0]);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.cloud.autoconfigure.implementation.properties.utils;

import com.azure.spring.cloud.core.implementation.util.AzurePropertiesUtils;
import com.azure.spring.cloud.core.properties.AzureProperties;

/**
* Util class for processor Azure Service properties.
hui1110 marked this conversation as resolved.
Show resolved Hide resolved
*/
public final class AzureServicePropertiesUtils {

public AzureServicePropertiesUtils() {
}

/**
* Load the default value to an Azure Service properties from the Azure properties.
*
* @param source The Azure properties.
* @param target The properties of an Azure Service, such as Storage Blob properties. Some common components of the
* service's properties have default value as set to the Azure properties. For example, the proxy of
* the Storage Blob properties takes the proxy set to the Azure properties as default.
* @param <T> The type of the properties of an Azure Service.
* @return The Azure Service's properties.
*/
public static <T extends AzureProperties> T loadServiceProperties(AzureProperties source, T target) {
AzurePropertiesUtils.copyAzureCommonPropertiesIgnoreNull(source, target);
AzurePropertiesUtils.copyPropertiesIgnoreNull(source, target);
return target;
}
hui1110 marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.cloud.autoconfigure.implementation.storage.common;

/**
* Global Properties for Azure Storage services.
*/
public class AzureStorageGlobalProperties extends AzureStorageProperties {

public static final String PREFIX = "spring.cloud.azure.storage";

hui1110 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.cloud.autoconfigure.storage;

import com.azure.spring.cloud.autoconfigure.AzureServiceConfigurationBase;
import com.azure.spring.cloud.autoconfigure.context.AzureGlobalProperties;
import com.azure.spring.cloud.autoconfigure.implementation.storage.common.AzureStorageGlobalProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Configuration for Azure Storage support.
*
* @since 4.0.0
hui1110 marked this conversation as resolved.
Show resolved Hide resolved
*/
@Configuration
public class AzureStorageConfiguration extends AzureServiceConfigurationBase {

AzureStorageConfiguration(AzureGlobalProperties azureGlobalProperties) {
super(azureGlobalProperties);
}

@Bean
@ConfigurationProperties(AzureStorageGlobalProperties.PREFIX)
AzureStorageGlobalProperties storageProperties() {
return loadProperties(getAzureGlobalProperties(), new AzureStorageGlobalProperties());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

package com.azure.spring.cloud.autoconfigure.storage.blob;

import com.azure.spring.cloud.autoconfigure.AzureServiceConfigurationBase;
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty;
import com.azure.spring.cloud.autoconfigure.context.AzureGlobalProperties;
import com.azure.spring.cloud.autoconfigure.implementation.properties.utils.AzureServicePropertiesUtils;
import com.azure.spring.cloud.autoconfigure.implementation.storage.blob.properties.AzureStorageBlobProperties;
import com.azure.spring.cloud.autoconfigure.storage.AzureStorageConfiguration;
import com.azure.spring.cloud.autoconfigure.implementation.storage.common.AzureStorageGlobalProperties;
import com.azure.spring.cloud.core.implementation.util.AzureSpringIdentifier;
import com.azure.spring.cloud.core.provider.connectionstring.ServiceConnectionStringProvider;
import com.azure.spring.cloud.core.provider.connectionstring.StaticConnectionStringProvider;
Expand All @@ -27,7 +28,10 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import static com.azure.spring.cloud.autoconfigure.context.AzureContextUtils.STORAGE_BLOB_CLIENT_BUILDER_BEAN_NAME;
import static com.azure.spring.cloud.autoconfigure.context.AzureContextUtils.STORAGE_BLOB_CLIENT_BUILDER_FACTORY_BEAN_NAME;
Expand All @@ -37,19 +41,18 @@
*
* @since 4.0.0
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass(BlobServiceClientBuilder.class)
@ConditionalOnProperty(value = "spring.cloud.azure.storage.blob.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnAnyProperty(prefix = "spring.cloud.azure.storage.blob", name = { "account-name", "endpoint", "connection-string" })
public class AzureStorageBlobAutoConfiguration extends AzureServiceConfigurationBase {

AzureStorageBlobAutoConfiguration(AzureGlobalProperties azureGlobalProperties) {
super(azureGlobalProperties);
}
@ConditionalOnAnyProperty(prefixes = { "spring.cloud.azure.storage.blob", "spring.cloud.azure.storage" }, name = { "account-name", "endpoint", "connection-string" })
hui1110 marked this conversation as resolved.
Show resolved Hide resolved
@Import(AzureStorageConfiguration.class)
public class AzureStorageBlobAutoConfiguration {

@Bean
@ConfigurationProperties(AzureStorageBlobProperties.PREFIX)
AzureStorageBlobProperties azureStorageBlobProperties() {
return loadProperties(getAzureGlobalProperties(), new AzureStorageBlobProperties());
AzureStorageBlobProperties azureStorageBlobProperties(AzureStorageGlobalProperties storageGlobalProperties) {
return AzureServicePropertiesUtils.loadServiceProperties(storageGlobalProperties, new AzureStorageBlobProperties());
}

@Bean
Expand Down Expand Up @@ -130,7 +133,7 @@ BlobServiceClientBuilder blobServiceClientBuilder(@Qualifier(STORAGE_BLOB_CLIENT
}

@Bean
@ConditionalOnProperty("spring.cloud.azure.storage.blob.connection-string")
@ConditionalOnAnyProperty(prefixes = { AzureStorageBlobProperties.PREFIX, AzureStorageGlobalProperties.PREFIX }, name = { "connection-string" })
StaticConnectionStringProvider<AzureServiceType.StorageBlob> staticStorageBlobConnectionStringProvider(
AzureStorageBlobProperties properties) {
return new StaticConnectionStringProvider<>(AzureServiceType.STORAGE_BLOB, properties.getConnectionString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
@ConditionalOnClass({ AzureStorageBlobProtocolResolver.class })
@ConditionalOnProperty(value = "spring.cloud.azure.storage.blob.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnAnyProperty(prefix = "spring.cloud.azure.storage.blob", name = { "account-name", "endpoint", "connection-string" })
@ConditionalOnAnyProperty(prefixes = { "spring.cloud.azure.storage.blob", "spring.cloud.azure.storage" }, name = { "account-name", "endpoint", "connection-string" })
public class AzureStorageBlobResourceAutoConfiguration {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

package com.azure.spring.cloud.autoconfigure.storage.fileshare;

import com.azure.spring.cloud.autoconfigure.AzureServiceConfigurationBase;
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty;
import com.azure.spring.cloud.autoconfigure.context.AzureGlobalProperties;
import com.azure.spring.cloud.autoconfigure.implementation.properties.utils.AzureServicePropertiesUtils;
import com.azure.spring.cloud.autoconfigure.implementation.storage.common.AzureStorageGlobalProperties;
import com.azure.spring.cloud.autoconfigure.implementation.storage.fileshare.properties.AzureStorageFileShareProperties;
import com.azure.spring.cloud.autoconfigure.storage.AzureStorageConfiguration;
import com.azure.spring.cloud.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.cloud.core.implementation.util.AzureSpringIdentifier;
import com.azure.spring.cloud.core.provider.connectionstring.ServiceConnectionStringProvider;
Expand All @@ -28,26 +29,28 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* {@link EnableAutoConfiguration Auto-configuration} for Azure Storage File Share support.
*
* @since 4.0.0
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass(ShareServiceClientBuilder.class)
@ConditionalOnProperty(value = "spring.cloud.azure.storage.fileshare.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnAnyProperty(prefix = "spring.cloud.azure.storage.fileshare", name = { "account-name", "endpoint", "connection-string" })
public class AzureStorageFileShareAutoConfiguration extends AzureServiceConfigurationBase {

AzureStorageFileShareAutoConfiguration(AzureGlobalProperties azureGlobalProperties) {
super(azureGlobalProperties);
}
@ConditionalOnAnyProperty(prefixes = { "spring.cloud.azure.storage.fileshare", "spring.cloud.azure.storage" }, name = { "account-name", "endpoint", "connection-string" })
@Import(AzureStorageConfiguration.class)
public class AzureStorageFileShareAutoConfiguration {

@Bean
@ConfigurationProperties(AzureStorageFileShareProperties.PREFIX)
AzureStorageFileShareProperties azureStorageFileShareProperties() {
return loadProperties(getAzureGlobalProperties(), new AzureStorageFileShareProperties());
AzureStorageFileShareProperties azureStorageFileShareProperties(AzureStorageGlobalProperties storageGlobalProperties) {
return AzureServicePropertiesUtils.loadServiceProperties(storageGlobalProperties, new AzureStorageFileShareProperties());
}

/**
Expand Down Expand Up @@ -140,7 +143,7 @@ ShareDirectoryClient shareDirectoryClient(AzureStorageFileShareProperties proper
}

@Bean
@ConditionalOnProperty("spring.cloud.azure.storage.fileshare.connection-string")
@ConditionalOnAnyProperty(prefixes = { AzureStorageFileShareProperties.PREFIX, AzureStorageGlobalProperties.PREFIX }, name = { "connection-string" })
StaticConnectionStringProvider<AzureServiceType.StorageFileShare> staticStorageFileShareConnectionStringProvider(
AzureStorageFileShareProperties properties) {
return new StaticConnectionStringProvider<>(AzureServiceType.STORAGE_FILE_SHARE, properties.getConnectionString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
@ConditionalOnClass({ AzureStorageFileProtocolResolver.class })
@ConditionalOnProperty(value = "spring.cloud.azure.storage.fileshare.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnAnyProperty(prefix = "spring.cloud.azure.storage.fileshare", name = { "account-name", "endpoint", "connection-string" })
@ConditionalOnAnyProperty(prefixes = { "spring.cloud.azure.storage.fileshare", "spring.cloud.azure.storage" }, name = { "account-name", "endpoint", "connection-string" })
public class AzureStorageFileShareResourceAutoConfiguration {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

package com.azure.spring.cloud.autoconfigure.storage.queue;

import com.azure.spring.cloud.autoconfigure.AzureServiceConfigurationBase;
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty;
import com.azure.spring.cloud.autoconfigure.context.AzureGlobalProperties;
import com.azure.spring.cloud.autoconfigure.implementation.properties.utils.AzureServicePropertiesUtils;
import com.azure.spring.cloud.autoconfigure.implementation.storage.common.AzureStorageGlobalProperties;
import com.azure.spring.cloud.autoconfigure.implementation.storage.queue.properties.AzureStorageQueueProperties;
import com.azure.spring.cloud.autoconfigure.storage.AzureStorageConfiguration;
import com.azure.spring.cloud.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.cloud.core.implementation.util.AzureSpringIdentifier;
import com.azure.spring.cloud.core.provider.connectionstring.ServiceConnectionStringProvider;
Expand All @@ -24,26 +25,28 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* {@link EnableAutoConfiguration Auto-configuration} for Azure Storage Queue support.
*
* @since 4.0.0
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass(QueueServiceClientBuilder.class)
@ConditionalOnProperty(value = "spring.cloud.azure.storage.queue.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnAnyProperty(prefix = "spring.cloud.azure.storage.queue", name = { "account-name", "endpoint", "connection-string" })
public class AzureStorageQueueAutoConfiguration extends AzureServiceConfigurationBase {

AzureStorageQueueAutoConfiguration(AzureGlobalProperties azureGlobalProperties) {
super(azureGlobalProperties);
}
@ConditionalOnAnyProperty(prefixes = { "spring.cloud.azure.storage.queue", "spring.cloud.azure.storage" }, name = { "account-name", "endpoint", "connection-string" })
@Import(AzureStorageConfiguration.class)
public class AzureStorageQueueAutoConfiguration {

@Bean
@ConfigurationProperties(AzureStorageQueueProperties.PREFIX)
AzureStorageQueueProperties azureStorageQueueProperties() {
return loadProperties(getAzureGlobalProperties(), new AzureStorageQueueProperties());
AzureStorageQueueProperties azureStorageQueueProperties(AzureStorageGlobalProperties storageGlobalProperties) {
return AzureServicePropertiesUtils.loadServiceProperties(storageGlobalProperties, new AzureStorageQueueProperties());
}

/**
Expand Down Expand Up @@ -105,7 +108,7 @@ QueueServiceClientBuilder queueServiceClientBuilder(QueueServiceClientBuilderFac
}

@Bean
@ConditionalOnProperty("spring.cloud.azure.storage.queue.connection-string")
@ConditionalOnAnyProperty(prefixes = { AzureStorageQueueProperties.PREFIX, AzureStorageGlobalProperties.PREFIX }, name = { "connection-string" })
StaticConnectionStringProvider<AzureServiceType.StorageQueue> staticStorageQueueConnectionStringProvider(
AzureStorageQueueProperties storageQueueProperties) {

Expand Down
Loading