feat(templates): support templates versioning (#3729)

* feat(templates): Support templates versioning format

* Update app/portainer/models/template.js

Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io>
2.0
Maxime Bajeux 2020-04-21 02:05:30 +02:00 committed by Anthony Lapenna
parent f371dc5402
commit 54621ced9e
2 changed files with 51 additions and 33 deletions

View File

@ -1,7 +1,18 @@
import _ from 'lodash-es';
import { PorImageRegistryModel } from 'Docker/models/porImageRegistry';
export function TemplateViewModel(data) {
export class TemplateViewModel {
constructor(data, version) {
switch (version) {
case '2':
this.setTemplatesV2(data);
break;
default:
throw new Error('Unsupported template version');
}
}
setTemplatesV2(data) {
this.Id = data.Id;
this.Title = data.title;
this.Type = data.type;
@ -27,6 +38,7 @@ export function TemplateViewModel(data) {
this.Env = templateEnv(data);
this.Volumes = templateVolumes(data);
this.Ports = templatePorts(data);
}
}
function templatePorts(data) {

View File

@ -1,3 +1,4 @@
import _ from 'lodash-es';
import { TemplateViewModel } from '../../models/template';
angular.module('portainer.app').factory('TemplateService', [
@ -13,7 +14,7 @@ angular.module('portainer.app').factory('TemplateService', [
var service = {};
service.templates = function () {
var deferred = $q.defer();
const deferred = $q.defer();
$q.all({
templates: Templates.query().$promise,
@ -21,12 +22,17 @@ angular.module('portainer.app').factory('TemplateService', [
dockerhub: DockerHubService.dockerhub(),
})
.then(function success(data) {
const templates = data.templates.templates.map(function (item) {
const res = new TemplateViewModel(item);
const registry = RegistryService.retrievePorRegistryModelFromRepositoryWithRegistries(res.RegistryModel.Registry.URL, data.registries, data.dockerhub);
registry.Image = res.RegistryModel.Image;
res.RegistryModel = registry;
return res;
const version = data.templates.version;
const templates = _.map(data.templates.templates, (item) => {
try {
const template = new TemplateViewModel(item, version);
const registry = RegistryService.retrievePorRegistryModelFromRepositoryWithRegistries(template.RegistryModel.Registry.URL, data.registries, data.dockerhub);
registry.Image = template.RegistryModel.Image;
template.RegistryModel = registry;
return template;
} catch (err) {
deferred.reject({ msg: 'Unable to retrieve templates', err: err });
}
});
deferred.resolve(templates);
})