From 3f9669fdae5f7c6cb1bfe34742df35dfe3a14aa7 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Mon, 18 Jul 2022 17:25:41 +0100 Subject: Support a custom yaml value in tosca function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows to add a custom YAML value to properties in the TOSCA function feature. Change-Id: I15e65088a18537d9832428717be826ac0ef6049a Issue-ID: SDC-4099 Signed-off-by: André Schmid --- .../app/models/properties-inputs/input-fe-model.ts | 2 +- .../models/properties-inputs/property-be-model.ts | 4 +-- .../models/properties-inputs/property-fe-model.ts | 30 ++++++++++-------- catalog-ui/src/app/models/yaml-function.ts | 37 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 catalog-ui/src/app/models/yaml-function.ts (limited to 'catalog-ui/src/app/models') diff --git a/catalog-ui/src/app/models/properties-inputs/input-fe-model.ts b/catalog-ui/src/app/models/properties-inputs/input-fe-model.ts index 347b8a1665..bcdc95c18e 100644 --- a/catalog-ui/src/app/models/properties-inputs/input-fe-model.ts +++ b/catalog-ui/src/app/models/properties-inputs/input-fe-model.ts @@ -83,7 +83,7 @@ export class InputFEModel extends InputBEModel { } public getDefaultValueObj(): any { - return PropertyFEModel.parseValueObj(this.defaultValue, this.type, this.derivedDataType); + return PropertyFEModel.parseValueObj(this.defaultValue, this.type, this.derivedDataType, this.isToscaFunction()); } public resetDefaultValueObjValidation() { diff --git a/catalog-ui/src/app/models/properties-inputs/property-be-model.ts b/catalog-ui/src/app/models/properties-inputs/property-be-model.ts index ae7141353b..9429036f6e 100644 --- a/catalog-ui/src/app/models/properties-inputs/property-be-model.ts +++ b/catalog-ui/src/app/models/properties-inputs/property-be-model.ts @@ -189,9 +189,9 @@ export class PropertyBEModel { } /** - * Checks whether the property value is a tosca get function (e.g. get_input, get_property, get_attribute) + * Checks whether the property value is a TOSCA function (e.g. get_input, get_property, get_attribute, concat, etc.) */ - public isToscaGetFunction(): boolean { + public isToscaFunction(): boolean { return this.toscaFunction != null; } } diff --git a/catalog-ui/src/app/models/properties-inputs/property-fe-model.ts b/catalog-ui/src/app/models/properties-inputs/property-fe-model.ts index f231ec8aa8..eb18c4e4f9 100644 --- a/catalog-ui/src/app/models/properties-inputs/property-fe-model.ts +++ b/catalog-ui/src/app/models/properties-inputs/property-fe-model.ts @@ -21,9 +21,9 @@ */ import * as _ from "lodash"; -import {SchemaPropertyGroupModel, SchemaProperty} from '../schema-property'; -import { PROPERTY_DATA, PROPERTY_TYPES } from 'app/utils'; -import { FilterPropertiesAssignmentData, PropertyBEModel, DerivedPropertyType, DerivedFEPropertyMap, DerivedFEProperty } from 'app/models'; +import {PROPERTY_DATA, PROPERTY_TYPES} from 'app/utils'; +import {DerivedFEProperty, DerivedPropertyType, PropertyBEModel} from 'app/models'; +import * as jsYaml from 'js-yaml'; export class PropertyFEModel extends PropertyBEModel { @@ -96,7 +96,7 @@ export class PropertyFEModel extends PropertyBEModel { } public getValueObj = (): any => { - return PropertyFEModel.parseValueObj(this.value, this.type, this.derivedDataType, this.defaultValue); + return PropertyFEModel.parseValueObj(this.value, this.type, this.derivedDataType, this.isToscaFunction(), this.defaultValue); } public setNonDeclared = (childPath?: string): void => { @@ -278,23 +278,27 @@ export class PropertyFEModel extends PropertyBEModel { return valueObj.trim(); } - static parseValueObj(value: string, propertyType: PROPERTY_TYPES, propertyDerivedType: DerivedPropertyType, defaultValue?: string): any { - let valueObj; + static parseValueObj(value: string, propertyType: PROPERTY_TYPES, propertyDerivedType: DerivedPropertyType, isToscaFunction: boolean, + defaultValue?: string): any { + if (isToscaFunction) { + return jsYaml.load(value); + } if (propertyDerivedType === DerivedPropertyType.SIMPLE) { - valueObj = value || defaultValue || null; // use null for empty value object + const valueObj = value || defaultValue || null; // use null for empty value object if (valueObj && propertyType !== PROPERTY_TYPES.STRING && propertyType !== PROPERTY_TYPES.TIMESTAMP && propertyType !== PROPERTY_TYPES.JSON && PROPERTY_DATA.SCALAR_TYPES.indexOf(propertyType) == -1) { - valueObj = JSON.parse(value); // the value object contains the real value ans not the value as string + return JSON.parse(value); // the value object contains the real value ans not the value as string } - } else if (propertyDerivedType == DerivedPropertyType.LIST) { - valueObj = _.merge([], JSON.parse(defaultValue || '[]'), JSON.parse(value || '[]')); // value object should be merged value and default value. Value takes higher precedence. Set value object to empty obj if undefined. - } else { - valueObj = _.merge({}, JSON.parse(defaultValue || '{}'), JSON.parse(value || '{}')); // value object should be merged value and default value. Value takes higher precedence. Set value object to empty obj if undefined. + return valueObj; } - return valueObj; + if (propertyDerivedType == DerivedPropertyType.LIST) { + return _.merge([], JSON.parse(defaultValue || '[]'), JSON.parse(value || '[]')); // value object should be merged value and default value. Value takes higher precedence. Set value object to empty obj if undefined. + } + + return _.merge({}, JSON.parse(defaultValue || '{}'), JSON.parse(value || '{}')); // value object should be merged value and default value. Value takes higher precedence. Set value object to empty obj if undefined. }; static cleanValueObj(valueObj: any, unsetEmpty?: boolean): any { diff --git a/catalog-ui/src/app/models/yaml-function.ts b/catalog-ui/src/app/models/yaml-function.ts new file mode 100644 index 0000000000..e80d783424 --- /dev/null +++ b/catalog-ui/src/app/models/yaml-function.ts @@ -0,0 +1,37 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import {ToscaFunction} from "./tosca-function"; +import {ToscaFunctionType} from "./tosca-function-type.enum"; +import {ToscaFunctionParameter} from "./tosca-function-parameter"; + +export class YamlFunction implements ToscaFunction, ToscaFunctionParameter { + type = ToscaFunctionType.YAML; + value: any; + + constructor(yamlFunction?: YamlFunction) { + if (!yamlFunction) { + return; + } + this.value = yamlFunction.value; + } + +} \ No newline at end of file -- cgit 1.2.3-korg