2020-07-14 20:45:19 +00:00
|
|
|
import * as _ from 'lodash-es';
|
|
|
|
import angular from 'angular';
|
|
|
|
import PortainerError from 'Portainer/error';
|
|
|
|
import { KubernetesCommonParams } from 'Kubernetes/models/common/params';
|
|
|
|
import { KubernetesIngressConverter } from './converter';
|
|
|
|
|
|
|
|
class KubernetesIngressService {
|
|
|
|
/* @ngInject */
|
|
|
|
constructor($async, KubernetesIngresses) {
|
|
|
|
this.$async = $async;
|
|
|
|
this.KubernetesIngresses = KubernetesIngresses;
|
|
|
|
|
|
|
|
this.getAsync = this.getAsync.bind(this);
|
|
|
|
this.getAllAsync = this.getAllAsync.bind(this);
|
2020-08-12 23:30:23 +00:00
|
|
|
this.createAsync = this.createAsync.bind(this);
|
|
|
|
this.patchAsync = this.patchAsync.bind(this);
|
|
|
|
this.deleteAsync = this.deleteAsync.bind(this);
|
2020-07-14 20:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET
|
|
|
|
*/
|
|
|
|
async getAsync(namespace, name) {
|
|
|
|
try {
|
|
|
|
const params = new KubernetesCommonParams();
|
|
|
|
params.id = name;
|
|
|
|
const [raw, yaml] = await Promise.all([this.KubernetesIngresses(namespace).get(params).$promise, this.KubernetesIngresses(namespace).getYaml(params).$promise]);
|
|
|
|
const res = {
|
|
|
|
Raw: KubernetesIngressConverter.apiToModel(raw),
|
|
|
|
Yaml: yaml.data,
|
|
|
|
};
|
|
|
|
return res;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to retrieve Ingress', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllAsync(namespace) {
|
|
|
|
try {
|
|
|
|
const data = await this.KubernetesIngresses(namespace).get().$promise;
|
|
|
|
const res = _.reduce(data.items, (arr, item) => _.concat(arr, KubernetesIngressConverter.apiToModel(item)), []);
|
|
|
|
return res;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to retrieve Ingresses', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(namespace, name) {
|
|
|
|
if (name) {
|
|
|
|
return this.$async(this.getAsync, namespace, name);
|
|
|
|
}
|
|
|
|
return this.$async(this.getAllAsync, namespace);
|
|
|
|
}
|
2020-08-12 23:30:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CREATE
|
|
|
|
*/
|
2020-08-20 00:51:14 +00:00
|
|
|
async createAsync(ingress) {
|
2020-08-12 23:30:23 +00:00
|
|
|
try {
|
|
|
|
const params = {};
|
2020-08-20 00:51:14 +00:00
|
|
|
const payload = KubernetesIngressConverter.createPayload(ingress);
|
2020-08-12 23:30:23 +00:00
|
|
|
const namespace = payload.metadata.namespace;
|
|
|
|
const data = await this.KubernetesIngresses(namespace).create(params, payload).$promise;
|
|
|
|
return data;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to create ingress', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 00:51:14 +00:00
|
|
|
create(ingress) {
|
|
|
|
return this.$async(this.createAsync, ingress);
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PATCH
|
|
|
|
*/
|
|
|
|
async patchAsync(oldIngress, newIngress) {
|
|
|
|
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 this.KubernetesIngresses(namespace).patch(params, payload).$promise;
|
|
|
|
return data;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to patch ingress', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
patch(oldIngress, newIngress) {
|
|
|
|
return this.$async(this.patchAsync, oldIngress, newIngress);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE
|
|
|
|
*/
|
|
|
|
async deleteAsync(ingress) {
|
|
|
|
try {
|
|
|
|
const params = new KubernetesCommonParams();
|
2020-08-20 00:51:14 +00:00
|
|
|
params.id = ingress.IngressClass.Name;
|
2020-08-12 23:30:23 +00:00
|
|
|
const namespace = ingress.Namespace;
|
|
|
|
await this.KubernetesIngresses(namespace).delete(params).$promise;
|
|
|
|
} catch (err) {
|
|
|
|
throw new PortainerError('Unable to delete ingress', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(ingress) {
|
|
|
|
return this.$async(this.deleteAsync, ingress);
|
|
|
|
}
|
2020-07-14 20:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default KubernetesIngressService;
|
|
|
|
angular.module('portainer.kubernetes').service('KubernetesIngressService', KubernetesIngressService);
|