f0fe90bf7f39ba2aa5e49ec7d70e057bbdad72b7
[sdc.git] / catalog-ui / src / app / services / components / service-service.ts
1 /**
2  * Created by obarda on 2/4/2016.
3  */
4 'use strict';
5 import {IComponentService, ComponentService} from "./component-service";
6 import {Distribution, DistributionComponent, Service, PropertyModel, Component, IAppConfigurtaion} from "app/models";
7 import {SharingService} from "../sharing-service";
8
9 export interface IServiceService extends IComponentService {
10     getDistributionsList(uuid:string):ng.IPromise<Array<Distribution>>;
11     getDistributionComponents(distributionId:string):ng.IPromise<Array<DistributionComponent>>;
12     markAsDeployed(serviceId:string, distributionId:string):ng.IPromise<any>;
13     updateGroupInstanceProperties(serviceId:string, resourceInstanceId:string, groupInstanceId:string, groupInstanceProperties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>>;
14 }
15
16 export class ServiceService extends ComponentService implements IServiceService {
17
18     static '$inject' = [
19         'Restangular',
20         'sdcConfig',
21         'Sdc.Services.SharingService',
22         '$q',
23         '$base64'
24     ];
25
26     public distribution:string = "distribution";
27
28     constructor(protected restangular:restangular.IElement,
29                 protected sdcConfig:IAppConfigurtaion,
30                 protected sharingService:SharingService,
31                 protected $q:ng.IQService,
32                 protected $base64:any) {
33         super(restangular, sdcConfig, sharingService, $q, $base64);
34
35         this.restangular = restangular.one("services");
36     }
37
38     getDistributionsList = (uuid:string):ng.IPromise<Array<Distribution>> => {
39         let defer = this.$q.defer<Array<Distribution>>();
40         this.restangular.one(uuid).one("distribution").get().then((distributions:any) => {
41             defer.resolve(<Array<Distribution>> distributions.distributionStatusOfServiceList);
42         }, (err)=> {
43             defer.reject(err);
44         });
45         return defer.promise;
46     };
47
48     getDistributionComponents = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
49         let defer = this.$q.defer<Array<DistributionComponent>>();
50         this.restangular.one("distribution").one(distributionId).get().then((distributions:any) => {
51             defer.resolve(<Array<DistributionComponent>> distributions.distributionStatusList);
52         }, (err)=> {
53             defer.reject(err);
54         });
55         return defer.promise;
56     };
57
58     markAsDeployed = (serviceId:string, distributionId:string):ng.IPromise<any> => {
59         let defer = this.$q.defer<any>();
60         this.restangular.one(serviceId).one("distribution").one(distributionId).one("markDeployed").customPOST().then((result:any) => {
61             defer.resolve(result);
62         }, (err)=> {
63
64             defer.reject(err);
65         });
66         return defer.promise;
67     };
68
69     createComponentObject = (component:Component):Component => {
70         return new Service(this, this.$q, <Service>component);
71     };
72
73     updateGroupInstanceProperties = (serviceId:string, resourceInstanceId:string, groupInstanceId:string, groupInstanceProperties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
74         let defer = this.$q.defer<Array<PropertyModel>>();
75         this.restangular.one(serviceId).one("resourceInstance").one(resourceInstanceId).one('groupInstance').one(groupInstanceId).customPUT(JSON.stringify(groupInstanceProperties)).then((updatedProperties:any) => {
76             let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
77             _.forEach(updatedProperties, (propertyObj:PropertyModel) => {
78                 propertiesArray.push(new PropertyModel(propertyObj));
79             });
80             defer.resolve(propertiesArray);
81         }, (err)=> {
82             defer.reject(err);
83         });
84         return defer.promise;
85     };
86 }