88import com .azure .core .management .exception .ManagementException ;
99import com .microsoft .azure .toolkit .lib .Azure ;
1010import com .microsoft .azure .toolkit .lib .appservice .AzureAppService ;
11+ import com .microsoft .azure .toolkit .lib .common .cache .CacheManager ;
12+ import com .microsoft .azure .toolkit .lib .common .cache .Cacheable ;
1113import com .microsoft .azure .toolkit .lib .common .entity .CheckNameAvailabilityResultEntity ;
1214import com .microsoft .azure .toolkit .lib .common .model .ResourceGroup ;
1315import com .microsoft .azure .toolkit .lib .resource .AzureGroup ;
1416import com .microsoft .azure .toolkit .lib .springcloud .SpringCloudCluster ;
1517import org .apache .commons .lang3 .StringUtils ;
16- import org .apache .commons .lang3 .tuple .Pair ;
1718
18- import java .util .HashMap ;
1919import java .util .HashSet ;
20- import java .util .Map ;
2120import java .util .Objects ;
2221import java .util .Set ;
22+ import java .util .concurrent .ExecutionException ;
2323import java .util .regex .Matcher ;
2424import java .util .regex .Pattern ;
2525
@@ -36,9 +36,6 @@ public class ValidationUtils {
3636 private static final String SPRING_CLOUD_APP_NAME_PATTERN = "^[a-z][a-z0-9-]{2,30}[a-z0-9]$" ;
3737 private static final String APP_INSIGHTS_NAME_INVALID_CHARACTERS = "[*;/?:@&=+$,<>#%\\ \" \\ {}|^'`\\ \\ \\ [\\ ]]" ;
3838
39- private static Map <Pair <String , String >, String > appServiceNameValidationCache = new HashMap <>();
40- private static Map <String , String > resourceGroupValidationCache = new HashMap <>();
41-
4239 public static boolean isValidJavaPackageName (String packageName ) {
4340 return packageName != null && packageName .matches (PACKAGE_NAME_REGEX );
4441 }
@@ -60,45 +57,43 @@ public static boolean isValidVersion(String version) {
6057 return version != null && version .matches (VERSION_REGEX );
6158 }
6259
60+ @ Cacheable (cacheName = "appservice/validation/name" , key = "$subscriptionId/$appServiceName" )
6361 public static void validateAppServiceName (String subscriptionId , String appServiceName ) {
64- final Pair <String , String > cacheKey = Pair .of (subscriptionId , appServiceName );
65- if (appServiceNameValidationCache .containsKey (cacheKey )) {
66- throwCachedValidationResult (appServiceNameValidationCache .get (cacheKey ));
67- return ;
68- }
6962 if (StringUtils .isEmpty (subscriptionId )) {
70- cacheAndThrow ( appServiceNameValidationCache , cacheKey , message ("appService.subscription.validate.empty" ));
63+ throw new IllegalArgumentException ( message ("appService.subscription.validate.empty" ));
7164 }
7265 if (!isValidAppServiceName (appServiceName )) {
73- cacheAndThrow ( appServiceNameValidationCache , cacheKey , message ("appService.subscription.validate.invalidName" ));
66+ throw new IllegalArgumentException ( message ("appService.subscription.validate.invalidName" ));
7467 }
7568 final CheckNameAvailabilityResultEntity result = Azure .az (AzureAppService .class ).checkNameAvailability (subscriptionId , appServiceName );
7669 if (!result .isAvailable ()) {
77- cacheAndThrow ( appServiceNameValidationCache , cacheKey , result .getUnavailabilityMessage ());
70+ throw new IllegalArgumentException ( result .getUnavailabilityMessage ());
7871 }
79- appServiceNameValidationCache .put (cacheKey , null );
8072 }
8173
82- public static void validateResourceGroupName (String subscriptionId , String resourceGroup ) {
83- if (resourceGroupValidationCache .containsKey (subscriptionId )) {
84- throwCachedValidationResult (appServiceNameValidationCache .get (subscriptionId ));
85- return ;
74+ // todo: move validation and related cache management to toolkit lib
75+ public static void evictCacheForAppServiceNameValidation (String subscriptionId , String appServiceName ) {
76+ try {
77+ CacheManager .evictCache ("appservice/validation/name" , String .format ("%s/%s" , subscriptionId , appServiceName ));
78+ } catch (ExecutionException e ) {
79+ // swallow exception for clear cache
8680 }
81+ }
82+
83+ @ Cacheable (cacheName = "resourcegroup/validation/name" , key = "$subscriptionId/$resourceGroup" )
84+ public static void validateResourceGroupName (String subscriptionId , String resourceGroup ) {
8785 if (StringUtils .isEmpty (subscriptionId )) {
88- cacheAndThrow ( resourceGroupValidationCache , subscriptionId , message ("appService.subscription.validate.empty" ));
86+ throw new IllegalArgumentException ( message ("appService.subscription.validate.empty" ));
8987 }
9088 if (StringUtils .isEmpty (resourceGroup )) {
91- cacheAndThrow ( resourceGroupValidationCache , subscriptionId , message ("appService.resourceGroup.validate.empty" ));
89+ throw new IllegalArgumentException ( message ("appService.resourceGroup.validate.empty" ));
9290 }
9391 try {
9492 final ResourceGroup rg = Azure .az (AzureGroup .class ).get (subscriptionId , resourceGroup );
95- if (rg != null ) {
96- cacheAndThrow (resourceGroupValidationCache , subscriptionId , message ("appService.resourceGroup.validate.exist" ));
97- }
93+ throw new IllegalArgumentException (message ("appService.resourceGroup.validate.exist" ));
9894 } catch (ManagementException e ) {
9995 // swallow exception for get resources
10096 }
101- resourceGroupValidationCache .put (subscriptionId , null );
10297 }
10398
10499 public static void validateAppServicePlanName (String appServicePlan ) {
@@ -142,15 +137,4 @@ public static void validateSpringCloudAppName(final String name, final SpringClo
142137 throw new IllegalArgumentException (message ("springcloud.app.name.validate.exist" , name ));
143138 }
144139 }
145-
146- private static void cacheAndThrow (Map exceptionCache , Object key , String errorMessage ) {
147- exceptionCache .put (key , errorMessage );
148- throw new IllegalArgumentException (errorMessage );
149- }
150-
151- private static void throwCachedValidationResult (String errorMessage ) {
152- if (StringUtils .isNotEmpty (errorMessage )) {
153- throw new IllegalArgumentException (errorMessage );
154- }
155- }
156140}
0 commit comments