1 import { PropertyModel } from "app/models";
2 import { CommonUtils } from "app/utils";
3 import {IZoneInstanceMethod, IZoneInstanceAssignment} from "./zone-instance";
4 import {GroupInstance} from "./group-instance";
5 import {ComponentInstance} from "../../componentsInstances/componentInstance";
6 import {TargetUiObject} from "../../ui-models/ui-target-object";
7 import {TargetOrMemberType} from "../../../utils/constants";
9 /* The request and response should be same model, need to fix in BE */
10 export class PolicyTargetsMap {
11 COMPONENT_INSTANCES:Array<string>;
15 export class PolicyTargetsRequest {
17 requestItems:Array<PolicyTargetsRequestItem>;
19 constructor(groups:Array<string>,instances:Array<string>){
21 this.requestItems = [];
23 if (instances && instances.length>0) {
24 let instancesObj:PolicyTargetsRequestItem = {
25 type: "component_Instances",
28 this.requestItems.push(instancesObj);
30 if (groups && groups.length>0) {
31 let groupsObj:PolicyTargetsRequestItem = {
35 this.requestItems.push(groupsObj);
40 export class PolicyTargetsRequestItem {
42 uniqueIds:Array<String>;
45 export class PolicyInstance implements IZoneInstanceMethod {
54 normalizedName:string;
58 properties:Array<PropertyModel>;
59 targets:PolicyTargetsMap;
64 originArchived:boolean;
67 constructor(policy?:PolicyInstance) {
68 this.componentName = policy.componentName;
69 this.description = policy.description;
70 this.empty = policy.empty;
71 this.invariantName = policy.invariantName;
72 this.invariantUUID = policy.invariantUUID;
73 this.isFromCsar = policy.isFromCsar;
75 this.name = policy.name;
76 this.normalizedName =policy.normalizedName;
77 this.type = policy.type;
78 this.policyTypeUid = policy.policyTypeUid;
79 this.policyUUID = policy.policyUUID;
80 this.properties = CommonUtils.initProperties(policy.properties);
81 this.targets = policy.targets;
82 this.uniqueId = policy.uniqueId;
83 this.version = policy.version;
86 this.icon = 'icon-policy';
89 public getTargetsAsUiObject(componentInstances?:Array<ComponentInstance>, groupInstances?:Array<GroupInstance>):Array<TargetUiObject> {
90 let savedItems:Array<TargetUiObject> = [];
92 //get all targets from component instances
93 if (!_.isEmpty(this.targets.COMPONENT_INSTANCES)) {
94 this.targets.COMPONENT_INSTANCES.forEach((targetInstanceId:string)=> {
95 let componentInstance:ComponentInstance;
96 if (componentInstances) {
97 componentInstance = _.find(componentInstances, function (_componentInstance:ComponentInstance) {
98 return _componentInstance.uniqueId === targetInstanceId;
101 savedItems.push(new TargetUiObject(targetInstanceId, TargetOrMemberType.COMPONENT_INSTANCES, componentInstance ? componentInstance.name : undefined));
105 //get all targets from groupInstances
106 if (!_.isEmpty(this.targets.GROUPS)) {
107 this.targets.GROUPS.forEach((groupsTargetId:string)=> {
108 let groupInstance:GroupInstance;
109 if (groupInstances) {
110 groupInstance = _.find(groupInstances, function (_groupInstance:GroupInstance) {
111 return _groupInstance.uniqueId === groupsTargetId;
114 savedItems.push(new TargetUiObject(groupsTargetId, TargetOrMemberType.GROUPS, groupInstance? groupInstance.name : undefined));
120 public saveTargets = (newTargets:Array<TargetUiObject>):void => {
121 this.targets.COMPONENT_INSTANCES = newTargets.filter(target => target.type === TargetOrMemberType.COMPONENT_INSTANCES).map(target => target.uniqueId);
122 this.targets.GROUPS = newTargets.filter(target => target.type === TargetOrMemberType.GROUPS).map(target => target.uniqueId);
125 // This function is used for the zone to get and set the assignment
126 public getSavedAssignments = ():Array<IZoneInstanceAssignment> => {
127 return this.getTargetsAsUiObject();
130 public setSavedAssignments = (newMembers:Array<IZoneInstanceAssignment>):void => {
131 this.saveTargets(newMembers);
134 public get iconClass() {
135 return this.iconSprite + ' ' + this.icon;