2021-02-26 15:50:33 +00:00
|
|
|
import _ from 'lodash-es';
|
2020-07-14 20:45:19 +00:00
|
|
|
import angular from 'angular';
|
|
|
|
import PortainerError from 'Portainer/error';
|
|
|
|
import { KubernetesCommonParams } from 'Kubernetes/models/common/params';
|
|
|
|
import { KubernetesIngressConverter } from './converter';
|
|
|
|
|
2021-02-26 15:50:33 +00:00
|
|
|
/* @ngInject */
|
|
|
|
export function KubernetesIngressService($async, KubernetesIngresses) {
|
|
|
|
return {
|
|
|
|
get,
|
|
|
|
create,
|
|
|
|
patch,
|
|
|
|
delete: _delete,
|
|
|
|
};
|
2020-07-14 20:45:19 +00:00
|
|
|
|
2021-02-26 15:50:33 +00:00
|
|
|
async function getOne(namespace, name) {
|
2020-07-14 20:45:19 +00:00
|
|
|
try {
|
|
|
|
const params = new KubernetesCommonParams();
|
|
|
|
params.id = name;
|
2021-02-26 15:50:33 +00:00
|
|
|
const [raw, yaml] = await Promise.all([KubernetesIngresses(namespace).get(params).$promise, KubernetesIngresses(namespace).getYaml(params).$promise]);
|
2020-07-14 20:45:19 +00:00
|
|
|
const res = {
|
|
|
|
Raw: KubernetesIngressConverter.apiToModel(raw),
|
|
|
|
Yaml: yaml.data,
|
|
|
|
};
|
|
|
|
return res;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to retrieve Ingress', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:50:33 +00:00
|
|
|
async function getAll(namespace) {
|
2020-07-14 20:45:19 +00:00
|
|
|
try {
|
2021-02-26 15:50:33 +00:00
|
|
|
const data = await KubernetesIngresses(namespace).get().$promise;
|
2020-07-14 20:45:19 +00:00
|
|
|
const res = _.reduce(data.items, (arr, item) => _.concat(arr, KubernetesIngressConverter.apiToModel(item)), []);
|
|
|
|
return res;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to retrieve Ingresses', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:50:33 +00:00
|
|
|
function get(namespace, name) {
|
2020-07-14 20:45:19 +00:00
|
|
|
if (name) {
|
2021-02-26 15:50:33 +00:00
|
|
|
return $async(getOne, namespace, name);
|
2020-07-14 20:45:19 +00:00
|
|
|
}
|
2021-02-26 15:50:33 +00:00
|
|
|
return $async(getAll, namespace);
|
2020-07-14 20:45:19 +00:00
|
|
|
}
|
2020-08-12 23:30:23 +00:00
|
|
|
|
2021-02-26 15:50:33 +00:00
|
|
|
function create(ingress) {
|
|
|
|
return $async(async () => {
|
|
|
|
try {
|
|
|
|
const params = {};
|
|
|
|
const payload = KubernetesIngressConverter.createPayload(ingress);
|
|
|
|
const namespace = payload.metadata.namespace;
|
|
|
|
const data = await KubernetesIngresses(namespace).create(params, payload).$promise;
|
|
|
|
return data;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to create ingress', err);
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
2021-02-26 15:50:33 +00:00
|
|
|
});
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 15:50:33 +00:00
|
|
|
function patch(oldIngress, newIngress) {
|
|
|
|
return $async(async () => {
|
|
|
|
try {
|
|
|
|
const params = new KubernetesCommonParams();
|
|
|
|
params.id = newIngress.Name;
|
|
|
|
const namespace = newIngress.Namespace;
|
|
|
|
const payload = KubernetesIngressConverter.patchPayload(oldIngress, newIngress);
|
|
|
|
if (!payload.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = await KubernetesIngresses(namespace).patch(params, payload).$promise;
|
|
|
|
return data;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to patch ingress', err);
|
|
|
|
}
|
|
|
|
});
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 21:49:56 +00:00
|
|
|
function _delete(namespace, ingressClassName) {
|
2021-02-26 15:50:33 +00:00
|
|
|
return $async(async () => {
|
|
|
|
try {
|
|
|
|
const params = new KubernetesCommonParams();
|
2021-05-06 21:49:56 +00:00
|
|
|
params.id = ingressClassName;
|
2021-02-26 15:50:33 +00:00
|
|
|
await KubernetesIngresses(namespace).delete(params).$promise;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to delete ingress', err);
|
|
|
|
}
|
|
|
|
});
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
2020-07-14 20:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
angular.module('portainer.kubernetes').service('KubernetesIngressService', KubernetesIngressService);
|