-
Notifications
You must be signed in to change notification settings - Fork 85
/
ProvisioningService.ts
123 lines (115 loc) · 4.58 KB
/
ProvisioningService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import * as fs from "fs";
import * as readYaml from "js-yaml";
import { ImperativeError } from "@zowe/imperative";
import { IPropertiesInput, IProvisionOptionals } from "./";
/**
* Service class which helps to filter or edit input parameters.
* @export
* @class ProvisioningService
*/
export class ProvisioningService {
/**
* Parsers text with properties in key1=val1,key2=val2 format and returns IInputProperty[]
* @param {string} propertiesText - required runtime property objects passed as a string.
* @returns {IPropertiesInput[]} array of properties, @see {IPropertiesInput}
* @memberof ProvisioningService
*/
public static parseProperties(propertiesText: string): IPropertiesInput[] {
if (propertiesText === "") {
return [];
}
return propertiesText.split(",").map((property) => {
const tempArray = property.split("=");
if (tempArray.length === 2 && tempArray[0].length > 0) {
return {name: tempArray[0].trim(), value: tempArray[1].trim()};
} else {
throw new ImperativeError({msg: `Incorrect properties format: ${propertiesText}`});
}
});
}
/**
* Reads a YAML file with properties, parses it in key1=val1,key2=val2 format and returns IInputProperty[].
* @param {string} path - path of the file.
* @returns {IPropertiesInput[]} array of properties, @see {IPropertiesInput}
* @memberof ProvisioningService
*/
public static readPropertiesFromYamlFile(path: string): IPropertiesInput[] {
const props = readYaml.load(fs.readFileSync(path, "utf8"));
const propsArrayObj: IPropertiesInput[] = [];
if (typeof props !== "object") {
return [];
}
for (const key in props) {
if (Object.prototype.hasOwnProperty.call(props, key)) {
propsArrayObj.push({name: key, value: (props as any)[key]});
}
}
return propsArrayObj;
}
/**
* Helper method which checks if optional parameters were passed or not.
* @param {string} properties - required runtime property objects.
* @param {string} propertiesFile - path of the properties yaml file.
* @param {string} domainName - name of the domain.
* @param {string} tenantName - name of the tenant.
* @param {string} userDataId - ID for the user data specified with user-data.
* @param {string} userData - user data that is passed into the software services registry.
* @param {string} accountInfo - account information to use in the JCL JOB statement.
* @param {string[]} systemNickNames - nicknames of the systems upon which to provision a template.
* @returns {IProvisionOptionals} object with optional properties, @see {IProvisionOptionals}
* @memberof ProvisioningService
*/
public static checkForPassedOptionalParms(properties?: string,
propertiesFile?: string,
domainName?: string,
tenantName?: string,
userDataId?: string,
userData?: string,
accountInfo?: string,
systemNickNames?: string[]): IProvisionOptionals {
const optionalItems: IProvisionOptionals = {
"input-variables": null,
"domain-name": null,
"tenant-name": null,
"user-data-id": null,
"account-info": null,
"user-data": null,
"systems-nicknames": null
};
if (properties) {
optionalItems["input-variables"] = ProvisioningService.parseProperties(properties);
}
if (propertiesFile) {
optionalItems["input-variables"] = ProvisioningService.readPropertiesFromYamlFile(propertiesFile);
}
if (domainName) {
optionalItems["domain-name"] = domainName;
}
if (tenantName) {
optionalItems["tenant-name"] = tenantName;
}
if (accountInfo) {
optionalItems["account-info"] = accountInfo;
}
if (userDataId) {
optionalItems["user-data-id"] = userDataId;
}
if (userData) {
optionalItems["user-data"] = userData;
}
if (systemNickNames) {
optionalItems["systems-nicknames"] = systemNickNames;
}
return optionalItems;
}
}