Skip to content

Commit 19565f1

Browse files
authored
Sort azure resources (#3788)
1 parent cb4a312 commit 19565f1

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

Utils/azuretools-core/src/com/microsoft/azuretools/core/mvp/model/AzureMvpModel.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import com.microsoft.azure.management.Azure;
2727
import com.microsoft.azure.management.appservice.DeploymentSlot;
28-
import com.microsoft.azure.management.appservice.LogLevel;
2928
import com.microsoft.azure.management.appservice.PricingTier;
3029
import com.microsoft.azure.management.appservice.WebApp;
3130
import com.microsoft.azure.management.appservice.WebAppDiagnosticLogs;
@@ -36,22 +35,23 @@
3635
import com.microsoft.azuretools.authmanage.AuthMethodManager;
3736
import com.microsoft.azuretools.authmanage.models.SubscriptionDetail;
3837
import com.microsoft.azuretools.sdkmanage.AzureManager;
39-
4038
import com.microsoft.azuretools.utils.AzureModel;
4139
import com.microsoft.azuretools.utils.AzureModelController;
4240
import com.microsoft.azuretools.utils.CanceledByUserException;
41+
import org.apache.commons.lang3.StringUtils;
42+
import rx.Observable;
43+
import rx.schedulers.Schedulers;
44+
4345
import java.io.IOException;
44-
import java.io.InputStream;
45-
import java.io.OutputStream;
4646
import java.lang.reflect.Field;
4747
import java.lang.reflect.Modifier;
4848
import java.util.ArrayList;
49+
import java.util.Collections;
50+
import java.util.Comparator;
4951
import java.util.List;
5052
import java.util.Map;
53+
import java.util.function.Function;
5154
import java.util.stream.Collectors;
52-
import rx.Observable;
53-
import rx.functions.Func1;
54-
import rx.schedulers.Schedulers;
5555

5656
public class AzureMvpModel {
5757

@@ -112,6 +112,7 @@ public List<Subscription> getSelectedSubscriptions() {
112112
} catch (IOException e) {
113113
e.printStackTrace();
114114
}
115+
Collections.sort(ret, getComparator(Subscription::displayName));
115116
return ret;
116117
}
117118

@@ -134,6 +135,8 @@ public List<ResourceEx<ResourceGroup>> getResourceGroups(boolean forceUpdate) th
134135
resourceGroups.addAll(srgMap.get(sd).stream().map(
135136
resourceGroup -> new ResourceEx<>(resourceGroup, sd.getSubscriptionId())).collect(Collectors.toList()));
136137
}
138+
Collections.sort(resourceGroups, getComparator((ResourceEx<ResourceGroup> resourceGroupResourceEx) ->
139+
resourceGroupResourceEx.getResource().name()));
137140
return resourceGroups;
138141
}
139142

@@ -163,6 +166,7 @@ public List<ResourceGroup> getResourceGroupsBySubscriptionId(String sid) {
163166
} catch (IOException e) {
164167
e.printStackTrace();
165168
}
169+
Collections.sort(ret, getComparator(ResourceGroup::name));
166170
return ret;
167171
}
168172

@@ -197,12 +201,15 @@ public List<Deployment> listAllDeployments() {
197201
}
198202
subscriber.onCompleted();
199203
}).subscribeOn(Schedulers.io()), subs.size()).subscribeOn(Schedulers.io()).toBlocking().subscribe();
204+
Collections.sort(deployments, getComparator(Deployment::name));
200205
return deployments;
201206
}
202207

203208
public List<Deployment> listDeploymentsBySid(String sid) throws IOException {
204209
Azure azure = AuthMethodManager.getInstance().getAzureClient(sid);
205-
return azure.deployments().list();
210+
List<Deployment> deployments = azure.deployments().list();
211+
Collections.sort(deployments, getComparator(Deployment::name));
212+
return deployments;
206213
}
207214

208215
/**
@@ -215,6 +222,8 @@ public List<ResourceEx<Deployment>> getDeploymentByRgName(String sid, String rgN
215222
Azure azure = AuthMethodManager.getInstance().getAzureClient(sid);
216223
res.addAll(azure.deployments().listByResourceGroup(rgName).stream().
217224
map(deployment -> new ResourceEx<>(deployment, sid)).collect(Collectors.toList()));
225+
Collections.sort(res,
226+
getComparator((ResourceEx<Deployment> deploymentResourceEx) -> deploymentResourceEx.getResource().name()));
218227
return res;
219228
}
220229

@@ -287,6 +296,7 @@ public List<Location> listLocationsBySubscriptionId(String sid) {
287296
} catch (Exception e) {
288297
e.printStackTrace();
289298
}
299+
Collections.sort(locations, getComparator(Location::name));
290300
return locations;
291301
}
292302

@@ -304,9 +314,15 @@ public List<PricingTier> listPricingTier() throws IllegalAccessException {
304314
ret.add(pt);
305315
}
306316
}
317+
Collections.sort(ret, getComparator(PricingTier::toString));
307318
return correctPricingTiers(ret);
308319
}
309320

321+
private static <T> Comparator<T> getComparator(Function<T, String> toStringMethod) {
322+
return (first, second) ->
323+
StringUtils.compareIgnoreCase(toStringMethod.apply(first), toStringMethod.apply(second));
324+
}
325+
310326
// workaround for SDK not updated the PREMIUM pricing tiers to latest ones
311327
// https://github.com/Azure/azure-libraries-for-java/issues/660
312328
private List<PricingTier> correctPricingTiers(final List<PricingTier> pricingTiers) {

0 commit comments

Comments
 (0)