From 54621ced9e6dbbf64f00360ec352f5cbbacde9fa Mon Sep 17 00:00:00 2001 From: Maxime Bajeux Date: Tue, 21 Apr 2020 02:05:30 +0200 Subject: [PATCH] feat(templates): support templates versioning (#3729) * feat(templates): Support templates versioning format * Update app/portainer/models/template.js Co-authored-by: Anthony Lapenna --- app/portainer/models/template.js | 64 +++++++++++-------- app/portainer/services/api/templateService.js | 20 ++++-- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/app/portainer/models/template.js b/app/portainer/models/template.js index f0bd75fd4..5acc99bb1 100644 --- a/app/portainer/models/template.js +++ b/app/portainer/models/template.js @@ -1,32 +1,44 @@ import _ from 'lodash-es'; import { PorImageRegistryModel } from 'Docker/models/porImageRegistry'; -export function TemplateViewModel(data) { - this.Id = data.Id; - this.Title = data.title; - this.Type = data.type; - this.Description = data.description; - this.AdministratorOnly = data.AdministratorOnly; - this.Name = data.name; - this.Note = data.note; - this.Categories = data.categories ? data.categories : []; - this.Platform = data.platform ? data.platform : ''; - this.Logo = data.logo; - this.Repository = data.repository; - this.Hostname = data.hostname; - this.RegistryModel = new PorImageRegistryModel(); - this.RegistryModel.Image = data.image; - this.RegistryModel.Registry.URL = data.registry || ''; - this.Command = data.command ? data.command : ''; - this.Network = data.network ? data.network : ''; - this.Privileged = data.privileged ? data.privileged : false; - this.Interactive = data.interactive ? data.interactive : false; - this.RestartPolicy = data.restart_policy ? data.restart_policy : 'always'; - this.Labels = data.labels ? data.labels : []; - this.Hosts = data.hosts ? data.hosts : []; - this.Env = templateEnv(data); - this.Volumes = templateVolumes(data); - this.Ports = templatePorts(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; + this.Description = data.description; + this.AdministratorOnly = data.AdministratorOnly; + this.Name = data.name; + this.Note = data.note; + this.Categories = data.categories ? data.categories : []; + this.Platform = data.platform ? data.platform : ''; + this.Logo = data.logo; + this.Repository = data.repository; + this.Hostname = data.hostname; + this.RegistryModel = new PorImageRegistryModel(); + this.RegistryModel.Image = data.image; + this.RegistryModel.Registry.URL = data.registry || ''; + this.Command = data.command ? data.command : ''; + this.Network = data.network ? data.network : ''; + this.Privileged = data.privileged ? data.privileged : false; + this.Interactive = data.interactive ? data.interactive : false; + this.RestartPolicy = data.restart_policy ? data.restart_policy : 'always'; + this.Labels = data.labels ? data.labels : []; + this.Hosts = data.hosts ? data.hosts : []; + this.Env = templateEnv(data); + this.Volumes = templateVolumes(data); + this.Ports = templatePorts(data); + } } function templatePorts(data) { diff --git a/app/portainer/services/api/templateService.js b/app/portainer/services/api/templateService.js index 7788ae19c..973f73168 100644 --- a/app/portainer/services/api/templateService.js +++ b/app/portainer/services/api/templateService.js @@ -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); })