2022-02-01 17:38:45 +00:00
|
|
|
import { ResourceGroupViewModel } from '../models/resource_group';
|
|
|
|
import { SubscriptionViewModel } from '../models/subscription';
|
|
|
|
import { getResourceGroups } from './resource-groups.service';
|
|
|
|
import { getSubscriptions } from './subscription.service';
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
angular.module('portainer.azure').factory('AzureService', AzureService);
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
/* @ngInject */
|
|
|
|
export function AzureService($q, Azure, $async, EndpointProvider, ContainerGroupService) {
|
|
|
|
'use strict';
|
|
|
|
var service = {};
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
service.deleteContainerGroup = function (id) {
|
|
|
|
return Azure.delete(id, '2018-04-01');
|
|
|
|
};
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
service.subscriptions = async function subscriptions() {
|
|
|
|
return $async(async () => {
|
|
|
|
const environmentId = EndpointProvider.endpointID();
|
|
|
|
const subscriptions = await getSubscriptions(environmentId);
|
|
|
|
return subscriptions.map((s) => new SubscriptionViewModel(s));
|
|
|
|
});
|
|
|
|
};
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
service.resourceGroups = function resourceGroups(subscriptions) {
|
|
|
|
return $async(async () => {
|
|
|
|
return retrieveResourcesForEachSubscription(subscriptions, async (subscriptionId) => {
|
|
|
|
const environmentId = EndpointProvider.endpointID();
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
const resourceGroups = await getResourceGroups(environmentId, subscriptionId);
|
|
|
|
return resourceGroups.map((r) => new ResourceGroupViewModel(r, subscriptionId));
|
2020-06-09 02:43:32 +00:00
|
|
|
});
|
2022-02-01 17:38:45 +00:00
|
|
|
});
|
|
|
|
};
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
service.containerGroups = function (subscriptions) {
|
|
|
|
return retrieveResourcesForEachSubscription(subscriptions, ContainerGroupService.containerGroups);
|
|
|
|
};
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
service.aggregate = function (resourcesBySubscription) {
|
|
|
|
var aggregatedResources = [];
|
|
|
|
Object.keys(resourcesBySubscription).forEach(function (key) {
|
|
|
|
aggregatedResources = aggregatedResources.concat(resourcesBySubscription[key]);
|
|
|
|
});
|
|
|
|
return aggregatedResources;
|
|
|
|
};
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
function retrieveResourcesForEachSubscription(subscriptions, resourceQuery) {
|
|
|
|
var deferred = $q.defer();
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
var resources = {};
|
2020-06-09 02:43:32 +00:00
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
var resourceQueries = [];
|
|
|
|
for (var i = 0; i < subscriptions.length; i++) {
|
|
|
|
var subscription = subscriptions[i];
|
|
|
|
resourceQueries.push(resourceQuery(subscription.Id));
|
2020-06-09 02:43:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:38:45 +00:00
|
|
|
$q.all(resourceQueries)
|
|
|
|
.then(function success(data) {
|
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
var result = data[i];
|
|
|
|
resources[subscriptions[i].Id] = result;
|
|
|
|
}
|
|
|
|
deferred.resolve(resources);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve resources', err: err });
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return service;
|
|
|
|
}
|